• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nim game help  RSS feed

 
Amra Dzombic
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all. I'm trying to make nim game. So far I made class for handling piles, and thats where problem occurred. The class looks like this:


Test class for it is:



Now the split method was supposed to take one pile, split it in two, than make an extended Pile array, copy old piles and add new pile made by splitting original pile in two. The problem is that when I run Test size of the sticks[] remains the same, so how can I make sticks[] to refer new pile array made by splitChips(). Can anyone help me with this?
 
Janardan Kelkar
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the 'splitChips' method is working on an array of Piles, i suggest you make that method static. Now, about your doubt, when you call the 'splitChips' method a copy of the 'sticks' reference is created named 'currentPiles' ( during the call to the method), which is now pointing to the same array object as 'sticks'. At The same time, another array object is created inside the method with a reference name 'extended' . Now when code @line 59(int the business class) gets executed, the reference named 'currentPiles' will point to the object pointed to by 'extended'. However, the 'sticks' reference still points to the old array object.
Hence effectively only the local array reference is getting modified and not the one in the main method.
In order to achieve what you want, i suggest you return the reference variable and assign it to 'sticks' in main, so your code would be something like this...


also make sure to return 'extended' from 'splitChips'.

do tell me how it works out.
[edit]Add new line and change comment type to /* */ so as to fit width of screen. CR[/edit]
 
Amra Dzombic
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, thank you, thank you!!!

It works like charm
 
Janardan Kelkar
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome man!!.....enjoy your code...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!