first time asking around n.n
I'm doing a chess (quite early-staged yet) for collage, and i'm having problems with moving around pieces.. I'll copy the code that seems to be giving me trouble and explain:
public void choosePiece(int x, int y){
GUI.recolour();
if(active==null){
if(gameArray[x][y].isWhite()==whiteActive){
active=gameArray[x][y];
GUI.displayMoves(showMoves(active));
}
}else{
if(gameArray[x][y]==null){
gameArray[x][y]=active;
gameArray[x][y].setCoordinades(x,y);
gameArray[active.getX()][active.getY()]=null;
active=null;
}else if(gameArray[x][y].isWhite()==whiteActive){
active=gameArray[x][y];
GUI.displayMoves(showMoves(active));
}else{
if(whiteActive)
blackDied.add(gameArray[x][y]);
else
whiteDied.add(gameArray[x][y]);
gameArray[x][y]=active;
gameArray[x][y].setCoordinades(x,y);
gameArray[active.getX()][active.getY()]=null;
active=null;
}
guiRefresh();
}
}
The user is supposed to click on a piece on the board (a JButton grid), and the GUI class will call the GameHandler class with the coordinades of said JButton.
This code is supposed to get those x;y coordinades from a JButton grid, and check if there's no active piece; if so, pick gameArray[x][y] (a piece from an array of pieces) as active piece and display available moves changing JButton backgrounds to green in the JButton grid.
If there's an active piece already, it should check if the x;y target is empty or another piece:
first case, it will place the active piece in the empty space. if there's another piece in the target x;y, it will check if said piece is from the other color or not.
If it's the same color, it will change active piece, and show it's moves. If it's the other color, it will "kill" that piece, and replace it with the active piece.
Note that all other empty buttons other than those reachable by the piece are unenabled. That last guiRefresh() method goes through the whole gameArray and asks the GUI to place in x,y location a button representing X type of piece. I used a grid layout adding buttons from a 8x8 button matrix.
Well.. that sounds like working to me.. yet it doesn't :S As is, when A eats B, B disappears from the board, but A is never placed instead of B.. it stays the same.. but if you click again on A, the green buttons showing possible movements appear as if A would actually be in B.