Exemple #1
0
        /// <summary>
        /// Máy tính đi nước đi tốt nhất
        /// </summary>
        private void DoBestStep()
        {
            List <int[]> list       = board.GetEnableSteps(Board.WHITE);
            int          j          = 0;
            int          max        = -Int32.MaxValue;
            int          valueboard = 0;
            int          difficulty = Convert.ToInt32(numericUpDown1.Value);

            // Bản chất minimax là heuristic của bàn cờ phe địch sao cho bàn cờ phe địch đó là tệ nhất trong các bàn cờ tốt nhất của phe địch .
            // Căn cứ vào đó ta đánh nước cờ làm cho phe địch tổn thất nhất .
            // tính xem ứng với mỗi nước đi của quân trắng(máy) thì quân đen(người) sẽ đi nước đi nào tốt nhất ,
            // căn cứ vào giá trị minimax trả về ta chọn ra nước đi của quân trắng(máy) khiến quân đen(người) khó thắng nhất .
            for (int i = 0; i < list.Count; i++)
            {
                //Sở dĩ phải giả định các bàn cờ sau khi đi nước cờ của máy vì bàn cờ của ta lưu dưới dạng ma trận ,cần phải tìm ô tốt nhất để quân cờ di chuyển vào đó .

                Board copyboard1 = board.Copy();
                copyboard1.Move(list[i][0], list[i][1], Board.WHITE, true);

                int res = Board.GetBestStep(Board.WHITE, -Int32.MaxValue, Int32.MaxValue, difficulty, true, copyboard1);
                //int res = Board.GetBestStep(Board.BLACK, -Int32.MaxValue, Int32.MaxValue, difficulty, false, copyboard1);

                if (max < res)
                {
                    j   = i;
                    max = res;
                }
            }
            if (list.Count > j) // TH khác xảy ra là list.Count = j = 0 , khi này máy tính mất lượt .
            {
                Undo_Point.Push(new tuple(list[j][0], list[j][1]));
                board.Move(list[j][0], list[j][1], Board.WHITE, true);
                valueboard  = board.GetValue();
                board.X_Pre = list[j][0];
                board.Y_Pre = list[j][1];
            }
            Invoke(new EnableTimerDelegate(EnableTimer), false);
            Invoke(new InvokeShowGrade(ShowGrade), valueboard.ToString());

            try
            {
                Invoke(new InvokeAdd(InsertToTable), list, j);
            }
            catch
            {
                Invoke(new ShowMessageDelegate(ShowMessage), "Máy tính mất lượt", "Mất lượt"
                       , MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }