#include <iostream>
#include<ctime>
#include <cstdlib>
using namespace std;
void player()
{
char box1('1'
;
char box2('2'
;
char box3('3'
;
char box4('4'
;
char box5('5'
;
char box6('6'
;
char box7('7'
;
char box8('8'
;
char box9('9'
;
char player2icon('O'
;
char playericon('X'
;
char player2input;
char playerinput;
bool go;
go = true;
while(go = true)
{
//draws the board
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
cout<<"X:";
cin>>playerinput;//player 1 input
cout<<endl;
//checks for valid answer(and checks the answer)
if(playerinput == '1' && box1 == '1'
{
box1 = playericon;
}
else if(playerinput == '2' && box2 == '2'
{
box2 = playericon;
}
if(playerinput == '3' && box3 == '3'
{
box3 = playericon;
}
else if(playerinput == '4' && box4 == '4'
{
box4 = playericon;
}
if(playerinput == '5' && box5 == '5'
{
box5 = playericon;
}
else if(playerinput == '6' && box6 == '6'
{
box6 = playericon;
}
if(playerinput == '7' && box7 == '7'
{
box7 = playericon;
}
else if(playerinput == '8' && box8 == '8'
{
box8 = playericon;
}
else if(playerinput == '9' && box9 == '9'
{
box9 = playericon;
}
if(box1==box5&&box9==box5)
{
cout<<"YOU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"YOU WIN!";break;}
if(box7==box8&&box9==box
{cout<<"YOU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"YOU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"YOU WIN!";break;}
if(box3==box6&&box6==box9)
{
cout<<"YOU WIN!";break;}
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
cout<<"O:";
cin>>player2input;//player 2 input
cout<<endl;
//checks for valid answer(and checks the answer)
if(player2input == '1' && box1 == '1'
{
box1 = player2icon;
}
else if(player2input == '2' && box2 == '2'
{
box2 = player2icon;
}
if(player2input == '3' && box3 == '3'
{
box3 = player2icon;
}
else if(player2input == '4' && box4 == '4'
{
box4 = player2icon;
}
if(player2input == '5' && box5 == '5'
{
box5 = player2icon;
}
else if(player2input == '6' && box6 == '6'
{
box6 = player2icon;
}
if(player2input == '7' && box7 == '7'
{
box7 = player2icon;
}
else if(player2input == '8' && box8 == '8'
{
box8 = player2icon;
}
else if(player2input == '9' && box9 == '9'
{
box9 = player2icon;
}
if(box1==box5&&box9==box5)
{
cout<<"YOU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"YOU WIN!";break;}
if(box7==box8&&box9==box
{cout<<"YOU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"YOU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"YOU WIN!";break;}
if(box3==box6&&box6==box9)
{cout<<"YOU WIN!";break;}
}
cout<<endl;
}
void singleplayer()
{
char box1('1'
;
char box2('2'
;
char box3('3'
;
char box4('4'
;
char box5('5'
;
char box6('6'
;
char box7('7'
;
char box8('8'
;
char box9('9'
;
char player2icon('O'
;
char playericon('X'
;
int player2input;
char playerinput;
bool go;
go = true;
while(go = true)
{
//draws the board
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
cout<<"X:";
cin>>playerinput;//player 1 input
if(playerinput == '1' && box1 == '1'
{
box1 = playericon;
}
else if(playerinput == '2' && box2 == '2'
{
box2 = playericon;
}
if(playerinput == '3' && box3 == '3'
{
box3 = playericon;
}
else if(playerinput == '4' && box4 == '4'
{
box4 = playericon;
}
if(playerinput == '5' && box5 == '5'
{
box5 = playericon;
}
else if(playerinput == '6' && box6 == '6'
{
box6 = playericon;
}
if(playerinput == '7' && box7 == '7'
{
box7 = playericon;
}
else if(playerinput == '8' && box8 == '8'
{
box8 = playericon;
}
else if(playerinput == '9' && box9 == '9'
{
box9 = playericon;
}
if(box1==box5&&box9==box5)
{
cout<<"YOU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"YOU WIN!";break;}
if(box7==box8&&box9==box
{cout<<"YOU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"YOU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"YOU WIN!";break;}
if(box3==box6&&box6==box9)
{
cout<<"YOU WIN!";break;}
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
srand(time(0));
player2input = (rand() % 9) + 1;
cout<<"O:\n"<<player2input;
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '1' && box1 == '1'
{
box1 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '2' && box2 == '2'
{
box2 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '3' && box3 == '3'
{
box3 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '4' && box4 == '4'
{
box4 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '5' && box5 == '5'
{
box5 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '6' && box6 == '6'
{
box6 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '7' && box7 == '7'
{
box7 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '8' && box8 == '8'
{
box8 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '9' && box9 == '9'
{
box9 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(box1==box5&&box9==box5)
{
cout<<"CPU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"CPU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"CPU WIN!";break;}
if(box7==box8&&box9==box
{cout<<"CPU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"CPU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"CPU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"CPU WIN!";break;}
if(box3==box6&&box6==box9)
{cout<<"CPU WIN!";break;}
}
}
int main()
{
char c;
cout<<"Welcome to tic tac toe, single player [X] or 2 player[O]:";
cin>>c;
if(c == 'X'|| c == 'x'
player();
if(c == 'O'|| c == 'o'
singleplayer();
system("pause");
}
#include <iostream>
#include<ctime>
#include <cstdlib>
using namespace std;
void player()
{
char box1('1'
;
char box2('2'
;
char box3('3'
;
char box4('4'
;
char box5('5'
;
char box6('6'
;
char box7('7'
;
char box8('8'
;
char box9('9'
;
char player2icon('O'
;
char playericon('X'
;
char player2input;
char playerinput;
bool go;
go = true;
while(go = true)
{
//draws the board
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
cout<<"X:";
cin>>playerinput;//player 1 input
cout<<endl;
//checks for valid answer(and checks the answer)
if(playerinput == '1' && box1 == '1'
{
box1 = playericon;
}
else if(playerinput == '2' && box2 == '2'
{
box2 = playericon;
}
if(playerinput == '3' && box3 == '3'
{
box3 = playericon;
}
else if(playerinput == '4' && box4 == '4'
{
box4 = playericon;
}
if(playerinput == '5' && box5 == '5'
{
box5 = playericon;
}
else if(playerinput == '6' && box6 == '6'
{
box6 = playericon;
}
if(playerinput == '7' && box7 == '7'
{
box7 = playericon;
}
else if(playerinput == '8' && box8 == '8'
{
box8 = playericon;
}
else if(playerinput == '9' && box9 == '9'
{
box9 = playericon;
}
if(box1==box5&&box9==box5)
{
cout<<"YOU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"YOU WIN!";break;}
if(box7==box8&&box9==box
{cout<<"YOU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"YOU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"YOU WIN!";break;}
if(box3==box6&&box6==box9)
{
cout<<"YOU WIN!";break;}
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
cout<<"O:";
cin>>player2input;//player 2 input
cout<<endl;
//checks for valid answer(and checks the answer)
if(player2input == '1' && box1 == '1'
{
box1 = player2icon;
}
else if(player2input == '2' && box2 == '2'
{
box2 = player2icon;
}
if(player2input == '3' && box3 == '3'
{
box3 = player2icon;
}
else if(player2input == '4' && box4 == '4'
{
box4 = player2icon;
}
if(player2input == '5' && box5 == '5'
{
box5 = player2icon;
}
else if(player2input == '6' && box6 == '6'
{
box6 = player2icon;
}
if(player2input == '7' && box7 == '7'
{
box7 = player2icon;
}
else if(player2input == '8' && box8 == '8'
{
box8 = player2icon;
}
else if(player2input == '9' && box9 == '9'
{
box9 = player2icon;
}
if(box1==box5&&box9==box5)
{
cout<<"YOU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"YOU WIN!";break;}
if(box7==box8&&box9==box
{cout<<"YOU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"YOU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"YOU WIN!";break;}
if(box3==box6&&box6==box9)
{cout<<"YOU WIN!";break;}
}
cout<<endl;
}
void singleplayer()
{
char box1('1'
;
char box2('2'
;
char box3('3'
;
char box4('4'
;
char box5('5'
;
char box6('6'
;
char box7('7'
;
char box8('8'
;
char box9('9'
;
char player2icon('O'
;
char playericon('X'
;
int player2input;
char playerinput;
bool go;
go = true;
while(go = true)
{
//draws the board
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
cout<<"X:";
cin>>playerinput;//player 1 input
if(playerinput == '1' && box1 == '1'
{
box1 = playericon;
}
else if(playerinput == '2' && box2 == '2'
{
box2 = playericon;
}
if(playerinput == '3' && box3 == '3'
{
box3 = playericon;
}
else if(playerinput == '4' && box4 == '4'
{
box4 = playericon;
}
if(playerinput == '5' && box5 == '5'
{
box5 = playericon;
}
else if(playerinput == '6' && box6 == '6'
{
box6 = playericon;
}
if(playerinput == '7' && box7 == '7'
{
box7 = playericon;
}
else if(playerinput == '8' && box8 == '8'
{
box8 = playericon;
}
else if(playerinput == '9' && box9 == '9'
{
box9 = playericon;
}
if(box1==box5&&box9==box5)
{
cout<<"YOU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"YOU WIN!";break;}
if(box7==box8&&box9==box
{cout<<"YOU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"YOU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"YOU WIN!";break;}
if(box3==box6&&box6==box9)
{
cout<<"YOU WIN!";break;}
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
srand(time(0));
player2input = (rand() % 9) + 1;
cout<<"O:\n";
if(player2input == 1 && box1 == '1'
{
box1 = player2icon;
}
if(player2input == 2 && box2 == '2'
{
box2 = player2icon;
}
if(player2input == 3 && box3 == '3'
{
box3 = player2icon;
}
if(player2input == 4 && box4 == '4'
{
box4 = player2icon;
}
if(player2input == 5 && box5 == '5'
{
box5 = player2icon;
}
if(player2input == 6 && box6 == '6'
{
box6 = player2icon;
}
if(player2input == 7 && box7 == '7'
{
box7 = player2icon;
}
if(player2input == 8 && box8 == '8'
{
box8 = player2icon;
}
if(player2input == 9 && box9 == '9'
{
box9 = player2icon;
}
if(box1==box5&&box9==box5)
{
cout<<"CPU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"CPU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"CPU WIN!";break;}
if(box7==box8&&box9==box
{cout<<"CPU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"CPU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"CPU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"CPU WIN!";break;}
if(box3==box6&&box6==box9)
{cout<<"CPU WIN!";break;}
}
}
int main()
{
char c;
cout<<"Welcome to tic tac toe, single player [X] or 2 player[O]:";
cin>>c;
if(c == 'X'|| c == 'x'
player();
if(c == 'O'|| c == 'o'
singleplayer();
system("pause");
}
Copyright © 2026, NextGenUpdate.
All Rights Reserved.