-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tic-Tac-Toe.html
223 lines (184 loc) · 4.41 KB
/
Tic-Tac-Toe.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!DOCTYPE html>
<html>
<head>
<title>Tic-Tac-Toe</title>
<style type="text/css">
canvas {
margin: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
</head>
<body>
<script type="text/javascript">
var tiles = [];
var navigator = {};
var fired = true;
var delay = 0;
var player = "0";
var indices = [
["00", "01", "02"], ["10", "11", "12"], ["20", "21", "22"],
["00", "10", "20"], ["01", "11", "21"], ["02", "12", "22"],
["00", "11", "22"], ["02", "11", "20"]
];
var canvas, ctx;
const ROWS = 3, COLS = 3;
const canvasWidth = 490;
const canvasHeight = 490;
var game = {
wins: "",
winner: "",
message: "",
start: function(tile) {
ctx.lineWidth = 5;
ctx.lineCap = "rounded";
ctx.fillStyle = "#000";
ctx.fillRect(tile.x, tile.y, tile.width, tile.height);
},
naughts: function(tile) {
var centerX = tile.x + tile.width/2;
var centerY = tile.y + tile.height/2;
ctx.strokeStyle = "#fff";
ctx.beginPath();
ctx.arc(centerX, centerY, tile.width/3, 0, Math.PI*2);
},
crosses: function(tile) {
ctx.strokeStyle = "#fff";
ctx.moveTo(tile.x + 25, tile.y + 25);
ctx.lineTo(tile.x + tile.width - 25, tile.y + tile.height - 25);
ctx.moveTo(tile.x + tile.width - 25, tile.y + 25);
ctx.lineTo(tile.x + 25, tile.y + tile.height - 25);
},
register: function(tile) {
if (tile.clicked() && !tile.state) {
if (fired) {
tile.state = player;
this.evaluateScore(tiles, player);
player = (player == '0') ? 'X' : '0';
}
}
},
noValidMoves: function() {
for (var i=0; i < ROWS; i++) {
for (var j=0; j < COLS; j++) {
var tile = tiles[i][j];
if (tile.state == null) {
return false;
}
}
}
return true;
},
evaluateScore: function(tile, p) {
for (var i=0; i < indices.length; i++) {
for(var j=0; j < indices[i].length; j++) {
var idx = indices[i][j].split("");
var each = tile[Number(idx[0])][Number(idx[1])];
if (each.state == p) {
this.wins += "1";
} else {
this.wins += "0";
}
if (j == 2) {
if (this.wins == "111") {
this.winner = p;
}
this.wins = "";
}
if (this.winner == 'X') {
this.message = "The Cross PLayer Has Won";
} else if (this.winner == '0') {
this.message = "The Naught PLayer Has Won";
} else if (this.noValidMoves()) {
this.message = "The Game Is A Tie";
}
}
}
},
over: function() {
alert(this.message);
location.reload();
}
}
function main() {
canvas = document.createElement("canvas");
ctx = canvas.getContext("2d");
canvas.width = canvasWidth;
canvas.height = canvasHeight;
document.body.appendChild(canvas);
canvas.addEventListener("mousedown", function(evt) {
var targ = evt.target;
navigator.x = evt.pageX-targ.offsetLeft;
navigator.y = evt.pageY-targ.offsetTop;
fired = false;
});
canvas.addEventListener("mouseup", function(evt) {
fired = true;
});
run();
}
main();
function run() {
init();
loop();
}
function loop() {
update();
window.requestAnimationFrame(loop);
}
function update() {
canvas.width = canvas.width;
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
for (var i=0; i < ROWS; i++) {
for (var j=0; j < COLS; j++) {
var $t = tiles[i][j];
game.start($t);
if ($t.state == '0') {
game.naughts($t);
} else if ($t.state == 'X') {
game.crosses($t);
}
ctx.stroke();
game.register($t);
}
}
if (game.message) {
delay++;
if (delay == 10) {
game.over();
}
}
}
function init() {
for (var x=0; x < ROWS; x++) {
tiles[x] = [];
for (var y=0; y < COLS; y++) {
tiles[x][y] = new Tile(y, x, [x, y]);
}
}
}
function Tile(x, y, index) {
const gap = 20;
const width = 150;
const height = 150;
this.x = x * (width + gap);
this.y = y * (height + gap);
this.width = width;
this.height = height;
this.state = null;
this.index = index;
this.clicked = function() {
if (navigator.x > this.x && navigator.x < this.x + this.width) {
if (navigator.y > this.y && navigator.y < this.y + this.height) {
return this.index;
}
}
}
}
</script>
</body>
</html>