Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
let handleTurnTaken = (turnTakenEvent) => {
if(turnTakenEvent.gameboard[turnTakenEvent.row][turnTakenEvent.col] !== 'X' && turnTakenEvent.gameboard[turnTakenEvent.row][turnTakenEvent.col] !== 'O') {
console.log('Player ' + turnTakenEvent.player + ' has taken a turn');
let nextPlayer = turnTakenEvent.player === 'O' ? 'X' : 'O';
let nextGameBoard = turnTakenEvent.gameboard.map(
(row, rIndex) => {
return row.map((col, cIndex) => {
return rIndex === turnTakenEvent.row && cIndex === turnTakenEvent.col ?
turnTakenEvent.player : col
});
}
)
if (winChecker.thereIsAWinner(nextGameBoard)) {
eventDispatcher.dispatch({
name: 'winner-determined-event',
winner: turnTakenEvent.player,
gameboard: nextGameBoard
});
}
else if (winChecker.gameBoardIsFull(nextGameBoard)){
eventDispatcher.dispatch({
name: 'completed-with-no-winner-event',
gameboard: nextGameBoard
});
} else {
eventDispatcher.dispatch({
name: 'game-board-updated-event',
nextPlayer: nextPlayer,
gameboard: nextGameBoard
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment