1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/* This program will play tic tac toe with the user*/
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
void UserInterface(); //Plays game with user
void initializeBoard(string Barray[15][15]); //Draws starting board
void drawBoard(string Barray[15][15]); //Draws new board after each turn
bool HaveWon(string Barray[15][15], string XorO); //Checks if either
the computer or the user has won
void WinMessage(int i, int j, string XorO, string Barray[15][15]);
//Notifies user that they have won/lost
int main()
{
UserInterface(); //Call user interface
cout<<"Thank you for playing!"<<endl;
}
void UserInterface()
{
int turn, p, q;
string XorO;
char PlayAgain;
PlayAgain='y';
while (PlayAgain=='y')
{
p=0;
q=0;
int i, j;
string Barray[15][15]; //Barray= Board Array
initializeBoard(Barray); //Initialize and display starting playing board
cout<<"Would you like to play as X or O?"<<endl;
cin>>XorO;
while(XorO!="X"&&XorO!="O")
{
cout<<"Please enter either X or O"<<endl;
cin>>XorO;
}
turn=1;
cout<<"What space would you like to go in? Ex:: (3,1) is the bottom
left corner"<<endl;
cout<<""<<endl;
while(turn<9)
{
cout<<"Enter coordinate 1"<<endl;
cin>>i;
cout<<"Enter coordinate 2"<<endl;
cin>>j;
while(i>3||j>3||i<1||j<1) //Check that space entered is on game board
{
cout<<"Please enter numbers between 1 and 3."<<endl;
cout<<""<<endl;
cout<<"Enter coordinate 1"<<endl;
cin>>i;
cout<<"Enter coordinate 2"<<endl;
cin>>j;
}
while(Barray[4*i-1][4*j-1]!=" ") //Check that space entered is not already taken
{
cout<<"Please play in a space that is not already taken"<<endl;
cout<<""<<endl;
cout<<"Enter coordinate 1"<<endl;
cin>>i;
cout<<"Enter coordinate 2"<<endl;
cin>>j;
}
Barray[4*i-1][4*j-1]=XorO;
while(Barray[4*p-1][4*q-1]!=" ")
{
p=rand()%3+1;
q= rand()%3+1;
}
if(XorO=="O" )
{
Barray[4*p-1][4*q-1]='X';
}
else if(XorO=="X" )
{
Barray[4*p-1][4*q-1]='O';
}
drawBoard(Barray); //Display the board after the last turn
if(HaveWon(Barray, XorO)||turn==9)
{
cout<<"Would you like to play again? y/n"<<endl; //allow the user to
play multiple games
cin>>PlayAgain;
turn=9; //ends game by breaking while loop
}
turn++; //move to next turn and repeat while loop
}
}
}
void initializeBoard(string Barray[15][15])
{
int i, j;
for(i=0;i<15;i++) //initialize all variables in array to " "
{
for(j=0;j<15;j++)
{
Barray[i][j]=" ";
}
}
for(i=0; i<15; i++) //Draw tic tac toe board
{
Barray[0][i]="-";
Barray[i][0]="|";
Barray[i][14]="|";
Barray[5][i]="|";
Barray[9][i]="|";
Barray[i][5]="|";
Barray[i][9]="|";
Barray[14][i]="-";
}
for(i=0; i<15; i++)
{
for(j=0; j<15; j++)
{
cout<<Barray[i][j];
}
cout<<endl;
}
}
void drawBoard(string Barray[15][15])
{
int i, j;
for(i=0; i<15; i++)
{
for(j=0; j<15; j++)
{
cout<<Barray[i][j];
}
cout<<endl;
}
}
bool HaveWon(string Barray[15][15], string XorO) //check if user has won
{
int i;
if(Barray[3][3]==Barray[11][11] && Barray[3][3]==Barray[7][7]&&
Barray[3][3]!=" ")
{
WinMessage(3, 3, XorO, Barray); //Send message informing player that
they have won/lost
return true;
}
else if( Barray[3][11]==Barray[7][7] && Barray[3][11]==Barray[11][3]
&& Barray[3][11]!=" " )
{
WinMessage(3, 11, XorO, Barray);
return true;
}
for(i=1;i<4;i++)
{
if(Barray[4*i-1][3]==Barray[4*i-1][7] &&
Barray[4*i-1][3]==Barray[4*i-1][11] && Barray[4*i-1][3]!=" ")
{
WinMessage(4*i-1, 3, XorO, Barray);
return true;
}
else if(Barray[3][4*i-1]==Barray[7][4*i-1] &&
Barray[3][4*i-1]==Barray[11][4*i-1] && Barray[3][4*i-1]!=" ")
{
WinMessage(3, 4*i-1, XorO, Barray);
return true;
}
}
return false;
}
void WinMessage(int i, int j, string XorO, string Barray[15][15])
{
if(Barray[i][j]==XorO)
{
cout<<"Congratulations! You have won."<<endl;
}
else if(Barray[i][j]!=XorO)
{
cout<<"Better luck next time."<<endl;
}
}
|