-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
202 lines (176 loc) · 7.59 KB
/
index.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser-arcade-physics.min.js"></script>
</head>
<body>
<script>
var config = {
type: Phaser.AUTO,
width: 600,
height: 600,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 200 }
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
var game = new Phaser.Game(config);
var gameState = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]];
var turn = 1;
var images = [];
var zones = [];
var gameOver = false;
function preload() {
this.load.image('x', 'assets/x.png');
this.load.image('o', 'assets/o.png');
this.load.image('background', 'assets/background.png');
}
function create() {
var board = this.add.image(300, 300, 'background');
//board zones
zones.push(this.add.zone(100, 100, 200, 200).setName('TopLeft').setInteractive());
zones.push(this.add.zone(300, 100, 200, 200).setName('Top').setInteractive());
zones.push(this.add.zone(500, 100, 200, 200).setName('TopRight').setInteractive());
zones.push(this.add.zone(100, 300, 200, 200).setName('Left').setInteractive());
zones.push(this.add.zone(300, 300, 200, 200).setName('Middle').setInteractive());
zones.push(this.add.zone(500, 300, 200, 200).setName('Right').setInteractive());
zones.push(this.add.zone(100, 500, 200, 200).setName('BottomLeft').setInteractive());
zones.push(this.add.zone(300, 500, 200, 200).setName('Bottom').setInteractive());
zones.push(this.add.zone(500, 500, 200, 200).setName('BottomRight').setInteractive());
//Zone states determine if the space is empty or filled. All start empty
for (var i = 0; i < zones.length; i++) {
zones[i].state = 0;
}
this.input.on('gameobjectdown', function (pointer, gameObject) {
if (gameObject.state == 0) {
//render piece
images.push(this.add.image(gameObject.x, gameObject.y, turn == 1 ? 'x' : 'o'));
// update state and check win condition
if (updateGameState(gameObject, turn)) {
let doAlert = () => { alert(turn == 1 ? 'O WINS' : 'X WINS') }
setTimeout(doAlert, 500)
gameOver = true;
}
else if (checkGameDraw()){
let doAlert = () => { alert("Board is filled, draw")}
setTimeout(doAlert, 500)
gameOver = true;
}
//Mark zone filled so we can no longer place a piece there
gameObject.state = 1;
//Switch turns
turn == 1 ? turn = 2 : turn = 1;
}
}, this)
}
// This function serves to update the array holding the game state, as well as returns true when a victory condition is met
function updateGameState(gameObject, turn) {
if (gameObject.name == 'TopLeft') {
gameState[0][0] = turn == 1 ? 1 : 2;
if (gameState[0][1] == turn && gameState[0][2] == turn ||
gameState[1][1] == turn && gameState[2][2] == turn ||
gameState[1][0] == turn && gameState[2][0] == turn) {
return true;
}
}
else if (gameObject.name == 'Top') {
gameState[0][1] = turn == 1 ? 1 : 2;
if (gameState[0][0] == turn && gameState[0][2] == turn ||
gameState[1][1] == turn && gameState[2][1] == turn) {
return true;
}
}
else if (gameObject.name == 'TopRight') {
gameState[0][2] = turn == 1 ? 1 : 2;
if (gameState[0][0] == turn && gameState[0][1] == turn ||
gameState[2][0] == turn && gameState[1][1] == turn ||
gameState[1][2] == turn && gameState[2][2] == turn) {
return true;
}
}
else if (gameObject.name == 'Left') {
gameState[1][0] = turn == 1 ? 1 : 2;
if (gameState[1][1] == turn && gameState[1][2] == turn ||
gameState[0][0] == turn && gameState[2][0] == turn) {
return true;
}
}
else if (gameObject.name == 'Middle') {
gameState[1][1] = turn == 1 ? 1 : 2;
if (gameState[1][0] == turn && gameState[1][2] == turn ||
gameState[0][1] == turn && gameState[2][1] == turn ||
gameState[0][0] == turn && gameState[2][2] == turn ||
gameState[0][2] == turn && gameState[2][0] == turn) {
return true;
}
}
else if (gameObject.name == 'Right') {
gameState[1][2] = turn == 1 ? 1 : 2;
if (gameState[1][0] == turn && gameState[1][1] == turn ||
gameState[0][2] == turn && gameState[2][2] == turn) {
return true;
}
}
else if (gameObject.name == 'BottomLeft') {
gameState[2][0] = turn == 1 ? 1 : 2;
if (gameState[0][0] == turn && gameState[1][0] == turn ||
gameState[1][1] == turn && gameState[0][2] == turn ||
gameState[2][1] == turn && gameState[2][2] == turn) {
return true;
}
}
else if (gameObject.name == 'Bottom') {
gameState[2][1] = turn == 1 ? 1 : 2;
if (gameState[2][0] == turn && gameState[2][2] == turn ||
gameState[0][1] == turn && gameState[1][1] == turn) {
return true;
}
}
else if (gameObject.name == 'BottomRight') {
gameState[2][2] = turn == 1 ? 1 : 2;
if (gameState[2][0] == turn && gameState[2][1] == turn ||
gameState[0][0] == turn && gameState[1][1] == turn ||
gameState[0][2] == turn && gameState[1][2] == turn) {
return true;
}
}
}
//Checks if the game ends up in a draw
function checkGameDraw(){
if (gameState[0][0] != 0 && gameState[0][1] != 0 && gameState[0][2] != 0 &&
gameState[1][0] != 0 && gameState[1][1] != 0 && gameState[1][2] != 0 &&
gameState[2][0] != 0 && gameState[2][1] != 0 && gameState[2][2] != 0
)
return true;
}
function update() {
if (gameOver) {
let restart = () => { restartGame() }
setTimeout(restart, 500)
}
}
function restartGame() {
gameState = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]];
turn = 1;
for (var i = 0; i < images.length; i++) {
images[i].destroy();
}
for (var i = 0; i < zones.length; i++) {
zones[i].state = 0;
}
gameOver = false;
}
</script>
</body>
</html>