Introduction:
This C++ program is a console-based implementation of the classic game Tic-Tac-Toe.
It allows two players to take turns marking spaces on a 3x3 grid until one player wins by
getting three of their marks in a row, column, or diagonal, or the game ends in a tie if all
spaces are filled without a winner. The program validates player input, prevents players
from marking already occupied spaces, and displays the game board after each move.
It utilizes basic C++ concepts such as arrays, loops, conditional statements, and user
input/output.
Algorithm:
Step-1: Initialize the Board: Create a 3x3 grid using a 2D array initialized with empty
characters.
Step-2: Display the Board: Create a function to print the current state of the board.
Step-3: Player Move: Create a function to handle player moves, ensuring the move is
valid (the chosen cell is empty).
Step-4: Check for Win: Create a function to check all possible winning conditions
(rows, columns, and diagonals).
Step-5: Check for Draw: Create a function to check if the board is full without a winner.
Step-6: Main Game Loop:
• Alternate between players, allowing them to make moves.
• Display the board after each move.
• Check for a win or draw after each move.
• End the game if there is a win or a draw, otherwise continue.
Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
char board[3][3] = {
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '}
};
const char playerX = 'X';
const char playerO = 'O';
char currentPlayer = playerX;
int r = -1;
int c = -1;
char winner = ' ';
for (int i = 0; i < 9; i++) {
cout << " | | " << endl;
cout << " " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;
cout << "___|___|___" << endl;
cout << " | | " << endl;
cout << " " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;
cout << "___|___|___" << endl;
cout << " | | " << endl;
cout << " " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;
cout << " | | " << endl;
if (winner != ' ') {
break;
}
cout << "Current Player is " << currentPlayer << endl;
while (true) {
cout << "Enter r c from 0-2 for row and column: ";
cin >> r >> c;
if (r < 0 || r > 2 || c < 0 || c > 2) {
cout << "Invalid input, try again." << endl;
}
else if (board[r][c] != ' ') {
cout << "Tile is full, try again." << endl;
}
else {
break;
}
r = -1;
c = -1;
cin.clear();
cin.ignore(10000, '\n');
board[r][c] = currentPlayer;
currentPlayer = (currentPlayer == playerX) ? playerO : playerX;
for (int r = 0; r < 3; r++) {
if (board[r][0] != ' ' && board[r][0] == board[r][1] && board[r][1] == board[r][2]) {
winner = board[r][0];
break;
}
}
for (int c = 0; c < 3; c++) {
if (board[0][c] != ' ' && board[0][c] == board[1][c] && board[1][c] == board[2][c]) {
winner = board[0][c];
break;
}
}
if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
winner = board[0][0];
}
else if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[1][1] == board[2][0])
{
winner = board[0][2];
}
}
if (winner != ' ') {
cout << "Player" << winner << " is the winner!" << endl;
}
else {
cout << "Tie!" << endl;
}
}
Output: