Console Tic-Tac-Toe App (continued)

Tram Ho

We’ll start the code off naturally with the procedure Main and comment on the steps to take in the code.

  1. As soon as we launch the app , we will need to print an option screen for the User to choose the X or O symbol to write to the Tic-Tac-Toe table and show the steps of the User and the Computer .
  2. Scan the User ‘s input selection and save the User_Symbol state.
  3. After the user has selected the wildcard, the app needs to initialize a State that stores the state of the gameboard including the steps of the User , and the steps of the Computer , and also the remaining empty cells. This State may contain additional states such as the Match_Status to indicate that the game is over after a certain move by the User or Computer .

For a simple start, we will default that User will always take the first step. And the sequence of the next procedure to perform is:

  1. Print out the Tic-Tac-Toe so that the User can monitor the current state of the gameboard and calculate the step.
  2. Get input results to describe the step the User wants to take.
  3. Update the State of the application corresponding to the last step of the User .
  4. Check the status of the gameboard to see if the game is over. If it is over, then User wins or the result is a draw.

Similarly, with the Computer ‘s turn, we also have the same procedures as above:

  1. Print out the Tic-Tac-Toe so that the User can monitor the current state of the gameboard and calculate the step.
  2. Calls the sequence to create moves for the Computer based on the state of the current gameboard .
  3. Update the State of the application corresponding to the last step of Computer .
  4. Check the status of the gameboard to see if the game is over. If it is over, then the Computer wins or the result is a draw.

After a move of User and Computer like this, we just need to repeat the procedure from 3 .. 10 until the game is over.

Put_Symbol_Menu;

This is the expected output of Put_Symbol_Menu; and Get_User_Symbol; :

We can design the usage syntax of these initialization procedure in a similar form to Put (Symbol_Menu); and Get (User_Symbol); of the Ada.Text_IO library that we already know. However, for such a Put operation, defining a constant containing the entire contents of Symbol_Menu formatted as lines and indented spaces as above will be quite cumbersome in Ada .

So here we will perform the Put_Symbol_Menu operation with the contents of menu placed directly inside this procedure , and format it by printing it line by line. And the Get_User_Symbol operation will be designed as Get (User_Symbol); About saving the symbol type that User has selected, temporarily we will define an enum Symbol and store it in a local variable of the procedure Main .

And in case the selection entered by the user is not valid then we need to add a self-defined exception Error to raise and re-process the operation that requires the selection input.

The implementation code of Put_Symbol_Menu; then there’s nothing to note because we’re simply Put_Line line-by-line in the desired result.

Get(User_Symbol);

Get (User_Symbol) is also quite simple and the order to do is that we will Put Your choice: so that the cursor does not jump to a new line and the User ‘s input content will appear on the same line. Then perform the Get (User_Input) to display the input pointer and receive the information string.

All that remains is to convert the input string to the integer value Chosen_Number because the contents of menu are suggesting the user enter 1 or 2 to choose the X or O symbol. Then create a conditional structure to assign the appropriate result to the User_Symbol parameter, or raise SYMBOL_CHOICE_ERROR if the user enters an invalid choice.

The body of the procedure is a begin .. exception .. end block here which is still quite easy to follow because the code to be executed is not very long. As for the exception , here we can use others to include other exceptions such as converting the data type from User_Input to Chosen_Number .

Another note is that the procedure Get that we define ourselves will be considered an overload version of Ada.Text_IO.Get . With a symbol signature Get (out Symbol) can be easily distinguished by the compiler from Ada.Text_IO ‘s Get (out String) . So when we use Get (User_Input) at the beginning of the procedure and then call Get (User_Symbol) in the exception , there will be no need to refer to the name of the package to distinguish.


Finally, the code used at Main , we need to add a local variable User_Symbol to store the symbol that the user has selected during the game.
Share the news now

Source : Viblo