-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (119 loc) · 3.26 KB
/
index.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
var board = [];
for (var i = 1; i <=9; i++){
board[i] = document.getElementById("B" + i.toString());
}
var alertbox = document.getElementById("alert-box");
var turn = 1;
var playSymbol = "X"
var isOver = false;
var table = document.getElementById("table");
var play = document.getElementById("play-button");
var reset = document.getElementById("reset")
function pressPlay(){
play.style.display = "none";
table.style.display = "block";
reset.style.display = "block";
}
function turnPlayed(box){
if (board[box].innerHTML !== ""){
alertbox.innerHTML = "This box is already played";
return;
}
if (turn % 2 == 0){
playSymbol = "O";
}
if (isOver){
return ;
}
board[box].innerHTML = playSymbol;
var winningCondition = checkForWinners();
if (winningCondition > 0){
showWinner(winningCondition);
isOver = true;
}
else {
turn++;
if (turn == 10){
callDraw();
}
playSymbol = "X";
}
}
function checkForWinners(){
if (board[5].innerHTML === playSymbol){
if (board[1].innerHTML === playSymbol && board[9].innerHTML === playSymbol){
return 1;
}
else if (board[3].innerHTML === playSymbol && board[7].innerHTML === playSymbol){
return 2;
}
else if (board[4].innerHTML === playSymbol && board[6].innerHTML === playSymbol){
return 4;
}
else if (board[2].innerHTML === playSymbol && board[8].innerHTML === playSymbol){
return 7;
}
}
if (board[1].innerHTML === playSymbol){
if (board[2].innerHTML === playSymbol && board[3].innerHTML === playSymbol){
return 3;
}
if (board[4].innerHTML === playSymbol && board[7].innerHTML === playSymbol){
return 6;
}
}
if (board[9].innerHTML === playSymbol){
if (board[7].innerHTML === playSymbol && board[8].innerHTML === playSymbol){
return 5;
}
if (board[6].innerHTML === playSymbol && board[3].innerHTML === playSymbol){
return 8;
}
}
return -1;
}
function showWinner(winningCondition) {
var turnedElements = [];
if (winningCondition === 1){
turnedElements = [1, 5, 9];
}
else if(winningCondition === 2){
turnedElements = [3, 5, 7];
}
else if (winningCondition === 3){
turnedElements = [1, 2, 3];
}
else if (winningCondition === 4){
turnedElements = [4, 5, 6];
}
else if (winningCondition === 5){
turnedElements = [7, 8, 9];
}
else if (winningCondition === 6){
turnedElements = [1, 4, 7];
}
else if (winningCondition === 7){
turnedElements = [2, 5, 8];
}
else if (winningCondition === 8){
turnedElements = [3, 6, 9];
}
for (i of turnedElements){
board[i].className = "winner";
}
alertbox.innerHTML = playSymbol + " player has Won!";
isOver = true;
}
function callDraw(){
alertbox.innerHTML = "The game has ended in a Draw.";
}
function reset(){
for (var i = 1; i < board.length; i++){
board[i].innerHTML = "";
board[i].className = "";
}
alertbox.innerHTML = ""
turn = 1;
playSymbol = "X";
isOver = false;
}