Console Tic-Tac-Toe App (continued)

Tram Ho

The state data that we need to manage in the model section includes the value sets representing the moves of User and Computer , and the remaining empty cells on the table. In addition, another type of state data is also needed, Match_Status , which represents the current state of the game, whether it is continuing or finished with a certain result.

App_State

To store sets of values ​​representing the steps taken by User and Computer , and the remaining blank cells on the table the most suitable is probably the Set structure as known in the JavaScript Sub-Series . Because we will be able to guarantee that at least the values ​​in each Set will not be duplicated and are unique.

Compared to C , Ada ‘s standard library is much broader and has more common data structures in the DSA topic, only missing the Stack . Obviously with a with .. use clause we will be able to use Set instead of self-defined Array to solve the situation.

However, my purpose when creating this project was to practice a little bit about my ability to define data types myself and use binding tools like subtype and contract ; So…

We have a central definition of type State with the following components:

  • Common_Set – The remaining empty cells on the table
  • User_Set – Cells that User has checked
  • Computer_Set – Cells that Computer has checked
  • Match_Status – The current state of the game

The convention used here is that *_Set data sets will store significant arithmetic values ​​of 1 .. 9 representing the cells on the Tic-Tac-Toe table. And the value 0 is used as a meaningless value and represents no cell on the table. So at the beginning of each chess game we will have a Common_Set containing all the values 1 .. 9 , and User_Set and Computer_Set will contain the values 0 .

The status type Status represents the states of the game listed as: In progress.. Result Draw.. User Wins.. and Computer wins.

Init_App_State;

When declaring a local variable App_State : State , the variable is essentially allocated a memory area for the record with uninitialized meaningful data fields. So should be similar to the Get_User_Symbol; , here manipulate Init_App_State; as intended can be defined with the syntax using procedure or function :


You can choose the method that suits your thinking style. And here, I choose the procedure to match the spirit of this Sub-Series.


In the code used in Main , we will temporarily add the test command App_State to check the operation of Init (App_State); .

Share the news now

Source : Viblo