forked from NoelCendana/2147483648
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ai.html
147 lines (138 loc) · 5.42 KB
/
ai.html
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
143
144
145
146
147
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>2048</title>
<link href="style/old.css" rel="stylesheet" type="text/css">
<link href="style/tiles2.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="favicon.ico">
<link rel="apple-touch-icon" href="meta/apple-touch-icon.png">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0, maximum-scale=1, user-scalable=no, minimal-ui">
</head>
<body>
<div class="container">
<div class="heading">
<h1 class="title">2048</h1>
<div class="scores-container">
<div class="score-container">0</div>
<div class="best-container">0</div>
</div>
</div>
<p class="game-intro"></br>
<div style="background:#88CCFF;padding:4px;">
<div style="margin:0px auto;text-align:center;">
<font color="white">I'm a robot.</font>
<button onclick="run(true, true, false);"><font color="red"> Run! </font></button>
<button onclick="run(true, true, true);">Get Crazy</button>
<button onclick="run(false, true, false);">One Step</button>
<button onclick="run(false, false, false);" disabled="true" id="stop">Stop</button>
</div>
</div>
</p>
<div class="game-container">
<div class="game-message">
<p></p>
<div class="lower">
<a class="keep-playing-button">Keep going</a>
<a class="retry-button">Try again</a>
</div>
</div>
<div class="grid-container">
<div class="grid-row">
<div class="grid-cell"></div>
<div class="grid-cell"></div>
<div class="grid-cell"></div>
<div class="grid-cell"></div>
</div>
<div class="grid-row">
<div class="grid-cell"></div>
<div class="grid-cell"></div>
<div class="grid-cell"></div>
<div class="grid-cell"></div>
</div>
<div class="grid-row">
<div class="grid-cell"></div>
<div class="grid-cell"></div>
<div class="grid-cell"></div>
<div class="grid-cell"></div>
</div>
<div class="grid-row">
<div class="grid-cell"></div>
<div class="grid-cell"></div>
<div class="grid-cell"></div>
<div class="grid-cell"></div>
</div>
</div>
<div class="tile-container">
</div>
</div>
<p class="game-explanation">
<strong class="important">How to play:</strong> Use your <strong>arrow keys</strong> to move the tiles. Or you can let Artificial Intelligence do it for you. When two tiles with the same number touch, they <strong>merge into one!</strong>
</p>
<hr>
<p style="background:#CCFFCC">
<strong class="important">Note:</strong> This site is a forked version of the original 2048. It's to demonstrate an AI written for the 2048 game. You can play the game and use the AI for help.</br>
I also made a <a href="https://chrome.google.com/webstore/detail/2048-ai/ggiomnbaioeocdipdgfllknhkpcnggjk">chrome extension</a> using this AI. If you are using chrome, you can install the <a href="https://chrome.google.com/webstore/detail/2048-ai/ggiomnbaioeocdipdgfllknhkpcnggjk">extension</a> and go to the <a href="http://gabrielecirulli.github.com/2048/">original 2048 website</a> to play with it.</br>
</p>
<p style="background:#98F5FF">
<a href="http://sleepycoder.github.io/2014/04/01/2048-ai/">Here</a> is a blog post about this AI.
</p>
<hr>
<p>
Created by <a href="http://sleepycoder.github.com" target="_blank">Sleepy Coder.</a> Based on <a href="http://gabrielecirulli.github.com/2048/" target="_blank">2048 by Gabriele Cirulli.</a>
</p>
</div>
<script src="js_ai/animframe_polyfill.js"></script>
<script src="js_ai/keyboard_input_manager.js"></script>
<script src="js_ai/html_actuator.js"></script>
<script src="js_ai/grid.js"></script>
<script src="js_ai/tile.js"></script>
<script src="js_ai/local_score_manager.js"></script>
<script src="js_ai/game_manager.js"></script>
<script src="js_ai/application.js"></script>
<script src="js_ai/ai.js"></script>
<script>
var ai = new AI();
var continuous = false;
var time_out = null;
var interval = 500;
var after_win = false;
function ai_run() {
time_out = null;
if (!global_game || global_game.over || (global_game.won && !after_win)) return;
for (var i = 0; i < 4; ++i) {
for (var j = 0; j < 4; ++j) {
var t = global_game.grid.cells[i][j];
if (t) {
ai.SetTile(i, j, t.value);
} else {
ai.SetTile(i, j, 0);
}
}
}
var dir = [3, 2, 1, 0]
ai.StartSearch();
global_game.move(dir[ai.best_operation]);
if (continuous) {
if (time_out == null) {
time_out = window.setTimeout("ai_run()", interval);
}
}
}
function run(is_continuous, need_run, is_crazy) {
continuous = is_continuous;
document.getElementById("stop").disabled = !continuous;
interval = is_crazy ? 0 : 500;
if (need_run) {
if (global_game && global_game.won) {
after_win = true;
}
ai_run();
}
}
</script>
</body>
</html>