// devuelve una movida que maximiza el score y el score
        public double[] Maximize(Board board, BoardLogic boardLogic, int depth)
        {
            double maxScore = -10000;

            double[] result = new double[] { 0, 0, 0, 0, EvaluateBoard(board) };
            if (Math.Abs(EvaluateBoard(board)) > 500 || depth > Constants.depthLimit)
            {
                return(result);
            }
            HashSet <PieceBase> BackupWhitePiecesAPP = board.CloneWhitePieces();
            HashSet <PieceBase> BackupBlackPiecesAPP = board.CloneBlackPieces();
            var hashSet = board.Turn ? BackupWhitePiecesAPP : BackupBlackPiecesAPP;

            foreach (PieceBase piece in hashSet)
            {
                board = boardLogic.GoTo(board.TurnNumber, board);
                int x = piece.Position.PositionX;
                int y = piece.Position.PositionY;
                foreach (Position position in piece.ValidMoves(board))
                {
                    if (boardLogic.FinallyMove(x, y, position.x1, position.y1, board, boardLogic, false))
                    {
                        double[] minimized = Minimize(board, boardLogic, depth + 1);
                        double   Eval      = minimized[4];
                        if (Eval >= maxScore)
                        {
                            maxScore  = Eval;
                            result[0] = x;
                            result[1] = y;
                            result[2] = position.x1;
                            result[3] = position.y1;
                            result[4] = maxScore;
                        }
                        // back to previous board
                        board = boardLogic.GoTo(board.TurnNumber - 1, board);
                        boardLogic.futureMoves.Clear();
                    }
                }
            }
            return(result);
        }
Esempio n. 2
0
        public bool FinallyMove(int x1, int y1, int x2, int y2, Board board, BoardLogic boardLogic, bool print = true)
        {
            PieceBase piece = board.GetPiece(x1, y1);

            if (piece.LogicMove(x1, y1, x2, y2, board, boardLogic))
            {
                TurnChange(board);
                Move move = new Move();
                move.x1 = x1;
                move.y1 = y1;
                move.x2 = x2;
                move.y2 = y2;
                pastMoves.Push(move);
                if (print)
                {
                    GameDisplay.PrintBoard(board, this, x1, y1, x2, y2);
                }
                return(true);
            }
            return(false);
        }
        /// <summary>
        /// devuelve un array con todas las movidas posibles que mejoran el puntaje de un estado del tablero
        /// </summary>
        /// <param name="board"></param>
        /// <returns></returns>
        public List <Move> AllPosiblePlays(Board board, BoardLogic boardLogic)
        {
            double      actualValue = EvaluateBoard(board);
            List <Move> AllMoves    = new List <Move>();

            HashSet <PieceBase> BackupWhitePiecesAPP = board.CloneWhitePieces();
            HashSet <PieceBase> BackupBlackPiecesAPP = board.CloneBlackPieces();
            var hashSet = board.Turn ? BackupWhitePiecesAPP : BackupBlackPiecesAPP;

            foreach (PieceBase piece in hashSet)
            {
                bool TurnShow = board.Turn;
                AllMoves = AllPosiblePiecePlays(board, boardLogic, piece, AllMoves, actualValue);
            }
            if (AllMoves != null)
            {
                return(AllMoves);
            }
            Console.WriteLine("Algo raro, falla AllPosiblePlays");
            AllMoves.Add(BestResponse(board, boardLogic));

            return(AllMoves);
        }
Esempio n. 4
0
        //public GameDisplay(board)
        //{
        //    // Inicializacion de variables
        //    player1 = true;
        //    Turn = true;
        //    TurnNumber = 0;
        //    FullMoveNumber = 0;
        //    this.stackFullPlay = new Stack<string>();
        //    FullMoveNumber = 1;
        //    Board board = new Board(player1);
        //}
        public static void PrintBoard(Board board, BoardLogic boardLogic, int x1, int y1, int x2, int y2)
        {
            Console.WriteLine();
            Console.WriteLine("----- MOVE: " + " x1 = " + x1 + ", y1 = " + y1 + ", x2 = " + x2 + ", y2 = " + y2 + " -----");
            Console.WriteLine();
            PrintElement(0, 7, board); PrintElement(1, 7, board); PrintElement(2, 7, board); PrintElement(3, 7, board); PrintElement(4, 7, board); PrintElement(5, 7, board); PrintElement(6, 7, board); PrintElement(7, 7, board); Console.WriteLine(" ");
            PrintElement(0, 6, board); PrintElement(1, 6, board); PrintElement(2, 6, board); PrintElement(3, 6, board); PrintElement(4, 6, board); PrintElement(5, 6, board); PrintElement(6, 6, board); PrintElement(7, 6, board); Console.WriteLine(" ");
            PrintElement(0, 5, board); PrintElement(1, 5, board); PrintElement(2, 5, board); PrintElement(3, 5, board); PrintElement(4, 5, board); PrintElement(5, 5, board); PrintElement(6, 5, board); PrintElement(7, 5, board); Console.WriteLine(" ");
            PrintElement(0, 4, board); PrintElement(1, 4, board); PrintElement(2, 4, board); PrintElement(3, 4, board); PrintElement(4, 4, board); PrintElement(5, 4, board); PrintElement(6, 4, board); PrintElement(7, 4, board); Console.WriteLine(" ");
            PrintElement(0, 3, board); PrintElement(1, 3, board); PrintElement(2, 3, board); PrintElement(3, 3, board); PrintElement(4, 3, board); PrintElement(5, 3, board); PrintElement(6, 3, board); PrintElement(7, 3, board); Console.WriteLine(" ");
            PrintElement(0, 2, board); PrintElement(1, 2, board); PrintElement(2, 2, board); PrintElement(3, 2, board); PrintElement(4, 2, board); PrintElement(5, 2, board); PrintElement(6, 2, board); PrintElement(7, 2, board); Console.WriteLine(" ");
            PrintElement(0, 1, board); PrintElement(1, 1, board); PrintElement(2, 1, board); PrintElement(3, 1, board); PrintElement(4, 1, board); PrintElement(5, 1, board); PrintElement(6, 1, board); PrintElement(7, 1, board); Console.WriteLine(" ");
            PrintElement(0, 0, board); PrintElement(1, 0, board); PrintElement(2, 0, board); PrintElement(3, 0, board); PrintElement(4, 0, board); PrintElement(5, 0, board); PrintElement(6, 0, board); PrintElement(7, 0, board); Console.WriteLine(" ");
            Console.WriteLine();

            if (board.IsCheckmateFlag)
            {
                Console.WriteLine("------------Game Over - CHECK MATE------------");
            }
            else if (board.IsCheckFlag && !board.IsCantMoveCheckFlag)
            {
                Console.WriteLine("------------Be Carefull - IS CHECK------------");
            }
            else if (board.IsCheckFlag && board.IsCantMoveCheckFlag)
            {
                Console.WriteLine("------------Can't move there and is Still CHECK------------");
            }
            else if (!board.IsCheckFlag && board.IsCantMoveCheckFlag)
            {
                Console.WriteLine("------------Can't move there, is CHECK------------");
            }
            string colorTurn = board.Turn ? "WhiteArmy" : "BlackArmy";

            Console.WriteLine("---------------- TURN: " + colorTurn + " ----------------");
            Console.WriteLine();
        }
        /// <summary>
        /// devuelve la movida que mas aumenta el puntaje de un estado del tablero (o si es negativo que lo hace menos negativo) Es un metodo para simular la movida del humano
        /// </summary>
        /// <param name="board"></param>
        /// <returns></returns>
        public Move BestResponse(Board board, BoardLogic boardLogic)
        {
            bool Turno = board.Turn;

            board = boardLogic.GoTo(board.TurnNumber, board);
            double actualValue     = EvaluateBoard(board);
            Move   response        = new Move();
            Move   responseNotNull = new Move();

            HashSet <PieceBase> BackupWhitePieces = board.CloneWhitePieces();
            HashSet <PieceBase> BackupBlackPieces = board.CloneBlackPieces();
            var hashSet = board.Turn ? BackupWhitePieces : BackupBlackPieces;

            foreach (PieceBase piece in hashSet)
            {
                int i = piece.Position.PositionX;
                int j = piece.Position.PositionY;
                foreach (Position position in piece.ValidMoves(board))
                {
                    var removedPiece = board.GetPiece(position.x1, position.y1);
                    if (boardLogic.FinallyMove(i, j, position.x1, position.y1, board, boardLogic, false))
                    {
                        responseNotNull.x1 = i;
                        responseNotNull.y1 = j;
                        responseNotNull.x2 = position.x1;
                        responseNotNull.y2 = position.y1;

                        // si movió el de abajo
                        if (board.player1 != board.Turn) // va != porque el finally me lo acaba de cambiar a turn
                        {                                // guardo la mejor movida
                            double Eval = EvaluateBoard(board);
                            if (Eval >= actualValue)
                            {
                                actualValue = Eval;
                                response.x1 = i;
                                response.y1 = j;
                                response.x2 = position.x1;
                                response.y2 = position.y1;
                            }
                        }
                        else
                        {   // mueve el de arriba
                            if (EvaluateBoard(board) <= actualValue)
                            {
                                actualValue = EvaluateBoard(board);
                                response.x1 = i;
                                response.y1 = j;
                                response.x2 = position.x1;
                                response.y2 = position.y1;
                            }
                        }
                        // back to previous board
                        board = boardLogic.GoTo(board.TurnNumber - 1, board);
                        boardLogic.futureMoves.Clear();
                    }
                }
            }

            if (response != null)
            {
                return(response);
            }
            Console.WriteLine("Algo raro, falla bestResponse");
            return(responseNotNull);
        }