Console Tic-Tac-Toe App (continued)

Tram Ho

To add the logic of prioritizing the winning move to the Computer , we can think at the Imperative level, which is to use the branching structure to choose between the USER_WIN blocking move and the potential cell priority move. ability on the chessboard; Or you can observe Computer_Move from the Procedural level and perform overriding the USER_WIN step after selecting a move using the original priority logic.

However, no matter which method of coding we choose, we will need to separate the step selection logic into the sub- procedure that support Get (Computer_Move, App_State); .

Here, I will choose the code logic that overrides the state of Computer_Move by procedure with higher priority and the code that uses the support procedure at Get (Computer_Move, App_State); will look like this.

Ie first Get_Prioritized; will perform the logic of selecting the most potential tile on the chessboard that we had earlier. Then Block_Direct_Winning; will continue to override Computer_Move if it sees a winning move by the User on the next turn to prioritize blocking that move.

The logic of blocking the User ‘s winning move here is simply that we loop through the remaining empty squares on the chessboard and use each of these values ​​in turn to generate Foreseen User_Set predictive states; Then check the predictive states of User_Set on the next move to find the case of a winning move and use that move for Computer_Move to block it.

A small note here is the Computer_Move result parameter of Block_Direct_Winning; then we need to use more keyword in compared to Get_Prioritized; The reason is because the out keyword will not allow the code inside the procedure to read the value of Computer_Move before the code here performs an operation to assign a value to this parameter.

So in the case that we didn’t make a double move, the Computer was able to balance the result of the game. However, we still need to try running the software again to check that the User is able to make a double move to see how the Computer is choosing.

So in the move after taking the opposite corners, the Computer cannot see the winning move in the form of Double_Threat . The reason is because our winning move test is still stopping at the complete line checking logic connecting opposite sides of the chessboard.

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

Share the news now

Source : Viblo