Skip to content

Commit

Permalink
Merge pull request #11 from AngeloFilaseta/develop
Browse files Browse the repository at this point in the history
Fixed two minor bugs when closing game
  • Loading branch information
Tale152 committed Aug 9, 2021
2 parents 90e5872 + 2d876e1 commit 0d54383
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 4 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ lazy val root = (project in file("."))
libraryDependencies += scalaTest % Test
)

// System.exit calls are trapped to prevent the JVM from terminating.
// This is useful for executing user code that may call System.exit.
trapExit := false

// Tests Configurations
val numberOfTestProcessors = java.lang.Runtime.getRuntime.availableProcessors + 1
// Run tests in parallel
Expand Down
18 changes: 11 additions & 7 deletions src/main/scala/view/StoryView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,24 @@ object StoryView {
private var _narrative: String = ""
private var _pathways: Seq[Pathway] = Seq()

object NotValidInputStrategy {
//used to filter invalid input from user
val OnlyChoicesStrategy: String => Boolean = input =>
input == "" ||
!(input forall Character.isDigit) ||
input.toInt > _pathways.size ||
input.toInt < 1
}

/**
* Gets the user input and sends it to [[controller.subcontroller.StoryController]]
*/
private def waitForUserInput(): Unit = {
val condition: String => Boolean = input =>
(input == "" || !(input forall Character.isDigit) ||
input.toInt > _pathways.size ||
input.toInt > _pathways.size ||
input.toInt < 1) && _pathways.nonEmpty
val chosenPath = InputUtility.inputAsInt(inputStrategy, condition)
if (_pathways.nonEmpty) {
val chosenPath = InputUtility.inputAsInt(inputStrategy, NotValidInputStrategy.OnlyChoicesStrategy)
storyController.choosePathWay(_pathways(chosenPath))
} else {
scala.io.StdIn.readLine()
storyController.close()
}
}
Expand Down Expand Up @@ -66,4 +71,3 @@ object StoryView {
def apply(storyController: StoryController, inputStrategy: () => String): StoryView =
new StoryViewImpl(storyController, inputStrategy)
}

6 changes: 3 additions & 3 deletions src/main/scala/view/util/InputUtility.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ object InputUtility {
*
* @return the chosen pathway
*/
def inputAsInt(inputValueStrategy: () => String, notValidStrategy: String => Boolean): Int = {
def inputAsInt(inputValueStrategy: () => String, isNotValidStrategy: String => Boolean): Int = {
var input: String = ""
do {
input = inputValueStrategy()
if(notValidStrategy(input)){
if(isNotValidStrategy(input)){
println("Please insert a valid value.")
}
} while (notValidStrategy(input))
} while (isNotValidStrategy(input))
input.toInt - 1
}
}

0 comments on commit 0d54383

Please sign in to comment.