Skip to content

Commit 1b072cb

Browse files
committed
Add game logic
1 parent 4b7ac7b commit 1b072cb

File tree

5 files changed

+89
-18
lines changed

5 files changed

+89
-18
lines changed

css/style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ main {
2525
.square {
2626
width: auto;
2727
height: auto;
28+
color: white;
2829
background-color: black;
2930
}
3031

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
<div>Player 1 (x)</div>
2929
<div>Tie</div>
3030
<div>Player 2 (o)</div>
31-
<div>4</div>
32-
<div>1</div>
33-
<div>0</div>
31+
<div id="x-wins">0</div>
32+
<div id="ties">0</div>
33+
<div id="o-wins">0</div>
3434
</div>
3535
</main>
3636

js/app.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import navigateTo from './navigation.js';
1+
import Game from './game.js';
22

3-
document.getElementById('gameBtn').addEventListener('click', () => navigateTo('game'));
4-
document.getElementById('leaderboardBtn').addEventListener('click', () => navigateTo('leaderboard'));
5-
6-
navigateTo('home');
3+
const game = new Game();

js/game.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
export default class Game {
2+
3+
#state;
4+
#board;
5+
6+
#onTurn = 'X'; // Odd rounds => X starts, even rounds => O starts
7+
#xWins = 0;
8+
#oWins = 0;
9+
#ties = 0;
10+
11+
constructor() {
12+
this.#state = ['', '', '', '', '', '', '', '', ''];
13+
this.#board = document.querySelectorAll('.square');
14+
15+
this.#board.forEach((square, index) => square.addEventListener('click', e => this.#handleClick(e, index)));
16+
}
17+
18+
#handleClick(e, index) {
19+
// Square already taken
20+
if (this.#state[index] !== '') {
21+
return;
22+
}
23+
24+
this.#state[index] = this.#onTurn;
25+
e.target.textContent = this.#onTurn;
26+
this.#onTurn = this.#onTurn === 'X' ? 'O' : 'X';
27+
28+
const winner = this.#checkWinner();
29+
30+
if (winner !== null) {
31+
this.#endRound(winner);
32+
}
33+
}
34+
35+
#checkWinner() {
36+
const winningCombos = [
37+
[0, 1, 2], [3, 4, 5], [6, 7, 8], // rows
38+
[0, 3, 6], [1, 4, 7], [2, 5, 8], // columns
39+
[0, 4, 8], [2, 4, 6] // diagonals
40+
];
41+
42+
for (const combo of winningCombos) {
43+
const [a, b, c] = combo;
44+
45+
if (this.#state[a] && this.#state[a] === this.#state[b] && this.#state[a] === this.#state[c]) {
46+
return this.#state[a];
47+
}
48+
}
49+
50+
// No empty squares without a winner => tie
51+
if (!this.#state.includes('')) {
52+
return 'T';
53+
}
54+
55+
return null;
56+
}
57+
58+
#endRound(winner) {
59+
if (winner === 'T') {
60+
this.#ties++;
61+
} else if (winner === 'X') {
62+
this.#xWins++;
63+
} else if (winner === 'O') {
64+
this.#oWins++;
65+
}
66+
67+
this.#updateScore();
68+
this.#resetBoard();
69+
}
70+
71+
#updateScore() {
72+
document.querySelector('#x-wins').textContent = this.#xWins;
73+
document.querySelector('#o-wins').textContent = this.#oWins;
74+
document.querySelector('#ties').textContent = this.#ties;
75+
}
76+
77+
#resetBoard() {
78+
// Even number of rounds => next round is odd => X starts (and vice versa)
79+
this.#onTurn = (this.#ties + this.#xWins + this.#oWins) % 2 === 0 ? 'X' : 'O';
80+
this.#state = ['', '', '', '', '', '', '', '', ''];
81+
this.#board.forEach(square => square.textContent = '');
82+
}
83+
}

js/navigation.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)