Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Simulation for Grid Game?

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default 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!

    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:

    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
    }
    ^


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Simulation for Grid Game?

    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
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    lah2015 (May 11th, 2013)

  4. #3
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Simulation for Grid Game?

    I added it to my post, sorry about that.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    lah2015 (May 11th, 2013)

  7. #5
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default 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.

    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)) //

  8. The Following User Says Thank You to theoriginalanomaly For This Useful Post:

    lah2015 (May 11th, 2013)

  9. #6
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Simulation for Grid Game?

    So, is it okay to have an if-else statement nested within a for loop?

  10. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Simulation for Grid Game?

    Yes, a for loop can contain if statements and many other types of statements.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    lah2015 (May 11th, 2013)

  12. #8
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default 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.

    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?

  13. #9
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default 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.

  14. The Following User Says Thank You to theoriginalanomaly For This Useful Post:

    lah2015 (May 11th, 2013)

  15. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Simulation for Grid Game?

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    lah2015 (May 11th, 2013)

  17. #11
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default 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?

  18. #12
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: Simulation for Grid Game?

    You are assigning the values before the loop. Then going through the loop, with those assigned values.

  19. The Following User Says Thank You to theoriginalanomaly For This Useful Post:

    lah2015 (May 11th, 2013)

  20. #13
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Simulation for Grid Game?

    How can I make it reassign the values each time it goes through the loop?

  21. #14
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: Simulation for Grid Game?

    By removing the "How can" and the "?" from your last sentence.

  22. The Following User Says Thank You to theoriginalanomaly For This Useful Post:

    lah2015 (May 11th, 2013)

  23. #15
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Simulation for Grid Game?

    Sorry hehe silly question! Thank you so much.

Similar Threads

  1. How to make a good looking game grid in swing?
    By Matthieu105 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 7th, 2013, 11:22 AM
  2. How to make a good looking game grid in swing?
    By Matthieu105 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2013, 07:46 AM
  3. A grid game
    By LuluMM in forum Paid Java Projects
    Replies: 3
    Last Post: April 3rd, 2012, 12:44 PM
  4. Java TV Board Game/Grid + Moving objects
    By Massaslayer in forum Java Theory & Questions
    Replies: 6
    Last Post: December 13th, 2011, 08:03 AM
  5. Airline Management Simulation Game
    By aussiemcgr in forum The Cafe
    Replies: 23
    Last Post: July 8th, 2011, 03:59 PM