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!
18 Replies
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);"
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
I think you make functions beforehand and call on them later like in python? E.g. easy();, not too sure though
yeah exactly
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?
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
Huh, how would I write that if you could give an example?
something like this
could even make it even cleaner and move the difficulty parameters to its own method/lookup thats done in Main
dont forget that
maxNumber
there is the exclusive endtrue
You alive @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
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
:
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
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
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.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.
I converted the numbers to another datatype that its much easier to access digit by digit