Hi Guys,
Thanks for your help - much appreciated!
Think i'm getting somewhere now, but am a little stuck. Here's what I have done so far:
Code:
// TICTACTOE.H
// A Program to run the Tic-Tac-Toe Program
#include <iostream.h>
#include <iomanip.h>
class TicTacToe
{
private:
enu Status {WIN, DRAW, CONTINUE };
char board [3] [3];
public:
TicTacToe ();
void makeMove ();
void printBoard ();
bool validMove (int r, int c);
bool xoMove (int input)
Status gameStatus ();
void holdscreen ();
};
TicTacToe:: TicTacToe ()
{
// Initialising TicTacToe //
for (int r = 0; r < r; r++) //looping rows?
for (int c = 0; c < c; c++) //looping columns?
validMove[r][c] = '_';
// ** End TicTacToe ** //
// ** IS THIS SETTING EVERY ELEMENT ON THE BOARD TO A SPACE? ** //
}
bool TicTacToe::validMove (int r, int c)
{
// Validate Move Start for X //
cout << "Enter coordinates for X: ";
cin >> r;
while(true) {
if ((r>=3) || (r<0))
cout<<endl<<"invalid move. try again with a number from 0 to 2: ";
else break;}
// ** End Validate Move Start for X ** //
// Validate Move Start for O //
cout << "Enter coordinates for O: ";
cin >> c;
while(true) {
if ((c>=3) || (c<0))
cout<<endl<<"invalid move. try again with a number from 0 to 2: ";
else break;}
// ** End Validate Move Start for O ** //
}
TicTacToe:: Status TicTacToe:: gameStatus gameStatus(void)
{
int a;
// Check for win in diagonals //
if (board [0][0]!= '_' && board [0][0] == board [1][1]
&& board [0][0] == board [2][2])
return WIN;
else if (board [0][2]!= '_' && board [0][2] == board [1][1]
&& board [0][2] == board [2][0])
return WIN;
//** End Check for win in diagonals ** //
// Check for win in rows
if (board [0][0]!= '_' && board [0][0] == board [1][0]
&& board [0][0] == board [2][0])
return WIN;
else if (board [0][1]!= '_' && board [0][1] == board [1][1]
&& board [0][1] == board [2][2])
return WIN;
else if (board [0][2]!= '_' && board [0][2] == board [1][2]
&& board [0][2] == board [2][2])
return WIN;
//** End Check for win in rows **//
// Check for win in columns
if (board [0][0]!= '_' && board [0][0] == board [0][1]
&& board [0][0] == board [0][2])
return WIN;
else if (board [1][0]!= '_' && board [1][0] == board [1][1]
&& board [1][0] == board [1][2])
return WIN;
else if (board [2][0]!= '_' && board [2][0] == board [2][1]
&& board [2][0] == board [2][2])
return WIN;
// ** End Check for win in columns ** //
// Check for a completed game
{
for(r=0; r < r; r++) {
for(c=0; c < c; C++) {
if(array[r][c] == empty) {
return 0;
}
}
}
return CONTINUE;
}
else
return DRAW; // game is a draw
}
void TicTacToe::printBoard (void)
{
cout << " 0 1 2\n\n";
for ( int r=0; r < 3; ++r ) {
cout << r;
for ( int c=0; c < 3; ++c ) {
cout << setw (3) << static_cast < char > ( board [r] [c] );
if (c !=2 )
cout << " |";
}
if (r != 2)
cout << "\n ______|______|______"
<< "\n ______|______|______\n"
}
cout <<"\n\n";
void TicTacToe::makeMove(void)
{
printBoard();
while ( true ) {
if (xoMove ( 'X' ) )
break;
else if (xoMove ( 'O' ) )
break;
}
}
bool TicTacToe::xoMove (int symbol )
{
int x,y;
do {
cout << "Player" << static_cast < char > (symbol) << " enter move: ";
cin >> x >> y;
cout << '\n';
} while ( !validMove ( x, y ) );
board [x] [y] = symbol;
printBoard();
Status xoStatus = gamestatus();
if ( xoStatus == WIN ) {
cout << "Player " << static_cast < char > (symbol ) << " wins!\n";
return true;
}
else if (xoStatus == DRAW ) {
cout << "Game is a draw. \n";
return true;
}
else //Continue
return false;
}
Now i'm not sure on two bits;
initialising the table
Code:
TicTacToe:: TicTacToe ()
{
// Initialising TicTacToe //
for (int r = 0; r < r; r++) //looping rows?
for (int c = 0; c < c; c++) //looping columns?
validMove[r][c] = '_';
// ** End TicTacToe ** //
// ** IS THIS SETTING EVERY ELEMENT ON THE BOARD TO A SPACE? ** //
}
Would the above be setting every element on the board to a space?
Checking the game status
Code:
TicTacToe:: Status TicTacToe:: gameStatus gameStatus(void)
{
int a;
// Check for win in diagonals //
if (board [0][0]!= '_' && board [0][0] == board [1][1]
&& board [0][0] == board [2][2])
return WIN;
else if (board [0][2]!= '_' && board [0][2] == board [1][1]
&& board [0][2] == board [2][0])
return WIN;
//** End Check for win in diagonals ** //
// Check for win in rows
if (board [0][0]!= '_' && board [0][0] == board [1][0]
&& board [0][0] == board [2][0])
return WIN;
else if (board [0][1]!= '_' && board [0][1] == board [1][1]
&& board [0][1] == board [2][2])
return WIN;
else if (board [0][2]!= '_' && board [0][2] == board [1][2]
&& board [0][2] == board [2][2])
return WIN;
//** End Check for win in rows **//
// Check for win in columns
if (board [0][0]!= '_' && board [0][0] == board [0][1]
&& board [0][0] == board [0][2])
return WIN;
else if (board [1][0]!= '_' && board [1][0] == board [1][1]
&& board [1][0] == board [1][2])
return WIN;
else if (board [2][0]!= '_' && board [2][0] == board [2][1]
&& board [2][0] == board [2][2])
return WIN;
// ** End Check for win in columns ** //
// Check for a completed game
{
for(r=0; r < r; r++) {
for(c=0; c < c; C++) {
if(array[r][c] == empty) {
return 0;
}
}
}
return CONTINUE;
}
else
return DRAW; // game is a draw
}
As i've established by code for checking for a winner in rows and columns is right and i've been helped with the check for the completed game, so ELSE it would be a draw?
Thanks
Reb