# Simulation for Grid Game?

• May 11th, 2013, 10:03 AM
lah2015
Simulation for Grid Game?
I am trying to simulate a grid game using java (for loops, if/else). The game is played as follows: The player draws a card that has one of four colors (Red, green, blue, yellow). Then, the player rolls two dice and finds the product of the two numbers. The player tries to find the number on a grid with the multiples of numbers one through 6. Each number correlates with a color, and if the number the player gets matches the color you win. If not, you lose.

Here is the code I have attempted so far but I am getting errors I don't know how to fix, please help!

Code :

```public class ColorGrid { public static void main(String args[]) { //Pick a Color int color=(int)(Math.random()*4)+1; //Product of 2 Dice int a=(int)(Math.random()*6)+1; int b=(int)(Math.random()*6)+1; int product=(a*b);   int trial=1; int win=0; int lose=0; //Red for(color=1,trial<=500,trial++); { if(product==2,6,15,24) { win++; } else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) { lose++; } }```

Errors:

Code :

```ColorGrid.java:16: error: not a statement for(color=1,trial<=500,trial++); ^ ColorGrid.java:16: error: ';' expected for(color=1,trial<=500,trial++); ^ ColorGrid.java:17: error: illegal start of expression { ^ ColorGrid.java:18: error: ')' expected if(product==2,6,15,24) ^ ColorGrid.java:18: error: ';' expected if(product==2,6,15,24) ^ ColorGrid.java:18: error: illegal start of expression if(product==2,6,15,24) ^ ColorGrid.java:18: error: ';' expected if(product==2,6,15,24) ^ ColorGrid.java:18: error: illegal start of expression if(product==2,6,15,24) ^ ColorGrid.java:18: error: ';' expected if(product==2,6,15,24) ^ ColorGrid.java:18: error: illegal start of expression if(product==2,6,15,24) ^ ColorGrid.java:18: error: ';' expected if(product==2,6,15,24) ^ ColorGrid.java:22: error: illegal start of type else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: <identifier> expected else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: ';' expected else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: illegal start of type else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: <identifier> expected else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: ';' expected else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: illegal start of type else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: ';' expected else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: <identifier> expected else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: illegal start of type else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: <identifier> expected else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: ';' expected else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: illegal start of type else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: <identifier> expected else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: ';' expected else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: illegal start of type else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: <identifier> expected else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: ';' expected else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: illegal start of type else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:22: error: <identifier> expected else(product==!2)&&(product==!6)&&(product==!15)&&(product==!24) ^ ColorGrid.java:23: error: ';' expected { ^ ColorGrid.java:24: error: illegal start of type lose++; ^ ColorGrid.java:24: error: <identifier> expected lose++; ^ ColorGrid.java:24: error: ';' expected lose++; ^ ColorGrid.java:30: error: class, interface, or enum expected } ^```
• May 11th, 2013, 10:37 AM
Norm
Re: Simulation for Grid Game?
Quote:

I am getting errors
Please copy the full text of the error messages and paste it here.

Take a look at the tutorial for the correct syntax:
http://docs.oracle.com/javase/tutori...dbolts/if.html
http://docs.oracle.com/javase/tutori...bolts/for.html
http://docs.oracle.com/javase/tutori...bolts/op2.html
• May 11th, 2013, 10:40 AM
lah2015
Re: Simulation for Grid Game?
• May 11th, 2013, 10:44 AM
Norm
Re: Simulation for Grid Game?
There are several syntax errors in the code. You need to read the tutorials at the links I posted to see the correct way to write the code.
• May 11th, 2013, 10:46 AM
theoriginalanomaly
Re: Simulation for Grid Game?
in the for statement you need to separate with semicolons. for (color = 1; trial <=500; trial++) and not put a semicolon after the parentheses. In your if statements, you want to write != not ==!(int). Also in your compound else statement. Else is used to be done as long as the if statement is not used. It doesn't require any logic checks. If you want a logic check, you would need to use else if. And in your if statement, you cannot have commas like that to check multiple values.

Code java:

```for (color = 1; trial <= 500; trial++)   if ((product == 2) && (product == ... etc) // notice the double opening parentheses. This makes the compound if statement   else lose++ // or   else if ((product != 2) && (product != 4)) //```
• May 11th, 2013, 10:48 AM
lah2015
Re: Simulation for Grid Game?
So, is it okay to have an if-else statement nested within a for loop?
• May 11th, 2013, 10:51 AM
Norm
Re: Simulation for Grid Game?
Yes, a for loop can contain if statements and many other types of statements.
• May 11th, 2013, 11:05 AM
lah2015
Re: Simulation for Grid Game?
Ok, now it is compiling but it is not completing 500 trials like I want it to, instead it is only completing one.

Code :

```public class ColorGrid { public static void main(String args[]) { //Pick a Color int color=(int)(Math.random()*4)+1; //Product of 2 Dice int a=(int)(Math.random()*6)+1; int b=(int)(Math.random()*6)+1; int product=(a*b);   int trial=1; int win=0; int lose=0; //Red for(trial=1; trial<=500; trial++); { if(color==1) if((product==2)||(product==6)||(product==15)||(product==24)) { win++; } else lose++; //Blue else if(color==2) if((product==4)||(product==12)||(product==20)||(product==36)) { win++; } else lose++; //Green else if(color==3) if((product==3)||(product==10)||(product==16)||(product==30)) { win++; } else lose++; //Yellow else if(color==4) if((product==1)||(product==5)||(product==9)||(product==9)||(product==18)||(product==25)) { win++; } else lose++; } System.out.println("Number of Wins: "+win); System.out.println("Number of Losses: "+lose);   } }```

Is it something with the for loop that is incorrect?
• May 11th, 2013, 11:14 AM
theoriginalanomaly
Re: Simulation for Grid Game?
You didn't remove the semicolon from the end of the for loop. for (trial = 1; trial<=500;trial++) no semicolon. Other then that, you are sending the same numbers through the for loop, so even if it ran 500 times you would get either 500 wins or 500 losses.
• May 11th, 2013, 11:15 AM
Norm
Re: Simulation for Grid Game?
Quote:

it is only completing one.
Why do you think it is only completing one loop?

Add a call to the println() method inside the loop that prints out the values of trial and the value of color.
The print out will show you the number of loops the program is making.

Please copy the program's output that shows what you are talking about.
• May 11th, 2013, 11:16 AM
lah2015
Re: Simulation for Grid Game?
That should not be the case, but I don't really know how to fix that. I should be getting a combination of wins and losses?

Norm, at first the output only showed:
Number of Wins: 0
Number of Losses: 1

Now, after removing the semi-colon, it is:

Number of Wins: 0
Number of Losses: 500

or vice versa.

But I need it to be a combination of wins and losses. What am I doing wrong?
• May 11th, 2013, 11:21 AM
theoriginalanomaly
Re: Simulation for Grid Game?
You are assigning the values before the loop. Then going through the loop, with those assigned values.
• May 11th, 2013, 11:25 AM
lah2015
Re: Simulation for Grid Game?
How can I make it reassign the values each time it goes through the loop?
• May 11th, 2013, 11:37 AM
theoriginalanomaly
Re: Simulation for Grid Game?
By removing the "How can" and the "?" from your last sentence.
• May 11th, 2013, 11:42 AM
lah2015
Re: Simulation for Grid Game?
Sorry hehe silly question! Thank you so much.