Javatpoint Logo
Javatpoint Logo

Tic-Tac-Toe in C++

Tic-tac-toe is a straightforward two-player game that, if both players play their best, will always end in a tie. The game is also known as Xs and Os or zeros and crosses.

A computer or other device can be used to play the game of tic tac toe, which is typically played by drawing on paper. This timeless game provides the foundation for other ones, like Connect 4.

History of Tic-Tac-Toe

Around the first century B.C., a primitive version of the game was played in the Roman Empire. Three pebbles at a time is what the name "terni lapilli" denotes. Roman ruins have been discovered to be covered with chalk grid patterns from the game. Ruins in ancient Egypt have also yielded evidence of the game.

The game's British moniker, "noughts and crosses," saw its first print appearance in 1864. The term "tick-tack-toe" first appeared in literature in 1884, although it referred to a children's game played on a slate.

Rules of the Game

  • The game must be played by two players (in this program between HUMAN and COMPUTER).
  • Both players mark their cells with the letters "O" and "X".
  • The game ends when one of the players fills an entire row, column or diagonal with either the character ('O' or 'X') of that player.
  • If no one wins, the match is considered a draw.

Program Break Down

Here, we predefined some of our moves which we going to perform in the game. We have taken the length of the board to be 3, computer move will be denoted by 'O' and 'X' by the human or the user.

Above mentioned function i.e. void showBoard will demonstrate the current status of our board of size 3X3., and we have initialized it with 1 to 9 to indicate.

Above mentioned function i.e. void showInstruction will demonstrate the instructions like choose a cell numbered from 1 to 9 as below and play.

Above mentioned function i.e. void initialize which initialize the random number so that the same configuration doesn't arises, and initially the board is empty.

This function i.e. void declareWinner will declare the winner of the game whether computer won or human won the game.

Above function i.e. bool rowCrossed will return true or false if any of the row is crossed with the player's move.

Above function i.e. bool columnCrossed will return true or false if any of the column is crossed with the player's move.

Above function i.e. bool diagonalCrossed will return true or false if any of the diagonals are crossed with the player's move.

Above function i.e. bool gameOver will return true or false if the game is over.

Above function i.e. void playTictacToe will be the main function to play tic-tac-toe.

Program for Tic-Tac-Toe in C++

Output:

Tic-Tac-Toe

Choose a cell numbered from 1 to 9 as below and play
              1 | 2  | 3  
            - - - - - - - -
              4 | 5  | 6  
            - - - - - - - - 
              7 | 8  | 9  
-    -    -    -    -    -    -    -    -    -
COMPUTER has put a O in cell 6
                |    |    
            - - - - - - - -
                |    | O  
            - - - - - - - -
                |    |    
HUMAN has put a X in cell 7
                |    |    
            - - - - - - - -
                |    | O  
            - - - - - - - -
              X |    |    



COMPUTER has put a O in cell 5
                |    |    
            - - - - - - - -
                | O  | O  
            - - - - - - - -
              X |    |    
HUMAN has put a X in cell 1
              X |    |    
            - - - - - - - -
                | O  | O  
            - - - - - - - -
              X |    |    
COMPUTER has put a O in cell 9
              X |    |    
            - - - - - - - -
                | O  | O  
            - - - - - - - -
              X |    | O  
HUMAN has put a X in cell 8
              X |    |    
            - - - - - - - -
                | O  | O  
            - - - - - - - -
              X | X  | O  

COMPUTER has put a O in cell 4
              X |    |    
            - - - - - - - -
              O | O  | O  
            - - - - - - - -
              X | X  | O  
COMPUTER has won






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA