#include <iostream>
using std :: cin;
using std :: cout;
using std :: endl;
#include <iomanip>
using std :: setw;
const int rows = 3;
const int column = 3;
void PrintTicTacToe( char [][3] ); //The function prototpyes for this program
void p1( char [][3]);
void p2( char [][3]);
bool WinVert( char [][3]);
bool WinHori( char [][3]);
bool WinDiag( char [][3]);
void main() //The start of the main function block.
{
char TicTacToe[rows][column]; //Declaring an array.
for (int i=0; i <rows; i++) //Initializing the array to contain the element '*'.
for (int j=0; j < column; j++)
TicTacToe[i][j] = '*';
PrintTicTacToe(TicTacToe); //Displaying the array elements
p1(TicTacToe); //Excutes the player1 and player2 prompts to key in coordinates for their marks.
p2(TicTacToe); //A preset 9 rounds of prompt.
p1(TicTacToe);
p2(TicTacToe);
p1(TicTacToe);
p2(TicTacToe);
p1(TicTacToe);
p2(TicTacToe);
p1(TicTacToe);
if(WinVert(TicTacToe)) //Checks the array for matching horizontal, vertical or diagonal win.
cout << "vert";
else if(WinHori(TicTacToe))
cout << "Hori";
else if(WinDiag(TicTacToe))
cout << "diag";
else
cout << "Its a Draw.";
} //End of main function block.
void PrintTicTacToe( char tttoe[][3]) //Function parameters for all the prototypes.
{
for(int i=0; i<rows; i++)
for(int j=0; j<column; j++)
{
cout << tttoe[i][j] << (j%3==2 ? '\n' : '|');
if (j == 2 && i <= 1)
cout << "-----\n";
}
}
void p1( char a[][3]) //player1 function parameter.
{
int P1row;
int P1column;
cout << "\nPlayer 1, please key in the row you want place your 'X'(0-2): ";
cin >> P1row;
cout << "Player 1, please key in the column you wish to place your 'X'(0-2): ";
cin >> P1column;
cout << endl;
if((P1row >=0 || P1row < 3) && (P1column >= 0 || P1column <3 ))
{
a[P1row][P1column] = 'X';
PrintTicTacToe(a);
}
else
cout << "Wrong input. Goodbye!";
}
void p2( char a[][3]) //player 2 function prototype.
{
int P2row;
int P2column;
cout << "\nPlayer 2, please key in the row you want place your 'X'(0-2): ";
cin >> P2row;
cout << "Player 2, please key in the column you wish to place your 'X'(0-2): ";
cin >> P2column;
cout << endl;
if( (P2row <= 2 || P2row >= 0) && (P2column <=2 || P2column >=0) )
{
a[P2row][P2column] = 'O';
PrintTicTacToe(a);
}
else
cout << "Wrong input. Goodbye!";
}
bool WinVert( char atoe[][3]) //Boolean data type to check for winning rows/column.
{
return (atoe[0][0] == atoe[0][1] == atoe[0][2] ||
atoe[1][0] == atoe[1][1] == atoe[1][2] ||
atoe[2][0] == atoe[2][1] == atoe[2][2]);
}
bool WinHori( char btoe[][3])
{
return (btoe[0][0] == btoe[1][0] == btoe[2][0] ||
btoe[0][1] == btoe[1][1] == btoe[2][1] ||
btoe[0][2] == btoe[1][2] == btoe[2][2]);
}
bool WinDiag( char ctoe[][3])
{
return (ctoe[0][0] == ctoe[1][1] == ctoe[2][2] ||
ctoe[0][2] == ctoe[1][1] == ctoe[2][0]);
}
pixrix 0 Newbie Poster
Narue 5,707 Bad Cop Team Colleague
pixrix 0 Newbie Poster
iamthwee
pixrix 0 Newbie Poster
sillyboy 43 Practically a Master Poster
pixrix 0 Newbie Poster
iamthwee
pixrix 0 Newbie Poster
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.