#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void game();//function for making moves
void winning();//function for showing winning screen
void losing();
void draw();
int main()
{
srand(time(0));//seeding the random number generator
int z;
char sl;//selection
for(z=0; z<=1; z++) //a loop for showing menu afer each game
{
cout<<"******************************************"<<endl;
cout<<"* T I C - T A C - T O E ! *"<<endl;
cout<<"******************************************"<<endl<<endl;
cout<<"Start game? [y/n] ";
cin>>sl;//insert selection
if(sl=='y')
{
cout<<""<<endl;
cout<<"You write X."<<endl<<endl<<endl;
system("cls");//clear console screen
game();//call game function
z=0;
}
if(sl=='n')
{
cout<<"If you dont want to play..."<<endl;
z=1; // loop ends, app closes
}
}
return 0;
}
void game()
{
char s[9];//a character array for storing X and O for each square in table
int i=0;//loop index
int inp,j=0,outp,op,temp2,t,w;//some variables for certain operations
int e=9999;//loop end
for(op=0;op<9;op++) //sets character array values
{
s[op]=' ';
}
//showing table;
cout<<" --- --- --- "<<endl;
cout<<"| "<<s[0]<<" | "<<s[1]<<" | "<<s[2]<<" |"<<endl;
cout<<" --- --- --- "<<endl;
cout<<"| "<<s[3]<<" | "<<s[4]<<" | "<<s[5]<<" |"<<endl;
cout<<" --- --- --- "<<endl;
cout<<"| "<<s[6]<<" | "<<s[7]<<" | "<<s[8]<<" |"<<endl;
cout<<" --- --- --- "<<endl;
while(i<=e)//loop starts
{
//selection and AI output
cout<<""<<endl;
cout<<"Select your X field: "<<endl<<endl;
cout<<" 1 2 3"<<endl<<" 4 5 6"<<endl<<" 7 8 9"<<endl;
cin>>inp; //input
s[inp-1]='X';//character variable will be equal to X
//AI program
outp=rand()%9;
for(j=0;j<9;j++)
{
if((s[j]=='X' || s[j]=='O') && j==outp) //checking if generated number
// is not equal to the field in which something was written
{
temp2=outp;
outp=rand()%9;
for(t=0;t<20;t++)
{
if(temp2==outp) outp=rand()%9;
j=0;
}
}
}
s[outp]='O';//output
// AI makes its move
system("cls"); //clears screen
//showing table;
cout<<" --- --- --- "<<endl;
cout<<"| "<<s[0]<<" | "<<s[1]<<" | "<<s[2]<<" |"<<endl;
cout<<" --- --- --- "<<endl;
cout<<"| "<<s[3]<<" | "<<s[4]<<" | "<<s[5]<<" |"<<endl;
cout<<" --- --- --- "<<endl;
cout<<"| "<<s[6]<<" | "<<s[7]<<" | "<<s[8]<<" |"<<endl;
cout<<" --- --- --- "<<endl;
//checking if someone has won
for(j=0;j<3;j++) //several situations
{
if((s[j]=='X' && s[j+3]=='X' && s[j+6]=='X') || (s[j]=='O' && s[j+3]=='O' && s[j+6]=='O'))
{
if(s[j]=='X')
{
winning(); //calls function
}
if(s[j]=='O')
{
losing();
}
return; //if someone has won, returns to menu (int main)
}
}
for(j=0;j<7;j=j+3)
{
if((s[j]=='X' && s[j+1]=='X' && s[j+2]=='X') || (s[j]=='O' && s[j+1]=='O' && s[j+2]=='O'))
{
if(s[j]=='X')
{
winning();
}
if(s[j]=='O')
{
losing();
}
return;
}
}
if((s[0]=='X' && s[4]=='X' && s[8]=='X') || (s[0]=='O' && s[4]=='O' && s[8]=='O'))
{
if(s[0]=='X')
{
winning();
}
if(s[0]=='O')
{
losing();
}
return;
}
if((s[6]=='X' && s[4]=='X' && s[2]=='X') || (s[6]=='O' && s[4]=='O' && s[2]=='O'))
{
if(s[6]=='X')
{
winning();
}
if(s[6]=='O')
{
losing();
}
return;
}
//checking for draw:
if((s[0]=='X' || s[0]=='O') && (s[1]=='X' || s[1]=='O') && (s[2]=='X' || s[2]=='O') &&
(s[3]=='X' || s[3]=='O') && (s[4]=='X' || s[4]=='O') && (s[5]=='X' || s[5]=='O') &&
(s[6]=='X' || s[6]=='O') && (s[7]=='X' || s[7]=='O') && (s[8]=='X' || s[8]=='O'))
{
draw();
return;
}
}
}
void winning() //winning screen functions
{
cout<<""<<endl<<endl;
cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
cout<<"*-------------------------------------------------------------*"<<endl;
cout<<"*- -*"<<endl;
cout<<"*- Y O U W O N ! ! ! -*"<<endl;
cout<<"*- -*"<<endl;
cout<<"*-------------------------------------------------------------*"<<endl;
cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
cout<<""<<endl<<endl;
}
void losing()
{
cout<<""<<endl<<endl;
cout<<"C O M P U T E R W O N. "<<endl;
cout<<""<<endl<<endl;
}
void draw()
{
cout<<""<<endl<<endl;
cout<<" I T' S A D R A W ! "<<endl;
cout<<""<<endl<<endl;
}
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void game();//function for making moves
void winning();//function for showing winning screen
void losing();
void draw();
int main()
{
srand(time(0));//seeding the random number generator
int z;
char sl;//selection
for(z=0; z<=1; z++) //a loop for showing menu afer each game
{
cout<<" ******************************************"<<endl;
cout<<" * T I C - T A C - T O E ! *"<<endl;
cout<<" ******************************************"<<endl<<endl;
cout<<" Start game? [y/n] ";
cin>>sl;//insert selection
if(sl=='y')
{
cout<<""<<endl;
cout<<"You write X."<<endl<<endl<<endl;
system("cls");//clear console screen
game();//call game function
z=0;
}
if(sl=='n')
{
cout<<"If you dont want to play..."<<endl;
z=1; // loop ends, app closes
}
}
return 0;
}
void game()
{
char s[9];//a character array for storing X and O for each square in table
int i=0;//loop index
int inp,j=0,outp,op,temp2,t,w;//some variables for certain operations
int e=9999;//loop end
for(op=0;op<9;op++) //sets character array values
{
s[op]=' ';
}
//showing table; ;
cout<<" --- --- --- "<<endl;
cout<<"| "<<s[0]<<" | "<<s[1]<<" | "<<s[2]<<" |"<<endl;
cout<<" --- --- --- "<<endl;
cout<<"| "<<s[3]<<" | "<<s[4]<<" | "<<s[5]<<" |"<<endl;
cout<<" --- --- --- "<<endl;
cout<<"| "<<s[6]<<" | "<<s[7]<<" | "<<s[8]<<" |"<<endl;
cout<<" --- --- --- "<<endl;
while(i<=e)//loop starts
{
//selection and AI output
cout<<""<<endl;
cout<<"Select your X field: "<<endl<<endl;
cout<<" 1 2 3"<<endl<<" 4 5 6"<<endl<<" 7 8 9"<<endl;
cin>>inp; //input
w=0;
t=w+2;
for(w=0;w<t;w++)
{
if(s[inp-1]=='O' || s[inp-1]=='X')
{
cout<<"You have chosen the wrong square. Choose again: ";
cin>>inp;
t=w+2;
}
else
{
s[inp-1]='X';//character variable will be equal to X
w=t;
}
}
//AI program
outp=rand()%9;
for(j=0;j<9;j++)
{
if(s[j]=='X' && j==outp) //checking if generated number is not equal to the field in which something was written
{
outp=rand()%9;
j=0;
}
if(s[j]=='O' && j==outp) //checking if generated number is not equal to the field in which something was written
{
outp=rand()%9;
j=0;
}
}
if(s[outp]!='X' && s[outp]!='O') s[outp]='O';//output
// AI makes its move
system("cls"); //clears screen
//showing table;
cout<<" --- --- --- "<<endl;
cout<<"| "<<s[0]<<" | "<<s[1]<<" | "<<s[2]<<" |"<<endl;
cout<<" --- --- --- "<<endl;
cout<<"| "<<s[3]<<" | "<<s[4]<<" | "<<s[5]<<" |"<<endl;
cout<<" --- --- --- "<<endl;
cout<<"| "<<s[6]<<" | "<<s[7]<<" | "<<s[8]<<" |"<<endl;
cout<<" --- --- --- "<<endl;
//checking if someone has won
for(j=0;j<3;j++) //several situations
{
if((s[j]=='X' && s[j+3]=='X' && s[j+6]=='X') || (s[j]=='O' && s[j+3]=='O' && s[j+6]=='O'))
{
if(s[j]=='X')
{
winning(); //calls function
}
if(s[j]=='O')
{
losing();
}
return; //if someone has won, returns to menu (int main)
}
}
for(j=0;j<7;j=j+3)
{
if((s[j]=='X' && s[j+1]=='X' && s[j+2]=='X') || (s[j]=='O' && s[j+1]=='O' && s[j+2]=='O'))
{
if(s[j]=='X')
{
winning();
}
if(s[j]=='O')
{
losing();
}
return;
}
}
if((s[0]=='X' && s[4]=='X' && s[8]=='X') || (s[0]=='O' && s[4]=='O' && s[8]=='O'))
{
if(s[0]=='X')
{
winning();
}
if(s[0]=='O')
{
losing();
}
return;
}
if((s[6]=='X' && s[4]=='X' && s[2]=='X') || (s[6]=='O' && s[4]=='O' && s[2]=='O'))
{
if(s[6]=='X')
{
winning();
}
if(s[6]=='O')
{
losing();
}
return;
}
//checking for draw:
if((s[0]=='X' || s[0]=='O') && (s[1]=='X' || s[1]=='O') && (s[2]=='X' || s[2]=='O') &&
(s[3]=='X' || s[3]=='O') && (s[4]=='X' || s[4]=='O') && (s[5]=='X' || s[5]=='O') &&
(s[6]=='X' || s[6]=='O') && (s[7]=='X' || s[7]=='O') && (s[8]=='X' || s[8]=='O'))
{
draw();
return;
}
}
}
void winning() //winning screen functions
{
cout<<""<<endl<<endl;
cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
cout<<"*-------------------------------------------------------------*"<<endl;
cout<<"*- -*"<<endl;
cout<<"*- Y O U W O N ! ! ! -*"<<endl;
cout<<"*- -*"<<endl;
cout<<"*-------------------------------------------------------------*"<<endl;
cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
cout<<""<<endl<<endl;
}
void losing()
{
cout<<""<<endl<<endl;
cout<<"C O M P U T E R W O N. "<<endl;
cout<<""<<endl<<endl;
}
void draw()
{
cout<<""<<endl<<endl;
cout<<" I T' S A D R A W ! "<<endl;
cout<<""<<endl<<endl;
}
|
void game() is way too long. Consider refactoring into more manageable, smaller functions.