Results 1 to 14 of 14

Thread: clone problem

  1. #1
    feniger is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default clone problem

    I'm building a chess game, and I encounterred a problem when I'm checking to see if the NEXT move is valid:
    explanation:
    the problematic case is when a player "checked" his opponent,
    therefore the opponents next move is obligated to be a move that protects the king,
    the way I do it is by cloning the board, making the move of the "checked" opponent, checking if his king is still "checked", and if its so, I ask the opponent to enter a different move.
    the problem surprisingly occurs when the move protects the king,
    when that happens, for some reason the "future board" (the clone I was doing the check on) is not cloned properly and there are changes made to the original board, I have failed to find the bug yet, maybe I'm not well aware of the clone() method,
    here are the code snippets needed:

    // board is the original board, futureBoard is the one I'm testing
    // the next move on.
    // inside class Board I have:
    // theBoard - 8 on 8 objects named Square
    // thePieces - a list of objects named Piece - the are the pieces on the board
    // whiteKing, blackKing - Piece type, a referance to the location of the kings

    // this functions creates the clone of board - futureBoard
    static void cloneToFutureBoard(Board board)
    {
    futureBoard = new Board();
    futureBoard.initializeBoard(); //method inside Board that creates the board
    futureBoard.theBoard = board.theBoard.clone();
    futureBoard.thePieces = new Piece[board.thePieces.length];
    futureBoard.thePieces = board.thePieces.clone();
    Col tempBlackCol = board.blackKing.getCol();
    int blackRow = board.blackKing.getRow();
    int blackIntCol = colToInt(tempBlackCol);
    futureBoard.blackKing = futureBoard.theBoard[blackRow][blackIntCol].getPiece();
    Col tempWhiteCol = board.whiteKing.getCol();
    int whiteRow = board.whiteKing.getRow();
    int whiteIntCol = colToInt(tempWhiteCol);
    futureBoard.whiteKing = futureBoard.theBoard[whiteRow][whiteIntCol].getPiece();

    }

    // the next code snippet is from the flow of the game:
    // check is a boolean thats true when the king is "checked"


    if (check)
    {
    cloneToFutureBoard(board);
    if (futureBoard.getTheBoard()[newRow][intNewCol].getPiece() != null)
    futureBoard.thePieces = removePiece(futureBoard.thePieces, newCol, newRow);
    futureBoard.theBoard[currentRow][intCurCol].getPiece().moveTo(newCol, newRow);
    futureBoard.update();
    if (isCheck(futureBoard))
    {
    System.out.println("You are Checked!");
    continue;
    }
    else
    {
    System.out.println("Check is Over");
    check = false;
    }
    }
    if (board.getTheBoard()[newRow][intNewCol].getPiece() != null)
    board.thePieces = removePiece(board.thePieces, newCol, newRow);


    the final if is supposed to come out false because I move the king to an empty spot, instead (after trying to debug) I found out the king has already moved to the new spot, so what happens is that the original spot is empty, and the new spot is full! but because a piece is moving there, it removes this piece, so actually, the king is taking himself!
    I dont understand why messing around with the clone changes the original board,
    is there anything wrong with my cloning method?
    have a good one - Day I mean...

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Cloning

    Hello feniger

    The problem could be in any of the clone() methods that you defined. For example:
    Java Code:
    futureBoard.theBoard = board.theBoard.clone();
    Remember that when you clone an object, you must create new instances of every data type in your object. I created a chess game before and faced the same problem. I used cloning successfully, but I realized later, that if you implement AI then the game becomes painfully slow. (like two minutes between moves) I can help you with your game, but for me, cloning the whole chessboard was not a suitable solution. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    feniger is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    I guess cloning is not the smartest thing I can do here, and yet, I'd like to know what am I doing wrong,
    I made a new clone method that runs through the squares and Pieces and clone basically anything thats an Object (and not an Enumaration or primitive data type)
    and still, I cant get rid of this bug...
    maybe looking at the entire code is the only solution?...
    have a good one - Day I mean...

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by feniger View Post
    ....

    and still, I cant get rid of this bug...
    maybe looking at the entire code is the only solution?...
    I always prefer seeing as much code as you can stand to paste(please use code tags, it makes for easier reading).
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  5. #5
    feniger is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    thanks for your help tim! I finally managed to find the cloning problem thanks to your guidance,
    Cap'n, thanks for the intention, I managed on my own though,
    however I do have a newbie question: how do I use code tags?
    have a good one - Day I mean...

  6. #6
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Feniger, no problem. Would you care to share how you solved your clone problem, for future readers of your thread? I'm interested.

    You'll find the code next to the quote tags, they're very similar with a different name. '[' followed by the word code closed with the other bracket ']'. On the closing code tag, be sure use the forward slash / before the word code.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  7. #7
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Quote Originally Posted by CaptainMorgan View Post
    Feniger, no problem. Would you care to share how you solved your clone problem, for future readers of your thread? I'm interested.

    You'll find the code next to the quote tags, they're very similar with a different name. '[' followed by the word code closed with the other bracket ']'. On the closing code tag, be sure use the forward slash / before the word code.
    ahh now i finally know how to use code tags. i wanted to know how but i didn't want to ask anyone lol.

    Ohh, and I would also like to see how you solved the problem.

  8. #8
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Ohh, and I would also like to see how you solved the problem.
    LOL?, I'm an avid chess player so I found your problem interesting - doesn't necessarily mean I have the time to solve it! I simply encourage folks to provide their solution, if they want to, for future reference by other folks that may come across the same issue.

    Cheers
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  9. #9
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Quote Originally Posted by CaptainMorgan View Post
    LOL?, I'm an avid chess player so I found your problem interesting - doesn't necessarily mean I have the time to solve it! I simply encourage folks to provide their solution, if they want to, for future reference by other folks that may come across the same issue.

    Cheers
    Noooo dude, i was talking to the poster of this thread lol. (sorry for the confusion, i guess i should be careful with the word "you" lol).

  10. #10
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by gibsonrocker800 View Post
    Noooo dude, i was talking to the poster of this thread lol. (sorry for the confusion, i guess i should be careful with the word "you" lol).

    Hahah! Likewise for me too, I thought you were the original poster.:D wow.. heh.

    Cheers
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  11. #11
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Quote Originally Posted by CaptainMorgan View Post
    Hahah! Likewise for me too, I thought you were the original poster.:D wow.. heh.

    Cheers
    ohhh ok haha. I can only imagine what thoughts went through your head when you thought someone (me) told you do solve their problem lmao.

  12. #12
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by gibsonrocker800 View Post
    ohhh ok haha. I can only imagine what thoughts went through your head when you thought someone (me) told you do solve their problem lmao.
    You can imagine!! lol, thanks for the good chuckle this evening. ;)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  13. #13
    feniger is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default wowww

    lots of replys while I havent been around!
    anyway, the source of my problem was my lack of knowledge in cloning,
    what I found out is that I have to clone EVERY type of Object withing the class/object I am cloning, which means if I have an object, that one of its fields (attributes) is not a primitive data type, but another object (for example: Square will have int row, int col, but also a PieceType pieceOnSquare)
    then I'll have to clone pieceOnSquare as well,
    when I found that out, I STILL had the same problem!! it didnt go away and it took me time to understand that in order to clone pieceOnSquare in a valid way,
    I have to clone all the Object types inside THAT object,
    which basically means that as long as I havent gotten down to the most primitive data types/enumarations, I have to clone manually every Object within its attributes,

    I hope my explanation was clear enough,
    if anyone has trouble understanding that, tell me, I'll try and rephrase it...

    feniger
    have a good one - Day I mean...

  14. #14
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Had the same problem

    Hello feniger

    I had the same problem:
    Quote Originally Posted by feniger
    the source of my problem was my lack of knowledge in cloning,
    what I found out is that I have to clone EVERY type of Object withing the class/object I am cloning, which means if I have an object, that one of its fields (attributes) is not a primitive data type, but another object (for example: Square will have int row, int col, but also a PieceType pieceOnSquare)
    then I'll have to clone pieceOnSquare as well,
    That's why I said:
    Quote Originally Posted by tim
    The problem could be in any of the clone() methods that you defined
    Cloning objects is useful though. Do not be afraid to implement cloning because it is strange.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. How to clone an Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:46 PM
  2. An nslookup clone in Java
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:12 PM
  3. clone method
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 09:47 AM
  4. clone method
    By gapper in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 08:46 AM
  5. How to clone a JAXB object ?
    By simon in forum Advanced Java
    Replies: 1
    Last Post: 07-14-2007, 11:56 PM

Posting Permissions

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