-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tic_Tac_Toe.js
197 lines (175 loc) · 7.22 KB
/
Tic_Tac_Toe.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
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
/********************************************************************************************
* *
* project: Tic Tac Toe in web *
* author: Štěpán Michálek *
* purpose: make a working Tic Tac Toe using: html, css, bootstrap and js *
* *
********************************************************************************************/
//Initialize all the variables
var fieldOne = document.querySelector("#one");
var fieldTwo = document.querySelector("#two");
var fieldThree = document.querySelector("#three");
var fieldFour = document.querySelector("#four");
var fieldFive = document.querySelector("#five");
var fieldSix = document.querySelector("#six");
var fieldSeven = document.querySelector("#seven");
var fieldEight = document.querySelector("#eight");
var fieldNine = document.querySelector("#nine");
var progressBar = document.querySelector("#progressBar");
var restartButton = document.querySelector("#resbtn");
var scoreO = document.querySelector("#scoreO");
var scoreX = document.querySelector("#scoreX");
var scoreOCounter = 0;
var scoreXCounter = 0;
var player = "O";
var gamesPlayed = 0;
var numericToGamefield = {}
var gameField = [
0,
fieldOne,
fieldTwo,
fieldThree,
fieldFour,
fieldFive,
fieldSix,
fieldSeven,
fieldEight,
fieldNine,
]
for (let i = 1; i <=9; i++) {
gameField[i].style.color = "#a6a6a6";
}
// function to check the gamestate
function gameState (gameField){
// returns: 0 for in progress, 1 for a win, 2 for a tie
var result = 0;
if (gameField[1].textContent!=="1" && gameField[2].textContent!=="2" && gameField[3].textContent!=="3" && gameField[4].textContent!=="4" && gameField[5].textContent!=="5" && gameField[6].textContent!=="6" && gameField[7].textContent!=="7" && gameField[8].textContent!=="8" && gameField[9].textContent!=="9") {
result = 2;
}
if (gameField[1].textContent === gameField[2].textContent && gameField[2].textContent === gameField[3].textContent) {
result = 1;
}else if (gameField[4].textContent === gameField[5].textContent && gameField[5].textContent === gameField[6].textContent) {
result = 1;
}else if (gameField[7].textContent === gameField[8].textContent && gameField[8].textContent === gameField[9].textContent) {
result = 1;
}else if (gameField[1].textContent === gameField[4].textContent && gameField[4].textContent === gameField[7].textContent) {
result = 1;
}else if (gameField[2].textContent === gameField[5].textContent && gameField[5].textContent === gameField[8].textContent) {
result = 1;
}else if (gameField[3].textContent === gameField[6].textContent && gameField[6].textContent === gameField[9].textContent) {
result = 1;
}else if (gameField[1].textContent === gameField[5].textContent && gameField[5].textContent === gameField[9].textContent) {
result = 1;
}else if (gameField[7].textContent === gameField[5].textContent && gameField[5].textContent === gameField[3].textContent) {
result = 1;
}
return result
}
function restartGame(){
for (let i = 1; i <= 9; i++){
gameField[i].textContent = i;
gameField[i].style.color = "#a6a6a6";
}
player = "O";
switch (gamesPlayed) {
case 0:
progressBar.textContent = "Current state: A new game has started.";
break;
case 1:
progressBar.textContent = "Current state: Yet another game has started.";
break;
case 2:
progressBar.textContent = "Current state: YET another game has started.";
break;
case 3:
progressBar.textContent = "Current state: AND ANOTHER game has started.";
break;
case 4:
progressBar.textContent = "Current state: AND ANOTHER one.";
break;
case 5:
progressBar.textContent = "Current state: AND ANOTHER ONE!";
break;
case 6:
progressBar.textContent = "Current state: AND ANOTHER ONE!!!";
break;
case 7:
progressBar.textContent = "Current state: ANOTHER ONE !?!?!";
break;
case 8:
progressBar.textContent = "Current state: Seriously, how many games do you wanna play?";
break;
case 9:
progressBar.textContent = "Current state: Don't you have work to do?";
break;
case 10:
progressBar.textContent = "Current state: Or do you wanna break my project?";
break;
case 11:
progressBar.textContent = "Current state: You know it costs money to reset the game everytime...";
break;
case 12:
progressBar.textContent = "Current state: And you might even destroy the button.";
break;
case 13:
progressBar.textContent = "Current state: Think about it's poor life, what has he ever done to you?";
break;
case 14:
progressBar.textContent = "Current state: You're a monster...";
break;
case 15:
progressBar.textContent = "Current state: I have no reason to talk to you anymore >:(";
break;
default:
progressBar.textContent = "Current state: Game "+gamesPlayed+" started";
break;
}
gamesPlayed++;
}
restartButton.addEventListener("click",function(){
restartGame();
})
function checkField(x){
if (gameState(gameField) === 0) {
if (gameField[x].textContent==="O"||gameField[x].textContent==="X"){
alert("You cannot do that, please pick an empty field.");
}else{
gameField[x].textContent = player;
gameField[x].style.color = "black";
player = player === "O" ? "X" : "O";
}
if (gameState(gameField) === 2) {
alert("It's a tie.");
progressBar.textContent = "Current state: The game was a tie.";
}
if (gameState(gameField) === 1) {
alert("Player "+(player === "O" ? "X" : "O")+" has won");
scoreOCounter += 1*(player!=="O");
scoreXCounter += 1*(player!=="X");
scoreO.textContent = scoreOCounter;
scoreX.textContent = scoreXCounter;
progressBar.textContent = "Current state: Player "+(player === "O" ? "X" : "O")+" has won.";
}
if (gameState(gameField) === 0) {
progressBar.textContent = "Current state: Game in progress."
}
}else{
alert("The game has ended, please use the restart button.");
}
}
// for loop to initialize all the event listeners
for (let i = 1; i <= 9; i++) {
gameField[i].addEventListener("click",function(){
checkField(i);
})
}
function numericInput(event){
//console.log(event.charCode);
if (event.charCode > 48 && event.charCode < 58) {
i = event.charCode - 48;
checkField(i);
}
if (event.charCode === 114) {
restartGame();
}
}