From 13bf15cbf741ae4c3ea33e8b9932da92b2a43b40 Mon Sep 17 00:00:00 2001 From: Anthony Cicchetti Date: Sun, 30 Jun 2019 14:54:17 -0400 Subject: [PATCH] Added some logic to the BoardLocation, Column, and Row classes --- .../cs5004/assignment3/Board.kt | 76 ++++++++++++++++++- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/anthonycicchetti/cs5004/assignment3/Board.kt b/src/main/kotlin/com/anthonycicchetti/cs5004/assignment3/Board.kt index 8afc635..41b94bf 100644 --- a/src/main/kotlin/com/anthonycicchetti/cs5004/assignment3/Board.kt +++ b/src/main/kotlin/com/anthonycicchetti/cs5004/assignment3/Board.kt @@ -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