New to C#, need help on random number guessing game!

Hello! I'm very new and was doing python as apart of my class but I have switched to C# as I plan to begin game development in the summer, I am currently creating a random number guessing game that I have been asked to make by my teacher, here is the client brief: After logging into a games library website and then selecting the number guessing game, the program generates a random four-digit number. The player has to keep inputting four-digit numbers on a keyboard until they guess the randomly generated number. After each unsuccessful try it should say how many numbers they got correct, but not which position they got right. At the end of the game should congratulate the user and say how many tries it took. An extended versions of the game has an easy mode which uses three-digits and shows the user which position that they guessed correctly, a medium mode as described above, and a hard mode that gives five digits instead of four. After the game is finished, ask the user for their name and input their score into a table. Show them the high score at the start of the game so that it gives a sense of competition. Code done so far in the images sent! The problem I have with my program currently is that I don't know how to tell the user what parts they got correct in their input. Quick note: incase you didnt read the brief: easy and medium tell the user what numbers they got correct with the location of those numbers, hard difficulty only tells the location. and easy has 3 digits, medium has 4, hard has 5. Please help me out, completely clueless, sorry I haven't had time to comment, I appreciate any tips or advice on how to do this, thank you. Also if you have time, I think I can do this by myself later but I would like to return the user to the difficulty selection stage after a finished game, thank you!
No description
No description
No description
18 Replies
Totally Regular Business Man
also <--- i have it tell me the numbers just for testing purposes, im going to remove it after testing - so it can be ignored. This part ----> "Console.WriteLine(randomNumber);"
Pobiega
Pobiega•2mo ago
First things first, thanks for writing a proper help post with detailed description of what you are trying to do and including source. second, do you know how methods/functions work and are made? cause looking at your code now, you'll see that your "easy", "medium" and "hard" branches are almost identical, with so much repeated code
Totally Regular Business Man
I think you make functions beforehand and call on them later like in python? E.g. easy();, not too sure though
Pobiega
Pobiega•2mo ago
yeah exactly
Totally Regular Business Man
Yep, basically copied and pasted it and just changed the amount of digits So you are saying create functions for some repeated parts of my program for the different difficulties?
Pobiega
Pobiega•2mo ago
considering the game will work the exact same way, except that you will offer slightly different output per difficulty and the number of numbers, it would make sense to write all this code once
Totally Regular Business Man
Huh, how would I write that if you could give an example?
Pobiega
Pobiega•2mo ago
public static void Main()
{
Console.WriteLine("Welcome to Numberle!");

Console.WriteLine("Please select a difficulty level:");
Console.WriteLine("1. Easy");
Console.WriteLine("2. Medium");
Console.WriteLine("3. Hard");

var difficulty = Console.ReadLine()?.ToLower();

StartGame(difficulty);
}

private static void StartGame(string difficulty)
{
var maxNumber = difficulty switch
{
"easy" => 999,
"medium" => 9999,
"hard" => 99999,
};

var number = Random.Shared.Next(0, maxNumber);

Console.WriteLine($"I'm thinking of a number between 0 and {maxNumber}. Can you guess it?");

// loop to keep asking for guesses etc
}
public static void Main()
{
Console.WriteLine("Welcome to Numberle!");

Console.WriteLine("Please select a difficulty level:");
Console.WriteLine("1. Easy");
Console.WriteLine("2. Medium");
Console.WriteLine("3. Hard");

var difficulty = Console.ReadLine()?.ToLower();

StartGame(difficulty);
}

private static void StartGame(string difficulty)
{
var maxNumber = difficulty switch
{
"easy" => 999,
"medium" => 9999,
"hard" => 99999,
};

var number = Random.Shared.Next(0, maxNumber);

Console.WriteLine($"I'm thinking of a number between 0 and {maxNumber}. Can you guess it?");

// loop to keep asking for guesses etc
}
something like this could even make it even cleaner and move the difficulty parameters to its own method/lookup thats done in Main
cap5lut
cap5lut•2mo ago
dont forget that maxNumber there is the exclusive end
Pobiega
Pobiega•2mo ago
true You alive @Totally Regular Business Man ? 🙂
Totally Regular Business Man
Sorry I am having a barbecue with my family Gonna come back to this in a bit, ty Oh wow I’ve never used the var data type before that is useful
cap5lut
cap5lut•2mo ago
var isnt a data type itself it will be simply the type u assign it to, so in var number = Random.Shared.Next(0, maxNumber); it be be an int because Next(...) returns an int its also only possible for local variables to use var for its declaration so for example this code doesnt compile, because the compiler can not determine the type of i:
var i; // will error here
i = 1;
var i; // will error here
i = 1;
Pobiega
Pobiega•2mo ago
so, I actually implemented this entire program, but I couldn't help myself and did it in a non-beginner way with records, enums and a whole bunch of methods.. still, I got a pretty good grasp on the problem now 🙂 so if you get stuck, lemme know
Totally Regular Business Man
Just the incorrect part I'm still stuck on, don't know how I tell the user which parts of their answer were correct. and with the hard difficulty - how many were correct
Pobiega
Pobiega•2mo ago
Well, you need to compare what the user put in with what you know it should be, digit by digit Hint: int is actually a pretty poor datatype for this.
Totally Regular Business Man
do i take away user input from the number digit by digit? to see if it is 0 wait thats silly actually idk id like to see what you did.
Pobiega
Pobiega•2mo ago
I converted the numbers to another datatype that its much easier to access digit by digit
Want results from more Discord servers?
Add your server
More Posts