diff --git a/.idea/CS5004.iml b/.idea/CS5004.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/CS5004.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 82c2a2f..aa7004c 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,10 @@ - + + + diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..eb22821 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/main/kotlin/com/anthonycicchetti/cs5004/assignment5/Main.kt b/src/main/kotlin/com/anthonycicchetti/cs5004/assignment5/Main.kt index 45a75b0..426c1af 100644 --- a/src/main/kotlin/com/anthonycicchetti/cs5004/assignment5/Main.kt +++ b/src/main/kotlin/com/anthonycicchetti/cs5004/assignment5/Main.kt @@ -1,17 +1,18 @@ package com.anthonycicchetti.cs5004.assignment5 +import java.io.BufferedReader +import java.io.InputStreamReader + object Main { @JvmStatic fun main(args: Array) { val boardOne = Board() - println(boardOne.getGameState()) +// println(boardOne.getGameState()) - val boardTwo = Board(2, 3) - println(boardTwo.getGameState()) + val input = BufferedReader(InputStreamReader(System.`in`)) + val output = System.out - val board = Board() - board.move(3,1, 3,3) - - println(board.getGameState()) + val game = MarbleSolitaireControllerImpl(input, output) + game.playGame(boardOne) } } \ No newline at end of file diff --git a/src/main/kotlin/com/anthonycicchetti/cs5004/assignment5/MarbleSolitaireController.kt b/src/main/kotlin/com/anthonycicchetti/cs5004/assignment5/MarbleSolitaireController.kt new file mode 100644 index 0000000..d87c30f --- /dev/null +++ b/src/main/kotlin/com/anthonycicchetti/cs5004/assignment5/MarbleSolitaireController.kt @@ -0,0 +1,5 @@ +package com.anthonycicchetti.cs5004.assignment5 + +interface MarbleSolitaireController { + fun playGame(model: MarbleSolitaireModel): Unit +} \ No newline at end of file diff --git a/src/main/kotlin/com/anthonycicchetti/cs5004/assignment5/MarbleSolitaireControllerImpl.kt b/src/main/kotlin/com/anthonycicchetti/cs5004/assignment5/MarbleSolitaireControllerImpl.kt new file mode 100644 index 0000000..512ac24 --- /dev/null +++ b/src/main/kotlin/com/anthonycicchetti/cs5004/assignment5/MarbleSolitaireControllerImpl.kt @@ -0,0 +1,50 @@ +package com.anthonycicchetti.cs5004.assignment5 + +import java.nio.CharBuffer + +class MarbleSolitaireControllerImpl(private val rd: Readable, private val ap: Appendable) + : MarbleSolitaireController { + + private fun printGameStateToAppendable(model: MarbleSolitaireModel) { + ap.appendln(model.getGameState()) + ap.appendln("Score: ${model.getScore()}") + } + + override fun playGame(model: MarbleSolitaireModel) { + var cb = CharBuffer.allocate(100) + + printGameStateToAppendable(model) + + ap.appendln("Waiting for move:") + rd.read(cb) + + while (!(cb.array().filter { it != '\u0000' }.size == 2 && (cb.array()[0] == 'q' || cb.array()[0] == 'Q' ) )){ + + val input = cb.array().filter { it != '\u0000' } + + val filteredInput = input.filter { !it.isWhitespace() } + + if (filteredInput.size != 4) { + ap.appendln("Invalid input. Try again.") + } + + val (fromRow, fromCol, toRow, toCol) = filteredInput.map { it.toInt() - 48 } + + model.move(fromRow, fromCol, toRow, toCol) + + printGameStateToAppendable(model) + + ap.appendln("Waiting for next move:") + // clears out the buffer to prepare for the next event + cb = CharBuffer.allocate(100) + rd.read(cb) + } + + with (ap) { + appendln("Game quit!") + appendln("State of game when quit:\n ${model.getGameState()}") + appendln("Score: ${model.getScore()}") + } + + } +} \ No newline at end of file