CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2007
    Posts
    13

    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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Incrementing through an Enumeration

    Quote 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

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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?

  4. #4
    Join Date
    Dec 2007
    Posts
    13

    Re: Incrementing through an Enumeration

    Quote 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?

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Incrementing through an Enumeration

    I'd think
    Code:
    count = !counter;
    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
  •  





Click Here to Expand Forum to Full Width

Featured