Console Tic-Tac-Toe App (continued)

Tram Ho

Now we will add logic to handle the last move for Computer ; And given the average length of the articles that we are applying, it is likely that we will need a few more posts to complete the procedure . This is accompanied by a summary of the most remarkable things that I have learned about Procedural Programming with the help of Ada .

Here we will talk a little bit about how thinking handles the case of multiple Double_Threat at the end of the previous article.

Suppose if we put ourselves in the position of the Computer ‘s move, we will see that on the next turn the User on the other side has a chance to create a Double_Threat if we select the box in the upper left corner or the lower right corner. Obviously blocking Block_Double_Threat directly will not resolve the situation to equalize the outcome of the game. If so we need to create a Direct_Chance direct win chance to force the User side to prioritize blocking instead of creating Double_Threat on the next move as they intended.

We will probably have multiple options on the next move to generate a direct chance of winning with any empty cells that have the ability to combine with the boxes marked with O to create a near-perfect line. However, our goal in this case is not simply to generate Direct_Chance but to get the User to select Direct_Chance to intercept and deviate from the Double_Threat generation steps. And that means when choosing a Direct_Chance value for Computer , we have to exclude the case that this value is also present in the Double_Threat set from the User side.

So we’ll call the predictive move to initialize the chance Direct_Chance_Init , and the winning move that the User needs to block is Direct_Chance . And the supporting procedure that we need are:

  • Get_Double_Threats; – Predict the value set of steps the User might be taking to create Double_Threat .
  • Get_Direct_Chance_Inits; – Predicting the set of moves will initiate a direct chance of winning for the Computer .
  • Get_Direct_Chance; – Predict the move that will complete the winning line of the Computer corresponding to the previously predicted move and at the same time this is also the step that the Computer needs to choose to block.

And the configuration file of the package AI_Mockup will have a complete list with the new sub-program being temporarily -- comment as follows:

We’ll start with a line of code that adds logic to the procedure Get (Computer_Move) and omit -- comment to write code for each of the proposed supporting procedure . New additional processing logic called Redirect_User_Concern; and has higher priority than Block_Double_Threat and lower than Block_Direct_Winning .

And here we are expecting that Redirect_User_Concern; will only really work when we see multiple Double_Threat , because if we only see a single Double_Threat we would already have the desired result on the line calling Block_Double_Threat; just before that. So we will need to create a support operation to count the number of Count_Double_Threats before deciding to run the logic to handle the situation.

Here we consider that User ‘s next move can be any empty square left on the current chessboard because we haven’t fixed the move for Computer yet. And so it would be necessary to iterate through each significant value of the Common_Set assuming that it will be chosen by the User to generate Forseen_App_State predictive instances. Then for the case where we find the number of direct threats Count_Direct_Threats to be plural, we increase the count variable by one.

Now we need to check the chess case at the beginning of the article, User has the ability to create Double_Threat in the empty cells in the upper left corner and the lower right corner.

And another situation where User goes first in the central blank with more chance of creating Double_Threat .

[Procedural Programming + Ada] Lesson 26 – Console Tic-Tac-Toe App (continued)

Share the news now

Source : Viblo