Пример #1
0
        private bool HasLiberties(Stone stone, bool?[][] visitedParam = null)
        {
            if (!Has(stone))
            {
                return(false);
            }

            bool?[][] visited = visitedParam ?? JaggedArray.InitalizeStoneMap(Width, Height);

            if (!(visited[stone.Column][stone.Row] is null))
            {
                return(false);
            }

            List <Stone> neighbors = GetNeighbors(stone);

            if (neighbors.Any(n => Get(n) is null))
            {
                return(true);
            }

            visited[stone.Column][stone.Row] = true;

            return(neighbors.Where(n => Get(n) == stone.Color).Any(n => HasLiberties(n, visited)));
        }
Пример #2
0
        public bool IsValid()
        {
            bool?[][] liberties = JaggedArray.InitalizeStoneMap(Width, Height);

            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    Stone stone = new Stone()
                    {
                        Row = x, Column = y, Color = null
                    };
                    stone.Color = Get(stone);
                    if (stone.Color is null || liberties[x][y] is true)
                    {
                        continue;
                    }
                    if (!HasLiberties(stone))
                    {
                        return(false);
                    }

                    GetChain(stone).ForEach(v => liberties[v.Row][v.Column] = true);
                }
            }

            return(true);
        }
Пример #3
0
 public Board(List <Stone> stones, int height, int width, User blackPlayer, User whitePlayer)
 {
     StoneMap = JaggedArray.InitalizeStoneMap(height, width, stones);
     KoInfo   = new Stone();
     Height   = height;
     Width    = width;
     Capture  = new Dictionary <bool, int> {
         { true, 0 }, { false, 0 }
     };
     Player = new Dictionary <User, bool> {
         { whitePlayer, true }, { blackPlayer, false }
     };
 }
Пример #4
0
        private List <Stone> GetLiberties(Stone stone)
        {
            if (!Has(stone) || Get(stone) is null)
            {
                return(null);
            }

            List <Stone> chain     = GetChain(stone);
            List <Stone> liberties = new List <Stone>();

            bool?[][] added = JaggedArray.InitalizeStoneMap(Width, Height);

            foreach (Stone libertie in chain)
            {
                List <Stone> freeNeighbors = GetNeighbors(libertie).Where(n => Get(n) is null).ToList();

                freeNeighbors.ForEach(n => added[n.Column][n.Row] = true);
                liberties.AddRange(freeNeighbors.Where(n => !(added[n.Column][n.Row] is null)));
            }

            return(liberties);
        }