Southperry.net
Java logic. - Printable Version

+- Southperry.net (https://www.southperry.net)
+-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14)
+--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58)
+--- Thread: Java logic. (/showthread.php?tid=56520)



Java logic. - DeanNim - 2012-08-02

Im supposed to create a game with 32 buttons. It consists of a start game button, roll the dice button, 2 'counters' and and 32 buttons that create a 'game board'.

Im utterly confused about the logic im supposed to use to make the 'counter' move across the 'board'/buttons.

(viii) When the Roll the dice button is clicked, it should perform the following actions:
• Simulate the rolling of a dice and place the counter in the correct square. If the position of the counter exceeds Square 32, it will go backwards. E.g. if the counter is on Square 31 and the dice number is 4, it will go backwards and land on Square 29.
• Update the message in the text area to display the game status.
• When the counter lands exactly on Square 32, a message will be displayed to show the completion of the game:


 GUI

can somebody help explain what im supposed to do, step-by-step ?? just dont write the code for me. i do need help on what i need to declare though.


Java logic. - Fiel - 2012-08-02

Quote:(viii) When the Roll the dice button is clicked, it should perform the following actions:
• Simulate the rolling of a dice and place the counter in the correct square.

So use Java's Random class. (Generate an integer % 6) + 1

Quote: If the position of the counter exceeds Square 32, it will go backwards. E.g. if the counter is on Square 31 and the dice number is 4, it will go backwards and land on Square 29.

Run an update loop for this. It's the simplest option (though not the most mathematically astute).

Code:
while diceroll > 0
   if position == 32
     position -= diceroll
     diceroll = 0
   else
     position++
     diceroll--

if position == 32
  show congratulations
  reset game state

To make it appear as though the player is moving across the board, make the buttons in the depressed state to show the current position.

EDIT: Oh, you need to show the graphics on top of the boxes. Just draw on top of them. >_>

Quote:• Update the message in the text area to display the game status.
• When the counter lands exactly on Square 32, a message will be displayed to show the completion of the game:

These seem self-explanatory. All you're doing is updating text. Just put in the text area of the screen what position the player is at, what the dice roll was, and maybe how many turns have been played thus far.

For the counter landing on square 32, just show a modal popup that says "CONGRATULATIONS YOU WON" and show the stats for the game again. When the user clicks on "OK", reset the game state so the user can play again.


Java logic. - DeanNim - 2012-08-03

im still figuring out how to get the diceroll to add on to the 'current position' ;-; ;-; ;-;

ok i got that part figured out.

anyway, im at here, being all dumb about how to continue :

Code:
if(e.getSource()==roll){
            diceroll = game.Dice();
            position += diceroll;
            
            String update = "You rolled : "+diceroll + "\n";
            String positionupdate = "Your position: "+ (position) + "\n\n";
            
            textArea.append(update+positionupdate);
            

            

            }

also where do I put the above loop ? and im having trouble making the icon appear on the buttons as well.... there goes my assignment marks Sad


Java logic. - Unauthorized Intruder - 2012-08-03

I think you should put it right between update and positionupdate. I think loop could also be like this.

Also, delete position += diceroll line; position will be added within the loop.

Code:
boolean reach = false;
while(diceroll > 0)
   if(position == 32)
      reach = true;
   if(reach)
      position--;
   else
      position++;
   diceroll--;

That's the case you may need to animate the movement, but if you don't just use this

Code:
position += diceroll;
if(position > 32)
    position = 64 - position;



Java logic. - DeanNim - 2012-08-03

Unauthorized Intruder Wrote:That's the case you may need to animate the movement, but if you don't just use this

Code:
position += diceroll;
if(position > 32)
    position = 64 - position;

that works
[Image: 9TfME.png]