This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <E> dropWhile(list: List<E>, f: (E) -> Boolean): List<E> { | |
tailrec fun loop(remaining: List<E>): List<E> = | |
when(remaining) { | |
is Empty -> remaining | |
is Cons -> if (f(remaining.head)) loop(remaining.tail) else remaining | |
} | |
return loop(list) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const tests = () => { | |
let testsPassed; | |
let gameboardWithNoCompletedRowsOrColumnsButAllCellsCompleted = [ | |
['X','O', 'X'], | |
['O','O', 'X'], | |
['O','X', 'O'] | |
] | |
testsPassed = !winChecker.thereIsAWinner(gameboardWithNoCompletedRowsOrColumnsButAllCellsCompleted); | |
console.log('Test thereIsAWinner gameboardWithNoCompletedRowsOrColumnsButAllCellsCompleted ' + testsPassed); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let eventDispatcher = function() { | |
let replaying = false; | |
let events = []; | |
let subscribers = {}; | |
let dispatchEvent = (ev) => { | |
if (!replaying) { | |
events.push(ev); | |
} | |
for (const sub of subscribers[ev.name]) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cellView.addEventListener('click', | |
() => gameEngine.handleEvent( | |
{ | |
name: 'turn-taken-event', | |
player: viewModel.currentPlayer, | |
row: rIndex, | |
col: cIndex, | |
gameboard: gameBoard | |
})); |
OlderNewer