I'm writing a program for a Tic Tac Toe game. I have it together but it is riddled with errors seemingly all stemming from one piece of code in my header file. The "OnOff" and "gameOnOff(void)" part. I'm not sure what is wrong with this part and errors are not helping much.
Code:
#ifndef TicTacToe_h
#define TicTacToe_h
class TicTacToe
{
public:
TicTacToe();
void displayBoard(void);
bool playerInput(int);
void placeXo(void);
bool goodGrid (int, int);
OnOff gameOnOff(void);
private:
enum OnOff {Winner, Draw, Continue};
int board[ 3 ][ 3 ];
};
#endif
Code:
#include <iostream>
#include <iomanip>
#include "TicTacToe.h"
using namespace std;
TicTacToe::TicTacToe()
{
int x;
int y;
for(x = 0; x < 3; ++x)
for(y = 0; y < 3; ++y)
board[ x ][ y ] = ' ';
}
void TicTacToe::displayBoard(void)
{
int x;
int y;
cout << " 0 1 2\n\n";
for(x = 0; x < 3; ++x)
{
cout << x;
for(y = 0; y < 3; ++y)
{
cout << setw( 3 ) << static_cast < char > (board [ x ][ y ]);
if(y != 2)
cout << " |";
}
if(x != 2)
cout << "\n __|__|__"
<< "\n __|__|__\n";
}
cout << "\n\n";
}
bool TicTacToe::playerInput(int playername)
{
int x;
int y;
do
{
cout << "Player " << static_cast < char > (playername)
<< " Make your move: ";
cin >> x >> y;
cout << '\n';
}
while(!goodGrid(x, y));
board [ x ][ y ] = playername;
displayBoard();
OnOff xoroOnOff = gameOnOff();
if(xoroOnOff == Win)
{
cout << "Player " << static_cast < char > (playername) << " is the winner!\n";
return true;
}
else if(xoroOnOff == Draw)
{
cout << "The game is a draw.\n";
return true;
}
else
return false;
}
void TicTacToe::placeXo(void)
{
displayBoard();
while(true)
{
if(playerInput('X'))
break;
else if( playerInput('O'))
break;
}
}
bool TicTacToe::goodGrid(int x, int y)
{
return x >= 0 && x < 3 && y >= 0 && y < 3 && board [ x ][ y ] == ' ';
}
TicTacToe::OnOff TicTacToe::gameOnOff(void)
{
int a;
int x;
int y;
if(board[ 0 ][ 0 ] != ' ' && board[ 0 ][ 0 ] == board[ 1 ][ 1 ]
&& board[ 0 ][ 0 ] == board[ 2 ][ 2 ])
return Winner;
else if(board[ 2 ][ 0 ] != ' ' && board[ 2 ][ 0 ] == board[ 1 ][ 1 ]
&& board[ 2 ][ 0 ] == board[ 0 ][ 2 ])
return Winner;
for(a = 0; a < 3; ++a)
if(board[ a ][ 0 ] != ' ' && board[ a ][ 0 ] == board[ a ][ 1 ]
&& board[ a ][ 0 ] == board[ a ][ 2 ])
return Winner;
for(a = 0; a < 3; ++a)
if(board[ 0 ][ a ] != ' ' && board[ 0 ][ a ] == board[ 1 ][ a ]
&& board[ 0 ][ a ] == board[ 2 ][ a ])
return Winner;
for(x = 0; x < 3; ++x)
for(y = 0; y < 3; ++y)
if(board [ x ][ y ] = ' ')
return Continue;
return Draw;
}
Code:
#include <iostream>
#include "TicTacToe.h"
using namespace std;
int main()
{
TicTacToe xogame;
xogame.placeXo();
return 0;
}