Hi! I'm creating a checkers game and I'm trying to move the checkers on the board using the MouseInfo, however it's not working. Here is code from my world and methods of my checkers positions.
public CheckersLogic(int rows, int columns)
{
row_ = rows;
column_ = columns;
currentPlayer_ = red_; //the starting player is red
position_ = new int[row_][column_]; //position relevent to the row and columns
position_[0][1] = red_;
position_[0][3] = red_;
position_[0][5] = red_;
position_[0][7] = red_;
position_[1][0] = red_;
position_[1][2] = red_;
position_[1][4] = red_;
position_[1][6] = red_;
position_[2][1] = red_;
position_[2][3] = red_;
position_[2][5] = red_;
position_[2][7] = red_;
position_[5][0] = white_;
position_[5][2] = white_;
position_[5][4] = white_;
position_[5][6] = white_;
position_[6][1] = white_;
position_[6][3] = white_;
position_[6][5] = white_;
position_[6][7] = white_;
position_[7][0] = white_;
position_[7][2] = white_;
position_[7][4] = white_;
position_[7][6] = white_;
}
public int getPosition(int row, int column)
{
//check if the space is within the boundaries
if(row < 0 || row > row_ || column < 0 || column > column_)
{
return invalid_; //this spot is not on the board
}
else //if the position is valid
{
return position_[row][column]; //return the position
}
}public void movePieces()
{
int r = logic_.getRows();
int c = logic_.getColumns();
//need to track the piece being moved
//setLocation the checker piece being moved
MouseInfo mouse = Greenfoot.getMouseInfo();
int initialX = 0;
int initialY = 0;
int finalX = 0;
int finalY = 0;
Red red = new Red();
White white = new White();
for(int y = 0; y < r; y++)
{
for(int x = 0; x < c; x++)
{
int position = logic_.getPosition(y, x); //will get the position on the board
if(logic_.currentPlayer_ == logic_.red_)
{
//if the position is red and the current player is the red player
if(isSelected_ && Greenfoot.mousePressed(red))
{
isSelected_ = true;
//removeObjects(getObjectsAt(y, x, Red.class));
initialX = red.getX(); //gets the intial coordinates
initialY = red.getY();
}
else if(isSelected_ == true && Greenfoot.mouseDragged(red))
{
red.setLocation(mouse.getX(), mouse.getY());
}
else if(isSelected_ == true && Greenfoot.mouseDragEnded(red))
{
isSelected_ = false; //is no longer selected
finalX = mouse.getX();
finalY = mouse.getY();
red.setLocation(finalX, finalY); //set piece where mouse is
}
}
else if(logic_.currentPlayer_ == logic_.white_)
{
//if the position is white and the current player is the white player
if(isSelected_ && Greenfoot.mousePressed(white))
{
isSelected_ = true;
initialX = white.getX(); //gets the intial coordinates
initialY = white.getY();
}
else if(isSelected_ == true && Greenfoot.mouseDragged(white))
{
white.setLocation(mouse.getX(), mouse.getY());
}
else if(isSelected_ == true && Greenfoot.mouseDragEnded(white))
{
isSelected_ = false; //is no longer selected
finalX = mouse.getX();
finalY = mouse.getY();
white.setLocation(finalX, finalY);
}
}
}
}
}
