![]() |
| | #1 |
| Registered User Join Date: Nov 2005
Posts: 25
| Constructor problem 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 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); 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;
}
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);
}
|
| rebel is offline | |
| | #2 |
| Registered User Join Date: Nov 2005
Posts: 85
| constructors dont have return types |
| jlf029 is offline | |
| | #3 | ||
| Registered User Join Date: Aug 2005
Posts: 1,303
| Quote:
Quote:
Kurt | ||
| ZuK is offline | |
| | #4 |
| Registered User Join Date: Nov 2005
Posts: 25
| Thanks Guys, Fixed the first error: But still don't understand the second totally, are you guys saying in the cpp file I can't use (int r, int c) and (int symbol)? Thanks 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);
}
|
| rebel is offline | |
| | #5 |
| Registered User Join Date: Aug 2005
Posts: 1,303
| I'm saying you have to call functions like this Code: game.validMove(r, c); |
| ZuK is offline | |
| | #6 |
| Registered User Join Date: Nov 2005
Posts: 25
| Agggh! I understand now! Thanks guys Trouble is I get get me program to compile, but it does absolutely nothing. Displays a blank screen quickly then disappears! Can anyone please advise as I can't see why it would not work? 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:
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;
}
Code: //tictactoe.cpp
// To get tictactoe program running
# include "tictactoe.h"
#include <cstdlib>
int main ()
{
int r;
int c;
int symbol;
TicTacToe game;
game.validMove(r,c);
game.gameStatus();
game.printBoard();
game.makeMove();
game.xoMove(symbol);
system ("pause");
}
|
| rebel is offline | |
| | #7 |
| Registered User Join Date: Aug 2005
Posts: 1,303
| Try running your program from the commandline. That might give you a hint of what is happening. Kurt |
| ZuK is offline | |
| | #8 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| Also, you should move all your method definitions from the .h file into the .cpp file. Then there's something wrong with your main: Code: int main ()
{
// Declare some variables. Since they're not initialized, their values are pretty much random.
int r;
int c;
int symbol;
// Create the game board. The constructor takes care of initialization.
TicTacToe game;
// Now this is weird. From the name validMove I would guess it takes a row and a column
// index and tests whether placing a stone there is valid. It returns true if it is, and false if
// it isn't.
// That's not what validMove does, though. What it does is ignoring the row and column
// passed and asking the user for coordinates. The peculiar thing is that it asks for
// "the coordinates for O" and those "for X" - when what it reads is actually the row and
// column for a single, unspecified move.
// It then fails to return a value. Didn't the compiler complain about that?
// You seem to be under the wrong impression that upon leaving the function, r and c
// here have the values they got in the function. They don't.
game.validMove(r,c);
// This is where you check the game status. The problem here is that no moves have been
// made yet, so the check is useless. The other problem is that you're ignoring the return
// value. It's like asking how the game is standing, and then not listening to the answer.
game.gameStatus();
// Here you print the map. Looks fine, except for the superfluous cast to char.
game.printBoard();
// Ah, now things get interesting. Here you call a function that contains the game loop.
game.makeMove();
// Nope, not good. This is one more useless call.
game.xoMove(symbol);
system ("pause");
}
Code: int main()
{
TicTacToe game;
game.makeMove();
}
Then it should approximately work.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #9 |
| Registered User Join Date: Nov 2005
Posts: 25
| Thanks, Bit further on now: Trouble I have no errors and despite changing the code ValidMove function which I think was a problem it just loads a blank screen and then jumps out: tictactoe Code: // TICTACTOE.H
// A Program to run the Tic-Tac-Toe Program
#include <iostream>
using namespace std;
#include <iomanip>
class TicTacToe
{
private:
enum 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 ()
{
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)
{
if(board[r][c]==0)
return true;
else
return false;
}
TicTacToe:: Status TicTacToe:: gameStatus(void)
{
// 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;
}
Code: //tictactoe.cpp
// To get tictactoe program running
# include "tictactoe.h"
int main()
{
TicTacToe game;
game.makeMove();
}
|
| rebel is offline | |
| | #10 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| What do you mean "jump out"? Simply ends? Crashes? Any error messages?
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #11 |
| Registered User Join Date: Nov 2005
Posts: 25
| By jumping-out I mean the program compiles, but just shows a black dos box and then it just disappears after about a second. No errors though? |
| rebel is offline | |
| | #12 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| Then do what zuk said. Start a command line (Start->Run "cmd") and navigate to the directory the compiled exe is in. Then just type in the name of the exe. This will make the console stay open and you can see if there's any error message.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #13 |
| Registered User Join Date: Nov 2005
Posts: 25
| Ta Guys, Tried running it in dos, but all it does it wait about a second and then just jumps back to the original command prompt... |
| rebel is offline | |
| | #14 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| Put cout << flush; at the end of printBoard and see if the output changes.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #15 |
| Registered User Join Date: Nov 2005
Posts: 25
| Cheers for the idea corned bee, but still just jumps out. My complier hates me! :-( |
| rebel is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|