File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes need to implement stored moves in linked lists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need to implement stored moves in linked lists" Watch "need to implement stored moves in linked lists" New topic

need to implement stored moves in linked lists

stu dus

Joined: Oct 12, 2008
Posts: 1
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

Joined: Oct 13, 2005
Posts: 46367
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 ]
I agree. Here's the link:
subject: need to implement stored moves in linked lists
It's not a secret anymore!