How to represent a chain of moves?
I'm having trouble as to how to solve this problem with Java since it hides the memory management for you. I need to be able to store a chain of moves. I want it to be possible to store different variations for each move and each move can point to a list of next moves.
Here's what I've got so far: I'm using a linked list in each stone that represent the next possible moves. SM is the stone manager and has a list of possible first moves. Then each of these moves can branch off into whatever variations are added by editing or played out if a move is undone. The remove of stones doesn't work.
The boardgame I'm trying to write an engine for is called Go or Igo.
Code :
package test;
import java.util.LinkedList;
public class Main
{
public static void main(String[] args)
{
SM sm = new SM();
sm.addStone(new Stone(0));
sm.addStone(new Stone(1));
sm.addStone(new Stone(2));
sm.addStone(new Stone(3));
sm.addStone(new Stone(4));
sm.removeStone( sm.getPlay(0), 1);
sm.print();
}
}
class SM
{
private Stone m_C = null; // the current stone
private LinkedList m_Plays = new LinkedList(); // stores the first move[s]
public void addStone(Stone stone)
{
if( isEmpty() )
{
m_Plays.addLast(stone);
m_C = stone;
}
else
{
m_C.addNext(stone);
m_C = stone;
}
}
public Stone getPlay(int index) // retrieve a first move
{
if( i >= 0 && i < m_Plays.size() )
{
return (Stone)m_Plays.get(index);
}
return null;
}
public void removeStone(Stone stone, int id)
{
if( stone == null ) return;
if( stone.getID() == id )
{
stone = null;
return;
}
for( int i=0; i<stone.nextCount(); i++)
{
removeStone(stone.getNext(i), id );
}
}
public void print()
{
Stone temp = this.getPlay(0);
do
{
System.out.println("ID: " + temp.getID());
if( temp.isLast() == false )
temp = temp.getNext(0);
}while( temp.isLast() == false );
}
public boolean isEmpty(){ return m_Plays.isEmpty(); }
}
class Stone
{
public int m_id = 0;
private LinkedList m_Next = new LinkedList();
private int m_INext = 0;
private Stone m_Previous = null;
public Stone( int id )
{
m_id = id;
}
public int getID()
{
return m_id;
}
public void addNext(Stone stone)
{
m_Next.addLast(stone);
}
public Stone getNext(int i)
{
if( i >=0 && i < m_Next.size() )
{
return (Stone)m_Next.get(i);
}
return null;
}
public void removeNext(int id)
{
for( int i=0; i<m_Next.size(); i++ )
{
if( ((Stone)m_Next.get(i)).getID() == id )
{
m_Next.remove(i);
}
}
}
public int nextCount(){ return m_Next.size(); }
public Stone getPrevious(){ return m_Previous; }
public void setPrevious(Stone s){ m_Previous = s; }
public boolean isLast(){ return m_Next.isEmpty(); }
}