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
|
#include <iostream>
using namespace std;
void locations(int &, int &);
void tables(char [][3], int);
int main()
{
const int ROW = 3;
const int COLUMN = 3;
char table[ROW][COLUMN] = { '*', '*', '*',
'*', '*', '*',
'*', '*', '*'};
int rows, columns;
cout << " *** TIC TAC TOE *** " << endl;
cout << "_____________________\n\n";
tables(table, ROW);
cout << "\n Player 1 is X\n Player 2 is O" << endl;
for(int count = 0; count < 5; count++)
{
if (count < 5)
{
cout << "\n Player 1" << endl;
locations(rows, columns);
table[rows][columns] = 'X';
tables(table, ROW);
if (table[0][0]=='X' && table[0][0]==table[1][1] && table[1][1]==table[2][2]) {cout<<"Player 1 wins!"<<endl;}
if (table[2][0]=='X' && table[2][0]==table[1][1] && table[1][1]==table[0][2]) {cout<<"Player 1 wins!"<<endl;}
if (table[0][0]=='X' && table[0][0]==table[1][0] && table[1][0]==table[2][0]) {cout<<"Player 1 wins!"<<endl;}
if (table[0][1]=='X' && table[0][1]==table[1][1] && table[1][1]==table[2][1]) {cout<<"Player 1 wins!"<<endl;}
if (table[0][2]=='X' && table[0][2]==table[1][2] && table[1][2]==table[2][2]) {cout<<"Player 1 wins!"<<endl;}
if (table[0][0]=='X' && table[0][0]==table[0][1] && table[0][1]==table[0][2]) {cout<<"Player 1 wins!"<<endl;}
if (table[1][0]=='X' && table[1][0]==table[1][1] && table[1][1]==table[1][2]) {cout<<"Player 1 wins!"<<endl;}
if (table[2][0]=='X' && table[2][0]==table[2][1] && table[2][1]==table[2][2]) {cout<<"Player 1 wins!"<<endl;}
}
if (count < 4)
{
cout << "\n Player 2" << endl;
locations(rows, columns);
table[rows][columns] = 'O';
tables(table, ROW);
if (table[0][0]=='O' && table[0][0]==table[1][1] && table[1][1]==table[2][2]) {cout<<"Player 2 wins!"<<endl;}
if (table[2][0]=='O' && table[2][0]==table[1][1] && table[1][1]==table[0][2]) {cout<<"Player 2 wins!"<<endl;}
if (table[0][0]=='O' && table[0][0]==table[1][0] && table[1][0]==table[2][0]) {cout<<"Player 2 wins!"<<endl;}
if (table[0][1]=='O' && table[0][1]==table[1][1] && table[1][1]==table[2][1]) {cout<<"Player 2 wins!"<<endl;}
if (table[0][2]=='O' && table[0][2]==table[1][2] && table[1][2]==table[2][2]) {cout<<"Player 2 wins!"<<endl;}
if (table[0][0]=='O' && table[0][0]==table[0][1] && table[0][1]==table[0][2]) {cout<<"Player 2 wins!"<<endl;}
if (table[1][0]=='O' && table[1][0]==table[1][1] && table[1][1]==table[1][2]) {cout<<"Player 2 wins!"<<endl;}
if (table[2][0]=='O' && table[2][0]==table[2][1] && table[2][1]==table[2][2]) {cout<<"Player 2 wins!"<<endl;}
}
}
return 0;
}
void locations(int &row, int &column)
{
cout << " Enter a row (between 0 and 2): ";
cin >> row;
while(row < 0 || row > 2)
{
cout << "\n You must enter a number from 0 to 2.\n";
cout << " Enter a number for the row: ";
cin >> row;
}
cout << " Enter a column (between 0 and 2): ";
cin >> column;
while(column < 0 || column > 2)
{
cout << "\n You must enter a number from 0 to 2.\n";
cout << " Enter a number for the column: ";
cin >> column;
}
}
void tables(char table[][3], int rows)
{
for(int x = 0; x < rows; x++)
{
for(int y = 0; y < 3; y++)
{
cout << " " << table[x][y] << " | ";
}
cout << endl;
cout << " ------------- " << endl;
}
}
|