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

2015/10/18

Diagonal Movement

Programmer1998 Programmer1998

2015/10/18

#
For my checkers game, I am trying to implement my diagonal movement method to my mouse's movement. My mouse is currently able to drag checker pieces to any square that it chooses, but currently I am trying to restrict it to only moving diagonally. Later on I will work on having the pieces jump over enemy pieces, but I first must find a solution to my diagonal movement.
public boolean isValidMove(int row, int column, int dRow, int dColumn)
    {
        int redValid1 = getPosition(row + dRow, column + dColumn);
        int redValid2 = getPosition(row + dRow, column - dColumn);
        int whiteValid1 = getPosition(row - dRow, column + dColumn);
        int whiteValid2 = getPosition(row - dRow, column - dColumn);
        
        if((redValid1 == space_ || redValid1 == red_)&&(redValid2 == space_ || redValid2 == red_))
        {
            return true;
        }
        
        if((whiteValid1 == space_ || whiteValid1 == white_)&&(whiteValid2 == space_ || whiteValid2 == white_))
        {
            return true;
        }
            
        return false;   
    }
    
    public boolean canMove(int x, int y)
    {
        for(x = 0; x < row_ ; x++)
        {
            for(y = 0; y < column_; y++)
            {
                if(currentPlayer_ == red_)
                {
                    if(isValidMove(x,y, x+1, y+1) || isValidMove(x,y, x+1, y-1))
                    {
                        return true;
                    }
                }
                
                if(currentPlayer_ == white_)
                {
                    if(isValidMove(x,y, x-1, y+1) || isValidMove(x,y, x-1, y-1))
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }
public void mouseMovement()
    {
       
        MouseInfo mouse = Greenfoot.getMouseInfo();
        int initialX = 0;
        int initialY = 0;
        int finalX = 0;
        int finalY = 0;
        
        if(Greenfoot.mousePressed(null)) //if nothing is pressed
        {
            isSelected_ = false; //nothing is selected
        }
        else
        {
            isSelected_ = true;
            Actor mouseActor = null;
            if(mouse != null) //if the mouse clicked an actor
            {
                mouseActor = mouse.getActor(); //get the actor the mouse is hovering over
                if(isSelected_ == true && Greenfoot.mousePressed(this))
                {
                    initialX = mouseActor.getX(); //gets the original coordinates
                    initialY = mouseActor.getY();
                }
                if(isSelected_ == true && Greenfoot.mouseDragged(mouseActor))
                {
                    mouseActor.setLocation(mouse.getX(), mouse.getY());
                }
                if(isSelected_ == true && Greenfoot.mouseDragEnded(mouseActor))
                {
                    isSelected_ = false;
                    finalX = mouse.getX(); //the final coordinates is the mouse's x and y
                    finalY = mouse.getY();
                    //mouseActor.setLocation(finalX, finalY); //set the actor/piece 
                    
                    if(logic_.currentPlayer_ == logic_.red_)
                    {
                        mouseActor.setLocation(finalX, finalY); //set the actor/piece 
                        changeTurns();
                    }
                }
            }
        }
       
    }
danpost danpost

2015/10/18

#
Probably the best way to go about the movement is to have one Checker class with an int field to hold a value that reflects what type of piece it is (positive values for white, negative values for red; '1' for a blot and '2' for a king). The int field can then be used to determine the possible moves for the piece (the location of the piece and of the other pieces on the board must also be taken into account). The int field can also be used to determine the image the piece is to have. Then, the world would control whose turn it is and detect and control piece movement. The validity of any move can be verified in either the Checker class or the world class. Also, it would be easier if, instead of a boolean field in the Checker class, you had a Checker field in the world class to hold the "mouse actor". As a boolean field in the Checker class, each checker would have a field for its dragging state. However, only one checker can move at any time. One field is easier to control than a bunch of fields. Also, as a Checker field in your subclass of World, you can determine both (1) if a piece is currently being dragged; and (2) the specific piece being dragged.
You need to login to post a reply.