Introduction
Tic-Tac-Toe is a classic game that is widely recognized and played by people of all ages. The game is usually played by two players, one using ‘X’ and the other using ‘O’. The goal is to place three of the player’s marks in a horizontal, vertical, or diagonal row on a 3×3 grid.
This program is a simple console-based implementation of the Tic-Tac-Toe game in C++. The program will allow two players to take turns and place their marks (‘X’ or ‘O’) on the grid until one player wins or the game ends in a draw.
Objective
The objective of this program is to create a functional, interactive Tic-Tac-Toe game where two players can take turns, the board is updated after each move, and the game will declare a winner or a draw at the end. The code should be simple enough for beginners to understand the logic behind the game and the basic structure of a C++ program.
Code
#include using namespace std; char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}}; // Tic-Tac-Toe board int choice; // User's choice for position to place mark int row, column; // Row and column to place mark char turn = 'X'; // Starting player is 'X' bool draw = false; // Condition to check if the game is a draw // Function to display the board void display_board() { cout << "\n\nTic-Tac-Toe Board\n"; cout << "Player 1 [X] - Player 2 [O]\n\n"; cout << " | | \n"; cout << " " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " \n"; cout << "_____|_____|_____\n"; cout << " | | \n"; cout << " " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " \n"; cout << "_____|_____|_____\n"; cout << " | | \n"; cout << " " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " \n"; cout << " | | \n"; } // Function to take player's input and update the board void player_turn() { cout << "Player " << turn << ", enter the number (1-9) where you want to place your mark: "; cin >> choice; // Determine the row and column from the player's input row = (choice - 1) / 3; column = (choice - 1) % 3; // If the position is already taken, ask the player to choose another spot if (board[row][column] != 'X' && board[row][column] != 'O') { board[row][column] = turn; // Place the player's mark } else { cout << "Position already taken. Try again.\n"; player_turn(); // Recurse until a valid move is made } } // Function to check the winning condition bool game_won() { // Check for horizontal, vertical, and diagonal wins for (int i = 0; i < 3; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) { return true; } if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) { return true; } } if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) { return true; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) { return true; } return false; } // Function to check for a draw bool game_draw() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] != 'X' && board[i][j] != 'O') { return false; } } } return true; } // Main game loop int main() { cout << "Welcome to Tic-Tac-Toe Game!\n"; display_board(); while (true) { player_turn(); display_board(); // Check if the current player has won if (game_won()) { cout << "Player " << turn << " wins the game!\n"; break; } // Check if the game is a draw if (game_draw()) { cout << "It's a draw!\n"; break; } // Switch turns turn = (turn == 'X') ? 'O' : 'X'; } return 0; }
Program Explanation
The program begins by initializing a 3×3 board using a 2D array where each position is initially filled with numbers 1-9. This allows the player to see the available positions before making their move.
The display_board()
function is used to display the current state of the Tic-Tac-Toe board to the players. The game board is printed with each player’s mark and the remaining available positions.
The player_turn()
function allows players to input their chosen position (1-9). The program checks if the position is already taken, and if so, prompts the player to select another one. Once a valid move is made, the board is updated with the player’s mark.
The game_won()
function checks whether there is a winner by checking for three consecutive marks horizontally, vertically, or diagonally. The game_draw()
function checks if all positions on the board are filled, indicating a draw.
The game continues until one of the players wins or the game ends in a draw. After each turn, the game alternates between Player ‘X’ and Player ‘O’.
How to Run the Program
To run the Tic-Tac-Toe game:
-
- Save the code in a file with a
.cpp
extension (e.g.,tictactoe.cpp
). - Open a terminal or command prompt and navigate to the directory where the file is saved.
- Compile the program using a C++ compiler. For example, if you are using
g++
, run the following command:
- Save the code in a file with a
g++ tictactoe.cpp -o tictactoe
-
- Run the compiled program:
./tictactoe
- Follow the on-screen prompts to play the game!