This site requires JavaScript, please enable it in your browser!
Greenfoot back
pablo0826
pablo0826 wrote ...

2018/6/14

How to make object more according to dice roll?

pablo0826 pablo0826

2018/6/14

#
I am very new to Greenfoot, and basically I'm making a snakes and ladders game. What happens is, first the user clicks a button to being the game, which asks them how many people are playing (I have not done anything yet with the number of players), and then asks them to click on the game piece they would like to play with, which are these animals that are already on the board. When they click it, the object moves to the start position of the board. Now what is supposed to happen is, when they click the golden ball on the board, it should generate a random number, print it out as a "showMessageDialog", and then the game piece should move a certain distance according to the number generated by the dice. I tried to debug by printing out the random number again after the message dialog displays it, and it was a different random number. I'm trying to keep the number the same but I can't seem to figure it out....The first code below is the the object which,when clicked should show a random number..
import greenfoot.*; 
import javax.swing.*;
import java.util.Random;

public class DiceRoll extends Actor
{
    Random rand = new Random();
    
    public DiceRoll()
    {
        roll();
    }
    public int roll() 
    {
        int randNumber = 1 + Greenfoot.getRandomNumber(6);
        return randNumber;
    }   
}
This is the code for the World...
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.*;
public class GameBoard extends World
{
    private DiceRoll dice = new DiceRoll();
    Hippo p1 = new Hippo();
    public GameBoard()
    {    
       super(1350, 1050, 1);
     
       addObject(new Start_Button(), 215, 200);
        
        addObject(new PlayerOne(), 85, 350);
        //addObject(new Hippo(), 200, 350);
        addObject(p1, 200, 350);
        
        addObject(new PlayerTwo(), 85, 430);
        addObject(new Wombat(), 200, 430);
        
        addObject(new PlayerThree(), 85, 510);
        addObject(new Pelican(), 200, 510);
        
        addObject(new PlayerFour(), 85, 590);
        addObject(new Fish(), 200, 590);
        
        addObject(new PlayerFive(), 85, 670);
        addObject(new Spider(), 200, 670);
        
        addObject(new Welcome(), 150, 65);
        
        addObject(new ClickHere(), 94, 200);
       
        //addObject(new DiceRoll(), 125, 840);
        addObject(dice, 125, 840);
        addObject(new RollDie(), 130, 770);
        
        addObject(new Start(), 228, 945);
        
    }
    public void act()
    {
        if(Greenfoot.mouseClicked(dice))
        {
            JOptionPane.showMessageDialog(new JInternalFrame(), "You have rolled a: " + dice.roll(),"Dice Roll", JOptionPane.INFORMATION_MESSAGE);
            if (dice.roll() == 1)
            {
                //p1.setLocation(350, 980);
                p1.move(100);
            }
            if (dice.roll() == 2)
            {
                //p1.setLocation(450, 980);
                p1.move(200);
            }
            if (dice.roll() == 3)
            {
                //p1.setLocation(550, 980);
                p1.move(300);
            }
            if (dice.roll() == 4)
            {
                //p1.setLocation(650, 980);
                p1.move(400);
            }
            if (dice.roll() == 5)
            {
                //p1.setLocation(750, 980);
                p1.move(500);
            }
            if (dice.roll() == 6)
            {
                //p1.setLocation(850, 980);
                p1.move(600);
            }
            //p1.setLocation(650, 1000);
            System.out.println(dice.roll());
        }
    }
}
pablo0826 pablo0826

2018/6/14

#
Title should be *move
danpost danpost

2018/6/14

#
pablo0826 wrote...
what is supposed to happen is, when they click the golden ball on the board, it should generate a random number, print it out as a "showMessageDialog", and then the game piece should move a certain distance according to the number generated by the dice. I tried to debug by printing out the random number again after the message dialog displays it, and it was a different random number
Break line 44 up into two lines -- one to roll the dice and the other to show what was rolled. The first line should assign a local variable the value of the rolled dice and subsequent lines should refer to that variable (not roll the dice again):
int roll = dice.roll();
JOptionPane.showMessageDialog(new JInternalFrame(), "You have rolled a: "+roll," Dice Roll", JOptionPane.INFORMATION_MESSAGE);
p1.move(roll*100);
pablo0826 pablo0826

2018/6/14

#
Yup, I just found out that I actually had to simply assign it to a variable, also, how would i add the option for the program to print out, "next player", and then they get to roll? Thanks for the really quick reply!
danpost danpost

2018/6/14

#
pablo0826 wrote...
Yup, I just found out that I actually had to simply assign it to a variable, also, how would i add the option for the program to print out, "next player", and then they get to roll?
That would use another field. You apparently have one for player#1 and one for player#2, p1 and p2. Now, add one for the current player and have it initially assigned to p1:
private Actor playerUp = p1;
Then refer to it instead of individually to p1 or p2. To change players, you can use this line:
if (playerUp == p1) playerUp = p2; else playerUp = p1;

// or equivalently
playerUp = playerUp == p1 ? p2 : p1;
pablo0826 pablo0826

2018/6/14

#
danpost wrote...
pablo0826 wrote...
Yup, I just found out that I actually had to simply assign it to a variable, also, how would i add the option for the program to print out, "next player", and then they get to roll?
That would use another field. You apparently have one for player#1 and one for player#2, p1 and p2. Now, add one for the current player and have it initially assigned to p1:
private Actor playerUp = p1;
Then refer to it instead of individually to p1 or p2. To change players, you can use this line:
if (playerUp == p1) playerUp = p2; else playerUp = p1;

// or equivalently
playerUp = playerUp == p1 ? p2 : p1;
Sorry, I'm kind of confused, where would I print out in a message dialog that it is the next player's turn?
danpost danpost

2018/6/14

#
pablo0826 wrote...
where would I print out in a message dialog that it is the next player's turn?
Well, you would probably (rather, should) control whose turn it is in the world. When it is determined that a turn is over, the world should change the playerUp assignment and display the message dialog indicating the player change.
pablo0826 pablo0826

2018/6/14

#
Oh alright, got it, thanks!
You need to login to post a reply.