VOOZH about

URL: https://www.geeksforgeeks.org/c/tic-tac-toe-game-in-c/

⇱ Tic Tac Toe Game in C - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Tic Tac Toe Game in C

Last Updated : 23 Jul, 2025

Tic Tac Toe is a game that was popularly played on paper or pen using the board. This is a fun game played by people of all ages. But now the digital version is becoming popular to have fun on the same game with friends. In this article, we will learn to write a program that allows a person to play tic tac toe against the computer.

👁 Tic-Tac-Toe-Game-in-C

Components of Tic Tac Toe Game

The working of tic tac toe game is same as traditional tic tac toe having following components:

  • Objective: To be the first to make a straight line with either 'X' or 'O'.
  • Game Board: The board consists of a 3x3 matrix-like structure, having 9 small boxes.
  • The computer: Since it is a two-player game each player gets one chance alternatively. i.e.; first player1 than player2.
  • Moves: The computer starts the game with O. After that player makes moves alternatively.
  • Winning: You win by making your symbol in a row or diagonal or column. Also, as a part of strategy you need to block your opponent from forming a straight line while making of your own.

Note: The program uses the algorithm discussed here to find the best possible move for the computer to make the game competitive.

Implementation of Tic-Tac-Toe Game

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 0 0
 O | | 
 --------------
 | | 
 --------------
 | | 
Enter your move (1-9): 9
 O | | 
 --------------
 | | 
 --------------
 | | X 
COMPUTER has put a O in cell 0 2
 O | | O 
 --------------
 | | 
 --------------
 | | X 
Enter your move (1-9): 2
 O | X | O 
 --------------
 | | 
 --------------
 | | X 
COMPUTER has put a O in cell 2 1
 O | X | O 
 --------------
 | | 
 --------------
 | O | X 
Enter your move (1-9): 5
 O | X | O 
 --------------
 | X | 
 --------------
 | O | X 
COMPUTER has put a O in cell 1 0
 O | X | O 
 --------------
 O | X | 
 --------------
 | O | X 
Enter your move (1-9): 6
 O | X | O 
 --------------
 O | X | X 
 --------------
 | O | X 
COMPUTER has put a O in cell 2 0
 O | X | O 
 --------------
 O | X | X 
 --------------
 O | O | X 
COMPUTER has won

Functions that are used in above code:

  • showboard(): this function is responsible for displaying the current state of the board.
  • showInstructions(): This displays the guide to playing the game.
  • initialise(): This function is used to display the dynamic moves of the game and set the moves with a corresponding empty cell.
  • declareWinner(): This declares who wins the game Computer or Human.
  • rowcrossed(), columncrossed(), diagonalcrossed(): These are used to check who wins the game ie.; which player crosses the row, column, or diagonal.
  • gameover(): this check. If any of the players win or the game draws.
  • makemove(): This updates the game board by the player move.
  • playtictactoe(): This is the main core of the game it checks everything like alternating between computer and human or checking to win or draw, etc.

Overall, this program implements a simple single-player Tic tac Toe game with input validation, user interface, winning logic and the game continues until there is a winner or draw.

If you want to implement Tic-Tac-Toe using Java, and Javascript, refer to the articles:

Comment