0

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.

Iñaki Guastalli
  • 147
  • 2
  • 13
  • 2
    welcome on SO, no idea, for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable – mKorbel Nov 19 '12 at 18:38
  • There is no way this code compiles. `gameArray[x][y]` has a different type on almost every single line. – thatidiotguy Nov 19 '12 at 18:40
  • `.setCoordinades` is spelled wrong... unless whatever object type it is has the method spelled wrong too... – durron597 Nov 19 '12 at 18:41
  • @thatidiotguy: it depends on what type `active` has. But... yeah. – durron597 Nov 19 '12 at 18:42
  • @durron597 Ah, that could explain it. Nevertheless, this is a pretty localized question. I would suggest more aggressive debugging. – thatidiotguy Nov 19 '12 at 18:44
  • i didn't exactly understand what SSCCE means.. you mean like adding the declarations for other methods used? – Iñaki Guastalli Nov 19 '12 at 18:46
  • @durron597 i wrote that method, so if it's spelled wrong, it's my fault hahah already refactored. Also, gameArray is Piece[][] while active is Piece, and whiteDied and blackDied are arrayList – Iñaki Guastalli Nov 19 '12 at 18:49
  • No it means that you should create a **SMALL** **COMPILABLE** example that illustartes the problem and can be run with a simple copy and paste. Right now Im playing find the fault with only half the code... – David Kroukamp Nov 19 '12 at 18:49
  • See also this [example](http://stackoverflow.com/a/2562685/230513) and [variation](http://stackoverflow.com/a/2563350/230513). – trashgod Nov 19 '12 at 21:16

1 Answers1

1

If setCoordinates is changing what getX() and getY() return, then the following statements are in the wrong order:

gameArray[x][y].setCoordinades(x,y);
gameArray[active.getX()][active.getY()]=null;
fgb
  • 18,439
  • 2
  • 38
  • 52