The University of Lahore, CS&IT Department
CS02115: Programming Fundamentals
FINAL PROJECT
Student name 1; TALHA NADEEM SAP ID; 70146903
Student name 2; AJAB GULL
Student name 3; AHMED SHEHRYAR
Student name 4; AUN ALI
Section: 2G Submitted to: mam Fareeha Ashfaq
Program: BSCS Date: 04/01/2025
Question:
“This code is for a two-player Tic Tac Toe game. Use symbols X and O to
play the game.”
Solution;
#include<iostream>
using namespace std;
char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
char turn = 'X';
int row, column;
bool draw = false;
string player1, player2;
void display_board() {
cout << "\n";
cout << "\t" << player1 << " [X]" << endl;
cout << "\t" << player2 << " [O]" << endl;
cout << "\n";
cout << "\t\t\t | | " << endl;
cout << "\t\t\t " << board[0][0] << " | " << board[0][1] << " | "
<< board[0][2] << endl;
cout << "\t\t\t_____|_____|_____" << endl;
cout << "\t\t\t | | " << endl;
cout << "\t\t\t " << board[1][0] << " | " << board[1][1] << " | "
<< board[1][2] << endl;
cout << "\t\t\t_____|_____|_____" << endl;
cout << "\t\t\t | | " << endl;
cout << "\t\t\t " << board[2][0] << " | " << board[2][1] << " | "
<< board[2][2] << endl;
cout << "\t\t\t | | " << endl;
cout << "\n";
}
void player_turn() {
int choice;
string currentPlayer = (turn == 'X') ? player1 : player2;
cout << "\t" << currentPlayer << " [" << turn << "] turn: ";
cin >> choice;
switch(choice) {
case 1: row = 0; column = 0; break;
case 2: row = 0; column = 1; break;
case 3: row = 0; column = 2; break;
case 4: row = 1; column = 0; break;
case 5: row = 1; column = 1; break;
case 6: row = 1; column = 2; break;
case 7: row = 2; column = 0; break;
case 8: row = 2; column = 1; break;
case 9: row = 2; column = 2; break;
default:
cout << "Invalid choice" << endl;
return;
}
if(board[row][column] != 'X' && board[row][column] != 'O') {
board[row][column] = turn;
cout << currentPlayer << " places an " << turn << " in position "
<< choice << "." << endl;
turn = (turn == 'X') ? 'O' : 'X';
} else {
cout << "Box already filled!" << endl;
cout << "Please try again!!" << endl;
player_turn();
}
display_board();
}
bool gameover() {
for(int i = 0; i < 3; i++)
if(board[i][0] == board[i][1] && board[i][0] == board[i][2] ||
board[0][i] == board[1][i] && board[0][i] == board[2][i])
return false;
if(board[0][0] == board[1][1] && board[0][0] == board[2][2] ||
board[0][2] == board[1][1] && board[0][2] == board[2][0])
return false;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
if(board[i][j] != 'X' && board[i][j] != 'O')
return true;
draw = true;
return false;
}
int main() {
cout << "\tWelcome to the Tic Tac Toe Game \n" << endl;
cout <<"\tEnter the name of Player 1 (X): ";
cin >> player1;
cout <<"\tEnter the name of Player 2 (O): ";
cin >> player2;
while(gameover()) {
display_board();
player_turn();
gameover();
}
if(turn == 'X' && draw == false)
cout << "\t" << player2 << " [O] wins! Congratulations." << endl;
else if(turn == 'O' && draw == false)
cout << "\t" << player1 << "[X] wins! Congratulations." << endl;
else
cout << "\t Game over, it's a draw !!" << endl;
return 0;
}