如何在oi-2048快速AK

温馨提示:作弊会影响自己与他人游戏体验,不建议用于真实游戏,仅供测试。
所有代码都可以按F12打开控制台后注入,直接使用。

前情提要

这几天在参加市集训,题目实在太难了还没带巧克力,在课间我们同学深度体验了一下oi-2048,然后成功AFO了好多次,为什么AI也会AFO啊。于是我们剖析魔改了一下代码,再用js注入,就有了这篇文章。

进入正题

首先因为自己按键太慢,于是我们让AI做了一个代码,可以随机按键,虽然容易AFO,但因为速度快可以很好的刷分,如果你运气好甚至可以达到PC,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// 随机方向键模拟器
// 当页面切换到后台时自动暂停,回到前台时恢复
(function() {
// 当前模拟的方向键(仅上、右两个方向)
const keys = ['ArrowUp', 'ArrowRight', 'ArrowDown', 'ArrowLeft'];
const keyCodes = {
'ArrowUp': 38,
'ArrowDown': 40,
'ArrowLeft': 37,
'ArrowRight': 39
};

let intervalId = null; // 定时器句柄
let isPaused = false; // 是否因后台而暂停
let isStopped = false; // 是否被手动停止
let pressCount = 0;

// 模拟按键事件(完整触发 keydown + keyup)
function simulateKey(direction) {
const code = keyCodes[direction];
const downEvent = new KeyboardEvent('keydown', {
key: direction,
code: direction,
keyCode: code,
which: code,
bubbles: true,
cancelable: true,
composed: true
});
document.dispatchEvent(downEvent);

const upEvent = new KeyboardEvent('keyup', {
key: direction,
code: direction,
keyCode: code,
which: code,
bubbles: true,
cancelable: true,
composed: true
});
document.dispatchEvent(upEvent);

pressCount++;

// 每10次输出一次日志
if (pressCount % 10 === 0) {
console.log(`[方向键模拟] 已发送 ${pressCount} 次按键,最近一次: ${direction}`);
}
}

// 随机选择一个方向(从 keys 数组中选取)
function getRandomDirection() {
return keys[Math.floor(Math.random() * keys.length)];
}

// 执行一次随机按键(仅在未停止且页面可见时)
function fireRandomKey() {
if (isStopped) return;
if (document.hidden) {
// 页面在后台,不执行按键(但定时器仍在跑,由 visibility 控制暂停/恢复)
return;
}
const direction = getRandomDirection();
simulateKey(direction);
}

// 启动定时器(内部调用)
function startTimer() {
if (intervalId !== null) {
clearInterval(intervalId);
intervalId = null;
}
if (isStopped) return;
intervalId = setInterval(fireRandomKey, 1); // 50ms = 0.05s
}

// 暂停(页面切到后台时调用)
function pause() {
if (intervalId !== null) {
clearInterval(intervalId);
intervalId = null;
isPaused = true;
console.log('⏸️ 页面进入后台,按键模拟已暂停');
}
}

// 恢复(页面回到前台时调用)
function resume() {
if (isStopped) return;
if (intervalId === null && isPaused) {
startTimer();
isPaused = false;
console.log('▶️ 页面回到前台,按键模拟已恢复');
}
}

// 停止(完全停止,不再恢复)
function stop() {
if (intervalId !== null) {
clearInterval(intervalId);
intervalId = null;
}
isStopped = true;
isPaused = false;
console.log(`🛑 模拟器已完全停止,总计模拟按键 ${pressCount} 次`);
}

// 监听页面可见性变化
document.addEventListener('visibilitychange', function() {
if (document.hidden) {
pause();
} else {
resume();
}
});

// 对外暴露停止函数
window.stopRandomKeys = stop;

// 启动
function start() {
if (intervalId !== null) {
console.warn('⚠️ 定时器已在运行,请勿重复启动');
return;
}
isStopped = false;
isPaused = false;
pressCount = 0;
startTimer();
console.log('✅ 模拟器已启动:每 50ms 随机按上或右方向键');
console.log('📌 页面切换到后台时自动暂停,回到前台自动恢复');
console.log('🛑 完全停止请执行: stopRandomKeys()');
}

// 如果已有旧定时器则先清理
if (window.stopRandomKeys) {
window.stopRandomKeys();
}

start();
})();

但这样分数仍然很低,连进入排行榜都做不到
于是我们寻找代码,发现了这样几处地方:

  1. 在grid.js?ver=54文件第150行位置我们发现了生成方块概率的函数,长这样:
1
2
3
4
5
6
7
8
9
10
11
Grid.prototype.addRandomTile = function (isStart = false) {
if (this.cellsAvailable()) {
var value1 = (Math.random() < 0.9 ? 1 : 2)*this.multiplier;
var value2 = Math.random() < 0.86 ? -1 : -2; // Do not change this
var value = (isStart || Math.random() < 0.87) ? value1 : value2;
//var value = Math.random() < 0.9 ? 256 : 512;
var tile = new Tile(this.randomAvailableCell(), value);

this.insertTile(tile);
}
};

经过调整后改成了这样:

1
2
3
4
5
6
7
8
9
10
11
Grid.prototype.addRandomTile = function (isStart = false) {
if (this.cellsAvailable()) {
var value1 = (Math.random() < 0.0 ? 1 : 2)*this.multiplier;
var value2 = Math.random() < 0.0 ? -1 : -2; // Do not change this
var value = (isStart || Math.random() < 0.20) ? value1 : value2;
//var value = Math.random() < 0.9 ? 256 : 512;
var tile = new Tile(this.randomAvailableCell(), value);

this.insertTile(tile);
}
};

其中第四行表示生成普通方块的概率,分别对应CE和Waiting,第五行表示特殊方块生成的概率,分别对应#和O2。我们把概率调成了80%生成O2,20%生成普通方块,实际可以自己调整。谁懂被满屏O3卡死的崩溃
2. 也是在这个文件第40行发现的可以调整生成位置的代码,长这样:

1
2
3
4
5
6
7
Grid.prototype.randomAvailableCell = function () {
var cells = this.availableCells();

if (cells.length) {
return cells[Math.floor(Math.random() * cells.length)];
}
};

我们改成了这样:

1
2
3
4
5
6
7
Grid.prototype.randomAvailableCell = function () {
var cells = this.availableCells();

if (cells.length) {
return {x:0,y:3};
}
};

表示方块始终生成在左下角。
这样就可以保证AK了,运气好甚至可以进入前20名。(更改生成位置会被判断非法,但其他不会,因为判非法主要在后端,所以我也不知道判断逻辑)


如何在oi-2048快速AK
http://hexiangshuo.github.io/2026/08/26/如何在oi-2048快速AK/
作者
Hexiangshuo
发布于
2026年8月26日
许可协议