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();
}
}
}
}
}
