How to Make TicTacToe Game In C++

The best way to start Game Development is by making Simple TicTacToe Game.The TicTacToe Code which I am Sharing will help you to get the basic idea of Game Development.This code is for both One Player and Two Players.Tutorial of this code will be available soon..!!


#include <iostream>
#include <fstream>
using namespace std;
void instructions();
void setinitialvalues(char** &game);
void dynamicarray(char** &game);
void printarray(char** &game);
void turnone(int player,char** &game);
void checkresults(int player,string* &result,char** &game);
void printplayerarray(int x,int y,char** &game);
void computer(char** &game);
int main(int argc, char *argv[])
{
char **game;


dynamicarray(game);
setinitialvalues(game);
string player1,player2;
instructions();
int choice;


cout<<"Press 1 to play with the computer:"<<endl;
cout<<"Press 2 to play with the 2nd Player:"<<endl;
cin>>choice;


cout<<"Enter Name Of Player One:";
cin>>player1;
if(choice==2){
cout<<"Enter Name Of Player Two:"
;
cin>>player2;
}
system("cls");
printarray(game);
string *result=new string;
*result="";

while(*result!="decide" *result!="drawn"){

turnone(1,game);
checkresults(1,result,game);
if(*result=="decide"){
cout<<player1<<" won..!!";

break;
}

if(*result=="drawn"){
cout<<"Match Drawn..!!";
break;
}


if(choice==2){ turnone(2,game);
}
if(choice==1){
computer(game);
}
checkresults(2,result,game);
if(*result=="decide"){
if(choice==1){
cout<<"Computer won..!!";
}
if(choice==2){
cout<<player2<<" won..!!";
}
break;
}





}
system("PAUSE");
return EXIT_SUCCESS;
}

void computer(char** &game){

//start code for checking if comp can win

if(game[0][0]=='y' && game[0][1]=='y' && game[0][2]!='x' && game[0][2]!='y'){

game[0][2]='y';
printarray(game);
return;

}
if(game[0][1]=='y' && game[0][2]=='y' && game[0][0]!='x' && game[0][0]!='y'){

game[0][0]='y';
printarray(game);
return;
}
if(game[0][0]=='y' && game[0][2]=='y' && game[0][1]!='x' && game[0][1]!='y'){

game[0][1]='y';
printarray(game);
return;
}
//2

if(game[1][0]=='y' && game[1][1]=='y' && game[1][2]!='x' && game[1][2]!='y'){

game[1][2]='y';
printarray(game);
return;
}
if(game[1][0]=='y' && game[1][2]=='y' && game[1][1]!='y' && game[1][1]!='y'){

game[1][1]='y';
printarray(game);
return;
}
if(game[1][1]=='y' && game[1][2]=='y' && game[1][0]!='x' && game[1][0]!='y'){

game[1][0]='y';
printarray(game);
return;
}


//3


if(game[2][0]=='y' && game[2][1]=='y' && game[2][2]!='x' && game[2][2]!='y'){

game[2][2]='y';
printarray(game);
return;
}
if(game[2][0]=='y' && game[2][2]=='y' && game[2][1]!='x' && game[2][1]!='y'){

game[2][1]='y';
printarray(game);
return;
}
if(game[2][1]=='y' && game[2][2]=='y' && game[2][0]!='x' && game[2][0]!='y'){

game[2][0]='y';
printarray(game);
return;
}


//4

if(game[0][0]=='y' && game[1][0]=='y' && game[2][0]!='x' && game[2][0]!='y'){

game[2][0]='y';
printarray(game);
return;
}
if(game[0][0]=='y' && game[2][0]=='y' && game[1][0]!='x' && game[1][0]!='y'){

game[1][0]='y';
printarray(game);
return;
}
if(game[1][0]=='y' && game[2][0]=='y' && game[0][0]!='x' && game[0][0]!='y'){

game[0][0]='y';
printarray(game);
return;
}

//5



if(game[0][1]=='y' && game[1][1]=='y' && game[2][1]!='x' && game[2][1]!='y'){

game[2][1]='y';
printarray(game);
return;
}
if(game[0][1]=='y' && game[2][1]=='y' && game[1][1]!='x' && game[1][1]!='y'){

game[1][1]='y';
printarray(game);
return;
}
if(game[1][1]=='y' && game[2][1]=='y' && game[0][1]!='x' && game[0][1]!='y'){

game[0][1]='y';
printarray(game);
return;
}
//6


if(game[0][2]=='y' && game[1][2]=='y'&& game[2][2]!='x' && game[2][2]!='y' ){

game[2][2]='y';
printarray(game);
return;
}
if(game[0][2]=='y' && game[2][2]=='y' && game[1][2]!='x' && game[1][2]!='y'){

game[1][2]='y';
printarray(game);
return;
}
if(game[1][2]=='y' && game[2][2]=='y' && game[0][2]!='x' && game[0][2]!='y'){

game[0][2]='y';
printarray(game);
return;
}
//7

if(game[0][0]=='y' && game[1][1]=='y' && game[2][2]!='x' && game[2][2]!='y'){

game[2][2]='y';
printarray(game);
return;
}
if(game[0][0]=='y' && game[2][2]=='y' && game[1][1]!='x' && game[1][1]!='y'){

game[1][1]='y';
printarray(game);
return;
}
if(game[1][1]=='y' && game[2][2]=='y' && game[0][0]!='x' && game[0][0]!='y'){

game[0][0]='y';
printarray(game);
return;
}
//8

if(game[0][2]=='y' && game[1][1]=='y' && game[2][0]!='x' && game[2][0]!='y'){

game[2][0]='y';
printarray(game);
return;
}
if(game[0][2]=='y' && game[2][0]=='y' && game[1][1]!='x' && game[1][1]!='y'){

game[1][1]='y';
printarray(game);
return;
}
if(game[1][1]=='y' && game[2][0]=='y' && game[0][2]!='x' && game[0][2]!='y'){

game[0][2]='y';
printarray(game);
return;
}






//end





if(game[0][0]=='x' && game[0][1]=='x' && game[0][2]!='x' && game[0][2]!='y'){

game[0][2]='y';
printarray(game);
return;

}
if(game[0][1]=='x' && game[0][2]=='x' && game[0][0]!='x' && game[0][0]!='y'){

game[0][0]='y';
printarray(game);
return;
}
if(game[0][0]=='x' && game[0][2]=='x' && game[0][1]!='x' && game[0][1]!='y'){

game[0][1]='y';
printarray(game);
return;
}
//2

if(game[1][0]=='x' && game[1][1]=='x' && game[1][2]!='x' && game[1][2]!='y'){

game[1][2]='y';
printarray(game);
return;
}
if(game[1][0]=='x' && game[1][2]=='x' && game[1][1]!='x' && game[1][1]!='y'){

game[1][1]='y';
printarray(game);
return;
}
if(game[1][1]=='x' && game[1][2]=='x' && game[1][0]!='x' && game[1][0]!='y'){

game[1][0]='y';
printarray(game);
return;
}


//3


if(game[2][0]=='x' && game[2][1]=='x' && game[2][2]!='x' && game[2][2]!='y'){

game[2][2]='y';
printarray(game);
return;
}
if(game[2][0]=='x' && game[2][2]=='x' && game[2][1]!='x' && game[2][1]!='y'){

game[2][1]='y';
printarray(game);
return;
}
if(game[2][1]=='x' && game[2][2]=='x' && game[2][0]!='x' && game[2][0]!='y'){

game[2][0]='y';
printarray(game);
return;
}


//4

if(game[0][0]=='x' && game[1][0]=='x' && game[2][0]!='x' && game[2][0]!='y'){

game[2][0]='y';
printarray(game);
return;
}
if(game[0][0]=='x' && game[2][0]=='x' && game[1][0]!='x' && game[1][0]!='y'){

game[1][0]='y';
printarray(game);
return;
}
if(game[1][0]=='x' && game[2][0]=='x' && game[0][0]!='x' && game[0][0]!='y'){

game[0][0]='y';
printarray(game);
return;
}

//5



if(game[0][1]=='x' && game[1][1]=='x' && game[2][1]!='x' && game[2][1]!='y'){

game[2][1]='y';
printarray(game);
return;
}
if(game[0][1]=='x' && game[2][1]=='x' && game[1][1]!='x' && game[1][1]!='y'){

game[1][1]='y';
printarray(game);
return;
}
if(game[1][1]=='x' && game[2][1]=='x' && game[0][1]!='x' && game[0][1]!='y'){

game[0][1]='y';
printarray(game);
return;
}
//6


if(game[0][2]=='x' && game[1][2]=='x'&& game[2][2]!='x' && game[2][2]!='y' ){

game[2][2]='y';
printarray(game);
return;
}
if(game[0][2]=='x' && game[2][2]=='x' && game[1][2]!='x' && game[1][2]!='y'){

game[1][2]='y';
printarray(game);
return;
}
if(game[1][2]=='x' && game[2][2]=='x' && game[0][2]!='x' && game[0][2]!='y'){

game[0][2]='y';
printarray(game);
return;
}
//7

if(game[0][0]=='x' && game[1][1]=='x' && game[2][2]!='x' && game[2][2]!='y'){

game[2][2]='y';
printarray(game);
return;
}
if(game[0][0]=='x' && game[2][2]=='x' && game[1][1]!='x' && game[1][1]!='y'){

game[1][1]='y';
printarray(game);
return;
}
if(game[1][1]=='x' && game[2][2]=='x' && game[0][0]!='x' && game[0][0]!='y'){

game[0][0]='y';
printarray(game);
return;
}
//8

if(game[0][2]=='x' && game[1][1]=='x' && game[2][0]!='x' && game[2][0]!='y'){

game[2][0]='y';
printarray(game);
return;
}
if(game[0][2]=='x' && game[2][0]=='x' && game[1][1]!='x' && game[1][1]!='y'){

game[1][1]='y';
printarray(game);
return;
}
if(game[1][1]=='x' && game[2][0]=='x' && game[0][2]!='x' && game[0][2]!='y'){

game[0][2]='y';
printarray(game);
return;
}


//tick on
if(game[1][1]!='x' && game[1][1]!='y'){

game[1][1]='y';
printarray(game);
return;
}


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

for(int j=0;j<3;j++){
if(game[i][j]!='x'&&game[i][j]!='y'){
game[i][j]='y';
printarray(game);
return;
}
}







}


void checkresults(int player,string* &result,char** &game){
char symbol;
if(player==1){
symbol='x';
}
if(player==2){
symbol='y';
}


int p=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(game[i][j]=='x' game[i][j]=='y'){
p++;
}
} }

if(p==9){
*result="drawn";
return;
}



if(game[0][0]==symbol && game[0][1]==symbol && game[0][2]==symbol){

*result="decide";
return;
}
if(game[1][0]==symbol && game[1][1]==symbol && game[1][2]==symbol){

*result="decide";
return;
}
if(game[2][0]==symbol && game[2][1]==symbol && game[2][2]==symbol){

*result="decide";
return;
}
if(game[0][0]==symbol && game[1][0]==symbol && game[2][0]==symbol){

*result="decide";
return;
}
if(game[0][1]==symbol && game[1][1]==symbol && game[2][1]==symbol){

*result="decide";
return;
}
if(game[0][2]==symbol && game[1][2]==symbol && game[2][2]==symbol){

*result="decide";
return;
}
if(game[0][0]==symbol && game[1][1]==symbol && game[2][2]==symbol){

*result="decide";
return;
}
if(game[0][2]==symbol && game[1][1]==symbol && game[2][0]==symbol){

*result="decide";
return;
}

}




void printarray(char** &game){
system("cls");


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

cout<<" "<<game[i][j];

}
cout<<endl;


}


}

void turnone(int player,char** &game){
int i=0;
int j=0,z;
cout<<"Player"<<player<<":";
cin>>z;


switch(z)
{
case 1:
if(game[0][0]!='x' && game[0][0]!='y'){
if(player==1){
game[0][0]='x';
}

if(player==2){
game[0][0]='y';
}

}
else{
cout<<"already filled..!!"<<endl;
turnone(player,game);
}
printarray(game);

break;
case 2:
if(game[0][1]!='x' && game[0][1]!='y'){
if(player==1){
game[0][1]='x';
}

if(player==2){
game[0][1]='y';
}
}
else{
cout<<"already filled..!!"<<endl;
turnone(player,game);
}
printarray(game);
break;
case 3:
if(game[0][2]!='x' && game[0][2]!='y'){
if(player==1){
game[0][2]='x';
}

if(player==2){
game[0][2]='y';
}
}
else{
cout<<"already filled..!!"<<endl;
turnone(player,game);
}
printarray(game);
break;
case 4:
if(game[1][0]!='x' && game[1][0]!='y'){
if(player==1){
game[1][0]='x';
}

if(player==2){
game[1][0]='y';
}
}
else{
cout<<"already filled..!!"<<endl;
turnone(player,game);
}
printarray(game);
break;
case 5:
if(game[1][1]!='x' && game[1][1]!='y'){
if(player==1){
game[1][1]='x';
}

if(player==2){
game[1][1]='y';
}
}
else{
cout<<"already filled..!!"<<endl;
turnone(player,game);
}
printarray(game);
break;
case 6:
if(game[1][2]!='x' && game[1][2]!='y'){
if(player==1){
game[1][2]='x';
}

if(player==2){
game[1][2]='y';
}
}
else{
cout<<"already filled..!!"<<endl;
turnone(player,game);
}
printarray(game);
break;
case 7:
if(game[2][0]!='x' && game[2][0]!='y'){
if(player==1){
game[2][0]='x';
}

if(player==2){
game[2][0]='y';
}
}
else{
cout<<"already filled..!!"<<endl;
turnone(player,game);
}
printarray(game);
break;
case 8:
if(game[2][1]!='x' && game[2][1]!='y'){
if(player==1){
game[2][1]='x';
}

if(player==2){
game[2][1]='y';
}
}
else{
cout<<"already filled..!!"<<endl;
turnone(player,game);
}
printarray(game);
break;
case 9:
if(game[2][2]!='x' && game[2][2]!='y'){
if(player==1){
game[2][2]='x';
}

if(player==2){
game[2][2]='y';
}
}
else{
cout<<"already filled..!!"<<endl;
turnone(player,game);
}
printarray(game);
break;
default:
{
cout<<"please enter correct input."<<endl;
turnone(player,game);

}


}





}

void dynamicarray(char** &game){
game=new char*[3];
game[0]=new char[3];
game[1]=new char[3];
game[2]=new char[3];
}

void setinitialvalues(char** &game){

for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
game[i][j]='0';

}

}

}
void instructions(){

int inst[3][3];
int m=1;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
inst[i][j]=m;
m++;
cout<<" "<<inst[i][j];
}


cout<<endl;
}
cout<<"Use the above Number's to tick..!!"<<endl<<endl;
}