This file contains hidden or 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 | |
})); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
tailrec fun <E> dropWhile(list: List<E>, f: (E) -> Boolean): List<E> = | |
when(list) { | |
Empty -> Empty | |
is Cons -> if (f(list.head)) dropWhile(list.tail, f) else list | |
} |
This file contains hidden or 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> = | |
when(list) { | |
Empty -> Empty | |
is Cons -> if (f(list.head)) dropWhile(list.tail, f) else list | |
} | |
fun main() { | |
val myList = Cons(1, Cons(2, Cons(3, Cons(4, Empty)))) | |
dropWhile(myList) { it < 3 }.let(::println) |
This file contains hidden or 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
sealed class List<out E> | |
object Empty: List<Nothing>() { | |
override fun toString()= "[]" | |
} | |
data class Cons<E>(val head: E, val tail: List<E>): List<E>() { | |
override fun toString(): String = "$head :: $tail" | |
} | |
fun main() { |
This file contains hidden or 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 dropWhile(list: List<Int>, f: (Int) -> Boolean) |
NewerOlder