Advertisement
jules0707

BoardImpl.kt

Oct 20th, 2021
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.53 KB | None | 0 0
  1. package board
  2.  
  3. import board.Direction.*
  4. import java.lang.IllegalArgumentException
  5.  
  6. fun createSquareBoard(width: Int): SquareBoard = object : SquareBoard {
  7.     override val width: Int
  8.         get() = width
  9.  
  10.     val cells = mutableListOf<Cell>()
  11.  
  12.     init {
  13.         for (i in 1..width)
  14.             for (j in 1..width)
  15.                 cells.add(Cell(i, j))
  16.     }
  17.  
  18.     fun ijToInt(i: Int, j: Int) = i * width - (width - j)
  19.  
  20.     override fun getCellOrNull(i: Int, j: Int): Cell? =
  21.         if (i !in 1..width || j !in 1..width) null
  22.         else getCell(i, j)
  23.  
  24.  
  25.     override fun getCell(i: Int, j: Int): Cell =
  26.         if (i !in 1..width || j !in 1..width) throw IllegalArgumentException()
  27.         else cells[ijToInt(i, j) - 1]
  28.    
  29.     override fun getAllCells(): Collection<Cell> = cells
  30.  
  31.     override fun getRow(i: Int, jRange: IntProgression): List<Cell> {
  32.         val rows = mutableListOf<Cell>()
  33.         for (j in jRange)
  34.             getCellOrNull(i, j)?.let { rows.add(it) }
  35.         return rows
  36.     }
  37.  
  38.     override fun getColumn(iRange: IntProgression, j: Int): List<Cell> {
  39.         val cols = mutableListOf<Cell>()
  40.         for (i in iRange)
  41.             getCellOrNull(i, j)?.let { cols.add(it) }
  42.         return cols
  43.     }
  44.  
  45.     override fun Cell.getNeighbour(direction: Direction): Cell? {
  46.         return when (direction) {
  47.             DOWN -> getCellOrNull(i + 1, j)
  48.             UP -> getCellOrNull(i - 1, j)
  49.             LEFT -> getCellOrNull(i, j - 1)
  50.             RIGHT -> getCellOrNull(i, j + 1)
  51.         }
  52.     }
  53. }
  54.  
  55.  
  56. fun <T> createGameBoard(width: Int): GameBoard<T> = object : GameBoard<T>, SquareBoard by createSquareBoard(width) {
  57.  
  58.     val cells = getAllCells().toList()
  59.     val cellsValues = mutableMapOf<Cell, T?>()
  60.  
  61.     init {
  62.         for (cell in cells)
  63.                 cellsValues[cell] = null // we want to store null reference as default values
  64.     }
  65.  
  66.     override fun get(cell: Cell): T? {
  67.         return cellsValues[cell]
  68.     }
  69.  
  70.     override fun set(cell: Cell, value: T?) {
  71.         cellsValues[cell] = value
  72.     }
  73.  
  74.     override fun filter(predicate: (T?) -> Boolean): Collection<Cell> =
  75.         cellsValues.filter { predicate(it.value) }.keys
  76.  
  77.     override fun find(predicate: (T?) -> Boolean): Cell? =
  78.         filter { predicate(it) }.firstOrNull()
  79.  
  80.     override fun any(predicate: (T?) -> Boolean): Boolean =
  81.         cellsValues.filter { predicate(it.value) }.isNotEmpty()
  82.  
  83.     override fun all(predicate: (T?) -> Boolean): Boolean =
  84.         filter { predicate(it) }.size == cells.size
  85. }
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement