Hi Guys,
With my TicTac Toe game i'm creating I think i'm nearly there, but have a problem with my constructor.
When I try and run my program I get the message:
return type specification for constructor invalid
in this line:
Code:
int TicTacToe(); //in public
And also another message saying
in function int main () // in CPP file
expected primary-expression before "int"
in these lines:
Code:
game.validMove(int r, int c);
Code:
game.xoMove(int symbol);
Here's my full code, can anyone please advise?:
Thanks
TicTacToe.h
Code:
// TICTACTOE.H
// A Program to run the Tic-Tac-Toe Program
#include <iostream>
using namespace std;
#include <iomanip>
using namespace std;
class TicTacToe
{
private:
enum Status {WIN, DRAW, CONTINUE };
char board [3] [3];
public:
int TicTacToe();
void makeMove ();
void printBoard ();
bool validMove (int r, int c);
bool xoMove (int input);
Status gameStatus ();
void holdscreen ();
};
TicTacToe:: TicTacToe ()
{
int numOfRows;
int numOfCols;
// Initialising TicTacToe //
for (int r = 0; r < numOfRows; r++) //looping rows
for (int c = 0; c < numOfCols; c++) //looping columns
board[r][c] = ' ';
}
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(void)
{
int a;
int r;
int c;
// 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 ** //
}
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;
}
CPP File
Code:
//tictactoe.cpp
// To get tictactoe program running
# include "tictactoe.h"
int main ()
{
TicTacToe game;
game.validMove(int r, int c);
game.gameStatus();
game.printBoard();
game.makeMove();
game.xoMove(int symbol);
}