Esempio n. 1
0
        protected void Clear()
        {
            AllBlocks = new List <GoBlockBase>();

            GoEmptyBlock lEmptyBlock = new GoEmptyBlock(this);

            UndoStack = new UndoStack(this);

            Turn                 = Color.Black;
            ZobrishHashes        = new Dictionary <ZobristHash, int>();
            LastMove             = CoordinateSystem.PASS;
            MoveList             = new List <KeyValuePair <Color, int> >();
            SimpleKoPoint        = CoordinateSystem.PASS;
            MoveNbr              = 0;
            Komi                 = 0;
            GameOver             = false;
            CapturedStoneCnt     = new int[2];
            ColorEnclosedRegions = null;
            SafetyStatusMap      = null;

            ZobristHash = new ZobristHash();
            Cells       = new GoCell[Coord.BoardArea];

            for (int i = 0; i < Coord.BoardArea; i++)
            {
                Cells[i] = new GoCell(i, this, lEmptyBlock);
            }

            for (int i = 0; i < Coord.BoardArea; i++)
            {
                Cells[i].SetNeighbors();
            }
        }
Esempio n. 2
0
        public void Undo()
        {
            int lUndoMove = LastMove;

            ColorEnclosedRegions = null;
            SafetyStatusMap      = null;

            UndoStack.Undo();

            if (ZobrishHashes.Count > 0)
            {
                int lMove;

                if (!ZobrishHashes.TryGetValue(ZobristHash, out lMove))
                {
                    return;
                }

                if (lMove == MoveNbr)
                {
                    ZobrishHashes.Remove(ZobristHash);
                }
            }

            //#if DEBUG
            //			if (!_SelfTest())
            //				Console.WriteLine("GoBoard::_SelfTest() Failed");
            //#endif
        }
Esempio n. 3
0
        protected void SolveSafety()
        {
            CreateEnclosedRegions();

            if (SafetyStatusMap == null)
            {
                SafetyStatusMap = new SafetyMap(this, SafetySolver);
            }
        }
Esempio n. 4
0
        // mark as alive:

        // TODO - 1. chains with at least two shared liberities of alive stones
        // DONE - 2. chains with at least one shared protected liberties neighboring an alive chain

        public void MarkSafe(SafetyMap safetyMap, Color color)
        {
            bool lDone = false;

            while (!lDone)
            {
                if (ProtectedLiberitiesLeft.Count == 0)
                {
                    return;
                }

                List <int> lNewProtectedLiberitiesLeft = new List <int>(ProtectedLiberitiesLeft.Count);

                foreach (int lIndex in ProtectedLiberitiesLeft)
                {
                    bool           lSafeNeighbor     = false;
                    List <GoBlock> lNonSafeNeighbors = new List <GoBlock>();

                    foreach (int lNeighbor in Board.Coord.Neighbors[lIndex])
                    {
                        if (Board.Cells[lNeighbor].Color == color)
                        {
                            if (safetyMap[lNeighbor].IsAlive)
                            {
                                lSafeNeighbor = true;
                            }
                            else
                            if (!lNonSafeNeighbors.Contains((GoBlock)Board.Cells[lNeighbor].Block))
                            {
                                lNonSafeNeighbors.Add((GoBlock)Board.Cells[lNeighbor].Block);
                            }
                        }
                    }

                    if ((lSafeNeighbor) && (lNonSafeNeighbors.Count != 0))
                    {
                        foreach (GoBlock lGoBlock in lNonSafeNeighbors)
                        {
                            safetyMap.AddAliveBlock(lGoBlock);
                        }
                    }

                    if ((lSafeNeighbor) && (lNonSafeNeighbors.Count == 0))
                    {
                        lNewProtectedLiberitiesLeft.Add(lIndex);
                    }
                }

                lDone = (ProtectedLiberitiesLeft.Count == lNewProtectedLiberitiesLeft.Count);

                ProtectedLiberitiesLeft = lNewProtectedLiberitiesLeft;
            }
        }
Esempio n. 5
0
        public void UpdateSafetyKnowledge(SafetyMap safetyMap)
        {
            foreach (GoBlock lGoBlock in Blocks.Keys)
            {
                safetyMap.AddAliveBlock(lGoBlock);

                foreach (ColorEnclosedRegion lColorEnclosedRegion in Blocks[lGoBlock])
                {
                    safetyMap.AddTerritoryBlocks(lColorEnclosedRegion.EmptyBlocks, Color);
                    safetyMap.AddDeadBlocks(lColorEnclosedRegion.InteriorAttackerBlocks);
                }
            }
        }
Esempio n. 6
0
        public GoBoardUndoState(GoBoard goBoard)
        {
            Turn = goBoard.Turn;
            SimpleKoPoint = goBoard.SimpleKoPoint;
            Komi = goBoard.Komi;

            CapturedStoneCnt0 = goBoard.CapturedStoneCnt[0];
            CapturedStoneCnt1 = goBoard.CapturedStoneCnt[1];
            LastMove = goBoard.LastMove;

            SafetyStatusMap = goBoard.SafetyStatusMap;
            GameOver = goBoard.GameOver;
            ColorEnclosedRegions = goBoard.ColorEnclosedRegions;
        }
        public GoBoardUndoState(GoBoard goBoard)
        {
            Turn          = goBoard.Turn;
            SimpleKoPoint = goBoard.SimpleKoPoint;
            Komi          = goBoard.Komi;

            CapturedStoneCnt0 = goBoard.CapturedStoneCnt[0];
            CapturedStoneCnt1 = goBoard.CapturedStoneCnt[1];
            LastMove          = goBoard.LastMove;

            SafetyStatusMap      = goBoard.SafetyStatusMap;
            GameOver             = goBoard.GameOver;
            ColorEnclosedRegions = goBoard.ColorEnclosedRegions;
        }
Esempio n. 8
0
        public int CountSafePoints(Color color, SafetySolverType pSafetySolverType)
        {
            if ((SafetyStatusMap != null) && (SafetyStatusMap.SafetySolverType == pSafetySolverType))
            {
                CreateEnclosedRegions();
                return(SafetyStatusMap.CountSafePoints(color));
            }
            else
            {
                ColorEnclosedRegions lColorEnclosedRegions = ColorEnclosedRegions;
                ColorEnclosedRegions = null;
                CreateEnclosedRegions();
                SafetyMap lSafetyStatusMap = new SafetyMap(this, pSafetySolverType);

                ColorEnclosedRegions = lColorEnclosedRegions;

                return(lSafetyStatusMap.CountSafePoints(color));
            }
        }
Esempio n. 9
0
        // mark as alive:
        // TODO - 1. chains with at least two shared liberities of alive stones
        // DONE - 2. chains with at least one shared protected liberties neighboring an alive chain
        public void MarkSafe(SafetyMap safetyMap, Color color)
        {
            bool lDone = false;

            while (!lDone)
            {
                if (ProtectedLiberitiesLeft.Count == 0)
                    return;

                List<int> lNewProtectedLiberitiesLeft = new List<int>(ProtectedLiberitiesLeft.Count);

                foreach (int lIndex in ProtectedLiberitiesLeft)
                {
                    bool lSafeNeighbor = false;
                    List<GoBlock> lNonSafeNeighbors = new List<GoBlock>();

                    foreach (int lNeighbor in Board.Coord.Neighbors[lIndex])
                        if (Board.Cells[lNeighbor].Color == color)
                        {
                            if (safetyMap[lNeighbor].IsAlive)
                                lSafeNeighbor = true;
                            else
                                if (!lNonSafeNeighbors.Contains((GoBlock)Board.Cells[lNeighbor].Block))
                                    lNonSafeNeighbors.Add((GoBlock)Board.Cells[lNeighbor].Block);
                        }

                    if ((lSafeNeighbor) && (lNonSafeNeighbors.Count != 0))
                        foreach (GoBlock lGoBlock in lNonSafeNeighbors)
                            safetyMap.AddAliveBlock(lGoBlock);

                    if ((lSafeNeighbor) && (lNonSafeNeighbors.Count == 0))
                        lNewProtectedLiberitiesLeft.Add(lIndex);
                }

                lDone = (ProtectedLiberitiesLeft.Count == lNewProtectedLiberitiesLeft.Count);

                ProtectedLiberitiesLeft = lNewProtectedLiberitiesLeft;
            }
        }
Esempio n. 10
0
        public bool PlayStone(int index, Color color, bool allowUndo)
        {
            if (!IsLegal(index, color))
                return false;

            if (allowUndo)
            {
                UndoStack.Mark();
                UndoStack.Add(new GoBoardUndoState(this));
            }
            else
                UndoStack.Clear();

            if (index == CoordinateSystem.RESIGN)
                GameOver = true;

            if ((index == CoordinateSystem.PASS) && (LastMove == CoordinateSystem.PASS) && (MoveNbr > 0))
                GameOver = true;

            ColorEnclosedRegions = null;
            SafetyStatusMap = null;

            // set turn
            Turn = color;
            LastMove = index;
            MoveList.Add(new KeyValuePair<Color, int>(color, index));

            // save hash
            if (LastMove != CoordinateSystem.PASS)
                if (!ZobrishHashes.ContainsKey(ZobristHash))
                    ZobrishHashes.Add(ZobristHash.Clone(), MoveNbr);

            // increment move number
            MoveNbr++;

            //			Console.WriteLine("# " + MoveNbr + " " + color.ToString() + " " + Coord.ToString(index));
            //			Dump();

            SimpleKoPoint = CoordinateSystem.PASS;

            if (!(index == CoordinateSystem.PASS) || (index == CoordinateSystem.RESIGN))
            {
                // update blocks
                ExecutePlay(index, color, allowUndo);
            }

            //#if DEBUG
            //			if (!_SelfTest())
            //				Console.WriteLine("GoBoard::_SelfTest() Failed");
            //#endif

            return true;
        }
Esempio n. 11
0
 public void UpdateSafetyKnowledge(SafetyMap safetyMap)
 {
     SafetySolver.UpdateSafetyKnowledge(safetyMap);
     MarkSafe(safetyMap, Color);
 }
Esempio n. 12
0
 public void UpdateSafetyKnowledge(SafetyMap safetyMap)
 {
 }
Esempio n. 13
0
 public void UpdateSafetyKnowledge(SafetyMap safetyMap)
 {
     SafetySolver.UpdateSafetyKnowledge(safetyMap);
     MarkSafe(safetyMap, Color);
 }
Esempio n. 14
0
        public void Undo()
        {
            int lUndoMove = LastMove;

            ColorEnclosedRegions = null;
            SafetyStatusMap = null;

            UndoStack.Undo();

            if (ZobrishHashes.Count > 0)
            {
                int lMove;

                if (!ZobrishHashes.TryGetValue(ZobristHash, out lMove))
                    return;

                if (lMove == MoveNbr)
                    ZobrishHashes.Remove(ZobristHash);
            }

            //#if DEBUG
            //			if (!_SelfTest())
            //				Console.WriteLine("GoBoard::_SelfTest() Failed");
            //#endif
        }
Esempio n. 15
0
        public void UpdateSafetyKnowledge(SafetyMap safetyMap)
        {
            foreach (GoBlock lGoBlock in Blocks.Keys)
            {
                safetyMap.AddAliveBlock(lGoBlock);

                foreach (ColorEnclosedRegion lColorEnclosedRegion in Blocks[lGoBlock])
                {
                    safetyMap.AddTerritoryBlocks(lColorEnclosedRegion.EmptyBlocks, Color);
                    safetyMap.AddDeadBlocks(lColorEnclosedRegion.InteriorAttackerBlocks);
                }
            }
        }
Esempio n. 16
0
 public void SetSafetySolver(SafetySolverType pSafetySolverType)
 {
     SafetySolver = pSafetySolverType;
     SafetyStatusMap = null;
     ColorEnclosedRegions = null;
 }
Esempio n. 17
0
 public void SetSafetySolver(SafetySolverType pSafetySolverType)
 {
     SafetySolver         = pSafetySolverType;
     SafetyStatusMap      = null;
     ColorEnclosedRegions = null;
 }
Esempio n. 18
0
        protected void Clear()
        {
            AllBlocks = new List<GoBlockBase>();

            GoEmptyBlock lEmptyBlock = new GoEmptyBlock(this);

            UndoStack = new UndoStack(this);

            Turn = Color.Black;
            ZobrishHashes = new Dictionary<ZobristHash, int>();
            LastMove = CoordinateSystem.PASS;
            MoveList = new List<KeyValuePair<Color, int>>();
            SimpleKoPoint = CoordinateSystem.PASS;
            MoveNbr = 0;
            Komi = 0;
            GameOver = false;
            CapturedStoneCnt = new int[2];
            ColorEnclosedRegions = null;
            SafetyStatusMap = null;

            ZobristHash = new ZobristHash();
            Cells = new GoCell[Coord.BoardArea];

            for (int i = 0; i < Coord.BoardArea; i++)
                Cells[i] = new GoCell(i, this, lEmptyBlock);

            for (int i = 0; i < Coord.BoardArea; i++)
                Cells[i].SetNeighbors();
        }
Esempio n. 19
0
 public void UpdateSafetyKnowledge(SafetyMap safetyMap)
 {
 }
Esempio n. 20
0
        public bool PlayStone(int index, Color color, bool allowUndo)
        {
            if (!IsLegal(index, color))
            {
                return(false);
            }

            if (allowUndo)
            {
                UndoStack.Mark();
                UndoStack.Add(new GoBoardUndoState(this));
            }
            else
            {
                UndoStack.Clear();
            }

            if (index == CoordinateSystem.RESIGN)
            {
                GameOver = true;
            }

            if ((index == CoordinateSystem.PASS) && (LastMove == CoordinateSystem.PASS) && (MoveNbr > 0))
            {
                GameOver = true;
            }

            ColorEnclosedRegions = null;
            SafetyStatusMap      = null;

            // set turn
            Turn     = color;
            LastMove = index;
            MoveList.Add(new KeyValuePair <Color, int>(color, index));

            // save hash
            if (LastMove != CoordinateSystem.PASS)
            {
                if (!ZobrishHashes.ContainsKey(ZobristHash))
                {
                    ZobrishHashes.Add(ZobristHash.Clone(), MoveNbr);
                }
            }

            // increment move number
            MoveNbr++;

            //			Console.WriteLine("# " + MoveNbr + " " + color.ToString() + " " + Coord.ToString(index));
            //			Dump();

            SimpleKoPoint = CoordinateSystem.PASS;

            if (!(index == CoordinateSystem.PASS) || (index == CoordinateSystem.RESIGN))
            {
                // update blocks
                ExecutePlay(index, color, allowUndo);
            }

            //#if DEBUG
            //			if (!_SelfTest())
            //				Console.WriteLine("GoBoard::_SelfTest() Failed");
            //#endif

            return(true);
        }
Esempio n. 21
0
        public int CountSafePoints(Color color, SafetySolverType pSafetySolverType)
        {
            if ((SafetyStatusMap != null) && (SafetyStatusMap.SafetySolverType == pSafetySolverType))
            {
                CreateEnclosedRegions();
                return SafetyStatusMap.CountSafePoints(color);
            }
            else
            {
                ColorEnclosedRegions lColorEnclosedRegions = ColorEnclosedRegions;
                ColorEnclosedRegions = null;
                CreateEnclosedRegions();
                SafetyMap lSafetyStatusMap = new SafetyMap(this, pSafetySolverType);

                ColorEnclosedRegions = lColorEnclosedRegions;

                return lSafetyStatusMap.CountSafePoints(color);
            }
        }
Esempio n. 22
0
        protected void SolveSafety()
        {
            CreateEnclosedRegions();

            if (SafetyStatusMap == null)
                SafetyStatusMap = new SafetyMap(this, SafetySolver);
        }