Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (49 loc) · 1.83 KB
/
Copy pathscript.js
File metadata and controls
55 lines (49 loc) · 1.83 KB
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
$(document).ready(function(){
var move = 1;
var play = true;
$("#board tr td").click(function(){
if($(this).text() === "" && play) {
if((move % 2) === 1) {
$(this).append("X");
} else {
$(this).append("O");
}
move++;
if(winnerCheck() !== -1 && winnerCheck() !== "") {
if(winnerCheck() === "X"){
alert("Player 1 wins!");
} else {
alert("Player 2 wins!");
}
play = false;
}
}
});
function winnerCheck(){
var space1 = $("#board tr:nth-child(1) td:nth-child(1)").text();
var space2 = $("#board tr:nth-child(1) td:nth-child(2)").text();
var space3 = $("#board tr:nth-child(1) td:nth-child(3)").text();
var space4 = $("#board tr:nth-child(2) td:nth-child(1)").text();
var space5 = $("#board tr:nth-child(2) td:nth-child(2)").text();
var space6 = $("#board tr:nth-child(2) td:nth-child(3)").text();
var space7 = $("#board tr:nth-child(3) td:nth-child(1)").text();
var space8 = $("#board tr:nth-child(3) td:nth-child(2)").text();
var space9 = $("#board tr:nth-child(3) td:nth-child(3)").text();
// check rows
if ((space1==space2) && (space2==space3)) { return space3; }
else if ((space4==space5) && (space5==space6)) { return space6; }
else if ((space7==space8) && (space8==space9)) { return space9; }
// check columns
else if ((space1==space4) && (space4==space7)) { return space7; }
else if ((space2==space5) && (space5==space8)) { return space8; }
else if ((space3==space6) && (space6==space9)) { return space9; }
// check diagonals
else if ((space1==space5) && (space5==space9)) { return space9; }
else if ((space3==space5) && (space5==space7)) { return space7; }
// no winner
return -1;
}
$('#replay').click(function() {
location.reload();
});
});