Results 1 to 4 of 4
  1. #1
    wil3789 is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default problem selecting a chess piece

    I am making a basic 2 player chess game, no AI. I have not found a thread on this forum that address my problem. I have my pieces in an array called PieceArray. I have a Board class where most of the logic takes place;
    Java Code:
    whitePawn = new WhitePawn(i, j, theWidth, theHeight, this, whitepawnid);
    					pieceArrayList.add(whitePawn);
    					whitepawnid++;			// set the pawn I.D.
    					allSquares[i][j].setSquareid(1);
    
    here a pawn is created, and is assigned a unique ID (1-8).  
    
    When a piece gets captured, Piece Array is resized;
    
    public void removePiece()
    	{
    		ArrayList<GamePiece> pieceArrayList = new ArrayList<GamePiece>(); //make a new list of elements	
    		GamePiece[] tempArray;			// a temporary array to store the elements of the updated pieceArray
    
    		for (int i=0; i<pieceArray.length; i++)
    		{
    			pieceArrayList.add(pieceArray[i]);
    			if (pieceArray[i].isCaptured())		//if a piece was captured
    				pieceArrayList.remove(pieceArray[i]);	//remove it from the list of elements
    		}
    		
    		tempArray = new GamePiece[pieceArrayList.size()];
    		setPieceArray(pieceArrayList.toArray(tempArray));
    		
    		pieceArray = tempArray;
    		System.out.println("The new array length is " + pieceArray.length);
    	}
    
    However, the ID for the piece changed (pawn ID 4 becomes pawn ID 5 in the new array);
    
    for (int i=0; i<pieceArray.length; i++)
    			{
    				if((pieceArray[i] instanceof WhitePawn ) && pieceArray[i].isSelected())  //a white pawn selected
    				{
    					if(pieceArray[i].isSelected()==true)
    					{
    						pieceArray[i].setWhitePiece(true);
    						pieceArray[i].move(clearPath(pieceArray[i].getSquares(allSquares, row, col)), row, col);
    						if (pieceArray[i].getMoved()==true)
    							pieceArray[i].setSquaresid(0, 1);
    					}
    					pieceArray[i].setSelected(false);
    					
    					if (pieceArray[i].isSelected()==false)
    						System.out.println("White Pawn selected id#  "+ ((WhitePawn) pieceArray[i]).getId_() + "   "+pieceArray[i]  + " unselected");
    				}
    To illustrate my point, I've attached my entire project. Try the following sequence of moves:

    White Pawn 4: 4, 4
    Black Pawn 4: 3, 3
    White Pawn 4: 3, 3 (capture Black Pawn 4)
    --- this first capture works fine, but White Pawn 4 remains selected

    Black Pawn 3: 2, 2 (Glitch occurs, Black Pawn 3 is captured by White Pawn 5)

    any advice??
    Attached Files Attached Files
    Last edited by Norm; 01-04-2012 at 04:52 PM. Reason: added code tags

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: problem selecting a chess piece

    Could you modify your post to use [code][/code] tags? Then we can more easily see what you're trying to do!

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,121
    Rep Power
    20

    Default Re: problem selecting a chess piece

    "However, the ID for the piece changed (pawn ID 4 becomes pawn ID 5 in the new array);"
    This here has got to be an incorrect assumption unless you have code that changes the id explicitly.
    There is no way simply shuffling objects around an array will change the data inside those objects...so the implication is that the wrong objects are being removed.

    Why do you have arrays like this, though? What would be wrong with simply having a List of your pieces and working with that?

  4. #4
    wil3789 is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: problem selecting a chess piece

    Upon uploading my project to the forum, I edited the contents and when I tried to open any of my .java files, all I got when the message

    "resource is out of sync with file system".

    I refreshed my project and retrieved the code, but now I am missing my .settings file for the project - and when the program runs, no pieces paint (the squares do). So now I have bigger problems to worry about.

    @quad64bit:

    here a pawn is created, and is assigned a unique ID (1-8).

    Java Code:
    whitePawn = new WhitePawn(i, j, theWidth, theHeight, this, whitepawnid);
                        pieceArrayList.add(whitePawn);
                        whitepawnid++;          // set the pawn I.D.
                        allSquares[i][j].setSquareid(1);
    When a piece gets captured, Piece Array is resized;

    Java Code:
    public void removePiece()
        {
            ArrayList<GamePiece> pieceArrayList = new ArrayList<GamePiece>(); //make a new list of elements
            GamePiece[] tempArray;          // a temporary array to store the elements of the updated pieceArray
     
            for (int i=0; i<pieceArray.length; i++)
            {
                pieceArrayList.add(pieceArray[i]);
                if (pieceArray[i].isCaptured())     //if a piece was captured
                    pieceArrayList.remove(pieceArray[i]);   //remove it from the list of elements
            }
             
            tempArray = new GamePiece[pieceArrayList.size()];
            setPieceArray(pieceArrayList.toArray(tempArray));
             
            pieceArray = tempArray;
            System.out.println("The new array length is " + pieceArray.length);
        }
    However, the ID for the piece changed (pawn ID 4 becomes pawn ID 5 in the new array)

    Java Code:
    for (int i=0; i<pieceArray.length; i++)
                {
                    if((pieceArray[i] instanceof WhitePawn ) && pieceArray[i].isSelected())  //a white pawn selected
                    {
                        if(pieceArray[i].isSelected()==true)
                        {
                            pieceArray[i].setWhitePiece(true);
                            pieceArray[i].move(clearPath(pieceArray[i].getSquares(allSquares, row, col)), row, col);
                            if (pieceArray[i].getMoved()==true)
                                pieceArray[i].setSquaresid(0, 1);
                        }
                        pieceArray[i].setSelected(false);
                         
                        if (pieceArray[i].isSelected()==false)
                            System.out.println("White Pawn selected id#  "+ ((WhitePawn) pieceArray[i]).getId_() + "   "+pieceArray[i]  + " unselected");
                    }
    @ tolls:

    Sorry to be brief, but I typed out a detailed explanation of my ideas, but navigated away from the post and lost my response. In short, try doing the series of moves I listed, then click on the starting spot for White Pawn 4. Clicking on the empty spot gives the message "White Pawn 4 unselected". Clicking on the pawn that has moved (which should be White Pawn 4) gives the message "White Pawn 5 selected". It seems like there is a collection of ID's that is 1 larger than the collection of pieces, and White Pawn 4 is inheriting White Pawn 5's id tag. I'll take your word for it that that is not what's going on, I just don't know what is. In response to your questions, I only have 1 array for all my pieces, it's just that each piece is assigned an I.D. number when it's created. If you mean "why do you have a square array, board array, and a piece array", the answer to question is that I am not a great programmer and I attacked this project somewhat halfheartedly, and now I'm to deep in it to overhaul all my code. Anyway, if you have the time, take a look at the program, otherwise thank you for your input. Again, I'm S*** outta luck if I can't get my pieces to paint.

Similar Threads

  1. Chess Piece Painting Location on Board
    By 5myl in forum New To Java
    Replies: 4
    Last Post: 01-02-2012, 12:57 AM
  2. JTable selecting single row problem
    By ContoNick in forum New To Java
    Replies: 2
    Last Post: 12-31-2011, 03:31 PM
  3. Java Chess piece Array
    By obious in forum New To Java
    Replies: 1
    Last Post: 05-04-2011, 02:20 PM
  4. Working out chess piece location / piece name
    By danborgir in forum New To Java
    Replies: 5
    Last Post: 04-20-2011, 11:14 AM
  5. problem getting a piece of data out of XML
    By Sparky in forum New To Java
    Replies: 8
    Last Post: 02-14-2011, 10:30 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •