-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.js
173 lines (153 loc) · 4.89 KB
/
view.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
164
165
166
167
168
169
170
171
172
173
import Game from "./engine/game.js";
function gameview() {
return $(`
<div id="root">
<section id="header">
<div class="header-text has-text-weight-semibold">2048</div>
</section>
<section id="game-view">
<div id="scoreboard">
<div class="container">
<div class="button has-text-weight-semibold" id="score-panel" disabled>
<p>Score: </p>
<p id="score"></p>
</div>
<button class="button is-white has-text-weight-semibold" id="newgame-button">New Game</button>
</div>
</div>
<div id="game-pro">
<div id="game-board"></div>
</div>
<div id="game-message">
<p> <br>
Join the numbers and get to the 2048 tile! <br><br>
2048 implemented in grayscale for players with a visual impairment.
</p>
</div>
</section>
<section id="credit">
Adam Alston, 2019
</section>
</div>
`);
}
function loadGame(game) {
let size = game.size;
// Generate empty tiles
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
$('#game-board').append(
$('<div class="game-tile empty-tile"></div>')
.css({top: ((1 + i * 15)+'vmin'), left: ((1 + j * 15)+'vmin')})
);
}
}
loadScore(game);
loadTiles(game);
}
function loadScore(game) {
$('#score').empty().text(game.gameState.score);
}
function loadTiles(game) {
let size = game.size;
let board = game.gameState.board;
let gameboard = $('#game-board');
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
gameboard.find('#' + (i * size + j)).remove();
let currentValue = board[i * size + j];
if (currentValue != 0) {
gameboard.append(
$(`<div class="game-tile centerall tile-${currentValue}">${currentValue}</div>`)
.css({top: ((1 + i * 15)+'vmin'), left: ((1 + j * 15)+'vmin')})
.attr('id', (i * size + j))
);
}
}
}
}
function detachAll() {
$(document).off('keydown');
let winMessage = $('#win-message');
if (winMessage.length) {
winMessage.remove();
}
let loseMessage = $('#lose-message');
if (loseMessage.length) {
loseMessage.remove();
}
}
function attachEventHandlers(game) {
$(document).keydown((keyEvent) => {
switch(keyEvent.which) {
case 37: // left
game.move('left');
break;
case 38: // up
game.move('up');
break;
case 39: // right
game.move('right');
break;
case 40: // down
game.move('down');
break;
};
loadTiles(game);
loadScore(game);
});
game.onLose(() => {
// alert("You lost!\n\nselect New Game to try again");
$('<div id="lose-message"></div>')
.appendTo('#root')
.append(
$(`
<div>
<p>You lost, press <b>New Game</b> to try again</p>
</div>
`)
.css({ "font-size": "6vmin", "color": "white",
"text-align": "center", "opacity": "1",
"background-color": "black"})
)
});
game.onWin(() => {
// alert("You Win!\n\nKeep going!");
if(!($('#root').find('#win-message').length)) {
$('<div id="win-message"></div>').appendTo('#root').append(
$(`<div>
<p>You won! Your score was: ${game.gameState.score}</p>
<p> Press <b>Continue</b> to keep going <br>
or <b>New Game</b> to start over</p>
<button class="button is-white has-text-weight-semibold" id="continue">Continue</button>
</div>`)
.css({ "font-size": "6vmin", "color": "white",
"text-align": "center", "opacity": "0.9",
"background-color": "black"}))
.find('#continue').on('click', () => {
$('#win-message').empty();
})
}
});
}
function reloadGame(game) {
detachAll();
loadScore(game);
loadTiles(game);
attachEventHandlers(game);
}
function initializePage() {
let body = $('body');
// Load initial empty page
body.empty()
body.append(gameview());
// Load game board
let game = new Game(4);
loadGame(game);
attachEventHandlers(game);
// Set up new game button
$('#newgame-button').on('click', () => {
reloadGame(new Game(4));
});
}
$(document).ready(initializePage());