Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: How to represent a chain of moves?

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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.

    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(); }
    }
    Last edited by maikeru; December 31st, 2009 at 01:28 AM.


Similar Threads

  1. Method for legal moves in connect four.
    By mandar9589 in forum Algorithms & Recursion
    Replies: 3
    Last Post: December 27th, 2009, 05:28 PM