Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed two minor bugs when closing game #11

Merged
merged 2 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}