0% found this document useful (0 votes)
7 views24 pages

Tictaktoe C++

The document describes code for a Tic Tac Toe game. It includes functions for displaying the board, cleaning the board, allowing player and computer turns, counting symbols, checking for a winner, and playing multiple games. The code allows for games where the computer or player can be X or O.

Uploaded by

sidra afzaal
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)
7 views24 pages

Tictaktoe C++

The document describes code for a Tic Tac Toe game. It includes functions for displaying the board, cleaning the board, allowing player and computer turns, counting symbols, checking for a winner, and playing multiple games. The code allows for games where the computer or player can be X or O.

Uploaded by

sidra afzaal
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
You are on page 1/ 24

// FINAL PROJECT (TIC TAK TOE)

// CODE

#include<iostream>

#include<ctime>

#include<stdlib.h>

using namespace std;

char board[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '}; //making an array

void show_board(){ //making board for tic tak toe.

cout << " | | " << endl;

cout << " " << board[8] << " | " << board[7] << " | " << board[6] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << board[5] << " | " << board[4] << " | " << board[3] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << board[2] << " | " << board[1] << " | " << board[0] << endl;

cout << " | | " << endl << endl;

void clean_board() // cleaning board


{

int length = sizeof(board);

cout<<"***********************"<<endl;

cout << "PREPARING NEW BOARD" << endl;

cout<<"***********************"<<endl;

for (int i = 0; i < length; i++)

board[i] = ' ';

void x_players_choice(){

while (true){ //it will run forever until we specify break statement.

cout<<"************************************"<<endl;

cout<<"select your position to put symbol:"<<endl;

cout<<"************************************"<<endl;

int choice;

cin>>choice;

choice--; // the array starts from 0 if user enter 8 it will mark symbol on
9.

if(choice<0 || choice>8){

cout<<"**************"<<endl;

cout<<"invalid choice"<<endl;

cout<<"**************"<<endl; //if user enter wrong position.

}
else if(board[choice]!= ' '){

cout<<"****************************"<<endl;

cout<<"please enter valid position."<<endl;

cout<<"****************************"<<endl;

else {

board[choice]='x';

break ;

}}

void o_players_choice(){

int o;

while (true){ //it will run forever until we specify break statement.

cout<<"***********************************"<<endl;

cout<<"select your position to put symbol:"<<endl;

cout<<"***********************************"<<endl;

int choice;

cin>>choice;

choice--; // the array starts from 0 if user enter 8 it will mark


symbol on 9.

if(choice<0 || choice>8){

cout<<"**************"<<endl;

cout<<"invalid choice"<<endl;

cout<<"**************"<<endl; //if user enter wrong position.

else if(board[choice]!=' '){

cout<<"****************************"<<endl;

cout<<"please enter valid position."<<endl;


cout<<"****************************"<<endl;

else {

board[choice] = 'o';

break;

// copmuter with symbol o

void o_computers_choice(){ // function for computer turn

srand(time(0));

int choice;

do{

choice = rand()%10; //RANDOM FUNCTION //it will return the


random number between (0 to 9).

while(board[choice]!=' ');

board[choice]='o';

// computer with symbol 'x'.

void x_computers_choice(){ // function for computer turn

srand(time(0));

int choice;

do{

choice= rand()%10; //it will return the random number between (0


to 9).

}
while(board[choice]!=' ');

board[choice]='x';

int count_board(char symbol){ // it will count the number of occurence of particullar symbol (i.e
'x'/'o') in board.

int total=0;

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

if (board[i]==symbol){

total+=1;

}}

return total;

char check_winner(){

int d;

//checking winner in horizontal column.

if(board[0]==board[1]&&board[1]==board[2]&&board[0]!=' '){

return board[0];

if(board[3]==board[4]&&board[4]==board[5]&&board[3]!=' '){

return board[3];

if(board[6]==board[7]&&board[7]==board[8]&&board[6]!=' '){

return board[6];

//checking winner in vertical column.


if (board[0]==board[3]&&board[3]==board[6]&&board[0]!=' '){

return board[0];

if (board[1]==board[4]&&board[4]==board[7]&&board[1]!=' '){

return board[1];

if (board[2]==board[5]&&board[5]==board[8]&&board[2]!=' '){

return board[2];

//checking winner in diagonal column.

if (board[0]==board[4]&&board[4]==board[8]&&board[0]!=' '){

return board[0];

if (board[2]==board[4]&&board[4]==board[6]&&board[2]!=' '){

return board[2];

if(count_board('x')+count_board('o')<9){

else{

return 'd';

//computer vs player

// here computer is 'o' and player is 'x'.

void o_computer_vs_x_player(){

string name;

cout<<"**********************"<<endl;
cout<<"enter the player name "<<endl;

cout<<"**********************"<<endl;

cin>>name;

while(true) { //it will run forever until we specify break statement.

show_board();

if(count_board('x')==count_board('o')){ //here if x=o it means its players turn.

cout<<"************************"<<endl;

cout<<name<<" 's turn."<<endl;

cout<<"************************"<<endl;

x_players_choice();

else {

o_computers_choice();

char winner=check_winner();

if(winner=='x'){

show_board();

cout<<"************************************"<<endl;

cout<<"*CONGRATULATION "<< name << " won the game*"<<endl;

cout<<"************************************"<<endl;

// menu to ask user weather he wants to play again or not.

cout<<"************"<<endl;

cout<<"1. exit ."<<endl;

cout<<"2. continue."<<endl;

cout<<"************"<<endl;

int n,play;
cout<<"************************"<<endl;

cout<<"press the key of choice."<<endl;

cout<<"************************"<<endl;

cin>>n;

if(n==1){

cout<<"************************"<<endl;

cout<<"*thank you for playing*"<<endl;

cout<<"************************"<<endl;

else if (n==2){

clean_board();

o_computer_vs_x_player();

break;

else if (winner=='o'){

show_board();

cout<<"************************************"<<endl;

cout<<"CONGRATULATION! computer won the game"<< endl;

cout<<"************************************"<<endl;

// menu to ask user weather he wants to play again or not.

cout<<"************"<<endl;

cout<<"1. exit ."<<endl;

cout<<"2. continue."<<endl;
cout<<"************"<<endl;

int n,play;

cout<<"************************"<<endl;

cout<<"press the key of choice."<<endl;

cout<<"************************"<<endl;

cin>>n;

if(n==1){

cout<<"************************"<<endl;

cout<<"*thank you for playing*"<<endl;

cout<<"************************"<<endl;

else if (n==2){

clean_board();

o_computer_vs_x_player();

break;

else if (winner=='d'){

cout<<"*****************"<<endl;;

cout<<"the game is draw."<<endl;

cout<<"*****************"<<endl;

// menu to ask user weather he wants to play again or not.

cout<<"************"<<endl;

cout<<"1. exit ."<<endl;


cout<<"2. continue."<<endl;

cout<<"************"<<endl;

int n,play;

cout<<"************************"<<endl;

cout<<"press the key of choice."<<endl;

cout<<"************************"<<endl;

cin>>n;

if(n==1){

cout<<"************************"<<endl;

cout<<"*thank you for playing*"<<endl;

cout<<"************************"<<endl;

else if (n==2){

clean_board();

o_computer_vs_x_player();

break;

}}

//computer vs player //computer symbol os x an player symbol is o.

void x_computer_vs_o_player(){

string name;

cout<<"**********************"<<endl;

cout<<"enter the player name "<<endl;

cout<<"**********************"<<endl;
cin>>name;

while(true) { //it will run forever until we specify break statement.

show_board();

if(count_board('o')==count_board('x')){ //here if x=o it means its players turn.

cout<<"************************"<<endl;

cout<<name<<" 's turn."<<endl;

cout<<"************************"<<endl;

o_players_choice();

else {

x_computers_choice();

char winner=check_winner();

if(winner=='o'){

cout<<"************************************"<<endl;

cout<<"CONGRATULATION "<< name << " won the game"<<endl;

cout<<"************************************"<<endl;

// menu to ask user weather he wants to play again or not.

cout<<"************"<<endl;

cout<<"1. exit ."<<endl;

cout<<"2. continue."<<endl;

cout<<"************"<<endl;

int n,play;

cout<<"************************"<<endl;

cout<<"press the key of choice."<<endl;

cout<<"************************"<<endl;

cin>>n;
if(n==1){

cout<<"************************"<<endl;

cout<<"*thank you for playing*"<<endl;

cout<<"************************"<<endl;

else if (n==2){

clean_board();

x_computer_vs_o_player();

break;

else if (winner=='x'){

cout<<"*************************************"<<endl;

cout<<"CONGRATULATION! computer won the game"<<endl;

cout<<"*************************************"<<endl;

// menu to ask user weather he wants to play again or not.

cout<<"************"<<endl;

cout<<"1. exit ."<<endl;

cout<<"2. continue."<<endl;

cout<<"************"<<endl;

int n,play;

cout<<"************************"<<endl;

cout<<"press the key of choice."<<endl;

cout<<"************************"<<endl;

cin>>n;
if(n==1){

cout<<"************************"<<endl;

cout<<"*thank you for playing*"<<endl;

cout<<"************************"<<endl;

else if (n==2){

clean_board();

x_computer_vs_o_player();

break;

else if (winner=='d'){

cout<<"********************"<<endl;

cout<< "the game is draw."<<endl;

cout<<"********************"<<endl;

// menu to ask user weather he wants to play again or not.

cout<<"************"<<endl;

cout<<"1. exit ."<<endl;

cout<<"2. continue."<<endl;

cout<<"************"<<endl;

int n,play;

cout<<"************************"<<endl;

cout<<"press the key of choice."<<endl;

cout<<"************************"<<endl;

cin>>n;
if(n==1){

cout<<"************************"<<endl;

cout<<"*thank you for playing*"<<endl;

cout<<"************************"<<endl;

else if (n==2){

clean_board();

x_computer_vs_o_player();

break;

}}

// player vs player

void o_player_vs_x_player(){

string p1 , p2;

cout<<"***************************"<<endl;

cout<<"enter the first player name"<<endl;

cout<<"***************************"<<endl;

cin>>p1;

cout<<"****************************"<<endl;

cout<<"enter the second player name"<<endl;

cout<<"****************************"<<endl;

cin>>p2;

while(true){

show_board();
if(count_board('o')==count_board('x')){ //here if x=o it means its 1 players turn.

cout<<"************************"<<endl;

cout<<p1 <<" 's turn."<<endl;

cout<<"************************"<<endl;

o_players_choice();

else {

cout<<"*********************"<<endl;

cout<<p2 <<" 's turn."<<endl;

cout<<"*********************"<<endl;

x_players_choice();

char winner=check_winner();

if(winner=='o'){

show_board();

cout<<"*************************************"<<endl;

cout<<"CONGRATULATIONs! "<< p1 << " won the game"<<endl;

cout<<"*************************************"<<endl;

// menu to ask user weather he wants to play again or not.

cout<<"************"<<endl;

cout<<"1. exit ."<<endl;

cout<<"2. continue."<<endl;

cout<<"************"<<endl;

int n,play;

cout<<"************************"<<endl;

cout<<"press the key of choice."<<endl;

cout<<"************************"<<endl;
cin>>n;

if(n==1){

cout<<"************************"<<endl;

cout<<"*thank you for playing*"<<endl;

cout<<"************************"<<endl;

else if (n==2){

clean_board();

o_player_vs_x_player();

break;

else if (winner=='x'){

show_board();

cout<<"*************************************"<<endl;

cout<<"CONGRATULATIONs!" <<p2 << " won the game"<< endl;

cout<<"*************************************"<<endl;

// menu to ask user weather he wants to play again or not.

cout<<"************"<<endl;

cout<<"1. exit ."<<endl;

cout<<"2. continue."<<endl;

cout<<"************"<<endl;

int n,play;

cout<<"************************"<<endl;

cout<<"press the key of choice."<<endl;


cout<<"************************"<<endl;

cin>>n;

if(n==1){

cout<<"************************"<<endl;

cout<<"*thank you for playing*"<<endl;

cout<<"************************"<<endl;

else if (n==2){

clean_board();

o_player_vs_x_player();

break;

else if (winner=='d'){

cout<<"*****************"<<endl;

cout<<"the game is draw."<<endl;

cout<<"*****************"<<endl;

// menu to ask user weather he wants to play again or not.

cout<<"************"<<endl;

cout<<"1. exit ."<<endl;

cout<<"2. continue."<<endl;

cout<<"************"<<endl;

int n,play;

cout<<"************************"<<endl;

cout<<"press the key of choice."<<endl;


cout<<"************************"<<endl;

cin>>n;

if(n==1){

cout<<"************************"<<endl;

cout<<"*thank you for playing*"<<endl;

cout<<"************************"<<endl;

else if (n==2){

clean_board();

o_player_vs_x_player();

break;

}}

void x_player_vs_o_player(){

string p1 , p2;

cout<<"***************************"<<endl;

cout<<"enter the first player name"<<endl;

cout<<"***************************"<<endl;

cin>>p1;

cout<<"****************************"<<endl;

cout<<"enter the second player name"<<endl;

cout<<"****************************"<<endl;

cin>>p2;
while(true){

show_board();

if(count_board('x')==count_board('o')){ //here if x=o it means its 1 players turn.

cout<<"************************"<<endl;

cout<<p1 <<" 's turn."<<endl;

cout<<"************************"<<endl;

x_players_choice();

else {

cout<<"**********************"<<endl;

cout<<p2 <<" 's turn."<<endl;

cout<<"**********************"<<endl;

o_players_choice();

char winner=check_winner();

if(winner=='x'){

cout<<"**************************************"<<endl;

cout<<"CONGRATULATIONs! "<< p1 << " won the game"<<endl;

cout<<"**************************************"<<endl;

// menu to ask user weather he wants to play again or not.

cout<<"************"<<endl;

cout<<"1. exit ."<<endl;

cout<<"2. continue."<<endl;

cout<<"************"<<endl;

int n,play;

cout<<"************************"<<endl;

cout<<"press the key of choice."<<endl;


cout<<"************************"<<endl;

cin>>n;

if(n==1){

cout<<"************************"<<endl;

cout<<"*thank you for playing*"<<endl;

cout<<"************************"<<endl;

else if (n==2){

clean_board();

x_player_vs_o_player();

break;

else if (winner=='o'){

cout<<"*************************************"<<endl;

cout<<"CONGRATULATIONs!" <<p2 << " won the game"<< endl;

cout<<"*************************************"<<endl;

// menu to ask user weather he wants to play again or not.

cout<<"************"<<endl;

cout<<"1. exit ."<<endl;

cout<<"2. continue."<<endl;

cout<<"************"<<endl;

int n,play;

cout<<"************************"<<endl;

cout<<"press the key of choice."<<endl;


cout<<"************************"<<endl;

cin>>n;

if(n==1){

cout<<"************************"<<endl;

cout<<"*thank you for playing*"<<endl;

cout<<"************************"<<endl;

else if (n==2){

clean_board();

x_player_vs_o_player();

break;

else if (winner=='d'){

cout<<"*****************"<<endl;

cout<<"the game is draw."<<endl;

cout<<"*****************"<<endl;;

// menu to ask user weather he wants to play again or not.

cout<<"************"<<endl;

cout<<"1. exit ."<<endl;

cout<<"2. continue."<<endl;

cout<<"************"<<endl;

int n,play;

cout<<"************************"<<endl;

cout<<"press the key of choice."<<endl;


cout<<"************************"<<endl;

cin>>n;

if(n==1){

cout<<"************************"<<endl;

cout<<"*thank you for playing*"<<endl;

cout<<"************************"<<endl;

else if (n==2){

clean_board();

x_player_vs_o_player();

break;

}}

int main(){

int mode; //menu shown to the user on screen.

cout<< "****************************************************************"<<endl;

cout <<"*---------------T I C K -- T A C -- T O E -- G A M E-----------*\n";

cout<<"****************************************************************"<<endl;

cout<<" \n 1. Computer vs Player. \n"<<endl;

cout<<" \n 2. Player vs Player. \n"<<endl;

cout<<" *// enter mode of your choice//* "<<endl;

cin>>mode;

switch(mode){
case 1:

int s;

cout<<"********************************"<<endl;

cout<<"enter the symbol of your choice."<<endl; //asking user his choice of


symbol.

cout<<"********************************"<<endl;

cout<<"*******"<<endl;

cout<<"1. 'x' "<<endl;

cout<<"2. 'o' "<<endl;

cout<<"*******"<<endl;

cin>>s;

if(s==1){

o_computer_vs_x_player();

else if (s==2){

x_computer_vs_o_player();

else {

cout<<"***************"<<endl;

cout<<"invalid choice."<<endl;

cout<<"***************"<<endl;

break;

case 2:

int n;

cout<<"********************************"<<endl;

cout<<"enter the symbol of your choice."<<endl; //asking user his choice of


symbol.
cout<<"********************************"<<endl;

cout<<"*******"<<endl;

cout<<"1. 'x' "<<endl;

cout<<"2. 'o' "<<endl;

cout<<"*******"<<endl;

cin>>n;

if(n==1){

x_player_vs_o_player();

else if(n==2){

o_player_vs_x_player();

else{

cout<<"***************"<<endl;

cout<<"invalid choice."<<endl;

cout<<"***************"<<endl;

break;

return 0;

You might also like