Since Kotlin doesn't have collection literals (i.e. [1, 2, 3]
), I made this small library to make it easier to work with
and test grids. Very useful for games like chess or checkers 😁
implementation 'com.vincentcarrier:kgrid:1.0.1'
data class ChessPiece(val color: Color, val type: Type) { enum class Color { WHITE, BLACK } enum class Type(val char: Char) { KING('K'), QUEEN('Q'), ROOK('R'), BISHOP('B'), KNIGHT('N'), PAWN('P') } } fun Char.toChessPiece(): ChessPiece? = ... class ChessboardTest { // Triple quotes and pipes are important here to trim out white space val chessboard = """ |rnbqkbnr |pppppppp |________ |________ |________ |________ |PPPPPPPP |RNBQKBNR """.toMutableGrid { char -> char.toChessPiece() } @Test fun `pawn to e4`() { val e2 = Cell(4, 6) // A Cell is a simple, immutable data class that holds XY coordinates val e4 = Cell(4, 4) chessboard.swap(e2, e4) assertEquals(ChessPiece(WHITE, PAWN), chessboard[e4]) } }
The library is built around the Grid<E>
interface
interface Grid<out E> {
val width: Int
val height: Int
operator fun get(x: Int, y: Int): E
}
and its mutable version, MutableGrid<E>
interface MutableGrid<E> : Grid<E> {
operator fun set(x: Int, y: Int, value: E)
}
The library provides the following implementations:
ArrayGrid<E>
andMutableArrayGrid<E>
, backed by a single arrayListGrid<E>
andMutableListGrid<E>
, backed by a list of lists