/** * Plays a turn and returns the result of the player's guess * */ public GuessResult PlayTurn(Guess guess) { if (HasWager()) { Card flippedCard = deck.Draw(); if (flippedCard != null) { bool isGuessCorrect = false; if (guess.Equals(Guess.High)) { isGuessCorrect = (flippedCard.Value >= 7 || flippedCard.Value == 14); } else if (guess.Equals(Guess.Low)) { isGuessCorrect = (flippedCard.Value <= 7 || flippedCard.Value == 14); } if (isGuessCorrect) { this.Balance += this.Wager; } else { this.Balance = ((this.Balance - this.Wager) >= 0) ? (this.Balance - this.Wager) : 0; } return(new GuessResult(flippedCard, isGuessCorrect, deck.Empty(), (this.Balance == 0))); } else { return(new GuessResult(true, (this.Balance == 0))); } } return(null); }
Task IHandleAsync <ScrollToGuessEventModel> .HandleAsync(ScrollToGuessEventModel message) { if (_gameBoard == null) { throw new BasicBlankException("Gameboard was never set. Rethink"); } PopulateControls(); Guess guess = message.Guess; var list = _gameBoard.GuessList; if (guess.Equals(list.First())) { _thisScroll !.ScrollToTop(); } else if (guess.Equals(list.Last())) { _thisScroll !.ScrollToBottom(); } else { int index = list !.IndexOf(guess); if (index <= 6) { _thisScroll !.ScrollToTop(); } else { _thisScroll !.ScrollToBottom(); } } return(this.RefreshBindingsAsync(_aggregator)); }
static void Main() { /* * Your assignment is to create a game called Hangman. * Hangman game is a pen and pencil guessing game for two or more players. * You can read more about it https://en.wikipedia.org/wiki/Hangman_(game). * * Done: One player (in our case the application) thinks of a word and the other player(s) tries to guess it by suggesting letters. * Done: The word to guess is represented by a row of dashes where each dash represents a letter in the word. * * Game Rules to implement: * Done: • The player has 10 guesses to complete the word before losing the game. * * • The player can make two type of guesses: * Done: • Guess for a specific letter. If player guess a letter that occurs in the word, the program should update by inserting the letter in the correct position(s). * Done? • Guess for the whole word. The player type in a word he/she thinks is the word. If the guess is correct player wins the game and the whole word is revealed. If the word is incorrect nothing should get revealed. * Done: • If the player guesses the same letter twice, the program will not consume a guess. * * Code Requirements: * Done: • The secret word should be randomly chosen from an array of Strings. * Done: • The incorrect letters the player has guessed, should be put inside a StringBuilder and presented to the player after each guess. * Done: • The correct letters should be put inside a char array. Unrevealed letters need to be represented by a lower dash ( _ ). * */ //One player (in our case the application) thinks of a word and the other player(s) tries to guess it by suggesting letters. Console.WriteLine($"Hello dear user, time to play the hanging man game! \nAnd I have choosen to use body parts as secret word. ;)"); string secretWord = RandomBodyParts().ToUpper(); Debug.Print(secretWord); StringBuilder badGuesses = new StringBuilder(); bool keepGuessing = true; //Define and fill guessArray with underscore chars... char[] guessArray = new char[secretWord.Length]; //for (int i = 0; i <guessArray.Length; i++) //{ // guessArray[i] = '_'; //} Array.Fill(guessArray, '_'); Console.WriteLine($"I'l help you out a little... the word is {secretWord.Length} letter long. Good luck :)"); do { string strGuess = Console.ReadLine().ToUpper(); //filter on word length, or first char in string only if (strGuess.Length != secretWord.Length) { strGuess = strGuess.Substring(0); Debug.Print(strGuess.Length.ToString()); } //Loop for each char in User input. foreach (char Guess in strGuess) { if (GuessRight(Guess, secretWord)) { // God guess. Add char to guessArray at the correct place. Can be more than one occurrance/slot! for (int i = 0; i < secretWord.Length; ++i) { if (Guess.Equals(secretWord[i])) { guessArray[i] = Guess; } } } else { //Bad guess: the letter to the badGuess Stringbuilder class. Letters only. if (badGuesses.ToString().IndexOf(Guess) < 0 && Char.IsLetter(Guess)) { badGuesses.Append(Guess + " "); } } } //Break when out of guesses = 10. String has additional whitespace = 20 chars if (badGuesses.Length > 20) { break; } PrintConsole(badGuesses, guessArray); //Break loop is array is filld ( = no underscores left) if (!guessArray.Contains('_')) { break; } } while (keepGuessing); //Loop exited, decide game. WonOrLost(guessArray); }