-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamesrc.js
130 lines (109 loc) · 3.19 KB
/
gamesrc.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
function resetGameStatus() {
activePlayer = 0;
currentRound = 1;
gameIsOver = false;
gameOverElement.firstElementChild.innerHTML =
'You won, <span id="winner-name">PLAYER NAME</span>!';
gameOverElement.style.display = 'none';
let gameBoardIndex = 0;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
gameData[i][j] = 0;
const gameBoardItemElement = gameBoardElement.children[gameBoardIndex];
gameBoardItemElement.textContent = '';
gameBoardItemElement.classList.remove('disabled');
gameBoardIndex++;
}
}
}
function startNewGame() {
if (players[0].name === '' || players[1].name === '') {
alert('Please set custom player names for both players!');
return;
}
resetGameStatus();
activePlayerNameElement.textContent = players[activePlayer].name;
gameAreaElement.style.display = 'block';
}
function switchPlayer() {
if (activePlayer === 0) {
activePlayer = 1;
} else {
activePlayer = 0;
}
activePlayerNameElement.textContent = players[activePlayer].name;
}
function selectGameField(event) {
if (event.target.tagName !== 'LI' || gameIsOver) {
return;
}
const selectedField = event.target;
const selectedColumn = selectedField.dataset.col - 1;
const selectedRow = selectedField.dataset.row - 1;
if (gameData[selectedRow][selectedColumn] > 0) {
alert('Please select an empty field!');
return;
}
selectedField.textContent = players[activePlayer].symbol;
selectedField.classList.add('disabled');
gameData[selectedRow][selectedColumn] = activePlayer + 1;
const winnerId = checkForGameOver();
if (winnerId !== 0) {
endGame(winnerId);
}
currentRound++;
switchPlayer();
}
function checkForGameOver() {
// Checking the rows for equality
for (let i = 0; i < 3; i++) {
if (
gameData[i][0] > 0 &&
gameData[i][0] === gameData[i][1] &&
gameData[i][1] === gameData[i][2]
) {
return gameData[i][0];
}
}
// Checking the columns for equality
for (let i = 0; i < 3; i++) {
if (
gameData[0][i] > 0 &&
gameData[0][i] === gameData[1][i] &&
gameData[0][i] === gameData[2][i]
) {
return gameData[0][i];
}
}
// Diagonal: Top left to bottom right
if (
gameData[0][0] > 0 &&
gameData[0][0] === gameData[1][1] &&
gameData[1][1] === gameData[2][2]
) {
return gameData[0][0];
}
// Diagonal: Bottom left to top right
if (
gameData[2][0] > 0 &&
gameData[2][0] === gameData[1][1] &&
gameData[1][1] === gameData[0][2]
) {
return gameData[2][0];
}
if (currentRound === 9) {
return -1;
}
return 0;
}
function endGame(winnerId) {
gameIsOver = true;
gameOverElement.style.display = 'block';
if (winnerId > 0) {
const winnerName = players[winnerId - 1].name;
gameOverElement.firstElementChild.firstElementChild.textContent =
winnerName;
} else {
gameOverElement.firstElementChild.textContent = "It's a draw!";
}
}