MCPcopy Create free account
hub / github.com/codemistic/Web-Development / validateBoard

Function validateBoard

bot-human-tictactoe/script.js:172–226  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

170 }
171
172 const validateBoard = ()=>{
173 //check rows
174 for(let i = 0; i < _state.length; i++){
175 if(_state[i][0] == _state[i][1] && _state[i][1] == _state[i][2] && _state[i][0] != 0){
176 _winner = _state[i][0]==1?PLAYER_X:PLAYER_Y
177 console.log(_winner);
178 _gameOver = true
179 }
180 }
181 //check columns
182 for(let i = 0; i < _state.length; i++){
183 if(_state[0][i] == _state[1][i] && _state[1][i] == _state[2][i] && _state[0][i] != 0){
184 _winner = _state[0][i]==1?PLAYER_X:PLAYER_Y
185 console.log(_winner);
186 _gameOver = true
187 }
188 }
189 //check diagonals
190 if(_state[0][0] == _state[1][1] && _state[1][1] == _state[2][2] && _state[0][0] != 0){
191 _winner = _state[0][0]==1?PLAYER_X:PLAYER_Y
192 console.log(_winner);
193 _gameOver = true
194 }
195 if(_state[0][2] == _state[1][1] && _state[1][1] == _state[2][0] && _state[0][2] != 0){
196 _winner = _state[0][2]==1?PLAYER_X:PLAYER_Y
197 console.log(_winner);
198 _gameOver = true
199 }
200 //check draw
201 let isTie = true
202 for(let i=0;i<grid_side;i++){
203 for(let j=0;j<grid_side;j++){
204 if(_state[i][j]==0){
205 isTie = false
206 }
207 }
208 }
209 if(isTie){
210 _winner = "tie"
211 _gameOver = true
212 }
213 if(_gameOver){
214 cells.forEach(cell=>{
215 cell.gameOver()
216 })
217 }
218 if(_gameOver){
219 if(isTie)
220 document.querySelector(".result").textContent = "Game Tied"
221 else if(_winner == PLAYER_X)
222 document.querySelector(".result").textContent = "Player X Won"
223 else if(_winner == PLAYER_Y)
224 document.querySelector(".result").textContent = "Player O Won"
225 }
226 }
227 const reset = ()=>{
228 _gameOver = false
229 _currPlayer = PLAYER_X

Callers 1

selectFunction · 0.85

Calls 1

logMethod · 0.45

Tested by

no test coverage detected