Console Tic-Tac-Toe App (continued)

Tram Ho

The final procedure in the User ‘s move is to update the state of the Update_Match_Status; . This procedure will also be reused on Computer ‘s move just like Put_Chess_Board; . So we write forked code that includes all the potential states of Match_Status defined by type Status :

  • Will be USER_WIN – if User_Set has at least one set of winning numbers.
  • Or COMPUTER_WIN – if Computer_Set has at least one winning set.
  • Or DRAW – if Common_Set has no more meaningful values ​​and the chessboard is empty.
  • Or PLAYING – if all of the above are not present.

Therefore we will have additional support function as Found_Wining (Move_Set); and Found_Empty (Move_Set); to check if any *_Set contains a winning set, and check if any *_Set is empty.

Update_Match_Status;

Found_Winning;

Testing for the presence of at least one set of winning numbers in User_Set or Computer_Set can be done in different ways:

  • List the winning sets then check for the presence of each of those sets in the Move_Set passed to the function .
  • Or get each set of numbers in Move_Set to check the total value. If at least one set of numbers sums to 15, return TRUE .

The first test can be applied to any numbering scheme on the chessboard. The second method can only be applied if the chessboard is numbered as we are currently using.

Any straight line between two opposite edges of the chessboard in this case will always pass through the cells forming a set of numbers that sums to 15 .

Found_Empty;

The function checks whether a passed Move_Set is empty or not.

Main;

And finally the code used in Main , we will try Update_User_Move; add few steps to check Update_Match_Status;


Share the news now

Source : Viblo