|
-
March 16th, 2008, 09:30 PM
#1
Incrementing through an Enumeration
I'm using Microdoft Visual C++ 2008 Express Edition, but I'm currently using it to create a console chess application. I have an enumerated type called Player, and a variable of type Player called TURN. I want to be able to use the incement operator on TURN (prefix or postfix, doesn't really matter). Although I think the prefix is easier to overload than the postfix... if, in fact, I have to overload it to make this work (I don't even know if you can overload an operator for an enumeration).
Code:
enum Player { HUMAN, COMPUTER, NO_PLAYER };
Player TURN = HUMAN;
bool MATE = false;
while(!MATE)
{
Move theMove; // Move class used to retrieve and store the user's move
if(TURN == NO_PLAYER)
TURN = HUMAN;
if(TURN == HUMAN) // use TURN to determine if it's human's turn to move
{
while(theMove.isValid() == false)
{
theMove.getMove(); // prompt user to input next move
}
applyMove(theMove); // carry out the move
}
else
{
// have AI determine computer's best next move
}
++TURN; // increment turn
if( . . . ) // test for checkmate
MATE = true;
}
So, how can I do this without getting errors from the compiler? I appreciate your help. Thanks.
-
March 16th, 2008, 09:38 PM
#2
Re: Incrementing through an Enumeration
 Originally Posted by RobotJones
I'm using Microdoft Visual C++ 2008 Express Edition, but I'm currently using it to create a console chess application. I have an enumerated type called Player, and a variable of type Player called TURN. I want to be able to use the incement operator on TURN
Sorry, it cannot be done this way. Enums are not traversable like this.
Regards,
Paul McKenzie
-
March 16th, 2008, 10:22 PM
#3
Re: Incrementing through an Enumeration
You could just pretend Player is an int, and deal with it that way. But this would rather defeat the point of using an enum.
Have you considered a simple class which could overload the ++ operator?
-
March 16th, 2008, 10:43 PM
#4
Re: Incrementing through an Enumeration
 Originally Posted by Lindley
Have you considered a simple class which could overload the ++ operator?
How would you go about writing that? I could make a class that has an overloaded increment operator and a private data member of type int called counter which would be incremented each time a player makes a move. But then I guess I'd have to test for even or odd and assign both players one or the other.
Code:
class TurnClass
{
public:
TurnClass() : counter(0) {}
~TurnClass() {}
const TurnClass& operator++();
Color getTurn()const;
private:
int counter;
} TURN;
const TurnClass& TurnClass::operator++()
{
++counter;
return *this;
}
Color TurnClass::getTurn()const
{
if(counter % 2 == 0)
return WHITE;
else
return BLACK;
}
Is this what you were suggesting?
-
March 17th, 2008, 07:19 AM
#5
Re: Incrementing through an Enumeration
I'd think
would be good enough, actually.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|