Added some logic to the BoardLocation, Column, and Row classes

This commit is contained in:
Anthony Cicchetti 2019-06-30 14:54:17 -04:00
parent 83bd6e73f4
commit 13bf15cbf7

View file

@ -1,17 +1,87 @@
package com.anthonycicchetti.cs5004.assignment3
import com.anthonycicchetti.cs5004.assignment3.pieces.IChessPiece
import java.lang.IllegalArgumentException
class Board() {
enum class Column {
A, B, C, D, E, F, G
A, B, C, D, E, F, G, H;
fun right(x: Int): Column? {
return when (this.ordinal + x) {
Column.A.ordinal -> return Column.A
Column.B.ordinal -> return Column.B
Column.C.ordinal -> return Column.C
Column.D.ordinal -> return Column.D
Column.E.ordinal -> return Column.E
Column.F.ordinal -> return Column.F
Column.G.ordinal -> return Column.G
Column.H.ordinal -> return Column.H
else -> null
}
}
fun left(x: Int): Column? {
return when (this.ordinal - x) {
Column.A.ordinal -> return Column.A
Column.B.ordinal -> return Column.B
Column.C.ordinal -> return Column.C
Column.D.ordinal -> return Column.D
Column.E.ordinal -> return Column.E
Column.F.ordinal -> return Column.F
Column.G.ordinal -> return Column.G
Column.H.ordinal -> return Column.H
else -> null
}
}
}
enum class Row {
ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT
ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT;
fun up(x: Int): Row? {
return when (this.ordinal + x) {
Row.ONE.ordinal -> return Row.ONE
Row.TWO.ordinal -> return Row.TWO
Row.THREE.ordinal -> return Row.THREE
Row.FOUR.ordinal -> return Row.FOUR
Row.FIVE.ordinal -> return Row.FIVE
Row.SIX.ordinal -> return Row.SIX
Row.SEVEN.ordinal -> return Row.SEVEN
Row.EIGHT.ordinal -> return Row.EIGHT
else -> null
}
}
fun down(x: Int): Row? {
return when (this.ordinal - x) {
Row.ONE.ordinal -> return Row.ONE
Row.TWO.ordinal -> return Row.TWO
Row.THREE.ordinal -> return Row.THREE
Row.FOUR.ordinal -> return Row.FOUR
Row.FIVE.ordinal -> return Row.FIVE
Row.SIX.ordinal -> return Row.SIX
Row.SEVEN.ordinal -> return Row.SEVEN
Row.EIGHT.ordinal -> return Row.EIGHT
else -> null
}
}
}
data class BoardSquare(val location: BoardLocation, var piece: IChessPiece?)
data class BoardLocation(val column: Column, val row: Row)
data class BoardLocation(val column: Column, val row: Row) {
fun move(columns: Int, rows: Int): BoardLocation {
val newColumn = when (val x = this.column.right(columns)) {
null -> throw IllegalArgumentException("Can't move to a bad column")
else -> x
}
val newRow = when (val x = this.row.up(rows)) {
null -> throw IllegalArgumentException("Can't move to a bad row")
else -> x
}
return BoardLocation(newColumn, newRow)
}
}
val state: List<BoardSquare>