Assignment 6

This commit is contained in:
Anthony Cicchetti 2019-07-25 17:58:40 -04:00
parent 68d2319224
commit 2e8c1aa491
6 changed files with 84 additions and 8 deletions

9
.idea/CS5004.iml generated Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

5
.idea/misc.xml generated
View file

@ -1,7 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="false" project-jdk-name="12" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="masterDetails">

8
.idea/modules.xml generated Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/CS5004.iml" filepath="$PROJECT_DIR$/.idea/CS5004.iml" />
</modules>
</component>
</project>

View file

@ -1,17 +1,18 @@
package com.anthonycicchetti.cs5004.assignment5
import java.io.BufferedReader
import java.io.InputStreamReader
object Main {
@JvmStatic
fun main(args: Array<String>) {
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)
}
}

View file

@ -0,0 +1,5 @@
package com.anthonycicchetti.cs5004.assignment5
interface MarbleSolitaireController {
fun playGame(model: MarbleSolitaireModel): Unit
}

View file

@ -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()}")
}
}
}