|
From: Daniel K. O. <dan...@ya...> - 2002-03-25 01:41:28
|
Try putting your includes like this:
#include <iostream.h>
#include <string.h>
And I noticed the identifiers you're using in the enum type:
enum SquareState { blank = ' ' , X = 'X', 0= '0' };
Look: you're using 0 (zero) instead O (letter). Identifiers shouldn't start
with a digit, like 0 (zero).
The same where you're returning zero instead O.
Hope this can help you.
=============================
Daniel K. O.
-----------------------------
Computer Science - UNISC
http://www.unisc.br
=============================
ICQ# 117887205
http://danielko.cjb.net
dan...@ie...
dan...@ya...
-----Mensagem original-----
De: dev...@li...
[mailto:dev...@li...]Em nome de Danny Austin
Enviada em: domingo, 24 de março de 2002 21:56
Para: dev...@li...
Assunto: [Dev-C++] errors
HEY GUYS,
I'm that newbie guy that keeps having trouble. I got a book "C++ for the
absolute beginner," well this beginner doesn't still get it. So I'm
wondering if you can explain what the book is failing to explain. Below is
a program I typed in, that is giving me fits over "enum" and all its
"returns" I feel like I missing something in my lib.'s but I'm not sure. I
have double checked everything several times to the book and from all that I
can see I have typed it out just as the book has it. I take it though one
complier will do things differently than another and I know the author is
using a different complier. So if anyone can help me I'm all ears.
THANKS DANNY!!
//6.5 - Tic Tac Toe
#include <iostream>
#include <string>
using namespace std;
enum SquareState { blank = ' ' , X = 'X', 0= '0' };
class gameBoard
{
private:
const int WIDTH;
const int HEIGHT;
int* GameBoard;
public:
gameBoard() : WIDTH(3), HEIGHT(3)
{
GameBoard = new int[9];
for (int i = 0; i < 9; i++)
*(GameBoard + i) = blank;
}
~gameBoard() {delete[] GameBoard;}
void setX(int h, int w);
void set0(int h, int w);
bool isTaken(int h, int w);
SquareState isLine();
void draw();
};
void gameBoard::setX(int h, int w)
{
*(GameBoard + h*HEIGHT + w) = X;
}
void gameBoard::set0(int h, int w)
{
*(GameBoard + h*HEIGHT + w) = 0;
}
bool gameBoard::isTaken (int h, int w)
{
return *(GameBoard + h*HEIGHT + w) != ' ';
}
SquareState gameBoard::isLine()
{
if(*GameBoard==X && *(GameBoard +1)==X && *(GameBoard +2)==X)
return X;
if(*GameBoard==0 && *(GameBoard +1)==0 && *(GameBoard +2)==0)
return 0;
if(*(GameBoard +3)==X && *(GameBoard +4)==X && *(GameBoard +5)==X)
return X;
if(*(GameBoard +3)==0 && *(GameBoard +4)==0 && *(GameBoard +5)==0)
return 0;
if(*(GameBoard +6)==X && *(GameBoard +7)==X && *(GameBoard +8)==X)
return X;
if(*(GameBoard +6)==0 && *(GameBoard +7)==0 && *(GameBoard +8)==0)
return 0;
if(*GameBoard==X && *(GameBoard +3)==X && *(GameBoard +6)==X)
return X;
if(*GameBoard==0 && *(GameBoard +3)==0 && *(GameBoard +6)==0)
return 0;
if(*(GameBoard +1)==X && *(GameBoard +4)==X && *(GameBoard +7)==X)
return X;
if(*(GameBoard +1)==0 && *(GameBoard +4)==0 && *(GameBoard +7)==0)
return 0;
if(*(GameBoard +2)==X && *(GameBoard +5)==X && *(GameBoard +8)==X)
return X;
if(*(GameBoard +2)==0 && *(GameBoard +5)==0 && *(GameBoard +8)==0)
return 0;
if(*GameBoard==X && *(GameBoard +4)==X && *(GameBoard +8)==X)
return X;
if(*GameBoard==0 && *(GameBoard +4)==0 && *(GameBoard +8)==0)
return 0;
if(*(GameBoard +2)==X && *(GameBoard +4)==X && *(GameBoard +6)==X)
return X;
if(*(GameBoard +2)==0 && *(GameBoard +4)==0 && *(GameBoard +6)==0)
return 0;
return blank;
}
void gameBoard::draw()
{
cout << endl;
for(int i=0; i < HEIGHT; i++)
{
cout << (char)*(GameBoard + i*HEIGHT);
for(int c=1; c < WIDTH; c++)
cout << " | " << (char)*(GameBoard + i*WIDTH + c);
cout << endl << "-------" << endl;
}
}
class Game
{
public:
gameBoard* doInput(string player, gameBoard* gb);
bool inRange(int test);
};
gameBoard* Game::doInput(string player, gameBoard* gb)
{
gb->draw();
string letter;
if (player.compare("one") == 0)
letter = "X";
else if (player.compare("two") == 0)
letter = "0";
else return gb;
int input1, input2;
do {
do {
cout << "\nPlayer " << player.c_str()
<< ", please enter a row number to put an "
<< letter.c_str() << ": ";
cin >> input1;
}while(!inRange(input1));
do {
cout << "\nPlease enter a column number to put an "
<< letter.c_str() << ": ";
cin >> input2;
}while(!inRange(input2));
}while (gb->isTaken(input1,input2));
if (player.compare("one") == 0)
gb->setX(input1, input2);
else gb->set0(input1, input2);
return gb;
}
bool Game::inRange(int test)
{
return test > -1 && test < 3;
)
int main( void )
{
gameBoard* gb = new gameBoard;
Game g;
string player1, player2;
cout << "Welcome to Tic Tac Toe!"
<< "\nPlayer one, please enter your name: ";
cin >> player1;
cout << "\nPlayer two, please enter your name: ";
cin >> player2;
while (gb->isLine() == ' ')
{
gb = g.doInput("one",gb);
gb = g.doInput("two",gb);
}
gb->draw();
if(gb->isLine() == X)
cout << "\nPlayer one, you win!"
<< "\nGame Over.";
else cout << "\nPlayer two, you win!"
<< "\nGame Over.";
return 0;
}
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
|