Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need to implement stored moves in linked lists

 
stu dus
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a fully functional tictactoe game and i even have the 2 back and forward implemented on the board, but i have no idea how to store the moves in a linked list and to go back and forward through these moves.

can someone please explain what exactly needs to be stored for a move, and how to call a previous move? then, if i click back and then click on the board for a new move, the "future" moves should be destroyed and the current move should be stored instead of those "future" moves
 
Campbell Ritchie
Sheriff
Posts: 48454
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Do they actually mean a linked list? Would an array list work as well? Do you have a Move class?
The commonest way to implement a linked list is to have a self-referential class

Where O2 means a O in square 2, etc.

Now your linked list node class would have a node reference "next" which starts off life null. You can also have a reference to "previous" which makes it a doubly-linked list, which I think is how the java.util.LinkedList class is implemented.
You need a method like findNext(int i) if i == 1 you return that node, otherwise findNext(i - 1) so it is recursive.
You can also have a method like addLast(Node n) which goes along until it finds a node where next == null, then sets that Node as next.
You can have a truncate(int i) method; if i == 1 you set "next" to null, otherwise try truncate(i - 1). Again recursive.

If you invoke truncate(6) you can lost everything after the 6th move, then addLast will allow you to put the next move back.

You need to check the i bits and the i - 1 bits carefully. Depending exactly how you implement it, truncate(6) might keep the 6th member and lose the 7th member, or it might keep the 5th member and lose the 6th member.

I hope that gives some sort of hint how to implement a linked list. There is a java.util.LinkedList class already, and a java.util.ArrayList class too. I suspect ArrayList will give you better performance, and it has a method allowing you to delete a range of elements, which LinkedList hasn't.
Anyway, good luck with it
[ October 12, 2008: Message edited by: Campbell Ritchie ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic