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

2015/11/13

Board Game Piece Movement

Programmer1998 Programmer1998

2015/11/13

#
I'm trying to move my pieces so that when I click on a piece I get its x and y coordinates so that I will be able to click on a spot on the board and move it to that spot after. Here is what I have so far:
public boolean selectPiece()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        int x = 0;
        int y = 0;
        
        if(mouse != null) //if the mouse if pressing on something
        {
            Actor piece = mouse.getActor();
            isSelected_ = true;
            
            if(isSelected_ == true && Greenfoot.mouseClicked(piece))
            {
                x = piece.getX();
                y = piece.getY();
            }
        }
    }
    
    public void movePiece()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        
        if(selectPiece())
        {
            if(isSelected_ == true && Greenfoot.mouseClicked(this))
            {
                int fx = mouse.getX();
                int fy = mouse.getY();
                //piece.setLocation(fx, fy);
            }
        }
    }
danpost danpost

2015/11/13

#
There are missing bits of information that are required to give you help. First, where (in what class -- and what class does it extend) is this code? I would presume it to be a World subclass; but, I need to ask, to be sure. Second, you said it was a board game; are there squares or specific locations that a piece can move and what are the parameters of your world ("super(??, ??, ??)' )? Finally, what conditions do you have in place in calling the 'movePiece' method (and where is it called from)? Additional query: what is to prevent a piece to be moved to an unwanted location (like under another piece that is then clicked on and moved)? To help in this, you can use the state of an object existing in the 'piece' field. That is, only pick up a piece when its value is null and only move a piece when it is not. Also, should not the 'x' and 'y' values be retained (as world instance fields) so that if an illegal move is made, it can be replaced to its original location?
Programmer1998 Programmer1998

2015/11/13

#
Hi, thank you for responding (: To begin with, yes, this class is a subclass of the World. It is a board game with squares just like a typical checkers or chess board, and the parameters are super(8, 8, 90);. I have some code in my logic class that is supposed to help with the movement of the pieces. One is just for moving while the other one is for jumping over other pieces. Here is the code:
public boolean canMove(int r1, int c1, int r2, int c2)
    {
        if(currentPlayer_ == red_)
        {
            if(position_[r1][c1] == red_)
            {
                return true;
            }
            if(position_[r2][c2] == space_ && r2 > r1)
            {
                return true;
            }
            return false;
        }
        else
        {
            if(position_[r1][c1] == white_)
            {
                return true;
            }
            if(position_[r2][c2] == space_ && r2 < r1)
            {
                return true;
            }
            return false;
        }
    }
    
    public boolean move(int row, int column)
    {
        if(position_[row][column] == currentPlayer_)
        {
            if(currentPlayer_ == red_)
            {
                if( (canMove(row, column, row + 1, column + 1)) ||
                    (canMove(row, column, row + 1, column - 1)))
                    {
                        return true;
                    }
            }
            else if(currentPlayer_ == white_)
            {
                if( (canMove(row, column, row - 1, column + 1)) ||
                    (canMove(row, column, row - 1, column - 1)))
                    {
                        return true;
                    }  
            }
        }
        return false;
    }
    
    public boolean canJump(int r1, int c1, int r2, int c2, int r3, int c3)
    {
        if(currentPlayer_ == red_)
        {
            if(position_[r1][c1] == red_) 
            {
                return true;
            }
            if(position_[r2][c2] == white_)
            {
                return true; //if the position in between is white, you can jump over
            }
            if(position_[r3][c3] == space_ && r3 > r1) //new row must be larger than current
            {
                return true;
            }
            return false; //everything else is invalid
        }   
        else
        {
            if(position_[r1][c1] == white_) 
            {
                return true;
            }
            if(position_[r2][c2] == red_)
            {
                return true; //if the piece if red you can jump over
            }
            if(position_[r3][c3] == space_ && r3 < 1) //new row much be smaller than current
            {
                return true;
            }
            return false;
        }
    }
    
    public boolean jump(int row, int column)
    {
        if(position_[row][column] == currentPlayer_)
        {
            if(currentPlayer_ == red_)
            {
                if( (canJump(row, column, row + 1, column + 1, row + 2, column + 2)) ||
                    (canJump(row, column, row + 1, column - 1, row + 2, column - 2)))
                    {
                        return true;
                    }
            }
            else if(currentPlayer_ == white_)
            {
                if( (canJump(row, column, row - 1, column + 1, row - 2, column + 2)) ||
                    (canJump(row, column, row - 1, column - 1, row - 2, column - 2)))
                    {
                        return true;
                    }
            }
        }
        return false;
    }
I'm not quite sure if I am supposed to implement these methods into my movePiece() method.
danpost danpost

2015/11/13

#
Where is this logic class code located (what is class name and what does it extend)?
Programmer1998 Programmer1998

2015/11/14

#
The logic class is just a new class (is under "other classes"). All of game's rules and logic is put into this class. I called it CheckersLogic.
danpost danpost

2015/11/14

#
Programmer1998 wrote...
The logic class is just a new class (is under "other classes"). All of game's rules and logic is put into this class. I called it CheckersLogic.
Oh, okay. Good for you! Most newer greenfoot programmers tend to refrain from creating classes that are neither World or Actor subclasses. In fact, I have quite frequently found that they create classes like this as a subclass of Actor and add it into the world (sometimes even with a greenfoot icon showing as its image). If this is a checkers game, then the way you set your logic up in your 'canMove' and 'canJump' methods is flawed. You need to return false for all invalid situations and return true at the end. As you currently have it coded, if any one of multiple conditions that are required for a valid move is true, you are returning true, regardless of whether the other requirements needed are true or not. To make this more clear, let us look at the 'canMove' method. If the current player is red and the piece at the starting square is red then you are returning true (at line 7). This return statement forces an immediate exit out of the method; so the following checks for the destination square and its orientation to the starting square is ignored.
You need to login to post a reply.