Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Hangman h = new Hangman();

            h.PickingWords();
            h.GameSequence();
        }
        private void Run()
        {
            bool    keepPlaying = true;
            Hangman hangman     = new Hangman();

            while (keepPlaying)
            {
                RenderGame(hangman);
                ConsoleKeyInfo keyInfo = Console.ReadKey();
                if (keyInfo.Key == ConsoleKey.Escape)
                {
                    keepPlaying = false;
                }
                else if (Char.IsLetter(keyInfo.KeyChar))
                {
                    hangman.Guess(keyInfo.KeyChar);
                    if (hangman.Lives == 0 || hangman.HasPlayerWon())
                    {
                        RenderGame(hangman);
                        Console.WriteLine("Press [Enter] to play again.");
                        Console.ReadLine();
                        hangman = new Hangman();
                    }
                }
            }
        }
Exemplo n.º 3
0
 public static void WinOrLose(Hangman h)  // Prints out win or lost with the correct word
 {
     if (h.CheckWin())
     {
         GameOver = true;
         Console.Clear();
         Console.SetCursorPosition(10, 2);
         Console.WriteLine("You win!!");
         Console.WriteLine();
         Console.Write(" The hidden word is: ");
         PrintHangman(h.getCurrentHangman());
         Console.Beep();
         Console.Beep();
     }
     //Group5 - isDone is not changed from false so will always be false. it shoudld be enough with "checkLoose"
     if ((!GameOver) && (h.CheckLoose()))
     {
         GameOver = true;
         Console.Clear();
         Console.SetCursorPosition(10, 2);
         Console.WriteLine("You lost");
         Console.Write("\n The correct word is \"");
         Console.ForegroundColor = ConsoleColor.Green;
         Console.Write(h.getSecretWord());
         Console.ForegroundColor = ConsoleColor.White;
         Console.Write("\", better luck next time ;)");
         Console.WriteLine("\n");
         Console.Beep();
         Console.Beep();
     }
 }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            Hangman game = new Hangman();

            game.Play();

            Console.ReadLine();
        }
Exemplo n.º 5
0
        public void OneGuessShouldReduceRemainingAttemptsByOne()
        {
            Hangman h = new Hangman("secret");

            h.Guess('e'); Assert.AreEqual(9, h.GetRemainingAttempts(), "guess e -> -e--e-");
            h.Guess('s'); Assert.AreEqual(8, h.GetRemainingAttempts(), "guess s -> se--e-");
            h.Guess('r'); Assert.AreEqual(7, h.GetRemainingAttempts(), "guess s -> se-re-");
            h.Guess('c'); Assert.AreEqual(6, h.GetRemainingAttempts(), "guess s -> secre-");
            h.Guess('t'); Assert.AreEqual(5, h.GetRemainingAttempts(), "guess s -> secret");
        }
Exemplo n.º 6
0
        private void BtnStart_Click(object sender, EventArgs e)
        {
            Wordprompt form2 = new Wordprompt();

            form2.Close();

            Hangman form = new Hangman();

            form.Show();
        }
Exemplo n.º 7
0
        public void MultipleGuessesShouldSolveTheSecret()
        {
            Hangman h = new Hangman("secret");

            Assert.AreEqual("-e--e-", h.Guess('e'));
            Assert.AreEqual("se--e-", h.Guess('s'));
            Assert.AreEqual("se-re-", h.Guess('r'));
            Assert.AreEqual("secre-", h.Guess('c'));
            Assert.AreEqual("secret", h.Guess('t'));
        }
        private void RenderGame(Hangman hangman)
        {
            string word = hangman.Word;

            word.ToCharArray().ToList().ForEach(c =>
            {
                if (!hangman.Guesses.Contains(c))
                {
                    word = word.Replace(c, '-');
                }
            });

            Console.Clear();
            Console.WriteLine("Press [Esc] to exit.");
            Console.Write("\nWord: ");
            hangman.Word.ToCharArray().ToList().ForEach(c =>
            {
                if (hangman.Lives == 0)
                {
                    Console.ForegroundColor = hangman.Guesses.Contains(c) ? ConsoleColor.Gray : ConsoleColor.Red;
                }
                Console.Write(hangman.Lives == 0 ? c.ToString() : (hangman.Guesses.Contains(c) ? c.ToString() : "-"));
            });
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine();

            Console.WriteLine("\nLives: " + hangman.Lives);


            List <char> usedLetters = new List <char>();

            Console.Write("\nGuesses: ");
            hangman.Guesses.ForEach(c =>
            {
                Console.ForegroundColor = !hangman.Word.Contains(c) ? ConsoleColor.Red : (usedLetters.Contains(c) ? ConsoleColor.Yellow : ConsoleColor.Gray);
                Console.Write(c + " ");
                usedLetters.Add(c);
            });
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine();

            if (hangman.Lives <= 0)
            {
                Console.WriteLine("\nYou lost.");
            }
            else if (hangman.HasPlayerWon())
            {
                Console.WriteLine("\nYou won!");
            }
            else
            {
                Console.Write("\n> ");
            }
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Hangman!");
            Hangman          hangman   = new Hangman();
            HangmanValidator validator = new HangmanValidator();
            string           guess;
            string           continueToPlayInput;
            bool             continueToPlay = false;

            do
            {
                // Continue to guess while the word has not been guessed or the user still has lives
                do
                {
                    Console.WriteLine("Word: {0} \n", hangman.GetHiddenWord());
                    // Ask user to guess a letter
                    do
                    {
                        Console.WriteLine("Guess a letter");
                        guess = Console.ReadLine();
                    } while (!validator.ValidateGuess(guess));
                    // Check if the letter has been guessed
                    if (!hangman.LetterAlreadyGuessed(guess[0]))
                    {
                        hangman.Guess(guess[0]);
                        Console.WriteLine(hangman.GetLettersGuessed());
                        Console.WriteLine("Lives remaining: {0}", hangman.GetLivesRemaining());
                    }
                    hangman.CheckIfLetterIsGuessed();
                } while (hangman.CanContinueToGuess() && !hangman.Guessed);
                Console.WriteLine("Word: {0} \n", hangman.GetHiddenWord());
                if (hangman.Guessed)
                {
                    Console.WriteLine("You guessed it!");
                }
                else
                {
                    Console.WriteLine("No more lives");
                }
                // Ask user if they want to continue playing
                do
                {
                    Console.WriteLine("Do you want to continue to play? [y/n]");
                    continueToPlayInput = Console.ReadLine();
                } while (!validator.ValidateContinueToPlayInput(continueToPlayInput[0]));
                continueToPlay = hangman.DecideContinueToPlay(continueToPlayInput[0]);
                if (continueToPlay)
                {
                    hangman.ResetGame();
                    Console.WriteLine("");
                }
            } while (continueToPlay);
        }
Exemplo n.º 10
0
        public void NoMoreGuessingPossibleOnceNoAttemptIsLeft()
        {
            Hangman h = new Hangman("abc");

            for (int c = 9; c > 0; c--)
            {
                h.Guess('x'); Assert.AreEqual(c, h.GetRemainingAttempts(), "attempt " + c + " guess x -> ---");
            }

            h.Guess('x'); Assert.AreEqual(0, h.GetRemainingAttempts(), "guess x -> ---");
            h.Guess('x'); Assert.AreEqual(0, h.GetRemainingAttempts(), "guess x -> ---");
        }
Exemplo n.º 11
0
        public void TestMethodHangmanParameter(string expected, string input)
        {
            var    h = new Hangman("Developer");
            string s = "";

            foreach (var c in input)
            {
                s = h.Guess(c);
            }

            Assert.AreEqual(s, expected);
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to hangman! I will pick a word and you will try to guess it");
            Console.WriteLine("If you guess wrong six times, i win");
            Console.WriteLine();
            Console.WriteLine("I have picked my word, below is a picture, and below is your current guess");
            Console.WriteLine("Every time you guess incorrectly, i add a body part of the picture");
            Console.WriteLine();
            bool doYouWantToPlay = true;

            while (doYouWantToPlay)
            {
                Console.WriteLine();
                Console.WriteLine("Alright, let's play!");
                var game = new Hangman();
                do
                {
                    //draw
                    Console.WriteLine();
                    Console.WriteLine(game.DrawPicture());
                    Console.WriteLine();
                    Console.WriteLine(game.GetFormalCurrentGuess());
                    Console.WriteLine(game.MysteryWord);
                    Console.WriteLine();
                    //get the guess
                    Console.WriteLine("Enter a character you think is in the word");
                    var guess = Console.ReadLine().ToLower();
                    Console.WriteLine();
                    //check if the character is guessed already
                    while (game.IsGuessedAlready(guess))
                    {
                        Console.WriteLine("Try again, you've already guessed that character");
                        guess = Console.ReadLine();
                    }
                    if (game.PlayGuess(guess))
                    {
                        Console.WriteLine("Great guess! That character is in the word!");
                    }
                    else
                    {
                        Console.WriteLine("Unfortunately that character isn't in the word");
                    }
                } while (!game.GameOver());
                //keep playing
                Console.WriteLine();
                Console.WriteLine("Do you want to play another round? Y/N");
                var repsonse = Console.ReadLine();
                doYouWantToPlay = repsonse == "Y";
            }
        }
Exemplo n.º 13
0
        public void OneGuessShouldReduceRemainingAttemptsByOne()
        {
            Hangman h = new Hangman("secret");

            Assert.AreEqual("-e--e-", h.Guess('e'));
            Assert.AreEqual(9, h.RemainingAttemps());
            Assert.AreEqual("se--e-", h.Guess('s'));
            Assert.AreEqual(8, h.RemainingAttemps());
            Assert.AreEqual("se-re-", h.Guess('r'));
            Assert.AreEqual(7, h.RemainingAttemps());
            Assert.AreEqual("secre-", h.Guess('c'));
            Assert.AreEqual(6, h.RemainingAttemps());
            Assert.AreEqual("secret", h.Guess('t'));
            Assert.AreEqual(5, h.RemainingAttemps());
        }
Exemplo n.º 14
0
        public void NoMoreGuessingPossibleOnceNoAttemptIsLeft()
        {
            Hangman h = new Hangman("abc");

            Assert.AreEqual("---", h.Guess('x')); Assert.AreEqual(9, h.RemainingAttemps());
            Assert.AreEqual("---", h.Guess('x')); Assert.AreEqual(8, h.RemainingAttemps());
            Assert.AreEqual("---", h.Guess('x')); Assert.AreEqual(7, h.RemainingAttemps());
            Assert.AreEqual("---", h.Guess('x')); Assert.AreEqual(6, h.RemainingAttemps());
            Assert.AreEqual("---", h.Guess('x')); Assert.AreEqual(5, h.RemainingAttemps());
            Assert.AreEqual("---", h.Guess('x')); Assert.AreEqual(4, h.RemainingAttemps());
            Assert.AreEqual("---", h.Guess('x')); Assert.AreEqual(3, h.RemainingAttemps());
            Assert.AreEqual("---", h.Guess('x')); Assert.AreEqual(2, h.RemainingAttemps());
            Assert.AreEqual("---", h.Guess('x')); Assert.AreEqual(1, h.RemainingAttemps());
            Assert.AreEqual("---", h.Guess('x')); Assert.AreEqual(0, h.RemainingAttemps());
            Assert.AreEqual("---", h.Guess('x')); Assert.AreEqual(0, h.RemainingAttemps());
        }
Exemplo n.º 15
0
        public void TestMethodHangman()
        {
            var h = new Hangman("Developer");

            Assert.AreEqual("---------", h.Guess('A'));
            Assert.AreEqual("-e-e---e-", h.Guess('E'));
            Assert.AreEqual("-e-e---e-", h.Guess('I'));
            Assert.AreEqual("-e-e-o-e-", h.Guess('O'));
            Assert.AreEqual("-e-e-o-e-", h.Guess('U'));
            Assert.AreEqual("-e-e-o-er", h.Guess('R'));
            Assert.AreEqual("-e-e-o-er", h.Guess('N'));
            Assert.AreEqual("-e-e-o-er", h.Guess('S'));
            Assert.AreEqual("-e-e-o-er", h.Guess('T'));
            Assert.AreEqual("-e-elo-er", h.Guess('L'));
            Assert.AreEqual("-e-eloper", h.Guess('P'));
            Assert.AreEqual("De-eloper", h.Guess('D'));
            Assert.AreEqual("Developer", h.Guess('V'));
        }
Exemplo n.º 16
0
        public void TestMethodHangman()
        {
            var h = new Hangman("Developer");

            Assert.AreEqual("---------", h.Guess('A'));
            Assert.AreEqual("-E-E---E-", h.Guess('E'));
            Assert.AreEqual("-E-E---E-", h.Guess('I'));
            Assert.AreEqual("-E-E-O-E-", h.Guess('O'));
            Assert.AreEqual("-E-E-O-E-", h.Guess('U'));
            Assert.AreEqual("-E-E-O-ER", h.Guess('R'));
            Assert.AreEqual("-E-E-O-ER", h.Guess('N'));
            Assert.AreEqual("-E-E-O-ER", h.Guess('S'));
            Assert.AreEqual("-E-E-O-ER", h.Guess('T'));
            Assert.AreEqual("-E-ELO-ER", h.Guess('L'));
            Assert.AreEqual("-E-ELOPER", h.Guess('P'));
            Assert.AreEqual("DE-ELOPER", h.Guess('D'));
            Assert.AreEqual("DEVELOPER", h.Guess('V'));
        }
Exemplo n.º 17
0
        static void Main()
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine(
                    "{0}{0}\tAre you ready to play a game of Hangman? [y/n]",
                    Environment.NewLine);

                if (Console.ReadLine().StartsWith("y"))
                {
                    Hangman game = new Hangman();
                    game.Start();
                }
                else
                {
                    break;
                }
            }
        }
Exemplo n.º 18
0
        static void Main(string[] args)
        {
            Hangman game = new Hangman();

            game.NewGame();

            while (!game.IsWordGuessed())
            {
                Console.Write("Enter letter: ");
                if (game.Guess(Console.ReadLine()[0]))
                {
                    Console.WriteLine(game.GetWord());
                }
                else
                {
                    Console.WriteLine("This letter is not in the word");
                }
            }
            Console.WriteLine("You have successfully guessed the word");
            Console.Read();
        }
Exemplo n.º 19
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to hangman!\n");
            Console.Write("Enter your secret word: ");
            string wordInput = Console.ReadLine();

            Hangman hangmanObject = new Hangman(wordInput);

            while (true)
            {
                Console.Clear();
                Console.WriteLine("Guess the word!\n");
                Console.WriteLine(hangmanObject.GetWordToBeDisplayed() + "\n");
                Console.WriteLine("Words Used: " + hangmanObject.GetUsedLettersToBeDisplayed());
                Console.WriteLine("Attempts Remaining: " + hangmanObject.Attempts + "\n");

                if (hangmanObject.IsGameOver())
                {
                    break;
                }

                Console.Write("Enter your guess: ");
                char enteredLetter = Console.ReadKey().KeyChar;

                hangmanObject.LetterExistsCheck(enteredLetter);
            }

            if (hangmanObject.HasWon == true)
            {
                Console.WriteLine("\nYou have won the game! :)");
            }
            else
            {
                Console.WriteLine("\nYou have lost the game! :(");
            }
        }
Exemplo n.º 20
0
        /*Constructor that creates all the elements of the GUI and sets
         * parameters of the window
         * computerScore: Score that the computer has
         * playerScore: score that the player has
         * difficulty: chosen difficulty of words to play the game with
         */
        public HangmanForm(int computerScore, int playerScore, int difficulty)
        {
            hangman            = new Hangman(difficulty);
            this.difficulty    = difficulty;
            this.StartPosition = FormStartPosition.CenterScreen;

            CreateResetButton();
            CreateHangedMan();
            CreateCurrentWord();
            CreateAlphabetList();
            CreateGuessWordBox();
            CreateGuessWordButton();
            CreateNewGameButton();
            CreateLetterLabel();
            player             = new Player(playerScore);
            this.computerScore = computerScore;
            CreateScoreLabel();

            this.Size = new Size(700, 700);
            //smaller than this and everything looks weird
            this.MinimumSize = new Size(550, 550);
            this.Text        = "Hangman";
            this.BackColor   = Color.FromName("white");
        }
Exemplo n.º 21
0
        static void Main(string[] args)
        {
            Hangman game;
            string  message; //message entered by the user that someone else will play the game with

            //ask user for the message to play the game with and instantiate hangman game
            Console.WriteLine("Please enter the message you'd like to play the game with.");
            message = Console.ReadLine();
            game    = new Hangman(message);

            //outputs the prompt for the user based on the message
            game.OutputGuess();

            while (!(game.BodyCounter == 6))
            {
                char guess; //the current guess

                Console.Write("Please enter the first letter that you'd like to guess: ");
                guess = Convert.ToChar(Console.ReadLine());

                game.Search(guess);
                game.OutputGuess();
            }
        }
Exemplo n.º 22
0
        static void Main(string[] args)
        {
            Hangman       hangman         = new Hangman();
            List <String> dictionary      = hangman.FillDictionary();
            int           numberOfGuesses = 10;
            Boolean       isPlaying       = true;
            Boolean       win             = false;
            Char          guess;
            Char          play;
            String        guessedLetters = "";

            while (isPlaying == true)
            {
                String word = hangman.SelectWord(dictionary);
                word = word.ToUpper();
                String  temp    = "";
                Boolean isFound = false;

                for (int i = 0; i < word.Count(); i++)
                {
                    if (word.ElementAt(i) != ' ')
                    {
                        temp = temp + "_";
                    }
                    else
                    {
                        temp = temp + " ";
                    }
                }

                for (int i = 0; i < numberOfGuesses; i++)
                {
                    isFound = false;
                    Console.Clear();
                    for (int z = 0; z < temp.Length; z++)
                    {
                        Console.Write(" " + temp[z]);
                    }
                    Console.WriteLine();
                    Console.WriteLine("You have " + (numberOfGuesses - i) + " guesses left.");
                    Console.WriteLine();
                    Console.WriteLine("Previous guesses:");
                    Console.WriteLine(guessedLetters);
                    Console.WriteLine();
                    Console.WriteLine("Enter Guess: ");



                    guess = Convert.ToChar(Console.ReadLine().ToUpperInvariant()[0]);
                    Console.WriteLine(guess);
                    guessedLetters += guess.ToString() + " ";


                    for (int k = 0; k < word.Length; k++)
                    {
                        if (word[k] == guess)
                        {
                            temp    = ReplaceAt(temp, k, guess);
                            isFound = true;
                        }
                    }

                    if (isFound)
                    {
                        i--;
                    }

                    if (!temp.Contains('_'))
                    {
                        i   = numberOfGuesses;
                        win = true;
                    }
                }

                Console.Clear();
                Console.WriteLine("Your word was : " + word);

                if (win)
                {
                    Console.WriteLine("You win!");
                }
                else
                {
                    Console.WriteLine("You lose!");
                }

                Console.WriteLine("Would you like to continue playing? Y/N");
                play = Convert.ToChar(Console.ReadLine().ToUpperInvariant());
                if (play == 'Y')
                {
                    isPlaying = true;
                }
                else
                {
                    isPlaying = false;
                }
            }
        }
Exemplo n.º 23
0
        // New Button
        // Starts the game
        private void button7_Click(object sender, EventArgs e)
        {
            // Create a new instance for each game
            // Easiest way - uses constructor to initialise everything
            h = new Hangman();
            lines = h.Load(wordlist); //Loads the List of words
            h.Lives = lives; // Gives lives according to difficulty
            richTextBoxLives.Text = Convert.ToString(h.Lives);
            richTextBoxGuessed.Text = h.GuessedValues;
            textBoxAlreadyGuessed.Text = "";
            richTextBoxEntry.Text = "";

            // Don't let user change diffculty or wordlists during game
            radioButtonEasy.Enabled = false;
            radioButtonMedium.Enabled = false;
            radioButtonHard.Enabled = false;
            radioButtonCountries.Enabled = false;
            radioButtonRandom.Enabled = false;
            radioButtonCountriesSwe.Enabled = false;
            radioButtonRandomSwe.Enabled = false;

            string st = "";
            int num = 0;
            st = h.ReturnRnd(); // returns a random word from the list
            h.CurrentWord = st; // Updates the class with the current word

            foreach (char c in st)
            {
                ++num;
            }

            richTextBoxDisplay.Text = h.Draw(num, 10); // The word the user sees starts off as all _'s
            h.Initialise(st);
            richTextBoxEntry.Enabled = true;

            // Gives focus of form onto the textbox
            this.ActiveControl = richTextBoxEntry;
        }
Exemplo n.º 24
0
 public WordManager(Hangman gmMenu)
 {
     InitializeComponent();
 }
Exemplo n.º 25
0
 public frmGameWindow(string word)
 {
     InitializeComponent();
     game = new Hangman(word);
     lblLetters.Text = game.ReturnGuessed();
 }
Exemplo n.º 26
0
        static void Main(string[] args)
        {
            //Start Game
            Game theGame = new Game();

            while (theGame.GetPlayGameStatus())
            {
                theGame = new Game();
                theGame.RestartGameStatus();

                //Get Word Before Game Starts
                Word    word    = new Word();
                Hangman hangMan = new Hangman();

                //Set Difficulty
                word.PromptDifficulty();
                hangMan.SetAttempts(Console.ReadLine());
                hangMan.SetHangman(0);
                hangMan.SetHangmanArea();

                theGame.ClearConsoleWindow();
                theGame.DisplayExitInfo();
                Console.ReadLine();
                while (theGame.GetGameStatus())
                {
                    theGame.ClearConsoleWindow();
                    if (word.GetWordCompleted() || hangMan.CheckManStatus() == false)
                    {
                        theGame.ChangeGameStatus();
                    }
                    else
                    {
                        theGame.DisplayRemainingTurns(hangMan.GetAttempts());
                        hangMan.DisplayHangmanArea();
                        theGame.PrintWord(word.GetWordLength(), word.GetWordArray(), word.GetWordState());
                        theGame.DisplayGuessStrings();
                        theGame.EnterGuess();
                        theGame.SetUserGuess(Console.ReadKey());
                        word.CheckGuessAgainstWord(theGame.GetUserGuess(), theGame.GetValidGuessState());
                        theGame.SetGuessStrings(word.GetCorrectGuessState());
                        theGame.SetAlphaGuess(word.GetCorrectGuessState());
                        word.SetWordCompleted();
                        hangMan.SetHangman(theGame.GetWrongAnswerCount());
                        hangMan.SetHangmanArea();
                    }
                }

                hangMan.DisplayHangmanArea();
                if (word.GetWordCompleted())
                {
                    theGame.DisplayVictoryMessage();
                }
                else
                {
                    theGame.DisplayDefeatMessage();
                }
                theGame.PromptUserToPlayAgain();
                theGame.SetPlayGameStatus(Console.ReadKey().KeyChar);
                theGame.ClearConsoleWindow();
            }
        }
Exemplo n.º 27
0
        public void GuessCorrectLetterShouldReturnStringWithRevealedChar(string secret, char guessedLetter, string solvedSecret)
        {
            Hangman h = new Hangman(secret);

            Assert.AreEqual(solvedSecret, h.Guess(guessedLetter));
        }
Exemplo n.º 28
0
        public void GuessedLetterIsFoundRegardlessTheCase(string secret, char guessedLetter, string solvedSecret)
        {
            Hangman h = new Hangman(secret);

            Assert.AreEqual(solvedSecret, h.Guess(guessedLetter));
        }
Exemplo n.º 29
0
        static void Main(string[] args)
        {
            var newGame = new Hangman();

            newGame.Game();
        }
Exemplo n.º 30
0
        public void GuessWrongLetterShouldReturnDashedString(string secret, char guessedLetter, string solvedSecret)
        {
            Hangman h = new Hangman(secret);

            Assert.AreEqual(solvedSecret, h.Guess(guessedLetter));
        }