Results 1 to 18 of 18

Thread: Array puzzled

  1. #1
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default Array puzzled

    Hiya

    I've got an unexpected problem with an exercise I'm doing. It's for the online Stanford course and uses the ACM library. However, despite the unfamiliar methods, I'm fairly certain I've misunderstood something about arrays. This exercise is meant to teach all about them. It's to program a game of Yahtzee, and in the code below I've intended to get the first thing going- which is to roll the dice and store them in an array. The first roll works fine, and the array displays as it should. Before the second throw there's a method waitForPlayerToSelectDice where the player selects the reroll dice. In the for loop after that, isDieSelected determines which dice to reroll and add to the array. However, all the numbers come up blank, including the previously rolled numbers.

    Is there something wrong about my thinking that I can just add new values to the array in this way? Or in thinking that the boolean isDieSelected in the if statement would add the values in the right way?



    Java Code:
    for (turn = 0; turn < nPlayers; turn++) {
    			int playernumber = turn + 1;
    			display.printMessage(playerNames[turn] + "'s turn! Click the " + "\"Roll Dice\" " + "button to roll the dice.");
    			
    			int[] dice = new int[5];
    
    					display.waitForPlayerToClickRoll(playernumber);
    			
    			
    				for (int die = 0; die < 5; die++) {
    					int roll = rgen.nextInt(1, 6);
    					dice[die] = roll;
    				}
    				display.displayDice(dice);	
    				
    				display.printMessage("Select the dice you wish to re-roll and click " + "\"Roll Again\"");
    				display.waitForPlayerToSelectDice();
    				display.waitForPlayerToClickRoll(playernumber);
    				
    				for (int die = 0; die < 5; die++) {
    					if (display.isDieSelected(die)) {
    						int roll = rgen.nextInt(1, 6);
    						dice[die] = roll;
    					}
    				}
    					display.displayDice(dice);

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Array puzzled

    Stick some debugging println() calls in there so you can see what he code is doing.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Array puzzled

    the previously rolled numbers.
    A new dice array is created for each time around the loop. Is there any data in that array you want to save from one loop to the next?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default Re: Array puzzled

    Ah.

    Yes, all the values that weren't selected by the player. I tried using the array as an ivar but got the same result.

    The println thing is great but tricky because the display methods are for an interface that's pre-built for the exercise. Its not a console program. The display accepts strings, so in order to see ints I'd have to create an arraylist and pass them into that, I think.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Array puzzled

    How do you debug code in that environment if you can't use something like printlns to show what is happening?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default Re: Array puzzled

    Indeed. Good question. I think by using the tools in Eclipse- but I have been and it's not nearly so transparent as a println.

  7. #7
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default Re: Array puzzled

    So I'm guessing the way to do this is to have two arrays- one for throws and one to keep track of the results...

  8. #8
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default Re: Array puzzled

    Maybe a hashmap for the throws- index + result..

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Array puzzled

    What problem are you trying to solve now?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default Re: Array puzzled

    Quote Originally Posted by Norm View Post
    What problem are you trying to solve now?
    To keep track of the parts of the dice array that the player hasnt selected to re-throw. So initially there are five integers between 1-6 that are stored in the array. I need to keep the ones that arent rerolled and then send those, together with the new results in the right places in the array, to the display.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Array puzzled

    Can there be duplicates in the array? Do you want to use a list of numbers or a list of the indexes to the numbers?
    The indexes will be unique, the numbers might not be if there are duplicates.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default Re: Array puzzled

    Quote Originally Posted by Norm View Post
    What problem are you trying to solve now?
    Oh man. This is one of those head-slappy moments. The boolean in the if statement should read if (display.isDieSelected(die) == true). So bloody obvious...

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Array puzzled

    No it shouldn't, since isDieSelected() returns a boolean.
    No point comparing a boolean to true...it wouldn't change the logic of the statement.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default Re: Array puzzled

    You're right, it didn't. This is really annoying. I've looked at someone else's code online now and it's the same.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Array puzzled

    What's in the display.displayDice(...) method? Does it modify the array contents in any way?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default Re: Array puzzled

    What difference would it make if this code was encapsulated into two methods? First roll and secondandthird roll? Taking player number as a parameter but void. This is the solution that I found online;

    Java Code:
           private void secondAndThirdRoll(int playerNumber) {
          	            for (int i = 0; i < 2; i++) {
    	            display.printMessage("Select the dice you wish to re-roll and click " + "\"Roll Again\"");
                        display.waitForPlayerToSelectDice();
                        for(int j = 0; j < N_DICE; j++) {
    	                if(display.isDieSelected(j) == true) {
                        int dieRoll = rgen.nextInt(1,6);
                        dieResults[j] = dieRoll;
    	                }
    	            }
    	            display.displayDice(dieResults);
    As far as I can tell the only difference is that its a private void...?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Array puzzled

    Comment on the code formatting: You need to use care in formatting code with {s and }s that they are properly aligned so the code can be easily read and understood. The ending } should be vertically in line beneath the start of the line with the starting {. Lines within {}s should be indented 3-4 spaces.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default Re: Array puzzled

    Wow. I finally found it. I turned it into two methods and tried that, made no difference. Turns out that the waitForPlayerToSelectDice method also fulfills the same function as the waitForPlayerToClickRoll method. So if you put the waitForPlayerToClickRoll method after it, it screws things up. Gotta say, pretty much every major headache I've had learning this stuff has boiled down to learning precisely what library methods are doing as they execute. Anyway, sorry about that- I've wasted your time...

Similar Threads

  1. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  2. Replies: 15
    Last Post: 10-31-2011, 04:55 AM
  3. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  4. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  5. Replies: 2
    Last Post: 09-06-2010, 01:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •