-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
139 lines (136 loc) · 4.22 KB
/
script.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
//@ts-check
let currentPlayer = "o";
let freeCells = [1,2,3,4,5,6,7,8,9];
let oCells = [];
let xCells = [];
let boardLocked = true;
function setPlayerButton(){
if(currentPlayer === "o"){
currentPlayer = "x";
// @ts-ignore
document.getElementById("setPlayerButton").innerHTML="x starts"
}else{
currentPlayer = "o";
// @ts-ignore
document.getElementById("setPlayerButton").innerHTML="o starts"
}
}
function reactiveButton(){
if(document.getElementById("startStopButton")?.innerHTML == "start"){
handleStart();
// @ts-ignore
document.getElementById("startStopButton").innerHTML = "stop";
}else if(document.getElementById("startStopButton")?.innerHTML == "stop"){
// @ts-ignore
document.getElementById("startStopButton").innerHTML = "resume";
boardLocked = true;
}else if(document.getElementById("startStopButton")?.innerHTML == "resume"){
// @ts-ignore
document.getElementById("startStopButton").innerHTML = "stop";
boardLocked = false;
}
}
function resetButton(){
if(document.getElementById("startStopButton")?.innerHTML != "start"){
// @ts-ignore
document.getElementById("startStopButton").innerHTML = "start";
handleReset()
}
}
function handleStart(){
boardLocked = false;
// @ts-ignore
document.getElementById("statusText").innerHTML = currentPlayer+"'s move.";
}
function handleReset(){
boardLocked = true;
freeCells = [1,2,3,4,5,6,7,8,9];
oCells = [];
xCells = [];
let i = 0;
while(i < 9){
let elementName = "cell" + freeCells[i];
// @ts-ignore
document.getElementById(elementName).innerHTML = freeCells[i];
// @ts-ignore
document.getElementById(elementName).classList.remove("red", "blue");
// @ts-ignore
document.getElementById("statusText").innerHTML = "Click start button to start the game.";
i++;
}
}
function switchPlayer(){
if(currentPlayer === "o"){
currentPlayer = "x";
// @ts-ignore
document.getElementById("statusText").innerHTML = "x's move...";
}else{
currentPlayer = "o";
// @ts-ignore
document.getElementById("statusText").innerHTML = "o's move...";
}
}
function handleClick(cell){
if(!boardLocked){
if(isFree(cell)){
claimCell(cell);
updateCell(cell);
if(isWinner()){
boardLocked = true;
// @ts-ignore
document.getElementById("statusText").innerHTML = currentPlayer + " won the game.";
}else{
switchPlayer();
}
}else{
// @ts-ignore
document.getElementById("statusText").innerHTML = "this cell is already claimed!";
}
}
}
function isFree(cell){
if(freeCells.includes(cell)){
return true;
}else{
return false;
}
}
function claimCell(cell){
if(currentPlayer == "o"){
oCells.push(cell);
}else{
xCells.push(cell);
}
let index = freeCells.indexOf(cell);
freeCells.splice(index, 1); //remove claimed cell from freeCells
}
function updateCell(cell){
let elementName = "cell" + cell;
// @ts-ignore
document.getElementById(elementName).innerHTML = currentPlayer;
if(currentPlayer == "o"){
// @ts-ignore
document.getElementById(elementName).classList.add("red");
}else{
// @ts-ignore
document.getElementById(elementName).classList.add("blue");
}
}
function isWinner() {
const winningCombinations = [
[1, 2, 3], [4, 5, 6], [7, 8, 9],
[1, 4, 7], [2, 5, 8], [3, 6, 9],
[1, 5, 9], [3, 5, 7]
];
for(let combo of winningCombinations) {
if(oCells.includes(combo[0]) && oCells.includes(combo[1]) && oCells.includes(combo[2])) {
currentPlayer = 'o'; // setting the currentPlayer to o, as o is the winner.
return true;
}
if(xCells.includes(combo[0]) && xCells.includes(combo[1]) && xCells.includes(combo[2])) {
currentPlayer = 'x'; // setting the currentPlayer to x, as x is the winner.
return true;
}
}
return false;
}