-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
119 lines (100 loc) · 2.57 KB
/
main.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
console.log('Connecting....');
setTimeout(function() {
// console.clear();
console.log('... JavaScript file is connected');
}, 1000);
setTimeout(function() {
console.log('...Congratulations!');
}, 1500);
/**
* The Game
* Tic Tac Toe
*/
let scoreX = 0;
let scoreO = 0;
function startGame() {
document.getElementById('restart').setAttribute('hidden', true);
setMessage('Game is loading...');
document.turn = "X";
document.winner = null;
for(let i = 1; i <= 9; i++) {
clearBox(i);
}
console.log('game is loaded');
setTimeout(function() {
console.clear();
}, 4000);
setTimeout(function() {
setMessage(document.turn + ' will start with his first move!');
}, 1700);
}
function setMessage(msg) {
document.getElementById('message').innerText = msg;
}
function setScoreO() {
scoreO += 1;
document.getElementById('o-score').innerText = scoreO;
}
function setScoreX() {
scoreX += 1;
document.getElementById('x-score').innerText = scoreX;
}
function nextMove(square) {
console.log('nextMove: ', square);
if (document.winner !== null) {
setMessage(document.turn + ' already won!');
} else if (square.innerText === '') {
square.innerText = document.turn;
switchTurn();
} else {
setMessage(document.turn + ' You can\'t set here, pick an empty square!');
}
}
function switchTurn() {
setTimeout(function() {
document.getElementById('restart').removeAttribute("hidden");
}, 2000);
if(checkForWinner(document.turn)) {
setMessage('Congrats ' + document.turn + ', you won!');
if (document.turn === 'X') {
setScoreX();
} else {
setScoreO();
}
document.winner = document.turn;
} else if (document.turn === 'X') {
document.turn = 'O';
setMessage(document.turn + ' is the next player!');
} else {
document.turn = 'X';
setMessage(document.turn + ' is the next player!');
}
}
function checkForWinner(move) {
let result = false;
if(checkRow(1, 2, 3, move) ||
checkRow(4, 5, 6, move) ||
checkRow(7, 8, 9, move) ||
checkRow(1, 4, 7, move) ||
checkRow(2, 5, 8, move) ||
checkRow(3, 6, 9, move) ||
checkRow(1, 5, 9, move) ||
checkRow(3, 5, 7, move)
) {
result = true;
}
return result;
}
function checkRow(a, b, c, move) {
let result = false;
if (getBox(a) === move && getBox(b) === move && getBox(c) === move) {
result = true;
}
return result;
}
function getBox(number) {
return document.getElementById('s' + number).innerText;
}
function clearBox(number) {
document.getElementById('s' + number).innerText = '';
}