Tictaktoe C++
Tictaktoe C++
// CODE
#include<iostream>
#include<ctime>
#include<stdlib.h>
char board[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '}; //making an array
cout << " " << board[8] << " | " << board[7] << " | " << board[6] << endl;
cout << " " << board[5] << " | " << board[4] << " | " << board[3] << endl;
cout << " " << board[2] << " | " << board[1] << " | " << board[0] << endl;
cout<<"***********************"<<endl;
cout<<"***********************"<<endl;
void x_players_choice(){
while (true){ //it will run forever until we specify break statement.
cout<<"************************************"<<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;
}
else if(board[choice]!= ' '){
cout<<"****************************"<<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<<"***********************************"<<endl;
int choice;
cin>>choice;
if(choice<0 || choice>8){
cout<<"**************"<<endl;
cout<<"invalid choice"<<endl;
cout<<"****************************"<<endl;
else {
board[choice] = 'o';
break;
srand(time(0));
int choice;
do{
while(board[choice]!=' ');
board[choice]='o';
srand(time(0));
int choice;
do{
}
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;
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];
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];
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
void o_computer_vs_x_player(){
string name;
cout<<"**********************"<<endl;
cout<<"enter the player name "<<endl;
cout<<"**********************"<<endl;
cin>>name;
show_board();
cout<<"************************"<<endl;
cout<<"************************"<<endl;
x_players_choice();
else {
o_computers_choice();
char winner=check_winner();
if(winner=='x'){
show_board();
cout<<"************************************"<<endl;
cout<<"************************************"<<endl;
cout<<"************"<<endl;
cout<<"2. continue."<<endl;
cout<<"************"<<endl;
int n,play;
cout<<"************************"<<endl;
cout<<"************************"<<endl;
cin>>n;
if(n==1){
cout<<"************************"<<endl;
cout<<"************************"<<endl;
else if (n==2){
clean_board();
o_computer_vs_x_player();
break;
else if (winner=='o'){
show_board();
cout<<"************************************"<<endl;
cout<<"************************************"<<endl;
cout<<"************"<<endl;
cout<<"2. continue."<<endl;
cout<<"************"<<endl;
int n,play;
cout<<"************************"<<endl;
cout<<"************************"<<endl;
cin>>n;
if(n==1){
cout<<"************************"<<endl;
cout<<"************************"<<endl;
else if (n==2){
clean_board();
o_computer_vs_x_player();
break;
else if (winner=='d'){
cout<<"*****************"<<endl;;
cout<<"*****************"<<endl;
cout<<"************"<<endl;
cout<<"************"<<endl;
int n,play;
cout<<"************************"<<endl;
cout<<"************************"<<endl;
cin>>n;
if(n==1){
cout<<"************************"<<endl;
cout<<"************************"<<endl;
else if (n==2){
clean_board();
o_computer_vs_x_player();
break;
}}
void x_computer_vs_o_player(){
string name;
cout<<"**********************"<<endl;
cout<<"**********************"<<endl;
cin>>name;
show_board();
cout<<"************************"<<endl;
cout<<"************************"<<endl;
o_players_choice();
else {
x_computers_choice();
char winner=check_winner();
if(winner=='o'){
cout<<"************************************"<<endl;
cout<<"************************************"<<endl;
cout<<"************"<<endl;
cout<<"2. continue."<<endl;
cout<<"************"<<endl;
int n,play;
cout<<"************************"<<endl;
cout<<"************************"<<endl;
cin>>n;
if(n==1){
cout<<"************************"<<endl;
cout<<"************************"<<endl;
else if (n==2){
clean_board();
x_computer_vs_o_player();
break;
else if (winner=='x'){
cout<<"*************************************"<<endl;
cout<<"*************************************"<<endl;
cout<<"************"<<endl;
cout<<"2. continue."<<endl;
cout<<"************"<<endl;
int n,play;
cout<<"************************"<<endl;
cout<<"************************"<<endl;
cin>>n;
if(n==1){
cout<<"************************"<<endl;
cout<<"************************"<<endl;
else if (n==2){
clean_board();
x_computer_vs_o_player();
break;
else if (winner=='d'){
cout<<"********************"<<endl;
cout<<"********************"<<endl;
cout<<"************"<<endl;
cout<<"2. continue."<<endl;
cout<<"************"<<endl;
int n,play;
cout<<"************************"<<endl;
cout<<"************************"<<endl;
cin>>n;
if(n==1){
cout<<"************************"<<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<<"***************************"<<endl;
cin>>p1;
cout<<"****************************"<<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<<"************************"<<endl;
o_players_choice();
else {
cout<<"*********************"<<endl;
cout<<"*********************"<<endl;
x_players_choice();
char winner=check_winner();
if(winner=='o'){
show_board();
cout<<"*************************************"<<endl;
cout<<"*************************************"<<endl;
cout<<"************"<<endl;
cout<<"2. continue."<<endl;
cout<<"************"<<endl;
int n,play;
cout<<"************************"<<endl;
cout<<"************************"<<endl;
cin>>n;
if(n==1){
cout<<"************************"<<endl;
cout<<"************************"<<endl;
else if (n==2){
clean_board();
o_player_vs_x_player();
break;
else if (winner=='x'){
show_board();
cout<<"*************************************"<<endl;
cout<<"*************************************"<<endl;
cout<<"************"<<endl;
cout<<"2. continue."<<endl;
cout<<"************"<<endl;
int n,play;
cout<<"************************"<<endl;
cin>>n;
if(n==1){
cout<<"************************"<<endl;
cout<<"************************"<<endl;
else if (n==2){
clean_board();
o_player_vs_x_player();
break;
else if (winner=='d'){
cout<<"*****************"<<endl;
cout<<"*****************"<<endl;
cout<<"************"<<endl;
cout<<"2. continue."<<endl;
cout<<"************"<<endl;
int n,play;
cout<<"************************"<<endl;
cin>>n;
if(n==1){
cout<<"************************"<<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<<"***************************"<<endl;
cin>>p1;
cout<<"****************************"<<endl;
cout<<"****************************"<<endl;
cin>>p2;
while(true){
show_board();
cout<<"************************"<<endl;
cout<<"************************"<<endl;
x_players_choice();
else {
cout<<"**********************"<<endl;
cout<<"**********************"<<endl;
o_players_choice();
char winner=check_winner();
if(winner=='x'){
cout<<"**************************************"<<endl;
cout<<"**************************************"<<endl;
cout<<"************"<<endl;
cout<<"2. continue."<<endl;
cout<<"************"<<endl;
int n,play;
cout<<"************************"<<endl;
cin>>n;
if(n==1){
cout<<"************************"<<endl;
cout<<"************************"<<endl;
else if (n==2){
clean_board();
x_player_vs_o_player();
break;
else if (winner=='o'){
cout<<"*************************************"<<endl;
cout<<"*************************************"<<endl;
cout<<"************"<<endl;
cout<<"2. continue."<<endl;
cout<<"************"<<endl;
int n,play;
cout<<"************************"<<endl;
cin>>n;
if(n==1){
cout<<"************************"<<endl;
cout<<"************************"<<endl;
else if (n==2){
clean_board();
x_player_vs_o_player();
break;
else if (winner=='d'){
cout<<"*****************"<<endl;
cout<<"*****************"<<endl;;
cout<<"************"<<endl;
cout<<"2. continue."<<endl;
cout<<"************"<<endl;
int n,play;
cout<<"************************"<<endl;
cin>>n;
if(n==1){
cout<<"************************"<<endl;
cout<<"************************"<<endl;
else if (n==2){
clean_board();
x_player_vs_o_player();
break;
}}
int main(){
cout<< "****************************************************************"<<endl;
cout<<"****************************************************************"<<endl;
cin>>mode;
switch(mode){
case 1:
int s;
cout<<"********************************"<<endl;
cout<<"********************************"<<endl;
cout<<"*******"<<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<<"*******"<<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;