Skip to content

Instantly share code, notes, and snippets.

@monkey-codes
Last active June 9, 2023 23:03
Show Gist options
  • Select an option

  • Save monkey-codes/dcc98e5c40110221642403753682b165 to your computer and use it in GitHub Desktop.

Select an option

Save monkey-codes/dcc98e5c40110221642403753682b165 to your computer and use it in GitHub Desktop.
Making a move on the tic tac toe game
...
fun make(move: Move): Either<GameError, Game> = either {
val (coord, symbol) = move
ensure(symbol == nextPlayer) { raise(NotPlayersTurn(symbol)) }
ensure(cell(move.coordinates) == BLANK) { raise(InvalidMove(move)) }
state
.mapIndexed { ri, row ->
if (ri == coord.row) {
row.mapIndexed { ci, cell -> if (ci == coord.col) symbol else cell }
} else row
}
.toGame()
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment