Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. package o1.peeveli
  2.  
  3. import GameState.Unrevealed
  4.  
  5.  
  6.  
  7. class GameState(val missesAllowed: Int, val previousGuesses: String, val visibleWord: String, val possibleSolutions: Vector[String]) {
  8.  
  9.  
  10.  
  11. def this(missesAllowed: Int, length: Int, vocabulary: Vector[String]) = {
  12. this(missesAllowed, "", Unrevealed.toString * length, vocabulary.map( _.toUpperCase )) // This means: pass these parameters to the "default constructor" defined in the class header.
  13. }
  14.  
  15.  
  16.  
  17. def wordLength = this.visibleWord.length
  18.  
  19.  
  20. def numberOfSolutions = possibleSolutions.size
  21.  
  22.  
  23. def isLost: Boolean = missesAllowed == -1
  24.  
  25.  
  26. def isWon = !this.isLost && !this.visibleWord.contains("_")
  27.  
  28.  
  29.  
  30. private def reveal(patternToReveal: String) = {
  31. var muuttuvaPattern = patternToReveal
  32. var kirjaimet = patternToReveal.filter(_ != '_')
  33. var uusiSana = visibleWord
  34.  
  35.  
  36. while(kirjaimet.size != 0) {
  37.  
  38. var indeksi = muuttuvaPattern.indexOf(kirjaimet.head)
  39. uusiSana = uusiSana.updated(indeksi, kirjaimet.head)
  40. kirjaimet = kirjaimet.drop(1)
  41. muuttuvaPattern = muuttuvaPattern.updated(indeksi, "/".head)
  42.  
  43.  
  44. }
  45.  
  46. uusiSana
  47.  
  48.  
  49.  
  50. }
  51.  
  52. def guessLetter(guess: Char) = {
  53. val actualGuess = guess.toUpper
  54. val solutions = this.possibleSolutions.filter(!_.contains(actualGuess))
  55. val solution1 = { possibleSolutions.map(_.map(n => if(n==actualGuess)n else Unrevealed)).groupBy(dir => dir).toVector.sortBy(pari => -pari._2.size) }
  56.  
  57. val exampleSolution = solution1.head._1
  58.  
  59. var success = exampleSolution.contains(actualGuess)
  60.  
  61. val uusi = reveal(exampleSolution)
  62. val newPossible = possibleSolutions.filter(exampleSolution == _.map(s => if(s==actualGuess)s else Unrevealed))
  63.  
  64. if (success) new GameState(this.missesAllowed, this.previousGuesses + actualGuess, uusi, newPossible) else new GameState(missesAllowed-1, this.previousGuesses + actualGuess, visibleWord, solutions)
  65.  
  66. }
  67.  
  68.  
  69.  
  70. override def toString =
  71. this.visibleWord + ", " +
  72. "missed guesses allowed: " + this.missesAllowed + ", " +
  73. "guesses made: " + (if (this.previousGuesses.isEmpty) "none" else this.previousGuesses) + ", " +
  74. "solutions left: " + this.numberOfSolutions
  75.  
  76. }
  77.  
  78.  
  79.  
  80. object GameState {
  81.  
  82.  
  83. val Unrevealed = '_'
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement