每日算法 — 使用java实现打地鼠:反应时间统计与概率分布

一、游戏介绍与问题建模

打地鼠(Whack-a-Mole)是一款经典的反应速度街机游戏,自上世纪 70 年代问世以来风靡全球。游戏面板上排列着多个地鼠洞,地鼠会随机从洞中探出,玩家需要在它们缩回之前用锤子快速击中。游戏简单直观,却蕴含着丰富的随机性建模和统计分析价值。

1.1 游戏规则

标准打地鼠游戏的核心规则:

  • 游戏面板:通常为 3×3 或 4×4 的地鼠洞矩阵
  • 地鼠出现:地鼠随机从某个洞中探出,停留一段时间后自动缩回
  • 击中判定:玩家在地鼠探出期间点击该洞即为击中,获得分数
  • 未击中惩罚:点击空洞或地鼠已缩回则扣分或无得分
  • 游戏时长:通常为 30-60 秒一局
  • 难度递增:随着游戏进行,地鼠出现频率加快,停留时间缩短

1.2 随机性来源分析

打地鼠是一个典型的随机过程问题,随机性主要来自三个维度:

随机维度 描述 统计模型
出现时机 下一只地鼠什么时候出现 泊松过程 / 指数分布
出现位置 地鼠从哪个洞出现 均匀分布 / 马尔可夫链
停留时长 地鼠探出后停留多久 正态分布 / 对数正态分布

这三种随机性的组合,构成了打地鼠游戏的核心挑战。玩家需要在不确定的环境中快速做出反应,而游戏设计者则需要通过调整这些随机参数来控制难度曲线。

1.3 问题建模思路

从算法角度,我们可以将打地鼠游戏建模为以下几个层次:

  1. 游戏引擎层:管理游戏状态、地鼠生命周期、得分系统
  2. 随机生成层:基于概率模型生成地鼠出现的时机、位置和时长
  3. 统计分析层:收集玩家反应时间数据,进行分布拟合和分析
  4. 难度控制层:根据玩家表现动态调整难度参数

本文将完整实现这四层架构,并重点讨论泊松分布建模地鼠出现间隔正态分布分析玩家反应时间这两个核心算法问题。


二、状态表示与编码

2.1 游戏面板与地鼠状态

游戏面板使用二维网格表示,每个格子代表一个地鼠洞。每个地鼠洞有三种状态:

/**
 * 地鼠洞状态枚举
 */
enum HoleState {
    EMPTY,      // 空洞,没有地鼠
    ACTIVE,     // 地鼠探出,可被击中
    HIT,        // 刚被击中,显示击中效果
    COOLDOWN    // 冷却中,短时间内不会再出现地鼠
}

地鼠本身有完整的生命周期管理:

/**
 * 地鼠类,管理单只地鼠的生命周期
 */
class Mole {
    private int row, col;           // 所在位置
    private long appearTime;        // 出现时间戳(毫秒)
    private long duration;          // 停留时长(毫秒)
    private boolean hit;            // 是否已被击中
    private int scoreValue;         // 击中得分

    public Mole(int row, int col, long appearTime, long duration, int scoreValue) {
        this.row = row;
        this.col = col;
        this.appearTime = appearTime;
        this.duration = duration;
        this.hit = false;
        this.scoreValue = scoreValue;
    }

    /**
     * 检查地鼠是否仍然活跃(未被击中且未超时)
     */
    public boolean isActive(long currentTime) {
        return !hit && (currentTime - appearTime) < duration;
    }

    /**
     * 检查地鼠是否已过期(超时未被击中)
     */
    public boolean isExpired(long currentTime) {
        return !hit && (currentTime - appearTime) >= duration;
    }

    // Getter 和 Setter
    public int getRow() { return row; }
    public int getCol() { return col; }
    public long getAppearTime() { return appearTime; }
    public long getDuration() { return duration; }
    public boolean isHit() { return hit; }
    public void setHit(boolean hit) { this.hit = hit; }
    public int getScoreValue() { return scoreValue; }
}

2.2 游戏引擎核心类

游戏引擎负责管理整体状态、生成地鼠、处理击中、计算得分:

import java.util.*;

/**
 * 打地鼠游戏引擎
 * 支持概率模型生成、反应时间统计、难度自适应
 */
class WhackAMoleGame {
    // 游戏配置
    private final int rows;
    private final int cols;
    private final long gameDuration;     // 游戏总时长(毫秒)

    // 游戏状态
    private HoleState[][] board;
    private List<Mole> activeMoles;      // 当前活跃的地鼠列表
    private int score;
    private int hits;                    // 击中次数
    private int misses;                  // 未击中次数
    private long startTime;
    private boolean gameOver;

    // 随机数生成器
    private Random random;

    // 概率模型参数
    private double spawnRate;            // 地鼠生成速率(每秒出现次数)
    private long baseMoleDuration;       // 地鼠基础停留时长(毫秒)
    private double difficultyFactor;     // 难度因子(随时间递增)

    // 反应时间统计
    private List<Long> reactionTimes;    // 每次击中的反应时间记录

    // 滑动窗口命中率统计
    private Deque<Boolean> hitWindow;    // 最近N次尝试的命中情况
    private int windowSize;
    private int windowHits;              // 窗口内命中数

    /**
     * 构造函数
     * @param rows 行数
     * @param cols 列数
     * @param gameDuration 游戏时长(毫秒)
     */
    public WhackAMoleGame(int rows, int cols, long gameDuration) {
        this.rows = rows;
        this.cols = cols;
        this.gameDuration = gameDuration;

        this.board = new HoleState[rows][cols];
        for (int i = 0; i < rows; i++) {
            Arrays.fill(board[i], HoleState.EMPTY);
        }

        this.activeMoles = new ArrayList<>();
        this.score = 0;
        this.hits = 0;
        this.misses = 0;
        this.gameOver = false;

        this.random = new Random();

        // 初始难度参数
        this.spawnRate = 1.0;          // 初始每秒1只
        this.baseMoleDuration = 1500;  // 基础停留1.5秒
        this.difficultyFactor = 1.0;

        this.reactionTimes = new ArrayList<>();

        // 滑动窗口:最近20次尝试
        this.windowSize = 20;
        this.hitWindow = new LinkedList<>();
        this.windowHits = 0;
    }

    /**
     * 开始游戏
     */
    public void startGame() {
        this.startTime = System.currentTimeMillis();
        this.gameOver = false;
    }

    /**
     * 更新游戏状态(每帧调用)
     */
    public void update() {
        if (gameOver) return;

        long currentTime = System.currentTimeMillis();
        long elapsed = currentTime - startTime;

        // 检查游戏是否结束
        if (elapsed >= gameDuration) {
            gameOver = true;
            return;
        }

        // 更新难度因子(随时间线性增加,最高3倍)
        difficultyFactor = 1.0 + 2.0 * ((double) elapsed / gameDuration);

        // 移除过期的地鼠
        Iterator<Mole> iterator = activeMoles.iterator();
        while (iterator.hasNext()) {
            Mole mole = iterator.next();
            if (mole.isExpired(currentTime)) {
                board[mole.getRow()][mole.getCol()] = HoleState.COOLDOWN;
                // 冷却时间:500ms
                scheduleCooldownReset(mole.getRow(), mole.getCol(), 500);
                iterator.remove();
            }
        }

        // 尝试生成新地鼠
        trySpawnMole(currentTime);
    }

    // ... 其他方法在后续章节逐步实现
}

2.3 得分系统与连击机制

打地鼠的得分系统通常包含基础得分和连击加成,以激励玩家保持高命中率:

    // 在 WhackAMoleGame 类中

    private int combo;           // 当前连击数
    private int maxCombo;        // 最大连击数
    private long lastHitTime;    // 上次击中时间

    /**
     * 处理玩家点击
     * @param row 点击行
     * @param col 点击列
     * @return 本次点击的得分(0表示未命中)
     */
    public int handleClick(int row, int col, long currentTime) {
        if (gameOver) return 0;
        if (row < 0 || row >= rows || col < 0 || col >= cols) return 0;

        // 检查是否击中活跃地鼠
        for (Mole mole : activeMoles) {
            if (mole.getRow() == row && mole.getCol() == col && mole.isActive(currentTime)) {
                // 击中!
                mole.setHit(true);

                // 计算反应时间
                long reactionTime = currentTime - mole.getAppearTime();
                reactionTimes.add(reactionTime);

                // 更新连击
                if (currentTime - lastHitTime < 2000) {
                    combo++;
                } else {
                    combo = 1;
                }
                maxCombo = Math.max(maxCombo, combo);
                lastHitTime = currentTime;

                // 计算得分(基础分 + 连击加成 + 反应速度加成)
                int baseScore = mole.getScoreValue();
                double comboMultiplier = 1.0 + (combo - 1) * 0.1;  // 每次连击+10%
                double speedBonus = Math.max(0, 1.0 - (double) reactionTime / mole.getDuration());

                int earnedScore = (int) (baseScore * comboMultiplier * (1 + speedBonus * 0.5));
                score += earnedScore;
                hits++;

                // 更新滑动窗口
                updateHitWindow(true);

                // 更新面板状态
                board[row][col] = HoleState.HIT;
                scheduleCooldownReset(row, col, 300);  // 击中效果显示300ms

                // 从活跃列表移除
                activeMoles.remove(mole);

                return earnedScore;
            }
        }

        // 未击中
        misses++;
        combo = 0;  // 重置连击
        updateHitWindow(false);

        return 0;
    }

    /**
     * 更新滑动窗口命中率
     */
    private void updateHitWindow(boolean hit) {
        hitWindow.offer(hit);
        if (hit) windowHits++;

        if (hitWindow.size() > windowSize) {
            boolean removed = hitWindow.poll();
            if (removed) windowHits--;
        }
    }

    /**
     * 获取当前滑动窗口命中率
     */
    public double getWindowHitRate() {
        if (hitWindow.isEmpty()) return 0;
        return (double) windowHits / hitWindow.size();
    }

    // 冷却重置的调度(简化实现,实际可使用ScheduledExecutorService)
    private void scheduleCooldownReset(int row, int col, long delayMs) {
        // 简化:在update中检查时间。实际项目中建议使用定时任务
        new Thread(() -> {
            try {
                Thread.sleep(delayMs);
                if (board[row][col] == HoleState.HIT || board[row][col] == HoleState.COOLDOWN) {
                    board[row][col] = HoleState.EMPTY;
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();
    }

三、地鼠出现概率模型

地鼠出现的时机和位置是打地鼠游戏随机性的核心。合理的概率模型不仅影响游戏体验,也是理解随机过程的绝佳案例。

3.1 泊松过程与指数分布

泊松过程(Poisson Process) 是描述事件在时间上随机发生的经典模型,它有两个关键性质:

  1. 独立增量:不相交的时间区间内事件发生次数相互独立
  2. 平稳性:事件发生的速率 λ 是常数

对于打地鼠游戏:
单位时间内出现的地鼠数量 服从泊松分布:P(N(t) = k) = (λt)^k × e^(-λt) / k!
相邻两只地鼠出现的时间间隔 服从指数分布:f(t) = λ × e^(-λt)

指数分布的一个重要性质是无记忆性——已经等待了多久不影响还需要等多久。这恰好符合地鼠出现的”不可预测性”。

    // 在 WhackAMoleGame 类中

    private long nextSpawnTime;  // 下一次生成地鼠的时间

    /**
     * 使用指数分布生成下一次地鼠出现的时间间隔
     * @param rate 生成速率(每秒出现次数)
     * @return 间隔时间(毫秒)
     */
    private long generateExponentialInterval(double rate) {
        // 指数分布的逆变换采样
        // 若 U ~ Uniform(0,1),则 -ln(U)/λ ~ Exponential(λ)
        double u = random.nextDouble();
        // 避免log(0)
        while (u == 0) {
            u = random.nextDouble();
        }
        double intervalSeconds = -Math.log(u) / rate;
        // 转换为毫秒
        return (long) (intervalSeconds * 1000);
    }

    /**
     * 尝试生成新地鼠
     */
    private void trySpawnMole(long currentTime) {
        if (currentTime < nextSpawnTime) return;

        // 计算当前难度下的生成速率
        double currentRate = spawnRate * difficultyFactor;

        // 生成下一次出现的时间间隔
        long interval = generateExponentialInterval(currentRate);
        nextSpawnTime = currentTime + interval;

        // 生成地鼠
        spawnMole(currentTime);
    }

3.2 地鼠位置生成策略

地鼠出现的位置可以有多种策略,从简单到复杂:

    /**
     * 策略1:完全随机位置(均匀分布)
     */
    private int[] getRandomPosition() {
        List<int[]> available = new ArrayList<>();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (board[i][j] == HoleState.EMPTY) {
                    available.add(new int[]{i, j});
                }
            }
        }
        if (available.isEmpty()) return null;
        return available.get(random.nextInt(available.size()));
    }

    /**
     * 策略2:中心偏好位置(正态分布采样)
     * 玩家通常更关注中心区域,让地鼠更多出现在边缘可以增加难度
     */
    private int[] getCenterBiasedPosition() {
        // 收集所有空位
        List<int[]> available = new ArrayList<>();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (board[i][j] == HoleState.EMPTY) {
                    available.add(new int[]{i, j});
                }
            }
        }
        if (available.isEmpty()) return null;

        // 计算每个位置的权重(距离中心越远权重越高)
        double centerRow = (rows - 1) / 2.0;
        double centerCol = (cols - 1) / 2.0;

        List<Double> weights = new ArrayList<>();
        double totalWeight = 0;
        for (int[] pos : available) {
            double distance = Math.sqrt(
                Math.pow(pos[0] - centerRow, 2) + 
                Math.pow(pos[1] - centerCol, 2)
            );
            double weight = 1.0 + distance;  // 距离越远权重越高
            weights.add(weight);
            totalWeight += weight;
        }

        // 加权随机选择
        double r = random.nextDouble() * totalWeight;
        double cumulative = 0;
        for (int i = 0; i < available.size(); i++) {
            cumulative += weights.get(i);
            if (r <= cumulative) {
                return available.get(i);
            }
        }

        return available.get(available.size() - 1);
    }

    /**
     * 策略3:马尔可夫链位置转移
     * 地鼠倾向于在相邻位置出现,模拟"地鼠在地下移动"的感觉
     */
    private int lastMoleRow = -1;
    private int lastMoleCol = -1;

    private int[] getMarkovPosition() {
        List<int[]> available = new ArrayList<>();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (board[i][j] == HoleState.EMPTY) {
                    available.add(new int[]{i, j});
                }
            }
        }
        if (available.isEmpty()) return null;

        // 第一只地鼠完全随机
        if (lastMoleRow == -1) {
            int[] pos = available.get(random.nextInt(available.size()));
            lastMoleRow = pos[0];
            lastMoleCol = pos[1];
            return pos;
        }

        // 计算与上一只地鼠的距离,距离越近权重越高
        List<Double> weights = new ArrayList<>();
        double totalWeight = 0;
        for (int[] pos : available) {
            double distance = Math.abs(pos[0] - lastMoleRow) + Math.abs(pos[1] - lastMoleCol);
            double weight = Math.exp(-distance * 0.5);  // 指数衰减
            weights.add(weight);
            totalWeight += weight;
        }

        double r = random.nextDouble() * totalWeight;
        double cumulative = 0;
        for (int i = 0; i < available.size(); i++) {
            cumulative += weights.get(i);
            if (r <= cumulative) {
                int[] pos = available.get(i);
                lastMoleRow = pos[0];
                lastMoleCol = pos[1];
                return pos;
            }
        }

        int[] pos = available.get(available.size() - 1);
        lastMoleRow = pos[0];
        lastMoleCol = pos[1];
        return pos;
    }

3.3 地鼠停留时长与难度曲线

地鼠停留的时长决定了玩家有多少反应时间,是难度控制的关键参数。

    /**
     * 生成地鼠停留时长(正态分布)
     * @param baseDuration 基础时长
     * @param stdDev 标准差
     * @return 停留时长(毫秒)
     */
    private long generateMoleDuration(long baseDuration, double stdDev) {
        // 使用 Box-Muller 变换生成正态分布随机数
        double u1 = random.nextDouble();
        double u2 = random.nextDouble();
        double z = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);

        long duration = (long) (baseDuration + z * stdDev);

        // 限制在合理范围内(最短300ms,最长3000ms)
        return Math.max(300, Math.min(3000, duration));
    }

    /**
     * 生成一只新地鼠
     */
    private void spawnMole(long currentTime) {
        // 使用马尔可夫策略选择位置
        int[] pos = getMarkovPosition();
        if (pos == null) return;

        int row = pos[0];
        int col = pos[1];

        // 根据难度调整停留时长(难度越高,停留越短)
        long adjustedDuration = (long) (baseMoleDuration / difficultyFactor);
        long duration = generateMoleDuration(adjustedDuration, adjustedDuration * 0.2);

        // 地鼠分值(停留时间越短分值越高)
        int scoreValue = (int) (100 * (baseMoleDuration / (double) duration));

        Mole mole = new Mole(row, col, currentTime, duration, scoreValue);
        activeMoles.add(mole);
        board[row][col] = HoleState.ACTIVE;
    }

3.4 难度曲线设计

好的难度曲线应该让玩家始终处于”跳一跳够得着”的状态——既不太简单也不太难。以下是几种常见的难度曲线:

/**
 * 难度曲线工具类
 */
class DifficultyCurve {

    /**
     * 线性难度增长
     * 最简单的形式,难度随时间均匀增加
     */
    public static double linear(double progress, double minFactor, double maxFactor) {
        return minFactor + (maxFactor - minFactor) * progress;
    }

    /**
     * 指数难度增长
     * 前期增长慢,后期加速增长,适合高手向游戏
     */
    public static double exponential(double progress, double minFactor, double maxFactor) {
        // 使用指数函数:f(x) = a * e^(bx)
        double b = Math.log(maxFactor / minFactor);
        return minFactor * Math.exp(b * progress);
    }

    /**
     * S型曲线(Logistic函数)
     * 前期慢、中期快、后期平缓,最符合人类学习曲线
     */
    public static double sigmoid(double progress, double minFactor, double maxFactor) {
        // Logistic函数:f(x) = L / (1 + e^(-k(x-x0)))
        double midpoint = 0.5;
        double steepness = 6;  // 控制中间的陡峭程度
        double logistic = 1.0 / (1.0 + Math.exp(-steepness * (progress - midpoint)));
        return minFactor + (maxFactor - minFactor) * logistic;
    }

    /**
     * 阶梯式难度
     * 每隔一段时间跳升一级,有明确的"关卡感"
     */
    public static double stepwise(double progress, double minFactor, double maxFactor, int steps) {
        int currentStep = (int) (progress * steps);
        currentStep = Math.min(currentStep, steps - 1);
        return minFactor + (maxFactor - minFactor) * (currentStep / (double)(steps - 1));
    }
}

四、玩家反应时间统计分析

收集和分析玩家的反应时间数据,不仅可以用于难度自适应调整,也是人体工学和认知心理学的重要研究方法。

4.1 反应时间数据收集

/**
 * 反应时间统计分析器
 */
class ReactionTimeAnalyzer {
    private List<Long> reactionTimes;

    public ReactionTimeAnalyzer() {
        this.reactionTimes = new ArrayList<>();
    }

    public void addReactionTime(long timeMs) {
        reactionTimes.add(timeMs);
    }

    public int getSampleCount() {
        return reactionTimes.size();
    }

4.2 基本统计量计算

    /**
     * 计算平均值
     */
    public double getMean() {
        if (reactionTimes.isEmpty()) return 0;
        long sum = 0;
        for (long t : reactionTimes) sum += t;
        return (double) sum / reactionTimes.size();
    }

    /**
     * 计算中位数
     */
    public double getMedian() {
        if (reactionTimes.isEmpty()) return 0;
        List<Long> sorted = new ArrayList<>(reactionTimes);
        Collections.sort(sorted);
        int n = sorted.size();
        if (n % 2 == 0) {
            return (sorted.get(n/2 - 1) + sorted.get(n/2)) / 2.0;
        } else {
            return sorted.get(n/2);
        }
    }

    /**
     * 计算标准差
     */
    public double getStandardDeviation() {
        if (reactionTimes.size() < 2) return 0;
        double mean = getMean();
        double sumSquaredDiff = 0;
        for (long t : reactionTimes) {
            sumSquaredDiff += Math.pow(t - mean, 2);
        }
        return Math.sqrt(sumSquaredDiff / (reactionTimes.size() - 1));
    }

    /**
     * 计算百分位数
     * @param percentile 百分位(0-100)
     */
    public double getPercentile(double percentile) {
        if (reactionTimes.isEmpty()) return 0;
        List<Long> sorted = new ArrayList<>(reactionTimes);
        Collections.sort(sorted);

        double index = (percentile / 100.0) * (sorted.size() - 1);
        int lower = (int) Math.floor(index);
        int upper = (int) Math.ceil(index);

        if (lower == upper) {
            return sorted.get(lower);
        }

        double weight = index - lower;
        return sorted.get(lower) * (1 - weight) + sorted.get(upper) * weight;
    }

    /**
     * 计算最小值和最大值
     */
    public long getMin() {
        if (reactionTimes.isEmpty()) return 0;
        return Collections.min(reactionTimes);
    }

    public long getMax() {
        if (reactionTimes.isEmpty()) return 0;
        return Collections.max(reactionTimes);
    }

4.3 正态分布拟合

反应时间通常近似服从正态分布(或对数正态分布)。我们可以用样本均值和方差来拟合正态分布,并计算拟合优度。

    /**
     * 正态分布拟合结果
     */
    static class NormalFitResult {
        double mean;           // 均值 μ
        double stdDev;         // 标准差 σ
        double skewness;       // 偏度
        double kurtosis;       // 峰度
        boolean isNormal;      // 是否近似正态分布

        @Override
        public String toString() {
            return String.format(
                "正态分布拟合:μ=%.1fms, σ=%.1fms, 偏度=%.3f, 峰度=%.3f, 近似正态=%s",
                mean, stdDev, skewness, kurtosis, isNormal
            );
        }
    }

    /**
     * 拟合正态分布
     * 使用样本均值和标准差作为参数估计
     * 通过偏度和峰度判断是否近似正态
     */
    public NormalFitResult fitNormalDistribution() {
        NormalFitResult result = new NormalFitResult();
        result.mean = getMean();
        result.stdDev = getStandardDeviation();

        int n = reactionTimes.size();
        if (n < 3) {
            result.isNormal = false;
            return result;
        }

        // 计算偏度(Skewness)
        // 正态分布偏度为0,正偏表示长尾在右侧
        double sumCubedDiff = 0;
        for (long t : reactionTimes) {
            sumCubedDiff += Math.pow((t - result.mean) / result.stdDev, 3);
        }
        result.skewness = (n / (double)((n-1)*(n-2))) * sumCubedDiff;

        // 计算峰度(Kurtosis)
        // 正态分布超额峰度为0,正峰度表示更尖峰
        double sumFourthDiff = 0;
        for (long t : reactionTimes) {
            sumFourthDiff += Math.pow((t - result.mean) / result.stdDev, 4);
        }
        double kurtosis = (n*(n+1) / (double)((n-1)*(n-2)*(n-3))) * sumFourthDiff
                        - (3.0*(n-1)*(n-1)) / (double)((n-2)*(n-3));
        result.kurtosis = kurtosis;  // 超额峰度

        // 粗略判断是否近似正态分布
        // 经验法则:偏度绝对值 < 1,超额峰度绝对值 < 2
        result.isNormal = Math.abs(result.skewness) < 1.0 && Math.abs(result.kurtosis) < 2.0;

        return result;
    }

4.4 历史趋势分析

分析反应时间随游戏进程的变化趋势,可以了解玩家的疲劳程度和学习效果。

    /**
     * 趋势分析结果
     */
    static class TrendResult {
        double slope;           // 斜率(毫秒/次,正表示变慢,负表示变快)
        double intercept;       // 截距
        double rSquared;        // R² 拟合优度
        String trend;           // 趋势描述

        @Override
        public String toString() {
            return String.format(
                "趋势:%s(斜率=%.2fms/次,R²=%.3f)",
                trend, slope, rSquared
            );
        }
    }

    /**
     * 线性回归分析反应时间趋势
     * x轴:第几次击中(1, 2, 3, ...)
     * y轴:反应时间
     */
    public TrendResult analyzeTrend() {
        TrendResult result = new TrendResult();
        int n = reactionTimes.size();
        if (n < 3) {
            result.trend = "数据不足";
            result.rSquared = 0;
            return result;
        }

        // 计算线性回归:y = a + bx
        double sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0, sumY2 = 0;

        for (int i = 0; i < n; i++) {
            double x = i + 1;
            double y = reactionTimes.get(i);
            sumX += x;
            sumY += y;
            sumXY += x * y;
            sumX2 += x * x;
            sumY2 += y * y;
        }

        double meanX = sumX / n;
        double meanY = sumY / n;

        // 斜率 b = Σ(xi-x̄)(yi-ȳ) / Σ(xi-x̄)²
        double numerator = sumXY - n * meanX * meanY;
        double denominator = sumX2 - n * meanX * meanX;

        if (denominator == 0) {
            result.trend = "无趋势";
            return result;
        }

        result.slope = numerator / denominator;
        result.intercept = meanY - result.slope * meanX;

        // 计算 R²
        double ssRes = 0;  // 残差平方和
        double ssTot = 0;  // 总平方和
        for (int i = 0; i < n; i++) {
            double x = i + 1;
            double y = reactionTimes.get(i);
            double predicted = result.intercept + result.slope * x;
            ssRes += Math.pow(y - predicted, 2);
            ssTot += Math.pow(y - meanY, 2);
        }

        result.rSquared = 1 - (ssRes / ssTot);

        // 判断趋势
        if (result.rSquared < 0.1) {
            result.trend = "无明显趋势";
        } else if (result.slope > 0) {
            result.trend = "反应逐渐变慢(可能疲劳)";
        } else {
            result.trend = "反应逐渐变快(学习效应)";
        }

        return result;
    }
}

五、复杂度分析与性能优化

5.1 时间复杂度分析

操作 时间复杂度 说明
点击检测 O(M) M 为当前活跃地鼠数,通常 ≤ 总洞数
地鼠生成 O(R×C) 需要遍历面板找空位,R行C列
状态更新 O(M) 遍历活跃地鼠检查过期
反应时间统计 O(N) N 为样本数,统计计算需遍历所有数据
正态分布拟合 O(N) 均值、方差、偏度、峰度均为线性
趋势分析(线性回归) O(N) 单次遍历即可完成计算

详细分析

  1. 点击检测:每次玩家点击时,需要遍历所有活跃地鼠来判断是否击中。活跃地鼠数量通常远少于总洞数(一般同时只有 1-3 只),因此实际非常快。如果需要进一步优化,可以使用二维数组直接索引。

  2. 地鼠生成:选择位置时需要遍历整个面板找出所有空位。对于 3×3 或 4×4 的面板,这完全不是问题。如果面板很大,可以维护一个空位列表来避免每次遍历。

  3. 统计计算:所有统计量(均值、方差、偏度、峰度、线性回归)都是 O(N) 的单次遍历。即使收集了上万个样本,计算也是毫秒级的。

5.2 空间复杂度分析

数据结构 空间复杂度 说明
游戏面板 O(R×C) 固定大小的二维数组
活跃地鼠列表 O(R×C) 最多等于洞的数量
反应时间记录 O(N) N 为击中次数,随游戏进行增长
滑动窗口 O(W) W 为窗口大小,固定值

空间复杂度的主要变量是反应时间记录。对于一局 60 秒的游戏,击中次数通常在几十到上百次,完全在内存可接受范围内。如果需要记录多局游戏的历史数据,可以考虑定期持久化到磁盘。

5.3 性能优化技巧

1. 位置索引优化

// 优化前:每次点击遍历所有活跃地鼠 O(M)
for (Mole mole : activeMoles) {
    if (mole.getRow() == row && mole.getCol() == col) { ... }
}

// 优化后:使用二维数组直接索引 O(1)
private Mole[][] moleGrid;  // 与面板同大小的地鼠引用数组

public int handleClick(int row, int col, long currentTime) {
    Mole mole = moleGrid[row][col];
    if (mole != null && mole.isActive(currentTime)) {
        // 击中处理
    }
}

2. 增量统计计算

不需要每次都重新遍历所有数据计算统计量,可以用增量方式维护:

/**
 * 增量统计计算器
 * 使用Welford算法在线计算均值和方差
 */
class IncrementalStats {
    private int n = 0;
    private double mean = 0;
    private double m2 = 0;  // 平方偏差和

    /**
     * 添加一个新数据点
     * 使用Welford在线算法,数值稳定性更好
     */
    public void add(double x) {
        n++;
        double delta = x - mean;
        mean += delta / n;
        double delta2 = x - mean;
        m2 += delta * delta2;
    }

    public double getMean() { return mean; }

    public double getVariance() {
        return n < 2 ? 0 : m2 / (n - 1);
    }

    public double getStdDev() {
        return Math.sqrt(getVariance());
    }

    public int getCount() { return n; }
}

Welford 算法的优势:
数值稳定性好:避免了大数相减导致的精度损失
增量更新:每次添加只需 O(1) 时间
内存占用小:只需要存储几个统计量

3. 预生成随机数

如果游戏需要非常高的时间精度,可以预先生成一批随机数缓存起来,避免实时计算的开销:

/**
 * 预生成随机数缓存
 */
class RandomCache {
    private double[] cache;
    private int index;
    private Random random;

    public RandomCache(int size) {
        cache = new double[size];
        random = new Random();
        refill();
    }

    private void refill() {
        for (int i = 0; i < cache.length; i++) {
            cache[i] = random.nextDouble();
        }
        index = 0;
    }

    public double nextDouble() {
        if (index >= cache.length) {
            refill();
        }
        return cache[index++];
    }
}

六、适用场景与扩展思路

6.1 适用场景

打地鼠游戏的算法框架可以推广到以下领域:

  1. 用户行为分析:点击流数据分析、响应时间统计、注意力模型
  2. A/B测试:比较不同版本的用户反应时间差异(t检验、非参数检验)
  3. 人机交互研究:界面元素位置对用户响应速度的影响
  4. 游戏化系统:积分系统、等级系统、难度自适应
  5. 性能监控:服务响应时间的统计分布分析、异常检测

6.2 扩展思路

1. 多人对战模式

支持多人同时游戏,比较反应速度和命中率:

/**
 * 多人对战管理器
 */
class MultiplayerManager {
    private Map<String, WhackAMoleGame> players;
    private long gameStartTime;
    private long gameDuration;

    /**
     * 比较两位玩家的表现是否有显著差异
     * 使用独立样本t检验
     */
    public static double tTest(List<Long> sampleA, List<Long> sampleB) {
        int n1 = sampleA.size(), n2 = sampleB.size();
        if (n1 < 2 || n2 < 2) return 0;

        // 计算均值
        double mean1 = sampleA.stream().mapToLong(Long::longValue).average().orElse(0);
        double mean2 = sampleB.stream().mapToLong(Long::longValue).average().orElse(0);

        // 计算方差
        double var1 = 0, var2 = 0;
        for (long x : sampleA) var1 += Math.pow(x - mean1, 2);
        for (long x : sampleB) var2 += Math.pow(x - mean2, 2);
        var1 /= (n1 - 1);
        var2 /= (n2 - 1);

        // 计算t统计量(Welch's t-test)
        double se = Math.sqrt(var1/n1 + var2/n2);
        if (se == 0) return 0;

        return (mean1 - mean2) / se;
    }
}

2. 难度自适应调整

根据玩家的实时表现动态调整难度,让每个玩家都能获得最佳体验:

/**
 * 自适应难度控制器
 * 基于PID控制思想,将命中率维持在目标值附近
 */
class AdaptiveDifficultyController {
    private double targetHitRate;    // 目标命中率
    private double currentDifficulty;
    private double integral;         // 积分项
    private double lastError;        // 上一次误差(用于微分项)

    // PID参数
    private double kp = 0.5;   // 比例系数
    private double ki = 0.1;   // 积分系数
    private double kd = 0.2;   // 微分系数

    public AdaptiveDifficultyController(double targetHitRate) {
        this.targetHitRate = targetHitRate;
        this.currentDifficulty = 1.0;
        this.integral = 0;
        this.lastError = 0;
    }

    /**
     * 根据当前命中率更新难度
     * @param currentHitRate 当前命中率(滑动窗口)
     * @return 新的难度因子
     */
    public double update(double currentHitRate) {
        // 计算误差(目标 - 当前)
        double error = targetHitRate - currentHitRate;

        // 积分项(累积误差)
        integral += error;
        // 积分限幅,防止积分饱和
        integral = Math.max(-5, Math.min(5, integral));

        // 微分项(误差变化率)
        double derivative = error - lastError;

        // PID输出
        double adjustment = kp * error + ki * integral + kd * derivative;

        // 更新难度(限制在合理范围)
        currentDifficulty = Math.max(0.5, Math.min(5.0, currentDifficulty + adjustment));

        lastError = error;

        return currentDifficulty;
    }
}

3. 地鼠类型多样化

引入不同类型的地鼠,增加游戏策略深度:

地鼠类型 特点 分值 出现概率
普通地鼠 标准速度,标准分值 100 60%
快速地鼠 停留时间短,分值高 200 25%
金色地鼠 停留极短,超高分值 500 10%
炸弹地鼠 击中会扣分,需要避开 -200 5%

4. 数据可视化

将统计结果以图表形式展示,帮助玩家了解自己的表现:

  • 反应时间分布直方图
  • 命中率变化曲线
  • 连击分布统计
  • 与历史最佳对比

6.3 总结

打地鼠虽然是一个简单的小游戏,但它涵盖了丰富的算法和统计知识:

  1. 泊松过程与指数分布是理解随机事件时间间隔的基础,广泛应用于排队论、可靠性分析、网络流量建模等领域。

  2. 正态分布与统计推断是数据分析的基石。从反应时间的分布拟合,到假设检验和趋势分析,这些方法在科学研究和工程实践中无处不在。

  3. 滑动窗口与在线算法让我们能够在数据流中实时计算统计量,这是实时系统和大数据处理的核心技术。

  4. 难度自适应与PID控制展示了控制理论在游戏设计中的应用。让系统自动调整到最佳状态,是智能系统的共同目标。

理解了打地鼠背后的这些算法思想,你就拥有了一把打开更广阔领域的钥匙——从用户行为分析到 A/B 测试,从性能监控到推荐系统,处处都能看到这些基础算法的身影。

思考练习:如果要实现一个”作弊检测”功能,识别出反应时间异常快(可能使用了外挂)的玩家,你会如何设计算法?提示:可以结合反应时间的统计分布、连续击中的概率、以及点击位置的精确性等多个维度进行综合判断。