Thread: Need some help with hangman game in c#.

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    1

    Need some help with hangman game in c#.

    This is a course project for school and this is what I'm supposed to modify. "This is your final modification to the project. Modify the design and program of the program so that the word is stored as an array. You can use code such as the following: char[] word = "happy".ToCharArray();Where "happy" is replaced with your name. Add a for loop to display each letter in the word character array. If it has been guessed correctly, display the correct letter, if not display the special character." Any help would be appreciated.

    Here's the c# code:
    insert
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace ConsoleApp7
    {
        class Program
        {
            static void Main(string[] args)
            {
            
    
    
                 Random random = new Random((int)DateTime.Now.Ticks);
    
    
                string[] wordBank = { "Venancio"};
                
                string wordToGuess = wordBank[random.Next(0, wordBank.Length)];
                string wordToGuessUppercase = wordToGuess.ToUpper();
    
    
                StringBuilder displayToPlayer = new StringBuilder(wordToGuess.Length);
                for (int i = 0; i < wordToGuess.Length; i++)
                    displayToPlayer.Append('*');
    
    
                List<char> correctGuesses = new List<char>();
                List<char> incorrectGuesses = new List<char>();
                
    
    
                int lives = 10;
                bool won = false;
                int lettersRevealed = 0;
                int score = 10;
                string input;
                char guess;
                while (!won && lives >  0)
                {
                    Console.Write("Guess a letter:");
    
    
                    input = Console.ReadLine().ToUpper();
                    guess = input[0];
                    
                    if (correctGuesses.Contains(guess))
                    {
                        Console.WriteLine("You've already tried '{0}', and it was correct!", guess);
                        continue;
                    }
                    else if (incorrectGuesses.Contains(guess))
                        
                    {
                        
    
    
    
    
                        Console.WriteLine("You've already tried '{0}', and it was wrong! ", guess);
                        
    
    
    
    
    
    
    
    
                        
    
    
    
    
    
    
    
    
    
    
                    }
                    
    
    
                    if (wordToGuessUppercase.Contains(guess))
                    {
                        correctGuesses.Add(guess);
    
    
                        for (int i = 0; i < wordToGuess.Length; i++)
                        {
                            if (wordToGuessUppercase[i] == guess)
                            {
                                displayToPlayer[i] = wordToGuess[i];
                                lettersRevealed++;
                            }
                        }
    
    
                        if (lettersRevealed == wordToGuess.Length)
                            won = true;
                    }
                    else
                    {
                        
                        incorrectGuesses.Add(guess);
                       
    
    
    
    
    
    
                        Console.WriteLine("Nope, there's no '{0}' in Ve!", guess);
                        
                       lives--;
                    }
    
    
                    Console.WriteLine(displayToPlayer.ToString());
                }
    
    
                if (won)
                    Console.WriteLine("You won!");
                else
                    Console.WriteLine("You lost! It was '{0}'", wordToGuess);
                    Console.WriteLine("You get '{0}' points! ", score);
                score = score + 1;
                Console.Write("Press ENTER to exit...");
                Console.ReadLine();
            }
        }
    }












  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    136
    ...word is stored as an array.
    You can use code such as the following: char[] word = "happy".ToCharArray();
    Where "happy" is replaced with your name.
    You are supposed to create an array of char for your word.

    Code:
    string[] wordBank = { "Venancio"};
    Why do you create an array of string ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game
    By GaitBait in forum C Programming
    Replies: 3
    Last Post: 12-06-2013, 01:32 PM
  2. Hangman Game
    By Mcdom34 in forum C# Programming
    Replies: 3
    Last Post: 10-13-2012, 11:21 AM
  3. Help with hangman game.
    By astarialexi in forum C Programming
    Replies: 8
    Last Post: 03-13-2011, 04:04 AM
  4. Hangman Game - Need Help!!
    By krobort in forum C++ Programming
    Replies: 3
    Last Post: 10-12-2006, 04:15 PM
  5. New game: Hangman!
    By abrege in forum Game Programming
    Replies: 6
    Last Post: 12-05-2002, 02:05 PM

Tags for this Thread