[Procedural Programming + Ada] Bài 26 – Console Tic-Tac-Toe App (tiếp theo)

Chúng ta đang muốn điều hướng User_Move ở bước đi tiếp theo tránh khỏi các bước tiên đoán được là sẽ tạo ra Double_Threat. Vì vậy nên sau khi kiểm tra thấy có nhiều Double_Threat từ User thì lúc này chúng ta bắt đầu thực hiện logic tiên đoán các bước tạo Double_Threat để tránh

Chúng ta đang muốn điều hướng User_Move ở bước đi tiếp theo tránh khỏi các bước tiên đoán được là sẽ tạo ra Double_Threat. Vì vậy nên sau khi kiểm tra thấy có nhiều Double_Threat từ User thì lúc này chúng ta bắt đầu thực hiện logic tiên đoán các bước tạo Double_Threat để tránh điều hướng nhầm.

Get_Double_Threats;

Và ở đây chúng ta nhắc lại Double_Threat là một bước đi tiên đoán sẽ tạo ra nhiều Direct_Threat. Như vậy trong Get_Double_Threats; chúng ta sẽ cần giả định từng bước đi tiếp theo của User để tạo ra các phiên bản trạng thái Foreseen_App_State rồi kiểm tra số lượng nguy cơ trực tiếp Count_Direct_Threats; được tạo ra.

with Ada.Text_IO; use Ada.Text_IO;

package body AI_Mockup is

   -- ...
   
   procedure Redirect_User_Concern
      ( Computer_Move : in out Digit
      ; App_State : in State )
   is -- local
      Foreseen_User_Moves : Digit_Array;
   begin
      if Count_Double_Threats (App_State) > 1 then
         Put_Line ("Double_Threats : " & Integer'Image (Count_Double_Threats (App_State)));
         --
         Foreseen_User_Moves := (others => 0);
         Get_Double_Threats (Foreseen_User_Moves, App_State);
         Put_Line ("Foreseen_User_Moves : " & Digit_Array'Image (Foreseen_User_Moves));
      end if;
   end Redirect_User_Concern;

   -- ...
   
   procedure Get_Double_Threats
      ( User_Moves : in out Digit_Array
      ; App_State : in State )
   is -- local
      Foreseen_Move : Digit := 0;
      Foreseen_App_State : State;
   begin
      for Index in App_State.Common_Set'Range loop
         if App_State.Common_Set (Index) /= 0 then
            Foreseen_Move := App_State.Common_Set (Index);
            Copy (Foreseen_App_State, App_State);
            Update_User_Set (Foreseen_App_State, Foreseen_Move);
            --
            if Count_Direct_Threats (Foreseen_App_State) > 1 then
               User_Moves (Index) := Foreseen_Move;
            end if;
         end if;
      end loop;
   end Get_Double_Threats;

end AI_Mockup;

Lần này chúng ta sẽ kiểm tra trường hợp User lấy trước ô trung tâm và góc đối diện với bước đi đầu tiên của Computer mà chúng ta đã chạy thử trước đó để xem tập kết quả tiên đoán các bước đi tiếp theo mà User đang hướng đến để tạo Double_Threat.

alr run

Choose your Symbol ...
   1. Letter 'X'
   2. Letter 'O'
Your choice: 1

You've chosen: X
+ - - + - - + - - +
|  2  |  9  |  4  |
+ - - + - - + - - +
|  7  |  5  |  3  |
+ - - + - - + - - +
|  6  |  1  |  8  |
+ - - + - - + - - +
Your move: 5

PLAYING ...
+ - - + - - + - - +
|  2  |  9  |  4  |
+ - - + - - + - - +
|  7  |  X  |  3  |
+ - - + - - + - - +
|  6  |  1  |  8  |
+ - - + - - + - - +
Computer move: 2

PLAYING ...
+ - - + - - + - - +
|  O  |  9  |  4  |
+ - - + - - + - - +
|  7  |  X  |  3  |
+ - - + - - + - - +
|  6  |  1  |  8  |
+ - - + - - + - - +
Your move: 8

PLAYING ...
+ - - + - - + - - +        + - - + - - + - - +
|  O  |  9  |  4  |   =>   |  O  |     |  ?  |
+ - - + - - + - - +        + - - + - - + - - +
|  7  |  X  |  3  |   =>   |     |  X  |  ?  |
+ - - + - - + - - +        + - - + - - + - - +
|  6  |  1  |  X  |   =>   |  ?  |  ?  |  X  |
+ - - + - - + - - +        + - - + - - + - - +
Double_Threats :  4
Foreseen_User_Moves :
[ 1,  0,  3,  4,  0,  6,  0,  0,  0]
Computer move: _

Bây giờ thì chúng ta cần tính toán các bước đi mà Computer có thể thực hiện ngay để khởi tạo Direct_Chance và khiến cho User phải tạm dừng kế hoạch tạo Double_Threat.

Get_Direct_Chance_Inits;

Lúc này logic xử lý sau khi lặp qua các ô trống còn lại trong Common_Set để tạo ra các trạng thái tiên đoán Forseen_App_State thì chúng ta cần đếm số lượng cơ hội thắng trực tiếp Count_Direct_Chances; của Computer thay vì nguy cơ trực tiếp Count_Direct_Threats; từ User.

with Ada.Text_IO; use Ada.Text_IO;

package body AI_Mockup is

   -- ...
   
   procedure Redirect_User_Concern
      ( Computer_Move : in out Digit
      ; App_State : in State )
   is -- local
      Foreseen_User_Moves : Digit_Array;
      Foreseen_Computer_Moves : Digit_Array;
   begin
      if Count_Double_Threats (App_State) > 1 then
         Put_Line ("Double_Threats : " & Integer'Image (Count_Double_Threats (App_State)));
         --
         Foreseen_User_Moves := (others => 0);
         Get_Double_Threats (Foreseen_User_Moves, App_State);
         Put_Line ("Foreseen_User_Moves : " & Digit_Array'Image (Foreseen_User_Moves));
         --
         Foreseen_Computer_Moves := (others => 0);
         Get_Direct_Chance_Inits (Foreseen_Computer_Moves, App_State);
         Put_Line ("Foreseen_Computer_Moves : " & Digit_Array'Image (Foreseen_Computer_Moves));
      end if;
   end Redirect_User_Concern;

   -- ...
   
   procedure Get_Direct_Chance_Inits
      ( Computer_Moves : in out Digit_Array
      ; App_State : in State )
   is -- local
      Foreseen_Move : Digit := 0;
      Foreseen_App_State : State;
   begin
      for Index in App_State.Common_Set'Range loop
         if App_State.Common_Set (Index) /= 0 then
            Foreseen_Move := App_State.Common_Set (Index);
            Copy (Foreseen_App_State, App_State);
            Update_Computer_Set (Foreseen_App_State, Foreseen_Move);
            --
            if Count_Direct_Chances (Foreseen_App_State) > 0 then
               Computer_Moves (Index) := Foreseen_Move;
            end if;
         end if;
      end loop;
   end Get_Direct_Chance_Inits;
   
   -- ...
   
   -- function Count_Double_Threats ...
   
   function Count_Direct_Chances (App_State : State)
      return Integer
   is -- local
      Foreseen_Computer_Move : Digit := 0;
      Foreseen_App_State : State;
      Counter : Integer := 0;
   begin
      for Index in App_State.Common_Set'Range loop
         if App_State.Common_Set (Index) /= 0 then
            Foreseen_Computer_Move := App_State.Common_Set (Index);
            Copy (Foreseen_App_State, App_State);
            Update_Computer_Set (Foreseen_App_State, Foreseen_Computer_Move);
            --
            if Found_Winning (Foreseen_App_State.Computer_Set) then
               Counter := Counter + 1;
            end if;
         end if;
      end loop;
      --
      return Counter;
   end Count_Direct_Chances;
   
end AI_Mockup;

Lần này chúng ta sẽ chạy thử với trường hợp User lấy trước các góc đối diện và để Computer lấy ô trung tâm để xem các khả năng còn lại mà Computer có thể tạo Direct_Chance.

alr run

Choose your Symbol ...
   1. Letter 'X'
   2. Letter 'O'
Your choice: 1
You've chosen: X

+ - - + - - + - - +
|  2  |  9  |  4  |
+ - - + - - + - - +
|  7  |  5  |  3  |
+ - - + - - + - - +
|  6  |  1  |  8  |
+ - - + - - + - - +
Your move: 6

PLAYING ...
+ - - + - - + - - +
|  2  |  9  |  4  |
+ - - + - - + - - +
|  7  |  5  |  3  |
+ - - + - - + - - +
|  X  |  1  |  8  |
+ - - + - - + - - +
Computer move: 5

PLAYING ...
+ - - + - - + - - +
|  2  |  9  |  4  |
+ - - + - - + - - +
|  7  |  O  |  3  |
+ - - + - - + - - +
|  X  |  1  |  8  |
+ - - + - - + - - +
Your move: 4

PLAYING ...
+ - - + - - + - - +        + - - + - - + - - +
|  2  |  9  |  X  |   =>   |     |     |  X  |
+ - - + - - + - - +        + - - + - - + - - +
|  7  |  O  |  3  |   =>   |     |  O  |     |
+ - - + - - + - - +        + - - + - - + - - +
|  X  |  1  |  8  |   =>   |  X  |     |     |
+ - - + - - + - - +        + - - + - - + - - +
Double_Threats :  2
Foreseen_User_Moves :
[ 0,  2,  0,  0,  0,  0,  0,  8,  0]
Foreseen_Computer_Moves :
[ 1,  2,  3,  0,  0,  0,  7,  8,  9]
Computer move: _

Oh, chúng ta đang có tất cả các ô trống còn lại trên bàn cờ đều là các bước đi mà Computer có thể chọn để tạo Direct_Chance. Bây giờ việc còn lại là chúng ta cần phải lấy ra từng giá trị này để tiếp tục tiên đoán giá trị Direct_Chance tương ứng, sau đó kiểm tra để chọn ra một trường hợp mà Direct_ChanceUser cần phải chọn vào để chặn lại sẽ không trùng hợp với mục đích tạo Double_Chance.

[Procedural Programming + Ada] Bài 27 – Console Tic-Tac-Toe App (kết thúc)

Nguồn: viblo.asia

Bài viết liên quan

Thay đổi Package Name của Android Studio dể dàng với plugin APR

Nếu bạn đang gặp khó khăn hoặc bế tắc trong việc thay đổi package name trong And

Lỗi không Update Meta_Value Khi thay thế hình ảnh cũ bằng hình ảnh mới trong WordPress

Mã dưới đây hoạt động tốt có 1 lỗi không update được postmeta ” meta_key=

Bài 1 – React Native DevOps các khái niệm và các cài đặt căn bản

Hướng dẫn setup jenkins agent để bắt đầu build mobile bằng jenkins cho devloper an t

Chuyển đổi từ monolith sang microservices qua ví dụ

1. Why microservices? Microservices là kiến trúc hệ thống phần mềm hướng dịch vụ,