Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-05-2008, 11:44 PM
Member
 
Join Date: Dec 2007
Posts: 17
feniger is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-06-2008, 12:14 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Cloning
Hello feniger

The problem could be in any of the clone() methods that you defined. For example:
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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-06-2008, 02:07 AM
Member
 
Join Date: Dec 2007
Posts: 17
feniger is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-06-2008, 02:15 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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).
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-06-2008, 03:44 AM
Member
 
Join Date: Dec 2007
Posts: 17
feniger is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-06-2008, 03:59 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-06-2008, 06:01 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
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.
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-06-2008, 06:10 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
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
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-06-2008, 08:07 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
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).
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 01-06-2008, 08:18 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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. wow.. heh.

Cheers
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 01-06-2008, 08:24 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
Quote:
Originally Posted by CaptainMorgan View Post
Hahah! Likewise for me too, I thought you were the original poster. 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.
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 01-06-2008, 08:30 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 01-12-2008, 09:58 PM
Member
 
Join Date: Dec 2007
Posts: 17
feniger is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 01-13-2008, 12:55 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to clone an Array Java Tip java.lang 0 04-14-2008 10:46 PM
An nslookup clone in Java Java Tip java.net 0 04-07-2008 10:12 PM
clone method javaplus New To Java 2 01-30-2008 11:47 AM
clone method gapper New To Java 1 01-20-2008 10:46 AM
How to clone a JAXB object ? simon Advanced Java 1 07-15-2007 01:56 AM


All times are GMT +3. The time now is 01:26 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org