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

2015/3/9

How do I make turns in a chess game?

astronomer800 astronomer800

2015/3/9

#
I'm relatively new to programming and I'm trying to make a 2-player chess game, which, of course, needs turns. I have a Piece class which has six sub-classes (one for each piece). In the piece class, I have a move method, which is then called from each piece in their act methods, and the move method has 2 parameter, one for which piece it is, and the other for which color it is (b or w). The move method moves the pieces if they are dragged, and are supposed to set up turns, but the problem is that my turn variable only changes for individual pieces, so I can move each white piece only once and cannot move any black pieces. This is my code: }
public class Piece extends Actor
{
    private int turn = 0;
    private boolean moved = false;
    
    public void move(Piece p, String color)
    {
        if (getTurn() == 0)
        {
            if (Greenfoot.mouseDragged(p) && color.equals("w")) 
            {
                MouseInfo mouse = Greenfoot.getMouseInfo();
                p.setLocation(mouse.getX(), mouse.getY());
                moved = true;
            }     
            if(Greenfoot.mouseDragEnded(p) && moved == true)
            {
                Actor a = getOneIntersectingObject(Piece.class);
                if (a != null)
                {
                    World world = getWorld();
                    world.removeObject(a);
                }  
                moved = false;
                setTurn(1);
            }               
        }   
        if (getTurn() == 1)
        {
            if (Greenfoot.mouseDragged(p) && color.equals("b"))
            {
                MouseInfo mouse = Greenfoot.getMouseInfo();
                p.setLocation(mouse.getX(), mouse.getY());
                moved = true;
            }
            if(Greenfoot.mouseDragEnded(p) && moved == true)
            { 
                Actor a = getOneIntersectingObject(Piece.class);
                if (a != null)
                {
                    World world = getWorld();
                    world.removeObject(a);
                }
                moved = false;
                setTurn(0);                
            }                  
        }
    } 
    
    public int getTurn()
    {
        return turn;
    }
    
    public void setTurn(int t)
    {
        turn = t;
    } 
   
}
Any ideas?
danpost danpost

2015/3/9

#
There are a couple of things you may have misconceptions about. (1) when a class (subclass) extends another class (superclass), then all non-private non-static methods and fields are inherited from the superclass to the subclass; this means if a non-private non-static method is in the superclass, you can directly call it from the subclass and the object the method is being executed for does not change. So, if a pawn calls 'move' in the Piece class, the 'move' methods will be executed on (or for) that pawn. (2) along the same lines, the 'String color' field could be declared in the Piece class and just assigned values in the individual piece classes (3) as far as the turns and whether the pieces have moved or not, that code should NOT be in the Piece class as the scope of the class is too narrow to encompass all pieces; the Piece class is for each man on the board, not for the board in general. Move the control of the game itself to your world subclass.
astronomer800 astronomer800

2015/3/10

#
Thanks. Moving the turn variable into the world subclass worked for switching turns.
You need to login to post a reply.