Ejemplo n.º 1
0
        public override void Play(Position m, Color playercolor)
        {
            GoPosition move = (GoPosition)m;

            if (move.Passed)
            {
                if (board[Shape.Size - 1] != Color.Empty)
                {
                    passed = true;
                }
                else
                {
                    passed = false;
                    board[Shape.Size - 1] = playercolor;
                }
            }
            else
            {
                board[Shape.Size - 1] = Color.Empty;
                this[move]            = playercolor;
                foreach (GoPosition pos in Neighbors(move))
                {
                    if (this[pos] != playercolor) // if of opposite color
                    {
                        CheckKill(pos);
                    }
                }
                CheckKill(move);
            }

            history.Add((Color[])State.Clone());
        }
Ejemplo n.º 2
0
        public Position Diagonal(Position p)
        {
            GoPosition pos = (GoPosition)p;

            if (pos.Passed)
            {
                return(Passed());
            }
            return(At(pos.Y, pos.X));
        }
Ejemplo n.º 3
0
        public Position Horizontal(Position p)
        {
            GoPosition pos = (GoPosition)p;

            if (pos.Passed)
            {
                return(Passed());
            }
            return(At(pos.X, Length - 1 - pos.Y));
        }
Ejemplo n.º 4
0
        public Position Vertical(Position p)
        {
            GoPosition pos = (GoPosition)p;

            if (pos.Passed)
            {
                return(Passed());
            }
            return(At(Length - 1 - pos.X, pos.Y));
        }
Ejemplo n.º 5
0
        public override int[] Result()
        {
            int white = 0;
            int black = 0;

            for (Position p = Shape.At(0); p.Index < Shape.Length * Shape.Length; p.Index++)
            {
                if (this[p] == Color.Black)
                {
                    black++;
                }
                else if (this[p] == Color.White)
                {
                    white++;
                }
                else
                {
                    Color[] adjacents =
                    {
                        next(p,  1, 0),
                        next(p, -1, 0),
                        next(p,  0, 1),
                        next(p,  0, -1)
                    };
                    if (adjacents.Contains(Color.Black) && !adjacents.Contains(Color.White))
                    {
                        black++;
                    }
                    else if (adjacents.Contains(Color.White) && !adjacents.Contains(Color.Black))
                    {
                        white++;
                    }
                }
            }
            return(new int[] { black, white });

            Color next(Position start, int xmove, int ymove)
            {
                GoPosition marker = (GoPosition)Shape.At(start.Index);

                while (Shape.Valid(marker.X + xmove, marker.Y + ymove))
                {
                    marker.X += xmove;
                    marker.Y += ymove;
                    if (this[marker] != Color.Empty)
                    {
                        return(this[marker]);
                    }
                }
                return(Color.Empty);
            }
        }
Ejemplo n.º 6
0
        private GoPosition Evaluate(float[] results)
        {
            GoPosition play = (GoPosition)board.Shape.At(0);
            float      max  = float.NegativeInfinity;

            for (int i = 0; i < results.Length; i++)
            {
                if (results[i] > max && board.IsLegal(board.Shape.At(i), color))
                {
                    play.Index = i;
                    max        = results[i];
                }
            }
            return(play);
        }
Ejemplo n.º 7
0
        public override Position Play()
        {
            float[] results = new float[board.Shape.Size];
            for (int i = 0; i < board.Shape.Size; i++)
            {
                Position test = board.Shape.At(i);
                if (board.IsLegal(test, color))
                {
                    board.Play(test, color);
                    results[i] = net.Compute(DigitalizeBoard())[0];
                    board.Undo();
                }
            }

            GoPosition play = Evaluate(results);

            board.Play(play, color);
            return(play);
        }
Ejemplo n.º 8
0
        private void CheckKill(GoPosition pos)

        {
            if (this[pos] == Color.Empty || pos.Index >= Shape.Length * Shape.Length)
            {
                return;
            }
            HashSet <GoPosition> visit   = new HashSet <GoPosition>();
            List <GoPosition>    visited = new List <GoPosition>();
            List <GoPosition>    tocheck = new List <GoPosition>();
            Color c = this[pos];

            tocheck.Add(pos);

            while (tocheck.Count > 0)
            {
                GoPosition check = tocheck[0];

                if (!visited.Contains(check))
                {
                    foreach (GoPosition position in Neighbors(check))
                    {
                        if (this[position] == Color.Empty) // freedom found
                        {
                            return;
                        }
                        if (this[position] == c)
                        {
                            tocheck.Add(position);
                        }
                    }
                    visited.Add(check);
                }
                tocheck.Remove(check);
            }

            // no freedom found
            foreach (GoPosition r in visited)
            {
                this[r] = Color.Empty;
            }
        }
Ejemplo n.º 9
0
        public GoPosition[] Neighbors(GoPosition c)
        {
            List <GoPosition> neighbors = new List <GoPosition>();

            if (Shape.Valid(c.X + 1, c.Y))
            {
                neighbors.Add(Shape.At(c.X + 1, c.Y));
            }
            if (Shape.Valid(c.X - 1, c.Y))
            {
                neighbors.Add(Shape.At(c.X - 1, c.Y));
            }
            if (Shape.Valid(c.X, c.Y + 1))
            {
                neighbors.Add(Shape.At(c.X, c.Y + 1));
            }
            if (Shape.Valid(c.X, c.Y - 1))
            {
                neighbors.Add(Shape.At(c.X, c.Y - 1));
            }
            return(neighbors.ToArray());
        }