Tic Tac Toe Game Code In C Language
Tic Tac Toe is one of the most popular games around the world. It is a simple two-player game that can be played using a paper and pen. But if you want to take it to the next level, you can build your own Tic Tac Toe game using C language. In this article, we will show you how to build your own Tic Tac Toe game using C language.
What is Tic Tac Toe Game?
Tic Tac Toe, also known as Noughts and Crosses, is a game played on a 3x3 grid. The game can be played by two players. The players take turns to place their mark, either X or O, on the grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game. If all the squares on the grid are filled and no player has won, then the game is a tie.
How to Build Tic Tac Toe Game in C Language?
Before you start building the game, you need to have a basic understanding of C programming language. You should know how to declare variables, use loops, and write functions in C language. Once you have a basic understanding of C language, you can follow the steps below to build your own Tic Tac Toe game in C language.
Step 1: Create a 3x3 Grid
The first step in building the Tic Tac Toe game is to create a 3x3 grid where the game will be played. You can create the grid using a 2D array in C language. Here is the code to create a 3x3 grid:
char grid[3][3] = {{'1', '2', '3'},{'4', '5', '6'},{'7', '8', '9'}};
The above code creates a 3x3 grid with numbers from 1 to 9. These numbers will be used to represent the squares on the grid. The players will enter the number of the square where they want to place their mark.
Step 2: Display the Grid
The next step is to display the grid on the screen. You can use a loop to iterate through the 2D array and print the values of each square. Here is the code to display the grid:
void display_grid() {int row, col;printf("\n");for(row = 0; row < 3; row++) {for(col = 0; col < 3; col++) {printf(" %c ", grid[row][col]);if(col != 2) {printf("|");}}printf("\n");if(row != 2) {printf("---|---|---\n");}}printf("\n");}
The above code uses a nested loop to iterate through the 2D array and print the values of each square. It also adds vertical and horizontal lines to make the grid look like a Tic Tac Toe game.
Step 3: Get Player Input
The next step is to get the input from the players. Each player will enter the number of the square where they want to place their mark. You can use the scanf() function in C language to get the input from the players. Here is the code to get the player input:
void get_player_input(char mark) {int row, col, square;printf("Player %c, enter a square number: ", mark);scanf("%d", &square);if(square < 1 || square > 9) {printf("Invalid input. Please enter a square number between 1 and 9.\n");get_player_input(mark);}else {row = (square - 1) / 3;col = (square - 1) % 3;if(grid[row][col] == 'X' || grid[row][col] == 'O') {printf("Square already taken. Please choose another square.\n");get_player_input(mark);}else {grid[row][col] = mark;}}}
The above code uses the mark (X or O) to identify which player is entering the input. It then uses the scanf() function to get the square number from the player. If the input is invalid or the square is already taken, it prompts the player to enter another square number.
Step 4: Check for a Winner
The next step is to check if any player has won the game. If a player has won the game, the game should end and the winner should be declared. You can use a series of if-else statements to check for a winner. Here is the code to check for a winner:
char check_winner() {int row, col;// Check for horizontal winfor(row = 0; row < 3; row++) {if(grid[row][0] == grid[row][1] && grid[row][1] == grid[row][2]) {return grid[row][0];}}// Check for vertical winfor(col = 0; col < 3; col++) {if(grid[0][col] == grid[1][col] && grid[1][col] == grid[2][col]) {return grid[0][col];}}// Check for diagonal winif(grid[0][0] == grid[1][1] && grid[1][1] == grid[2][2]) {return grid[0][0];}if(grid[0][2] == grid[1][1] && grid[1][1] == grid[2][0]) {return grid[0][2];}// Check for tiefor(row = 0; row < 3; row++) {for(col = 0; col < 3; col++) {if(grid[row][col] != 'X' && grid[row][col] != 'O') {return ' ';}}}return 'T';}
The above code checks for a winner by looking at the rows, columns, and diagonals of the grid. If there is a winner, it returns the mark (X or O) of the winner. If there is no winner and the grid is full, it returns 'T' for tie. If there is no winner and the grid is not full, it returns a space.
Step 5: Start the Game
The final step is to start the game. You can use a while loop to keep the game running until a winner is declared or the game ends in a tie. Here is the code to start the game:
void start_game() {char mark = 'X';char winner;while(1) {display_grid();get_player_input(mark);winner = check_winner();if(winner == 'X' || winner == 'O') {display_grid();printf("Player %c wins!\n", winner);break;}else if(winner == 'T') {display_grid();printf("Game over. It's a tie!\n");break;}if(mark == 'X') {mark = 'O';}else {mark = 'X';}}}
The above code uses the mark (X or O) to determine which player's turn it is. It then displays the grid, gets the player input, checks for a winner, and updates the mark for the next player. If there is a winner, it displays the grid and declares the winner. If the game ends in a tie, it displays the grid and declares the tie.
Conclusion
Building your own Tic Tac Toe game using C language is a fun and educational project. With the steps outlined in this article, you can build your own Tic Tac Toe game and customize it to your liking. Whether you are a beginner or an experienced programmer, this project will help you improve your programming skills and have fun at the same time.