Ejemplo n.º 1
0
        public Moves evaluate()
        {
            moves = new MovesTree(Positions, EnemyPieces, MyPieces, null);
            InitializeTree(3, moves.Root, true, Positions);
            List <int> nodes = new List <int>();

            foreach (var Child in moves.Root.Children)
            {
                nodes.Add(moves.AlphaBeta(Child, 2, int.MinValue, int.MaxValue, true));
            }
            int min = int.MaxValue;

            foreach (var nodeValues in nodes)
            {
                if (nodeValues < min)
                {
                    min = nodeValues;
                }
            }
            MovesNode move = moves.Root.Children[nodes.IndexOf(min)];

            if (move.move.PieceTaken)
            {
                EnemyPieces.Remove(Positions[move.move.destiny_X][move.move.destiny_Y].piece);
                Controls.Remove(Positions[move.move.destiny_X][move.move.destiny_Y].piece.Piece);
                BoardPieces.Remove(Positions[move.move.destiny_X][move.move.destiny_Y].piece.Piece);
                Positions[move.move.destiny_X][move.move.destiny_Y].ocupied = true;
                Positions[move.move.destiny_X][move.move.destiny_Y].piece   = Positions[move.move.startX][move.move.startY].piece;
                Positions[move.move.startX][move.move.startY].ocupied       = false;
                Positions[move.move.startX][move.move.startY].piece         = null;
                Positions[move.move.destiny_X][move.move.destiny_Y].piece.MovePiece(move.move.destiny_X, move.move.destiny_Y);
                AnimateMovement(BoardPos[move.move.destiny_X][move.move.destiny_Y], Positions[move.move.destiny_X][move.move.destiny_Y].piece.Piece);
                return(move.move);
            }
            else
            {
                Positions[move.move.destiny_X][move.move.destiny_Y].ocupied = true;
                Positions[move.move.destiny_X][move.move.destiny_Y].piece   = Positions[move.move.startX][move.move.startY].piece;
                Positions[move.move.startX][move.move.startY].ocupied       = false;
                Positions[move.move.startX][move.move.startY].piece         = null;
                Positions[move.move.destiny_X][move.move.destiny_Y].piece.MovePiece(move.move.destiny_X, move.move.destiny_Y);
                AnimateMovement(BoardPos[move.move.destiny_X][move.move.destiny_Y], Positions[move.move.destiny_X][move.move.destiny_Y].piece.Piece);
                return(move.move);
            }
        }
Ejemplo n.º 2
0
 public int AlphaBeta(MovesNode node, int depth, int alpha, int beta, bool maximizingPlayer)
 {
     if (depth == 0)
     {
         return(node.Value);
     }
     if (maximizingPlayer)
     {
         int Value = int.MinValue;
         foreach (MovesNode child in node.Children)
         {
             int cmpVal = AlphaBeta(child, depth - 1, alpha, beta, !maximizingPlayer);
             Value = Math.Max(Value, cmpVal);
             alpha = Math.Max(alpha, Value);
             if (alpha >= beta)
             {
                 break;
             }
         }
         return(Value);
     }
     else
     {
         int Value = int.MaxValue;
         foreach (MovesNode child in node.Children)
         {
             int cmpVal = AlphaBeta(child, depth - 1, alpha, beta, !maximizingPlayer);
             Value = Math.Min(Value, cmpVal);
             beta  = Math.Min(beta, Value);
             if (beta <= alpha)
             {
                 break;
             }
         }
         return(Value);
     }
 }
Ejemplo n.º 3
0
        public Object InitializeTree(int depth, MovesNode node, bool turn, Dictionary <char, Dictionary <int, positionInfo> > pos)
        {
            if (depth == 0)
            {
                return(null);
            }
            List <ChessPiece> white_Pieces = new List <ChessPiece>();
            List <ChessPiece> black_Pieces = new List <ChessPiece>();

            foreach (var x in pos.Values)
            {
                foreach (var y in x.Values)
                {
                    if (y.ocupied)
                    {
                        if (y.piece.Color == ChessColor.WHITE)
                        {
                            white_Pieces.Add(y.piece);
                        }
                        else
                        {
                            black_Pieces.Add(y.piece);
                        }
                    }
                }
            }
            foreach (var piece in turn ? black_Pieces : white_Pieces)
            {
                foreach (var move in piece.getValidMoves(white_Pieces, black_Pieces, pos))
                {
                    Dictionary <char, Dictionary <int, positionInfo> > positions = new Dictionary <char, Dictionary <int, positionInfo> >();
                    foreach (var chr in pos.Keys)
                    {
                        positions.Add(chr, new Dictionary <int, positionInfo>());
                        foreach (var intr in pos[chr].Keys)
                        {
                            if (pos[chr][intr].ocupied)
                            {
                                ChessPiece newPiece = null;
                                switch (pos[chr][intr].piece.Type)
                                {
                                case PieceType.PAWN: newPiece = new Pawn(pos[chr][intr].piece.Position.X, pos[chr][intr].piece.Position.Y, pos[chr][intr].piece.Color, pos[chr][intr].piece.Piece); ((Pawn)newPiece).startingPos = ((Pawn)pos[chr][intr].piece).startingPos; break;

                                case PieceType.ROOK: newPiece = new Rook(pos[chr][intr].piece.Position.X, pos[chr][intr].piece.Position.Y, pos[chr][intr].piece.Color, pos[chr][intr].piece.Piece); break;

                                case PieceType.KNIGHT: newPiece = new Knight(pos[chr][intr].piece.Position.X, pos[chr][intr].piece.Position.Y, pos[chr][intr].piece.Color, pos[chr][intr].piece.Piece); break;

                                case PieceType.BISHOP: newPiece = new Bishop(pos[chr][intr].piece.Position.X, pos[chr][intr].piece.Position.Y, pos[chr][intr].piece.Color, pos[chr][intr].piece.Piece); break;

                                case PieceType.QUEEN: newPiece = new Queen(pos[chr][intr].piece.Position.X, pos[chr][intr].piece.Position.Y, pos[chr][intr].piece.Color, pos[chr][intr].piece.Piece); break;

                                case PieceType.KING: newPiece = new King(pos[chr][intr].piece.Position.X, pos[chr][intr].piece.Position.Y, pos[chr][intr].piece.Color, pos[chr][intr].piece.Piece); break;
                                }
                                positions[chr].Add(intr, new positionInfo(newPiece, true));
                            }
                            else
                            {
                                positions[chr].Add(intr, new positionInfo(null, false));
                            }
                        }
                    }
                    List <ChessPiece> whitePieces = new List <ChessPiece>();
                    List <ChessPiece> blackPieces = new List <ChessPiece>();
                    foreach (var x in positions.Values)
                    {
                        foreach (var y in x.Values)
                        {
                            if (y.ocupied)
                            {
                                if (y.piece.Color == ChessColor.WHITE)
                                {
                                    whitePieces.Add(y.piece);
                                }
                                else
                                {
                                    blackPieces.Add(y.piece);
                                }
                            }
                        }
                    }
                    Moves movement = new Moves(positions[piece.Position.X][piece.Position.Y].piece, piece.Position.X, piece.Position.Y, move.X, move.Y);

                    if (positions[piece.Position.X][piece.Position.Y].ocupied && positions[move.X][move.Y].ocupied)
                    {
                        movement.PieceTaken = true;
                        movement.TakenType  = positions[move.X][move.Y].piece.Type.ToString();
                        if (positions[move.X][move.Y].piece.Color == ChessColor.WHITE)
                        {
                            whitePieces.Remove(positions[move.X][move.Y].piece);
                        }
                        else
                        {
                            whitePieces.Remove(positions[move.X][move.Y].piece);
                        }
                    }
                    if (positions[piece.Position.X][piece.Position.Y].ocupied)
                    {
                        positions[move.X][move.Y].ocupied = true;
                        positions[move.X][move.Y].piece   = positions[piece.Position.X][piece.Position.Y].piece;
                        positions[piece.Position.X][piece.Position.Y].ocupied = false;
                        positions[piece.Position.X][piece.Position.Y].piece   = null;
                        positions[move.X][move.Y].piece.MovePiece(move.X, move.Y);
                    }
                    if (random.Next(1, 10) < 6)
                    {
                        node.Children.Add(new MovesNode(positions, whitePieces, blackPieces, movement));
                    }
                }
            }
            if (!turn)
            {
                node.Children.Sort((i, j) => i.Value.CompareTo(j.Value));
            }
            else
            {
                node.Children.Sort((i, j) => j.Value.CompareTo(i.Value));
            }
            foreach (var child in node.Children)
            {
                InitializeTree(depth - 1, child, !turn, child.Board);
            }
            return(null);
        }
Ejemplo n.º 4
0
 public MovesTree(Dictionary <char, Dictionary <int, positionInfo> > board, List <ChessPiece> white, List <ChessPiece> black, Moves move)
 {
     Root = new MovesNode(board, white, black, null);
 }