This is the source code for Tic tac toe.

Click here to go back to the games page.

Click here to go back to the C++ Programming page.

// Tic tac toe by Emilie Sutterlin
//=============================================================================
#include <conio.h>
#include <constream.h>
#include <dos.h>
#include <graphics.h>
#include <iostream.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//================================Prototypes==================================
const int MAX =10;
void menu (void);
void print_board (int x[MAX]);
int win_move (int x[MAX]);
int block_move (int x[MAX]);
int random_move (int x[MAX]);
int xwin (int x[MAX]);
//===================================Main=====================================
int main (void)
{
textcolor (15); textbackground (0); clrscr();
int n=0,level=2, x[10]= {0,0,0,0,0,0,0,0,0,0};
char ch, ans;
cout<<"WELCOME TO TIC TAC TOE!"<<endl<<"by Emilie Sutterlin"<<endl<<endl;
cout<<"Press S to Start"<<endl
<<"Press H to learn How to play"<<endl
<<"Press Q to Quit"<<endl;

cin>>ch;
if (ch == 27 || ch =='Q'||ch=='q')
{
cout<<"Are you sure you want to quit?(y/n)";
cin>>ans;
if (ans=='y')
exit (0);
else main();
}//end case q
if (ch=='H' ||ch=='h')
{
print_board(x);
cout<<"This is the board. You are X. The Computer is O."
<<endl<<"Press the number of the box in which you want to place your X."
<<endl<<"The object of the game is to get three of your markers if a row."
<<endl<<"They can be diagonal, horizontal, or vertical."
<<endl<<"Level 1 is Easy, Level 2 is Hard.";
getch();
main();
}


cout<<"Which level?"<<endl<<"Level 1"<<endl<<"Level 2";
cin>>level;

print_board(x);

for (n=1;n<MAX;n++)
{
if (n%2==0)
{
if (level==1)x[random_move(x)]-=1;
else
{
int win=win_move(x);//check for winning move
if (win)
{
x[win]-=1;
print_board(x);
cout<<"I won.";
getch();
break;
}
else
{
int block=block_move(x);//check for blocking move
if (block) x[block]-=1;
else x[random_move(x)]-=1;
}//end block or random move
}
}//end computer turn
else
{
int h;
cout<<endl<<"Your turn";
cin>>h;
if (x[h]==0) x[h]+=1;
else
{
cout<<"Sorry, Space is already occupied. Please pick another.";
delay(1000);
n--;
x[0]--;
}
if (xwin(x))//check if human won
{
print_board(x);
cout<<"You win. ";
getch();
break;
}
}//end human turn

x[0]++;
print_board(x);
if (x[0]==9) cout<<"Tie Game";
}//end turn forloop

main();

//---------------------------------------------------------------------------
fflush(stdin); getche (); exit(0);
return (0);
}//endmain
//------------------------------Functions-------------------------------------
int win_move (int x[])
{
for (int n=1;n<=MAX;n++)
{
if (x[n]);
else
{
if ((n==1)&&( (x[5]==-1 && x[9]==-1) || (x[2]==-1&&x[3]==-1) || (x[4]==-1 && x[7]==-1) ) )return (n);
if ((n==2)&&( (x[1]==-1 && x[3]==-1) || (x[5]==-1&&x[8]==-1) ) )return (n);
if ((n==3)&&( (x[1]==-1 && x[2]==-1) || (x[5]==-1&&x[7]==-1) || (x[6]==-1 && x[9]==-1) ) )return (n);
if ((n==4)&&( (x[1]==-1 && x[7]==-1) || (x[5]==-1&&x[6]==-1) ) )return (n);
if ((n==5)&&( (x[1]==-1 && x[9]==-1) || (x[2]==-1&&x[8]==-1) || (x[3]==-1 && x[7]==-1) || (x[4]==-1&&x[5]==-1) ) )return (n);
if ((n==6)&&( (x[3]==-1 && x[9]==-1) || (x[4]==-1&&x[5]==-1) ) )return (n);
if ((n==7)&&( (x[1]==-1 && x[4]==-1) || (x[3]==-1&&x[5]==-1) || (x[8]==-1 && x[9]==-1) ) )return (n);
if ((n==8)&&( (x[2]==-1 && x[5]==-1) || (x[7]==-1&&x[9]==-1) ) )return (n);
if ((n==9)&&( (x[3]==-1 && x[6]==-1) || (x[1]==-1&&x[5]==-1) || (x[7]==-1 && x[8]==-1) ) )return (n);
}
}
return(0);
}

int block_move (int x[])
{
for (int n=1;n<=MAX;n++)
{
if (x[n]);
else
{
if ((n==1)&&( (x[5]==1 && x[9]==1) || (x[2]==1&&x[3]==1) || (x[4]==1 && x[7]==1) ) )return (n);
if ((n==2)&&( (x[1]==1 && x[3]==1) || (x[5]==1&&x[8]==1) ) )return (n);
if ((n==3)&&( (x[1]==1 && x[2]==1) || (x[5]==1&&x[7]==1) || (x[6]==1 && x[9]==1) ) )return (n);
if ((n==4)&&( (x[1]==1 && x[7]==1) || (x[5]==1&&x[6]==1) ) )return (n);
if ((n==5)&&( (x[1]==1 && x[9]==1) || (x[2]==1&&x[8]==1) || (x[3]==1 && x[7]==1) || (x[4]==1&&x[5]==1) ) )return (n);
if ((n==6)&&( (x[3]==1 && x[9]==1) || (x[4]==1&&x[5]==1) ) )return (n);
if ((n==7)&&( (x[1]==1 && x[4]==1) || (x[3]==1&&x[5]==1) || (x[8]==1 && x[9]==1) ) )return (n);
if ((n==8)&&( (x[2]==1 && x[5]==1) || (x[7]==1&&x[9]==1) ) )return (n);
if ((n==9)&&( (x[3]==1 && x[6]==1) || (x[1]==1&&x[5]==1) || (x[7]==1 && x[8]==1) ) )return (n);
}
}
return(0);
}

int random_move (int x[])
{
int n;
for(;;)
{
n=random (9)+1;
if (!x[n]) return(n);
}
}


void print_board (int x[])
{
clrscr();
cout<<"TIC TAC TOE"<<endl;
for (int n=1;n<MAX;n++)
{
if (!x[n])cout<<n;//" ";
if (x[n]==1) cout<<"x";
if (x[n]==-1) cout<<"O";

if (n%3 == 0)
cout<<endl;
}//end forloop
//getch();
}//end print_board

int xwin (int x[MAX])
{
if ( (x[1]==1)&& ( (x[5]==1 && x[9]==1) || (x[2]==1 && x[3]==1) || (x[4]==1 && x[7]==1) ) )return (1);
if ( (x[2]==1)&& (x[5]==1&&x[8]==1) ) return (1);
if ( (x[3]==1)&& ( (x[5]==1 && x[7]==1) || (x[6]==1 && x[9]==1) ) )return (1);
if ( (x[4]==1)&& (x[5]==1&&x[6]==1) ) return (1);
if ( (x[7]==1)&& (x[8]==1&&x[9]==1) ) return (1);
return (0);
}
//-------------------------End of Program by Emilie Sutterlin------------------

Click here to go back to the games page.

Click here to go back to the C++ Programming page.