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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
#include <iostream>
#include <string>
#include <time.h>
#include <conio.h>
using namespace std;
int MAX=4;
char playerInput (char piece, char player1piece, char player2piece, string player1,
string player2, char win, int &player, char input[4][4], int limit);
void board (char input[4][4], int limit);
char test (char win, char input[4][4], int limit);
unsigned char upright=191;
unsigned char botleft=192;
unsigned char botup=193;
unsigned char topdown=194;
unsigned char leftright=195;
unsigned char horiz=196;
unsigned char cross=197;
unsigned char vert=179;
unsigned char rightleft=180;
unsigned char botright=217;
unsigned char upleft=218;
unsigned char beep=7;
unsigned char X=1;
unsigned char s=' ';
int main()
{
char input[4][4], win='n', player1piece, player2piece, piece;
int player=1, x, y, limit=MAX;
string player1, player2;
char repeat='y';
cout <<"Final: Tic Tac Toe by Brandy McCoy\n\n";
cout <<"Player 1, please enter your name: ";
cin >>player1;
cout <<"\nPlayer 2, please enter your name: ";
cin >>player2;
while (repeat=='y' || repeat=='Y')
{
x=1;
while (x<=3)
{
y=1;
while (y<=3)
{
input[x][y]=' ';
y++;
}
x++;
}
player1piece='X';
player2piece='O';
system ("cls");
player=1;
do
{
board (input, limit);
if (player==1 && win=='n')
{
piece=player1piece;
win= playerInput(player1piece, player2piece, piece,
player1, player2, win, player,
input, limit);
player=2;
}
board (input, limit);
if (player==2 && win=='n')
{
piece=player2piece;
win= playerInput(piece, player1piece, player2piece,
player1, player2, win,
player, input, limit);
player=1;
}
board (input, limit);
system ("cls");
}
while (win=='n' || win=='N');
board (input, limit);
if (win=='d')
{
cout <<"The game is a draw.\n";
}
else if (win=='y')
{
if (player==2)
{
cout <<player1<<" wins!";
}
else if (player==1)
{
cout <<player2<<" wins!";
}
}
cout <<"Would you like to play again?\n";
cin >>repeat;
win='n';
}
return 0;
}
char playerInput (char piece, char player1piece, char player2piece, string player1,
string player2, char win, int &player, char input[4][4], int limit)
{
int row, col;
string name;
if (player==1)
{
name=player1;
}
else
{
name=player2;
}
cout <<name<<", please pick a row to put your "<<piece<<" in or 0 to quit: ";
cin >>row;
if (row==0)
{
player=1;
return win='O';
}
cout <<name<<", please pick a column to put your "<<piece<<" in: ";
cin >>col;
system ("cls");
if (input[row][col]==' ')
{
input[row][col]=piece;
board (input, limit);
win=test(win, input, limit);
}
else
{
cout<<"Please try again!";
}
return win;
}
void board(char input[4][4], int limit)
{
system ("cls");
cout <<"Final: Tic Tac Toe by Brandy McCoy\n";
cout <<"Player 1, you are X's.\n";
cout <<"Player 2, you are O's.\n\n";
cout <<" "<<s<<" "<<s<<s<<s<<" "<<s<<s<<s<<" "<<s<<endl<<endl;
cout <<" "<<s<<s<<s<<vert<<s<<s<<s<<vert<<s<<s<<s<<endl;
cout <<" "<<s<<input[1][1]<<s<<vert<<s<<input[2][1]<<s<<vert
<<s<<input[3][1]<<s<<endl;
cout <<" "<<s<<s<<s<<vert<<s<<s<<s<<vert<<s<<s<<s<<endl;
cout <<" "<<horiz<<horiz<<horiz<<cross<<horiz<<horiz<<horiz
<<cross<<horiz<<horiz<<horiz<<endl;
cout <<" "<<s<<s<<s<<vert<<s<<s<<s<<vert<<s<<s<<s<<endl;
cout <<" "<<s<<input[1][2]<<s<<vert<<s<<input[2][2]<<s<<vert
<<s<<input[3][2]<<s<<endl;
cout <<" "<<s<<s<<s<<vert<<s<<s<<s<<vert<<s<<s<<s<<endl;
cout <<" "<<horiz<<horiz<<horiz<<cross<<horiz<<horiz<<horiz
<<cross<<horiz<<horiz<<horiz<<endl;
cout <<" "<<s<<s<<s<<vert<<s<<s<<s<<vert<<s<<s<<s<<endl;
cout <<" "<<s<<input[1][3]<<s<<vert<<s<<input[2][3]<<s<<vert
<<s<<input[3][3]<<s<<endl;
cout <<" "<<s<<s<<s<<vert<<s<<s<<s<<vert<<s<<s<<s<<endl<<endl;
return ;
}
char test(char win, char input[4][4], int limit)
{
int i=1, j=1;
if (input[1][1]!=' ')
{
if (input[1][1]==input[2][1])
{
if (input[1][1]==input[3][1])
return win='y';
}
if (input[1][1]==input[1][2])
{
if (input[1][1]==input[1][3])
return win='y';
}
if(input[1][1]==input[2][2])
{
if (input[1][1]==input[3][3])
return win='y';
}
}
if (input[2][2]!=' ')
{
if (input[2][2]==input[2][1])
{
if (input[2][2]==input[2][3])
return win='y';
}
if (input[2][2]==input[1][2])
{
if (input[2][2]==input[3][2])
return win='y';
}
if(input[2][2]==input[1][3])
{
if (input[2][2]==input[3][1])
return win='y';
}
}
if (input[3][3]!=' ')
{
if (input[3][3]==input[3][2])
{
if (input[3][3]==input[3][1])
return win='y';
}
if (input[3][3]==input[1][3])
{
if (input[3][3]==input[2][3])
{
return win='y';
}
}
}
if (input[1][1]!=' ' && win!='y')
{
int count=0;
for (i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
if (input[i][j]!=' ')
{
count=count++;
}
}
}
if (count==9)
{
return win='d';
}
else
{
return win='n';
}
}
return win='n';
}
| |