-
-
Save monkey-codes/83fdec217e098efffde080d673d1b8af to your computer and use it in GitHub Desktop.
Game smart constructor
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
| typealias State = List<List<Symbol>> | |
| fun State.toGame(): Game = Game.of(this) | |
| ... | |
| sealed class Game(val state: State) { | |
| companion object { | |
| ... | |
| fun of(state: State): Game { | |
| val winningCells = | |
| (rows + columns + diagonals) | |
| .map { coordinates -> state.values(coordinates) } | |
| .firstOrNull { it.allSymbolsMatch(CROSS) || it.allSymbolsMatch(NOUGHT) } | |
| .orEmpty() | |
| val isDrawn by lazy { state.all { it.none { symbol -> symbol == BLANK } } } | |
| return when { | |
| winningCells.isNotEmpty() -> Won(state, winningCells.first().second) | |
| isDrawn -> Draw(state) | |
| else -> InProgress(state) | |
| } | |
| } | |
| ... | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment