예제 #1
0
파일: Board.cs 프로젝트: hoangkianh/Othello
        /// <summary>
        /// Tìm nước đi tốt nhất (MiniMax)
        /// </summary>
        /// <param name="piece"></param>
        /// <param name="alpha"></param>
        /// <param name="depth">Độ sâu</param>
        /// <param name="board"></param>
        /// <param name="panel"></param>
        /// <returns></returns>
        public static int GetBestStep(int piece, int alpha, int depth, int difficulty, Board board, Panel panel)
        {
            // Giới hạn độ sâu
            if (depth > difficulty || board.WhiteCount + board.BlackCount == 64)
            {
                int k = board.GetValue();
                return k;
            }

            // List các nước đi có thể xảy ra
            List<int[]> EnableStepsList = board.GetEnableSteps(piece);

            if (EnableStepsList.Count == 0 && piece == WHITE)
                return -Int32.MaxValue;

            int bestvalue = Int32.MaxValue;

            foreach (int[] s in EnableStepsList)
            {
                if (AlphaBetaPruning(alpha, bestvalue, depth))
                {
                    return alpha;
                }

                Board boardcopy = board.Copy();
                boardcopy.Move(s[1], s[0], piece, true);

                // đệ quy tiếp, tăng độ sâu lên 1
                int cur_value = GetBestStep(-piece, alpha, depth + 1, difficulty, boardcopy, panel);

                bestvalue = MinMaxValue(cur_value, -bestvalue, depth);
            }

            return bestvalue;
        }