示例#1
0
        /// <summary>
        /// Cắt tỉa Alpha - Beta
        /// </summary>
        /// <param name="board">thế cờ hiện tại</param>
        /// <param name="alpha">giá trị âm vô cùng</param>
        /// <param name="beta">giá trị dương vô cùng</param>
        /// <param name="depth">độ sâu tìm kiếm</param>
        /// <param name="player">người chơi 1 là Max, -1 là Min</param>
        /// <returns></returns>
        /// 
        public Computer AlphaBetaPruning(ChessBoard board, int alpha, int beta, int depth, int player)
        {
            Computer com = new Computer();

            if (depth == 0 || board.CheckWinNegativeTeam() || board.CheckWinPositiveTeam())
            {
                com.Value = board.Value;
                com.Board = board.Clone();
                return com;
            }

            if (player == 1)
            {
                foreach (var b in GenerateBoardPositiveTeam(board))
                {

                    int temp = AlphaBetaPruning(b, alpha, beta, depth - 1, -1).Value;
                    //Debug.WriteLine("temp1 = {0}, alpha = {1}, depth = {2}", temp, alpha, depth);
                    i++;
                    if (temp > alpha)
                    {
                        alpha = temp;
                        com.Value = alpha;
                        com.Board = b.Clone();
                    }

                    if (beta <= alpha) return com;
                }

                return com;
            }
            else
            {
                foreach (var b in GenerateBoardNegativeTeam(board))
                {

                    int temp = AlphaBetaPruning(b, alpha, beta, depth - 1, 1).Value;
                    i++;
                    //Debug.WriteLine("temp2 = {0}, beta = {1}, depth = {2}", temp, beta, depth);
                    if (temp < beta)
                    {
                        beta = temp;
                        com.Board = b.Clone();
                        com.Value = beta;
                    }

                    if (beta <= alpha) return com;
                }

                return com;
            }
        }
示例#2
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            board = new ChessBoard();
            btnUndo = new Entity(Content.Load<Texture2D>("undo"), new Vector2(0, 650));
            computer = new Computer();

            base.Initialize();
        }
示例#3
0
 public ChessBoard GetNextBoard(ChessBoard board)
 {
     Computer com = new Computer();
     i = 0;
     com = AlphaBetaPruning(board, -10000, 10000, 5, 1);
     Debug.WriteLine("Count = " + i);
     return com.Board;
 }