Create a word guessing game that asks a player to guess a random word. As we build the C# console application, we'll discuss some fundamental programming concepts.
We'll build upon the code we started in Numeric Guessing Game: C# Console Application. (If you haven't read it yet, you might want to start there!)
Game Instance
In the numeric guessing game we didn't create an instance of the Game class. Our first step will be to change our code so we have a Game object. With examples of each you can compare the two approaches.
Remove:
- Delete the static keyword from our Game class members.
- Clear the Game Play method call from Main.
Add:
- Make an instance of the Game class.
- Inside of Main, call the instance's method Play.
The two versions are below. If you run them they will appear the same to the player; the only changes are internal.
Original:
class Program { static void Main() { Game.Play(); } }
Revised:
class Program { static void Main() { Game theGame = new Game(); theGame.Play(); } }
Full Code: Updated Numeric Guessing Game Code
using System; namespace GuessingGame { class Game { int Guess = 0; int Target = 5; string Input = ""; Random RandomNumber = new Random(); public void Play() { Target = RandomNumber.Next(10) + 1; Console.Write("Guess what number I am thinking of... "); Input = Console.ReadLine(); if (int.TryParse(Input, out Guess)) { if (Guess == Target) { Console.WriteLine("Congratulations! You guessed " + Guess + " and the number I was thinking of was " + Target + "."); } else { Console.WriteLine("Too bad. You guessed " + Guess + " and the number I was thinking of was " + Target + ". Try again!"); } } else { Console.WriteLine("Please enter a number."); Console.WriteLine("Press enter to continue..."); Console.ReadLine(); Play(); } Console.ReadKey(); } } class Program { static void Main() { Game theGame = new Game(); theGame.Play(); } } }
More About Static vs. Non-Static
For a small program like our word guessing game, there isn't a big impact using one approach or the other. In larger projects the difference can be substantial.
Here are some helpful resources:
Word Game
Using the new version of our numeric game as a base, we will convert it into a game where the player tries to guess a random word instead of number.
Array
Our list of possible words will be stored in an array. We'll use the same random number code we used previously. Now, though, our random number will be used to select an element. The player will see the options and can enter their guess as to which word will match.
Add an array to the Game class and initialize it with a few words. In the example the words are: cat, hat, and rat.
class Game { static int Guess = 0; static int Target = 5; string[] Words = { "cat", "hat", "rat" }; static Random RandomNumber = new Random(); [...]
Show Options
Write out the word options and ask the player to guess one of them. As with the numeric game, you can make this a simple instruction ("Choose a word:") or make it more fanciful ("My crystal ball is showing something ... through the mists I am getting a message... do you know what I see?").
To print out each word in the array, you can use a foreach statement:
foreach (string word in Words) { Console.Write(word + " "); }
You could also add commas between each word as it is written out. For this type of approach, you would want to know when you've reached the last element in the array. A for loop makes that easy:
Console.Write(" Guess which word I am thinking of... is it "); for (int i = 0;i< Words.Length;i++) { if (i==(Words.Length-1)) Console.Write("or " + Words[i] + "? "); else Console.Write(Words[i] + ", "); }
Comparison
To select a word from the array, we'll use the same random number as in our numeric game. The range can be dynamically determined from the length of the Words array.
Target = RandomNumber.Next(Words.Length);
The code to check whether the input is a number won't be needed now that we are matching words instead of integers. We can also change our conditional statement. It will now compare what the player has typed to the random element from the Words array.
if (Input == Words[Target]) { Console.WriteLine("Congratulations! You guessed it!"); } else { Console.WriteLine("Not a match. Try again!"); }
If the player didn't guess the word, we can call the Play() method to restart the game.
Word Guessing Game Final Code
using System; namespace GuessingGame { class Game { int Guess = 0; int Target = 5; string Input = ""; string[] Words = { "cat", "hat", "rat" }; Random RandomNumber = new Random(); public void Play() { Target = RandomNumber.Next(Words.Length); Console.Write(" Guess which word I am thinking of... is it "); for (int i = 0; i < Words.Length; i++) { if (i == (Words.Length - 1)) Console.Write("or " + Words[i] + "? "); else Console.Write(Words[i] + ", "); } Input = Console.ReadLine(); if (Input == Words[Target]) { Console.WriteLine("Congratulations! You guessed it!"); } else { Console.WriteLine("Not a match. Try again!"); Console.WriteLine("Press enter to continue..."); Console.ReadLine(); Play(); } Console.ReadKey(); } } class Program { static void Main() { Game theGame = new Game(); theGame.Play(); } } }
Next Step
Think about your game's interface. Can you make it more engaging?