Skip to content

Instantly share code, notes, and snippets.

@zerobasedindex
Created February 8, 2018 02:13
Show Gist options
  • Save zerobasedindex/790927eca3d36b1733b4e85664e631dc to your computer and use it in GitHub Desktop.
Save zerobasedindex/790927eca3d36b1733b4e85664e631dc to your computer and use it in GitHub Desktop.
enum class Team {
RED,
BLACK,
EMPTY;
override fun toString(): String = when (this) {
RED -> "R"
BLACK -> "B"
EMPTY -> "O"
}
}
fun checkHorizontal(board: Board, row: Int, col: Int): Team? {
val team = board[row, col]
return if (team == board[row, col + 1] && team == board[row, col + 2] && team == board[row, col + 3]) team else Team.EMPTY
}
fun checkVertical(board: Board, row: Int, col: Int): Team? {
val team = board[row, col]
return if (team == board[row + 1, col] && team == board[row + 2, col] && team == board[row + 3, col]) team else Team.EMPTY
}
fun checkDiagonal(board: Board, row: Int, col: Int): Team? {
val team = board[row, col]
return if ((team == board[row + 1, col + 1] && team == board[row + 2, col + 2] && team == board[row + 3, col + 3]) ||
team == board[row + 1, col - 1] && team == board[row + 2, col - 2] && team == board[row + 3, col - 3]) team else Team.EMPTY
}
class Board() {
// Kotlin's way of doing constants
companion object {
const val ROWS = 6;
const val COLS = 7;
}
// Init array for board
var board = Array(7*6) { Team.EMPTY }
var winCheckers: MutableList<()->Team?> = mutableListOf()
init {
// Determine which checks are needed for each board position
for (row in 0..Board.ROWS - 1) {
for (col in 0..Board.COLS - 1) {
if (col + 3 < Board.COLS) {
winCheckers.add(fun(): Team? {
return checkHorizontal(this@Board, row, col)
})
if (row + 3 < Board.ROWS) {
winCheckers.add(fun(): Team? {
// This will still check both diagonals, just need to split the func
return checkDiagonal(this@Board, row, col)
})
}
}
if (row + 3 < Board.ROWS) {
winCheckers.add(fun(): Team? {
return checkVertical(this@Board, row, col)
})
if (col - 3 >= 0) {
winCheckers.add(fun(): Team? {
return checkDiagonal(this@Board, row, col)
})
}
}
}
}
}
// Overload operator so [row,col] works
operator fun get(row: Int, col: Int): Team? {
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) {
return null
} else {
return board[row * COLS + col]
}
}
// Overload operator so [row,col] = value works
operator fun set(row: Int, col: Int, value: Team) {
board[row * COLS + col] = value
}
// Overload toString to output pretty format
override fun toString(): String {
var boardStr = ""
board.forEachIndexed { idx, space -> boardStr += "${space}" + if ((idx + 1) % 7 == 0) "\n" else " " }
return boardStr
}
fun checkWin(): Team? {
winCheckers.forEach {
// Call each closure for a win condition
val team = it()
if (team != Team.EMPTY) {
return team
}
}
return null
}
}
fun main(args: Array<String>) {
var board = Board()
board[4, 0] = Team.RED
board[4, 1] = Team.RED
board[4, 2] = Team.RED
board[4, 3] = Team.RED
println(board)
println("Team ${board.checkWin()} wins")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment