예제 #1
0
        public string GetBoardState(PositionPiece color)
        {
            char[] boardState = new char[19 * 19];
            char available = '0';
            char unavailable = '1';
            char black = '2';
            char white = '3';
            GoPosition[] adjacentPositions = new GoPosition[4];

            foreach (var position in positions)
            {
                char state = available;
                if (position.piece == PositionPiece.Empty)
                {
                    state = unavailable;
                    if (lastLiberties.ContainsKey(position))
                    {
                        GetAdjacentPositions(position, adjacentPositions);
                        foreach (var adjacent in adjacentPositions)
                        {
                            if ( adjacent.piece == PositionPiece.Empty ||
                                 (adjacent.piece == color && !adjacent.group.hasManyLiberties) ||
                                 (adjacent.piece != color && adjacent.group.hasManyLiberties))
                            {
                                state = available;
                                break;
                            }
                        }
                    }
                    else if (falseEyes.Contains(position))
                    {
                        GetAdjacentPositions(position, adjacentPositions);
                        if (adjacentPositions[0].piece != color)
                        {
                            state = available;
                        }
                    }
                    else
                    {
                        state = available;
                    }
                }
                else
                {
                    state = position.piece == PositionPiece.Black ? black : white;
                }

                SetCharValue(state, position.rowIndex, position.colIndex, boardState);
            }

            return new String(boardState);
        }
예제 #2
0
 public GoBoardGroup(PositionPiece color)
 {
     this.groupColor = color;
     this.piecesInGroup = new List<GoPosition>();
     firstLiberty = null;
     hasManyLiberties = false;
 }
예제 #3
0
 private void Reset(string boardState)
 {
     for (int rowIndex = 0; rowIndex < 18; rowIndex++)
     {
         for (int colIndex = 0; colIndex < 18; colIndex++)
         {
             int state = boardState[GetIndex(rowIndex, colIndex)];
             PositionPiece piece = (state == '2' || state == '4') ? PositionPiece.Black : (state == '3' || state == '5') ? PositionPiece.White : PositionPiece.Empty;
             positions[rowIndex, colIndex] = new GoPosition(rowIndex, colIndex, piece);
         }
     }
 }
예제 #4
0
 private void GetAdjacentPositions(GoPosition position, GoPosition[] adjacentPositions)
 {
     int rowIndex = position.rowIndex;
     int colIndex = position.colIndex;
     adjacentPositions[0] = positions[rowIndex, colIndex == 0 ? 17 : colIndex - 1];
     adjacentPositions[1] = positions[rowIndex, colIndex == 17 ? 0 : colIndex + 1];
     adjacentPositions[2] = positions[rowIndex == 0 ? 17 : rowIndex - 1, colIndex];
     adjacentPositions[3] = positions[rowIndex == 17 ? 0 : rowIndex + 1, colIndex];
 }
예제 #5
0
        private void FindGroups()
        {
            GoPosition[] adjacentPositions = new GoPosition[4];

            // Create the groups
            for (int rowIndex = 0; rowIndex < 18; rowIndex++)
            {
                for (int colIndex = 0; colIndex < 18; colIndex++)
                {
                    var position = positions[rowIndex, colIndex];
                    GetAdjacentPositions(position, adjacentPositions);

                    if (position.Piece == PositionPiece.Empty)
                    {
                        if (adjacentPositions[0].Piece != PositionPiece.Empty &&
                            adjacentPositions[0].Piece == adjacentPositions[1].Piece &&
                            adjacentPositions[1].Piece == adjacentPositions[2].Piece &&
                            adjacentPositions[2].Piece == adjacentPositions[3].Piece)
                        {
                            falseEyes.Add(position);
                        }
                    }
                    else if (position.group == null)
                    {
                        AddGroup(position);
                    }
                }
            }
        }
예제 #6
0
        private void AddGroup(GoPosition position)
        {
            GoPosition[] adjacentPositions = new GoPosition[4];
            GoBoardGroup newGroup = new GoBoardGroup(position.piece);

            Stack<GoPosition> newToGroup = new Stack<GoPosition>();
            newToGroup.Push(position);

            while (newToGroup.Count > 0)
            {
                var newPosition = newToGroup.Pop();
                newPosition.group = newGroup;
                GetAdjacentPositions(position, adjacentPositions);

                foreach (GoPosition adjacent in adjacentPositions)
                {
                    if (adjacent.group == null && adjacent.piece == newGroup.groupColor)
                    {
                        newToGroup.Push(adjacent);
                    }

                    if (!newGroup.hasManyLiberties && adjacent.piece == PositionPiece.Empty)
                    {
                        if (newGroup.firstLiberty == null)
                        {
                            newGroup.firstLiberty = adjacent;
                        }
                        else if (newGroup.firstLiberty != adjacent)
                        {
                            newGroup.hasManyLiberties = true;
                        }
                    }
                }

                newGroup.piecesInGroup.Add(newPosition);
            }

            groups.Add(newGroup);
            if (!newGroup.hasManyLiberties && newGroup.firstLiberty != null)
            {
                if (!lastLiberties.ContainsKey(newGroup.firstLiberty))
                {
                    lastLiberties.Add(newGroup.firstLiberty, new List<GoBoardGroup>());
                }

                lastLiberties[newGroup.firstLiberty].Add(newGroup);
            }
            else if (newGroup.firstLiberty == null)
            {
                deadGroups.Add(newGroup);
            }
        }