Reputation: 3
I am programming a hangman game for a school assignment, and my plan was to create an empty array with a set length for each difficulty and randomly assign a word of that length to them.
I've been having trouble with this function for a while, and I've already made a lot of workarounds (like default: return defaultReturn;)
char[] defaultReturn = { 'E', 'R', 'R', 'O', 'R' };
char[] difficulty1 = new char[4];
char[] difficulty2 = new char[5];
char[] difficulty3 = new char[6];
char[] difficulty4 = new char[7];
char[] difficulty5 = new char[10];
char[] difficulty6 = new char[18];
public char[] RndWord(int difficulty)
{
Random rnd = new Random();
int rndSelect = rnd.Next(5);
switch(difficulty)
{
case 1:
switch(rndSelect)
{
case 0:
{
char[] difficulty1 = { 'C', 'A', 'S', 'A' };
return difficulty1;
}
case 1:
{
char[] difficulty1 = { 'S', 'A', 'G', 'A' };
return difficulty1;
}
case 2:
{
char[] difficulty1 = { 'T', 'E', 'S', 'E' };
return difficulty1;
}
case 3:
{
char[] difficulty1 = { 'V', 'I', 'D', 'A' };
return difficulty1;
}
case 4:
{
char[] difficulty1 = { 'G', 'U', 'M', 'E' };
return difficulty1;
}
default:
return defaultReturn;
}
case 2:
switch (rndSelect)...
case 3:
switch(rndSelect)...
case 4:
switch(rndSelect)...
case 5:
switch(rndSelect)...
case 6:
switch (rndSelect)...
default:
return defaultReturn;
}
}
I'm calling the function when you press a certain button, that updates the word you are currently trying to guess.
private void btnDif_Click(object sender, EventArgs e)
{
switch(numDif.Value)
{
case 1:
RndWord(1);
lblWord.Text = String.Format("{0} {1} {2} {3}",
difficulty1[0], difficulty1[1],
difficulty1[2], difficulty1[3]);
break;
// other cases...
}
Now, the problem is that the method never reaches any case except for default (Edit: I've tested it, and it reaches both default cases). I don't know if the problem is with the random number generation, the second case statement, the array, or something else entirely.
Upvotes: 0
Views: 56
Reputation: 13972
As this is homework I'm not going to solve the problem for you but I will tell you what you're doing wrong. I'm seeing 2 major issues.
As a member variable, you're creating difficulty1
. Then farther down inside your switch
statement, you're creating a new one. That's the crux of the issue. You are declaring and setting and entirely new variable with the same name.
char[] difficulty1 = { 'C', 'A', 'S', 'A' }; // the char[] at the beginning here denotes a declaration.
return difficulty1; // you're returning the local difficulty1, not the member variable
You then return it, but at the point you're calling that function you're throwing away the return value.
RndWord(1);
Calling it, but not setting the return into anything. Thus, you never actually have access to { 'C', 'A', 'S', 'A' }
.
However, this issue isn't obvious. You'll need to do what's called "debugging". Add a breakpoint to your code and use F10/F11 to "step" through your program line-by-line.
My guess would be that numDif.Value
isn't what you think it is. However, you'll need to step through the program by debugging to figure out what it actually is.
I highly encourage you to learn "debugging". It's a highly valuable skill.
Upvotes: 0