Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
let view = function() {
let viewModel = {
currentPlayer: 'X',
winner: null
};
let gameBoard = new Array(3).fill(new Array(3).fill(''));
eventDispatcher.subscribe('game-board-updated-event', gameBoardUpdatedEvent => {
viewModel.currentPlayer = gameBoardUpdatedEvent.nextPlayer;
gameBoard = gameBoardUpdatedEvent.gameboard;
drawBoard(gameBoard);
});
eventDispatcher.subscribe('winner-determined-event', winnerDeterminedEvent => {
viewModel.winner = winnerDeterminedEvent.winner;
gameBoard = winnerDeterminedEvent.gameboard;
drawBoard(gameBoard);
});
eventDispatcher.subscribe('completed-with-no-winner-event', completedWithNoWinnerEvent => {
viewModel.winner = 'NO_WINNER';
gameBoard = completedWithNoWinnerEvent.gameboard;
drawBoard(gameBoard);
});
document.getElementById('replay-button').addEventListener('click', () => {
viewModel = {
currentPlayer: 'X',
winner: null
};
let gameboard = new Array(3).fill(new Array(3).fill(''));
drawBoard(gameboard);
eventDispatcher.replay();
});
const drawBoard = (gameBoard) => {
let heading = '';
if (viewModel.winner === 'NO_WINNER') {
heading = 'Game completed with no winner';
} else if (viewModel.winner) {
heading = viewModel.winner + ' has won!!'
} else {
heading = 'Current Player is: ' + viewModel.currentPlayer;
}
let headingView = document.getElementById('heading');
headingView.textContent = heading;
let gameBoardView = document.getElementById('gameBoard');
...
...
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment