Board

data class Board(val side: Int, val pieces: List<Piece> = emptyList()) : Iterable<Piece>

Represents a board game grid.

Throws

if the side is not within the valid range or not even.

Constructors

Link copied to clipboard
constructor(side: Int, pieces: List<Piece> = emptyList())

Properties

Link copied to clipboard
private val pieces: List<Piece>

The list of pieces on the board.

Link copied to clipboard
val side: Int

The size of the board (side x side).

Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
fun addPiece(piece: Piece): Board

Adds a piece to the board.

fun addPiece(idx: Int, value: PieceType): Board

Adds a piece to the board at the specified linear index.

fun addPiece(coordinate: Coordinate, value: PieceType): Board

Adds a piece to the board at the specified coordinate.

Link copied to clipboard
fun changePiece(idx: Int): Board

Changes the piece at the specified linear index by swapping its color.

fun changePiece(coordinate: Coordinate): Board

Changes the piece at the specified coordinate by swapping its color from 'B' to 'W' or vice versa.

Link copied to clipboard
fun checkPosition(coordinate: Coordinate)

Checks if the specified row and column are within the bounds of the board.

Link copied to clipboard
operator fun get(idx: Int): PieceType?

Gets the piece at the specified index like a linear list.

operator fun get(coordinate: Coordinate): PieceType?

Gets the piece at the specified row and column.

Link copied to clipboard
open operator override fun iterator(): Iterator<Piece>

Returns an iterator of the value of pieces on the board.

Link copied to clipboard

Starts the board with the initial pieces in the center.

Link copied to clipboard

Converts a linear index to a Coordinate on the board.