Advertisement
jules0707

Board.kt

Oct 20th, 2021
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.97 KB | None | 0 0
  1. package board
  2.  
  3. data class Cell(val i: Int, val j: Int) {
  4.     override fun toString()= "($i, $j)"
  5. }
  6.  
  7. enum class Direction {
  8.     UP, DOWN, RIGHT, LEFT;
  9.  
  10.     fun reversed() = when (this) {
  11.         UP -> DOWN
  12.         DOWN -> UP
  13.         RIGHT -> LEFT
  14.         LEFT -> RIGHT
  15.     }
  16. }
  17.  
  18. interface SquareBoard {
  19.     val width: Int
  20.  
  21.     fun getCellOrNull(i: Int, j: Int): Cell?
  22.     fun getCell(i: Int, j: Int): Cell
  23.  
  24.     fun getAllCells(): Collection<Cell>
  25.  
  26.     fun getRow(i: Int, jRange: IntProgression): List<Cell>
  27.     fun getColumn(iRange: IntProgression, j: Int): List<Cell>
  28.  
  29.     fun Cell.getNeighbour(direction: Direction): Cell?
  30. }
  31.  
  32. interface GameBoard<T> : SquareBoard {
  33.  
  34.     operator fun get(cell: Cell): T?
  35.     operator fun set(cell: Cell, value: T?)
  36.  
  37.     fun filter(predicate: (T?) -> Boolean): Collection<Cell>
  38.     fun find(predicate: (T?) -> Boolean): Cell?
  39.     fun any(predicate: (T?) -> Boolean): Boolean
  40.     fun all(predicate: (T?) -> Boolean): Boolean
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement