Example #1
0
        // Returns true if placing a stone with color color on point coord is legal
        private bool LegalMove(Coord coord, State color, State opponent)
        {
            HashSet <Coord> neighbours = GetNeighbours(coord);

            if (_gridStones[coord.Column, coord.Row] != State.None) // there is a stone placed
            {
                return(false);
            }

            if (GetColoredNeighbour(neighbours, State.None).Count != 0)
            {
                return(true);
            }

            foreach (Coord n in GetColoredNeighbour(neighbours, color))
            {
                AbGroup g = _groups[_gridGroups[n.Column, n.Row]];
                // If the stone connects to a stone with 2 or more liberties It will not be captured.
                if (g.GetLiberties().Count > 1)
                {
                    return(true);
                }
            }
            foreach (Coord n in GetColoredNeighbour(neighbours, opponent))
            {
                AbGroup g = _groups[_gridGroups[n.Column, n.Row]];
                // If any opponent group has one liberty that group is captured and the move is Legal, as it now has a liberty.
                if (g.GetLiberties().Count == 1)
                {
                    return(true);
                }
            }
            return(false);
        }
Example #2
0
        public override void MergeGroup(AbGroup slaveGroup)
        {
            _stones.UnionWith(slaveGroup.GetStones());
            _liberties.UnionWith(slaveGroup.GetLiberties());
            _adjacent.UnionWith(slaveGroup.GetAdjacent());

            foreach (Coord coord in _stones)
            {
                _liberties.RemoveWhere(c => c.Row == coord.Row && c.Column == coord.Column);
            }
        }
Example #3
0
        /**
         * This method Updates the state of the board with the addition of the new stone
         */
        private void UpdateBoard(Coord coord, State color, State opponent)
        {
            _gridStones[coord.Column, coord.Row] = color; //Place the stone
            HashSet <Coord> neigh = GetNeighbours(coord); // Get the neighboouring coords.

            var g = new Group(GetNextGroupId(), coord, color, GetColoredNeighbour(neigh, State.None),
                              GetColoredNeighbour(neigh, opponent)); //Create the group

            _groups.Add(g.GetId(), g);                               // Add the group to the board
            _gridGroups[coord.Column, coord.Row] = g.GetId();        //Add group to grid

            foreach (Coord pos in neigh)                             //Merge adjacent groups of same color into this group.
            {
                int groupId = _gridGroups[pos.Column, pos.Row];
                if (!_groups.ContainsKey(groupId) || groupId == g.GetId()) // If there is no group of the ID do nothing
                {
                    continue;
                }
                AbGroup ng = _groups[_gridGroups[pos.Column, pos.Row]];
                if (ng?.GetColor() == color) // If the group is of the same collor merge the groups
                {
                    MergeGroupGrid(g, ng);
                    MergeGroup(g, ng);
                    g.AddStone(coord);
                }
                else if (ng?.GetColor() == opponent) // If the gorup is of the opponent
                {
                    ng.AddStone(coord);
                    if (ng.GetLiberties().Count == 0) // If we removed the last liberty capture the group
                    {
                        Capture(ng, color);
                    }
                }
            }

            // These methods prints the board and some debugg information in the output.
            WriteGroupBoard();
            WriteGroupLiberties();
        }