-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·163 lines (142 loc) · 4.65 KB
/
main.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
let game = new Game();
let DEBUG = true
let gameovered = false;
if (DEBUG){
var script=document.createElement('script');
script.src='https://rawgit.com/paulirish/memory-stats.js/master/bookmarklet.js';
document.head.appendChild(script);
}
$(document).ready(function(){
prepareForMobile();
$("body").bind("touchmove", function(e){
e.preventDefault();
});
newgame();
});
function prepareForMobile(){
if( documentWidth > 500 ){
gridContainerWidth = 500;
cellSpace = 20;
cellSideLength = 100;
}
$('#grid-container').css('width',gridContainerWidth - 2*cellSpace);
$('#grid-container').css('height',gridContainerWidth - 2*cellSpace);
$('#grid-container').css('padding', cellSpace);
$('#grid-container').css('border-radius',0.02*gridContainerWidth);
$('.grid-cell').css('width',cellSideLength);
$('.grid-cell').css('height',cellSideLength);
$('.grid-cell').css('border-radius',0.04*cellSideLength);
$('.number-cell').css('width',cellSideLength);
$('.number-cell').css('height',cellSideLength);
$('.number-cell').css('border-radius',0.02*cellSideLength);
}
function newgame() {
game.reset();
gameovered = false;
[board, score] = game.get_state();
updateBoardView(true);
}
function auto_move() {
ai = new AI(game.get_state());
direction = ai.compute_decision();
if (direction != -1) {
game.move_and_place(direction);
[board, score] = game.get_state();
updateBoardView();
updateScore();
}
}
function auto() {
if (game.game_over() && !gameovered) {
gameovered = true;
alert('Game over!Score:'+ score);
return;
}
auto_move();
setTimeout(function() {
auto();
}, 50);
}
function updateBoardView(init=false){
if ( init ) {
$("#grid-container").empty()
for( let i = 0 ; i < 4 ; i ++ ) {
for( let j = 0 ; j < 4 ; j ++ ){
$("#grid-container").append( '<div class="grid-cell" id="grid-cell-'+i+'-'+j+'"></div>' );
let gridCell = $('#grid-cell-'+i+"-"+j);
gridCell.css('top', getPosTop( i , j ) );
gridCell.css('left', getPosLeft( i , j ) );
}
}
}
for( let i = 0 ; i < 4 ; i ++ )
for( let j = 0 ; j < 4 ; j ++ ){
if ( init ){
$("#grid-container").append( '<div class="number-cell" id="number-cell-'+i+'-'+j+'"></div>' );
}
let theNumberCell = $('#number-cell-'+i+'-'+j);
if ( board[i][j] == 0 ){
theNumberCell.css('width','0px');
theNumberCell.css('height','0px');
theNumberCell.css('top',getPosTop(i,j) + cellSideLength/2 );
theNumberCell.css('left',getPosLeft(i,j) + cellSideLength/2 );
theNumberCell.text('');
}
else{
theNumberCell.css('width',cellSideLength);
theNumberCell.css('height',cellSideLength);
theNumberCell.css('top',getPosTop(i,j));
theNumberCell.css('left',getPosLeft(i,j));
theNumberCell.css('background-color',getNumberBackgroundColor( board[i][j] ) );
theNumberCell.css('color',getNumberColor( board[i][j] ) );
theNumberCell.text( board[i][j] );
}
}
$('.number-cell').css('line-height',cellSideLength+'px');
$('.number-cell').css('font-size',0.6*cellSideLength+'px');
}
function createColorForNumber(num){
let color = { r:256,g:256,b:256}
let dec = 64;
while(num > 1){
num /= 2;
if(color.b > 0) color.b -= dec;
else if(color.g > 0) {
color.g -= dec;
}
else if(color.r > 0) {
color.r -= dec;
color.g = 256;
}
}
return "rgb("+color.r+","+color.g+","+color.b+")";
}
function updateScore() {
$('#score').text(score);
}
$(document).keydown( function( event ){
// event.preventDefault();
switch( event.keyCode ){
case 37: //left
game.move_and_place(0)
break;
case 38: //up
game.move_and_place(1);
break;
case 39: //right
game.move_and_place(2);
break;
case 40: //down
game.move_and_place(3);
break;
default: //default
break;
}
[board, score] = game.get_state()
updateBoardView()
updateScore()
if (game.game_over() && !gameovered) {
gameovered = true;
alert('Game over!Score:'+ score);
}
});