-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
217 lines (198 loc) · 7.47 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const Mancala = () => {
// DOM elements
const $player1Score = document.getElementById('player-1-score');
const $player1Pits = Array.from(document.getElementById('player-1-pits').children);
const $player2Score = document.getElementById('player-2-score');
const $player2Pits = Array.from(document.getElementById('player-2-pits').children);
const $restartButton = document.querySelector('button');
const $turn = document.getElementById('turn');
const $count = document.getElementById('count');
const glowClass = 'game__pit--glow';
let currentPit;
let $currentHighlightedPit;
let interval = null;
const PLAYER_1 = 'player-1';
const PLAYER_2 = 'player-2'
const playersId = [PLAYER_1, PLAYER_2];
let currentHighlightedId = playersId[0];
let currentTurn = PLAYER_1;
// Stop interval function
const stopInterval = () => {
clearInterval(interval);
interval = null;
}
/**
* Once restart button is clicked, remove the focus from that button.
* Remove event listener from the keyboard because it`ll be initialized in the initializeGame function.
* Stop interval and re-initialize the game.
*/
const restartGame = (e) => {
e.target.blur();
document.removeEventListener('keyup', handleKeyboard);
stopInterval();
initializeGame();
}
/**
* Stop the tile move and check for current number.
* If the current pit is any of the score pit, then the same player will get to choose again.
* Else if the current pit number is greater than 1 then call moveGlowTile function again.
* Else other player will get the chance to choose and play.
*/
const stopTileMove = () => {
stopInterval();
const currentHighlightedPitNumber = parseInt($currentHighlightedPit.textContent);
const currentHighlightedPitId = $currentHighlightedPit.id;
if (currentHighlightedPitId === 'player-1-score' || currentHighlightedPitId === 'player-2-score') {
currentHighlightedId = currentHighlightedPitId === 'player-1-score' ? PLAYER_1 : PLAYER_2;
currentPit = 0;
document.addEventListener('keyup', handleKeyboard, false);
glowTile();
} else if (currentHighlightedPitNumber > 1) {
moveGlowTileAndReducePitNumber();
} else {
currentTurn = currentTurn === PLAYER_1 ? PLAYER_2 : PLAYER_1;
currentPit = 0;
document.addEventListener('keyup', handleKeyboard, false);
chooseTurn(true);
glowTile();
}
}
const checkForWin = () => {
const isPlayer1Empty = $player1Pits.every($ele => parseInt($ele.textContent) === 0);
const isPlayer2Empty = $player2Pits.every($ele => parseInt($ele.textContent) === 0);
if (isPlayer1Empty || isPlayer2Empty) {
stopInterval();
const player1Score = parseInt($player1Score.textContent);
const player2Score = parseInt($player2Score.textContent);
if(player1Score > player2Score) {
alert('Player 1 won');
} else if (player1Score < player2Score) {
alert('Player 2 won');
} else {
alert('It`s a tie')
}
document.removeEventListener('keyup', handleKeyboard);
initializeGame();
}
}
/**
* Once pit is selected, check its digit and store it in the count.
* Start the the interval function and move anti clock wise while reducing from the count and
* increasing the count of the other highlighted pits.
*/
const moveGlowTileAndReducePitNumber = () => {
const $reducingPit = document.getElementById(`${currentHighlightedId}-pit-${currentPit}`);
$count.textContent = $reducingPit.textContent;
$reducingPit.textContent = 0;
let isHighlightScore = false;
interval = setInterval(() => {
const currentSelectedPitNumber = parseInt($count.textContent);
if(currentSelectedPitNumber === 0) {
stopTileMove();
return;
}
$count.textContent = currentSelectedPitNumber - 1;
// if current highlighted pit is player-1`s and pit`s index is 0
if (currentHighlightedId === PLAYER_1 && currentPit === 0) {
if (!isHighlightScore && currentTurn === PLAYER_1) {
glowTile(true, $player1Score);
isHighlightScore = true;
} else {
isHighlightScore = false;
currentHighlightedId = PLAYER_2;
glowTile(true);
}
} else if (currentHighlightedId === PLAYER_1) {
currentPit -= 1
glowTile(true);
} else if (currentHighlightedId === PLAYER_2 && currentPit === 5) {
if (!isHighlightScore && currentTurn === PLAYER_2) {
glowTile(true, $player2Score);
isHighlightScore = true;
} else {
isHighlightScore = false;
currentHighlightedId = PLAYER_1;
glowTile(true);
}
} else if (currentHighlightedId === PLAYER_2) {
currentPit += 1;
glowTile(true);
}
// check for end condition
checkForWin();
}, 1000);
}
/**
* If player has chosen a pit then isIncrease will be true and tile hightlighted will be increased by 1.
* In case of player's pit, hightighted element will be the player score tile.
*/
const glowTile = (isIncrease = false, highlightedElement) => {
$currentHighlightedPit?.classList.remove(glowClass);
$currentHighlightedPit = highlightedElement ?? document.getElementById(`${currentHighlightedId}-pit-${currentPit}`);
isIncrease && ($currentHighlightedPit.textContent = parseInt($currentHighlightedPit.textContent) + 1);
$currentHighlightedPit.classList.add(glowClass);
}
/**
* If the keyboard event is Enter that measn the player has chosen a tile
* and we need to disable keyboard event. Also call the moveGlowTile function.
* If the keyboard is arrow left or right, glow the tile whichever gets hightlighted.
*/
const handleKeyboard = (e) => {
const keyBoardKey = e.key;
if (keyBoardKey === 'Enter'){
document.removeEventListener('keyup', handleKeyboard)
moveGlowTileAndReducePitNumber();
} else if (keyBoardKey === 'ArrowRight') {
const nextPit = currentPit + 1;
currentPit = nextPit > 5 ? 0 : nextPit;
glowTile();
} else if (keyBoardKey === 'ArrowLeft') {
const previousPit = currentPit - 1;
currentPit = previousPit < 0 ? 5 : previousPit;
glowTile();
}
}
/**
* Choose turn on random selection.
* If the passed boolean value is false (or not passed), the random selection will be done.
* Else whichever is currenty highlighted will be given the turn.
*/
const chooseTurn = (isChangeTurn) => {
if (!isChangeTurn) {
const rand = Math.random()
currentTurn = rand > 0.5 ? playersId[0] : playersId[1];
}
currentHighlightedId = currentTurn;
$turn.textContent = currentTurn === PLAYER_1 ? 'Player 1' : 'Player 2';
}
/**
* Add events.
* Restart the game.
* Keyboard events for right, left and enter key.
*/
const initializeEvents = () => {
$restartButton.addEventListener('click', restartGame, false);
document.addEventListener('keyup', handleKeyboard, false)
}
// Initialize boards
const initializeBoard = () => {
currentPit = 0;
chooseTurn();
glowTile();
$player1Score.textContent = '0';
$player2Score.textContent = '0';
$count.textContent = '0';
// DOM children needs to be iterative.
$player1Pits.forEach($ele => $ele.textContent = '6');
$player2Pits.forEach($ele => $ele.textContent = '6');
}
const initializeGame = () => {
initializeBoard();
initializeEvents();
}
return {
initializeGame
}
}
const mancala = Mancala();
mancala.initializeGame();