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

2017/2/24

Move pawn (player) from dice roll

NicoZorlo NicoZorlo

2017/2/24

#
I'm doing a "game" for a school project and I fall in a problem. I have 4 pawn (player) and I must to move them from the value of a dice roll in a snake circuit. Where must I put the player movement and how can i link it with the dice?
danpost danpost

2017/2/24

#
That would be game control, which belongs in your subclass of World. You can have a method in the class of the player to receive the number to move that is called by the world after the dice is "rolled". That method should perform the appropriate movement for the player.
NicoZorlo NicoZorlo

2017/2/28

#
I fall in a little problem. We must to make the game of the goose :/ the problem is that in the movement i must to add the pattern of the "road". (image of the board: ) . the problem that i have now is when i have added the "snake road". to make things easier i'll add my code :D the world class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class background extends World {
 
    private Dice dice = new Dice();
    private Player player1 = new Player(0);
    private Player player2 = new Player(1);
    private Player player3 = new Player(2);
    private Player player4 = new Player(3);
    private int hasTurn = 0;
     
    public background() {   
        super(9, 8, 100);
        addObject(new Tabellone(),4,4);
        addObject(player1, 0, 7);
        addObject(player2, 0, 7);
        addObject(player3, 0, 7);
        addObject(player4, 0, 7);
        addObject(dice,4, 7);
        prepare();
         
    }
     
    public void act (){
        /*if (Greenfoot.mouseClicked(dicebutton)){
            player1.act();
            //player1.move(dice.Dice());
            /*
             * switch
             *    
             */
        //}
         
    }
     
    private void prepare()
    {
    }
}
my player class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
 
public class Player extends Actor
{
    static GreenfootImage[] images = { new GreenfootImage("Player1.png"), new GreenfootImage("Player2.png"), new GreenfootImage("Player3.png"), new GreenfootImage("Player4.png")};
     
    private Dice dice = new Dice();
     
    int playerNumber;
     
    public Player(int pNum){
        playerNumber = pNum;
        setImage(images[pNum]);
    }
     
    public void act() {
         
        int tiroDadi = dice.Dice();
        System.out.println(tiroDadi);
        int movimenti = tiroDadi;
        for ( int i = 0; i < tiroDadi; i++){
            int x = getX();
            int y = getY();
            if (x==8 && y==0 && movimenti==0) {
                //win
            } else if (x==8 && y==0 && movimenti!=0){
                setLocation(x-movimenti, y);
                break;
            } else if (x==0 || x==8){
                setLocation(x, y+1);
                movimenti--;
            } else if (y==0 || y==2 || y==4 || y==6){
                setLocation(x+1, y);
                movimenti--;
            } else {
                setLocation(x-1, y);
                movimenti--;
            }
             
        }
         
    }   
     
     
}
and finally my dice class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
 
 
public class Dice extends Actor{
     
     
    public int Dice(){
        int rollData=0;
         
         
        rollData = roll();
             
        return rollData;
    }
     
    public int roll(){
        int randNumber1 = 1+Greenfoot.getRandomNumber(5);
        int randNumber2 = 1+Greenfoot.getRandomNumber(5);
        int randNumber = randNumber1+randNumber2;
        setImage(new GreenfootImage(""+randNumber1+" - "+""+randNumber2,25,Color.BLACK,Color.WHITE));
        System.out.println("roll()"+randNumber);
        return randNumber;
         
    }
     
}
thank you in advance for your help Nico Edit (28.02.2017 - 11:14): updated code
danpost danpost

2017/2/28

#
You can programmatically create the pattern of the road using the position of the player. The following should show what I mean: If player is at left or right edge, then getX()%8 will result in a value of zero. TEST: getX() = 0 or 8 : result 0 getX() = 1, 2, 3, 4, 5, 6 or 7 : result is 1, 2, 3, 4, 5, 6 or 7 When at left or right edge, if map goes up, then getY()%2 + (getX()%9)/8 will result in a value of one. TEST: getX() = 0, getY() = 1, 3, 5 or 7 : result (1 + 0) = 1 getX() = 0, getY() = 0, 2, 4 or 6 : result (0 + 0) = 0 (cannot move up) getX() = 8, getY() = 0, 2, 4 or 6 : result (0 + 1) = 1 getX() = 8, getY() = 1, 3, 5 or 7 : result (1 + 1) = 2 (cannot move up) If cannot move up then player moves right only when getY()%2 results in a value of 0. TEST: getY() = 0, 2, 4, or 6 : result 0 (move right) getY() = 1, 3, or 5 : result 1 (move left) So, if at edge and can move up, move up; else move horizontally:
1
2
3
4
5
public void moveOneSpace()
{
    if (getX()%8 == 0 && getY()%2+(getX()%9)/8 == 1) setLocation(getX(), getY()-1);
    else setLocation(getX()+1-2*(getY()%2), getY());
}
NicoZorlo NicoZorlo

2017/2/28

#
WoW thank you very much
You need to login to post a reply.