...

View Full Version : Mastermind program help



DPXJube
12-04-2011, 09:48 PM
Hello all
I've been working on a Mastermind program for a while now and I seem to be getting a problem when I run it.

Methods


public void casualcode()
{

do
{
boomhauer[variable1] = (int)(Math.random() * 6) + 1;
variable1++;
}
while (variable1 <= 3);

for(int bob = 0; bob <= 3; bob++)
{
System.out.println(boomhauer[bob]);
}
if (variable1 == 1) {
hank[variable2] = "Red";
} else if (variable1 == 2) {
hank[variable2] = "Blue";
} else if (variable1 == 3) {
hank[variable2] = "Green";
} else if (variable1 == 4) {
hank[variable2] = "Brown";
} else if (variable1 == 5) {
hank[variable2] = "Yellow";
} else if (variable1 == 6) {
hank[variable2] = "White";
}


for(int carl = 0; carl <= 3; carl++)
{
System.out.println(hank[carl]);
}





}
}

Runner/Main


import java.io.*;

public class Mastermindrunner
{
public static void main(String[] args)
{
String guile; String honda; String viper; int juri; String mature; char yoko;
Mastermind hadoken = new Mastermind();
guile = hadoken.firstinput();
honda = hadoken.helpmenu();
viper = hadoken.difficulty();
hadoken.casualcode();
hadoken.casualintro();


}
}


As you can see, the code is VERY incomplete at the moment, the only difficulty setting that works at the moment is Casual mode.
But anywho, that's not the point.
See this part of the methods?

public void casualcode()
{

do
{
boomhauer[variable1] = (int)(Math.random() * 6) + 1;
variable1++;
}
while (variable1 <= 3);

for(int bob = 0; bob <= 3; bob++)
{
System.out.println(boomhauer[bob]);
}
if (variable1 == 1) {
hank[variable2] = "Red";
} else if (variable1 == 2) {
hank[variable2] = "Blue";
} else if (variable1 == 3) {
hank[variable2] = "Green";
} else if (variable1 == 4) {
hank[variable2] = "Brown";
} else if (variable1 == 5) {
hank[variable2] = "Yellow";
} else if (variable1 == 6) {
hank[variable2] = "White";
}

for(int cool = 0; cool <= 3; cool++)
{
System.out.println(hank[cool]);
}
}
}

What SHOULD be happening is this..
An array of 4 random numbers is generated
THEN another array of 4 strings is generated
The strings will correspond with the random number array (ie if one of the random numbers is 4 then the array will print out Brown)
However when I run the program this is what I get

4
1
1
4
Brown
null
null
null


Does anyone know the reason as to why it prints out as null and only Brown prints out?
I would greatly appreciate any help.
(try to ignore the goofy object and variable names)

VIPStephan
12-04-2011, 10:28 PM
Java and JavaScript are two completely different languages. Please keep that in mind for the future. Moved to correct forum.

Old Pedant
12-04-2011, 10:43 PM
This is the JavaSCRIPT forum. About the only thing that JAVA (your code) and JavaScript have in common are the first 4 letters of their names.

But before you post in the right forum, some advice:

NOBODY could figure out FOR SURE from the code you have shown what your problem is.

You never show how variable2 is created and/or what values it might have.

But even having said that, your code makes no sense. And I *think* I see what is happening.

You say "and array of 4 random numbers is generated". Yes, I see that. In the array named boomhauer.

You then say "THEN another array of 4 strings is generated". No, that is not true.

You only ever set the value of *ONE* of the hank[..] strings.

There is NO LOOP in the code that does the hank[variable2] = "..." stuff. So ONLY ONE of those array entries will be given a string value. The rest WILL be null.

And we know what the value of variable1 will be when that is done: It will *ALWAYS* be 4! Because you ran this code:

do
{
boomhauer[variable1] = (int)(Math.random() * 6) + 1;
variable1++;
}
while (variable1 <= 3);
and at the end of it, variable1 will *ALWAYS* have a value of 4.

So when you get to the series of "if" conditions, the ONLY one that will be executed is this one:


} else if (variable1 == 4) {
hank[variable2] = "Brown";

So one and only one of the elements of the hank[] array will have the string "Brown".

And since you NEVER INITIALIZE OR CHANGE the value of variable2, that means that doing

hank[variable2] = "Brown";

is actually doing

hank[0] = "Brown";


And THAT is why you always get "Brown" as the one and only string that is displayed.

**********

In short, throw all that code away and start over. It's utter trash.



String[] colors = {"Red","Blue","Green","Brown","Yellow","White"};
for ( int i = 0; i <= 3; ++i )
{
int rnd = (int)(Math.random() * 6);
boomhauer[i] = rnd + 1;
hank[i] = colors[rnd];
System.out.println(hank[i]); // to demo that it worked
}

There. That just did the same thing your code purported to do, in one heluva lot less code.

******

EDIT: I see this thread has been moved to the Java forum. Good.

DPXJube
12-04-2011, 11:22 PM
Welp
I feel like an idiot
A thousand thanks Old Pendant
Also sorry for the forum mix up.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum