Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package o1.peeveli
- import GameState.Unrevealed
- class GameState(val missesAllowed: Int, val previousGuesses: String, val visibleWord: String, val possibleSolutions: Vector[String]) {
- def this(missesAllowed: Int, length: Int, vocabulary: Vector[String]) = {
- this(missesAllowed, "", Unrevealed.toString * length, vocabulary.map( _.toUpperCase )) // This means: pass these parameters to the "default constructor" defined in the class header.
- }
- def wordLength = this.visibleWord.length
- def numberOfSolutions = possibleSolutions.size
- def isLost: Boolean = missesAllowed == -1
- def isWon = !this.isLost && !this.visibleWord.contains("_")
- private def reveal(patternToReveal: String) = {
- var muuttuvaPattern = patternToReveal
- var kirjaimet = patternToReveal.filter(_ != '_')
- var uusiSana = visibleWord
- while(kirjaimet.size != 0) {
- var indeksi = muuttuvaPattern.indexOf(kirjaimet.head)
- uusiSana = uusiSana.updated(indeksi, kirjaimet.head)
- kirjaimet = kirjaimet.drop(1)
- muuttuvaPattern = muuttuvaPattern.updated(indeksi, "/".head)
- }
- uusiSana
- }
- def guessLetter(guess: Char) = {
- val actualGuess = guess.toUpper
- val solutions = this.possibleSolutions.filter(!_.contains(actualGuess))
- val solution1 = { possibleSolutions.map(_.map(n => if(n==actualGuess)n else Unrevealed)).groupBy(dir => dir).toVector.sortBy(pari => -pari._2.size) }
- val exampleSolution = solution1.head._1
- var success = exampleSolution.contains(actualGuess)
- val uusi = reveal(exampleSolution)
- val newPossible = possibleSolutions.filter(exampleSolution == _.map(s => if(s==actualGuess)s else Unrevealed))
- if (success) new GameState(this.missesAllowed, this.previousGuesses + actualGuess, uusi, newPossible) else new GameState(missesAllowed-1, this.previousGuesses + actualGuess, visibleWord, solutions)
- }
- override def toString =
- this.visibleWord + ", " +
- "missed guesses allowed: " + this.missesAllowed + ", " +
- "guesses made: " + (if (this.previousGuesses.isEmpty) "none" else this.previousGuesses) + ", " +
- "solutions left: " + this.numberOfSolutions
- }
- object GameState {
- val Unrevealed = '_'
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement