Building a Guessing Game | C# | Tutorial 19
ฝัง
- เผยแพร่เมื่อ 7 พ.ย. 2017
- Source Code - www.giraffeacademy.com/program... This video is one in a series of videos where we'll be looking at programming in C#. The course is designed for new programmers, and will introduce common programming topics using the C# language.
Throughout the course we'll be looking at various topics including variables, arrays, getting user input, loops, conditionals, object orientation and much more.
I've been watching an rewatching this series to get a better grasp on the basics of C# and just wanted to say that you're course has been a huge, huge help. I'd have to say it's the best course on C# for beginners that I've found on TH-cam, Skillshare, Udemy or any other site. Thanks so much for putting these videos up! :)
I was so willing to pay for the basic course and look through entire list of Udemy and couldn't really find anything decent and funny enough I found his channel and it's FREE!
literally love you, helped me finish my game when i was stuck
Hey Mike! I'm loving the tutorials, thank you! I took your idea from this tutorial and played with it a little bit. I figure you're too busy to check it out but I just wanted to say thanks for the hard work... you're definitely making a difference.
Great video! Easy to follow. Thanks, Mike!
Hello Mike. I noticed a difference in the code for this video and what is on your website.
while(!guess == secretWord && !outOfGuesses) Is what the website shows, but brings back an error.
while(guess != secretWord && !outOfGuesses) works correctly and is what is shown in the video.
Thank you for your tutorials. I am a beginner and these videos are extremely helpful.
Thank you so much Alex for pointing out this errors. I spent a lot of time to figure this out.
Thank you Mike for this great tutorial!
I benefitted a lot from this course, but there's just one thing I was wondering; how do you create a guessing game where the program selects a random string variable from an array, and then sets that as the word that you have to guess?
Thanks so much for this amazing tutorial :)
I managed to write the code below using a do while loop.
I'm also reading a textbook on C# as I go through your videos, so I was able to incorporate a couple of things that haven't been mentioned in your videos (so far). =)
string answer = "giraffe";
string guess = "";
int guessCounter = 5;
do
{
Console.WriteLine("You have {0} guesses left.", guessCounter);
Console.WriteLine("What is the most powerful animal in the universe?");
guess = Console.ReadLine();
Console.Write("
"); //noob line break implementation!
guessCounter -= 1;
}
while (guess != answer && guessCounter > 0);
if (guess == answer)
{
Console.WriteLine("
You won!~");
}
else
{
Console.WriteLine("
You died!");
}
Console.ReadLine();
Can u tell me the name of book??
hi do you have a word guessing game with ArrayList and StringBuilder example?
how do you make it where from the console player 1 enters a random word then player 2 guesses what it is?
I went ahead and added a counter like this :
int guessRemaining = guessLimit - guessCount;
Console.Write("You have " + guessRemaining + " guess(es) remaining. Guess the secret word: ");
guess = Console.ReadLine();
guessCount++;
It worked out well! 😺 I even let the user input how many guesses they wanted and did not let them specify a negative number or over 10. The code for that part looks messy though.
Added an array ( 03:11 ) to account for lower and uppercase (1st letter only):
static void Main(string[] args)
{
//LESSON 17
Console.WriteLine("LESSON 17");
Console.WriteLine("");
string[] secretWord = new string [2];
secretWord[0] = "Author";
secretWord[1] = "author";
string guessWord = "";
{
}
while (guessWord != secretWord[0] && guessWord != secretWord[1])
{
Console.Write("Enter a guess for the secret word: ");
guessWord = Console.ReadLine();
}
Console.WriteLine("You Win!");
Console.ReadLine();
}
I have a little question
In the code, your put the guessLimit at 3 meaning that you only want the user to answer 3 times. But under the while loop in the if statement you put the condition as (guessCount < guessLimit).
guessCount keeps incrementing it self, and well I don't know how to say this but I'll try, i expected that the comdition would be (guessCount
To make the user input case-insensitive you can use '.ToLower()'
Like this "guess = Console.ReadLine().ToLower();"
Theres a problem with the game though. It's lacking an if-statement before the loop begins.
Because if you get the word correct in 1st try, you are still prompted to guess again untill you hit 3/3 guesses.
Didn't do that for me:
static void Main(string[] args)
{
//LESSON 17
Console.WriteLine("LESSON 17");
Console.WriteLine("");
string[] secretWord = new string [2]; //Use an array to acount for Author or author being typed.
secretWord[0] = "Author";
secretWord[1] = "author";
string guessWord = "";
int guessCount = 0;
int guessLimit = 3;
bool outOfGuesses = false;
{
}
while (guessWord != secretWord[0] && guessWord != secretWord[1] && !outOfGuesses)
{
if (guessCount < guessLimit)
{
Console.Write("Enter a guess for the secret word: ");
guessWord = Console.ReadLine();
guessCount++;
}
else
{
outOfGuesses = true;
}
}
if (outOfGuesses)
{
Console.WriteLine("You lose! You ran out of guesses.");
}
else
{
Console.WriteLine("You Win!");
}
Console.ReadLine();
}
12:36 Final product using an Array:
static void Main(string[] args)
{
//LESSON 17
Console.WriteLine("LESSON 17");
Console.WriteLine("");
string[] secretWord = new string [2]; //Use an array to account for Author or author being typed.
secretWord[0] = "Author";
secretWord[1] = "author";
string guessWord = "";
int guessCount = 0;
int guessLimit = 3;
bool outOfGuesses = false;
{
}
while (guessWord != secretWord[0] && guessWord != secretWord[1] && !outOfGuesses)
{
if (guessCount < guessLimit)
{
Console.Write("Enter a guess for the secret word: ");
guessWord = Console.ReadLine();
guessCount++;
}
else
{
outOfGuesses = true;
}
}
if (outOfGuesses)
{
Console.WriteLine("You lose! You ran out of guesses.");
}
else
{
Console.WriteLine("You Win!");
}
Console.ReadLine();
}
You can watch a very simple tutorial to develop puzzle game in C# for beginners at th-cam.com/play/PLHGJljNavL-PZ6P7w1IhZAZhZPp-9f_2L.html
Tried to do "do" loop. I'm newbie, but it kinda worked.
string secretword = "ass";
string guess = "";
int tries = 0;
bool outoftries = false;
do
{
if (tries < 3)
{
Console.WriteLine("Your guess: ");
guess = Console.ReadLine();
tries++;
}
else
{
outoftries = true;
}
}
while (guess != secretword && !outoftries);
if (outoftries) { Console.WriteLine("You failed"); }
if (guess == secretword) { Console.WriteLine("You won!"); }
here from next tutorial, also tried to create this game using "for" loop, but i have no idea if i did it right, it just works.
string secretword = "ass";
string guess = "";
int tries;
for (tries = 0; tries < 3; tries++)
{
Console.WriteLine("Your guess: ");
guess = Console.ReadLine();
if (secretword == guess) { Console.WriteLine("You won"); break; }
}
if (tries == 3)
{ Console.WriteLine("You failed"); }
Just a beginner, isn't this code cleaner?
// MY CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Giraffe1
{
class Program
{
static void Main(string[] args){
int count = 0;
do {
string secretWord = "apple";
Console.WriteLine("Yo what should be the secret word?");
string answer = Console.ReadLine();
count++;
if (secretWord == answer)
{
Console.WriteLine("you're right!");
break;
}
} while (count < 3);
Console.ReadLine();
}
}
}
// MY CODE
yeah but this video is not about just making the game its about to leaning to coding by these commands etc........
Your code is cleaner even mine is the cleanest but he is trying to summarize previous lessons in one video
Can't you just use a simple break; instead of !outOfGuesses?
You can use break; with if and else condition but not in parameter. Nature of while condition is Boolean only.
Hello sir i created this game with do wile loop
string word = "adi";
string ans="";
int guess= 0;
int trial =1;
bool outofguess = false ;
Console.WriteLine("Press enter to start");
Console.ReadLine();
do
{
if (guess
static void Main()
{
string secretWord = "apple";
string guess = " ";
int guessCount = 0;
int guessLimit = 3;
bool outOfGuesses = false;
do
{
if (guessCount < guessLimit)
{
Console.WriteLine("Etner your guess");
guess = Console.ReadLine();
guessCount++;
}
else
{
outOfGuesses = true;
}
}
while (guess != secretWord && !outOfGuesses);
if (outOfGuesses)
{
Console.WriteLine("You Lose!!");
}
else
Console.WriteLine("You WIN!!!!");
I would argue that having a game you can't exit from within is a worse design decision than the player having infinite guesses :]
Sorry, but nobody would ever win that game.