0% found this document useful (0 votes)
12 views9 pages

Tic Tac Toe Game Program Code

Uploaded by

abdullahatharsh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

Tic Tac Toe Game Program Code

Uploaded by

abdullahatharsh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PF LAB FINAL PROJECT

NAME : ABDULLAH ATHAR S2025105046


RAMEEN IRFAN S2025105028
RAJA SHAHWAIZ S2025105016

RESOURCE PERSON:
NABEEL ALI KHAN

About project:

Tic tac toe Game program


 Description:
 The game board is a 3x3 grid.
 Two players take turns entering positions
1–9 to place their mark (X or O).
 The program checks for:
 A win condition (three in a row, column, or
diagonal).
 A draw condition (all positions filled with
no winner).
 The board updates after each move and
displays the current state.
CODE:
#include <iostream>
#include <string>
using namespace std;

char board[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};

string player1, player2;


char current_marker;
int current_player;

void drawBoard() {
cout << "\nCurrent Board:\n";
for (int i = 0; i < 3; i++) {
cout << " ";
for (int j = 0; j < 3; j++) {
cout << board[i][j];
if (j < 2) cout << " | ";
}
if (i < 2) cout << "\n---+---+---\n";
}
cout << "\n";
}

bool placeMarker(int slot) {


int row = (slot - 1) / 3;
int col = (slot - 1) % 3;

if (slot < 1 || slot > 9 || board[row][col]


== 'X' || board[row][col] == 'O') {
return false;
}

board[row][col] = current_marker;
return true;
}

int checkWin()
{

for (int i = 0; i < 3; i++) {


if ((board[i][0] == board[i][1] &&
board[i][1] == board[i][2]) ||
(board[0][i] == board[1][i] &&
board[1][i] == board[2][i]))
return current_player;
}

if ((board[0][0] == board[1][1] &&


board[1][1] == board[2][2]) ||
(board[0][2] == board[1][1] &&
board[1][1] == board[2][0]))
return current_player;

return 0;
}

void swapPlayerAndMarker() {
if (current_marker == 'X') {
current_marker = 'O';
current_player = 2;
} else {
current_marker = 'X';
current_player = 1;
}
}

int main() {
cout << "=== Welcome to Tic Tac Toe
Game ===\n\n";

cout << "Enter Player 1 name (X): ";


getline(cin, player1);

cout << "Enter Player 2 name (O): ";


getline(cin, player2);

current_marker = 'X';
current_player = 1;
int moveCount = 0;

drawBoard();

while (true) {
int slot;
string current_name = (current_player
== 1) ? player1 : player2;
cout << current_name << " (" <<
current_marker << "), choose your slot (1-
9): ";
cin >> slot;

if (!placeMarker(slot))
{
cout << "Invalid move. Please try
again.\n";
continue;
}

moveCount++;
drawBoard();

int winner = checkWin();


if (winner != 0) {
cout << "\n Congratulations " <<
current_name << "! You have won the game
in " << moveCount << " moves.\n";
break;
}

if (moveCount == 9) {
cout << "\n It's a draw! \n";
break;
}

swapPlayerAndMarker();
}

cout << "\nTHANK YOU FOR PLAYING!\n";


return 0;
}

 Output Screen:

You might also like