Using enum in if statement

Hello. The assignment I have to do is to create the beginning parts of the code for a checkers game. In this we need the board class and square class, along with operations in our main function to test the constructors and make sure they work properly. I posted my whole code that I have so far, but the main problem I'm having is with lines 48-66. I'm not sure how to test the square for it's specific color, rank, or square color, so the two functions under that are incomplete. I tried what I thought may work but I'm not 100% clear on enums. Would anybody be able to help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>

enum ChipColors {      // value as int EMPTY == 0 ; RED == 1 ; BLACK == 2
	EMPTY, RED, BLACK
};
enum ChipRanks  {    // value as int NOTKING == 0 ; KING == 1
	NOTKING, KING
};

enum SquareColors   {   // value as int WHITE_SQUARE == 0 ; BLACK_SQUARE == 1
    WHITE_SQUARE, BLACK_SQUARE
};

// Square class defines a square on a checkers board 
class Square    {

    public:
        Square();
        Square(ChipColors cColor, ChipRanks cRank, SquareColors sColor);
        void setColor(ChipColors newColor);
        void setRank(ChipRanks chipRank);
        void setSquareColor(SquareColors squareColor);
        ChipColors getChipColor();
        ChipRanks getChipRank();
        SquareColors getSquareColor();
    private:
        ChipColors chipColor;       // would look like "int chipColor;" if we didn't use enum
        ChipRanks chipRank;
        SquareColors squareColor;
};

// Your square member function code goes here        
Square::Square()    {
    chipColor = EMPTY;
    chipRank = NOTKING;
    squareColor = WHITE_SQUARE;
    }

Square::Square(ChipColors cColor, ChipRanks cRank, SquareColors sColor)  {
    chipColor = cColor;
    chipRank = cRank;
    squareColor = sColor;
}

ChipColors getChipColor()   {
    int chipColor;
    if(ChipColors == EMPTY)  {
        chipColor = 0;
        }
    if(ChipColors == RED)    {
        chipColor = 1;
        }
    if(ChipColors == BLACK)  {
        chipColor = 2;
        }
    return chipColor;
    }
ChipRanks getChipRank() {
    return ChipRanks;
    }
SquareColors getSquareColor()   {
    return SquareColors;
    }


// Board class defines an 8x8 board
class Board {
    
    public:
        void displayBoard();
        void makeMove();
        void changeRank();
        void displayChecker(int, int);
    private:
        void checkIfValidMove();
        void removeCapturedPiece();
        
};


// Your Board member function code goes here        
Board::Board(){
    
   int i;
   int j;
   int boardSize = 8;
   int color;
    
   for (i = 0; i < boardSize; ++i){
       color = (i) % 2;          // Mod 2 - needed to distinguish white and black squares
       for (j = 0; j < boardSize; ++j){
          if ( (j + color) %2 ){
              Board[i][j].setSquareColor(WHITE_SQUARE); 
          }
          else{
              Board[i][j].setSquareColor(BLACK_SQUARE); 
          }
      }
   }
}


// This function is provided for you

// display an 8x8 board of squares like the following:
//
//  +---+---+---+...
//  |***|   |***|
//  |***| x |***|
//  |***|   |***|
//  +---+---+---+...
//
//   where the square with the x in it is a white square and  contains the color (empty, R or B) 
//   and the square with the ***'s indicates a black square on the board

void Board::displayBoard(){
   std::string dashes = "+-----";      //characters for square top and bottom bounds
   std::string spaces = "|     ";      //fill for white squares
   std::string stars = "|*****";       // fill for black squares
   std::string line = "";
   std::string filler;
   int boardSize = 8;
   int i, j;
 
    for (i = 0; i < boardSize; i++){   //repeat 8 times, once for each row of squares 
        
        line="";  
        for(j = 0; j < boardSize; j++){     //build line of cells for boundary  ( +---+ )
            line = line + dashes;
        }
        std::cout << line << "+" << std::endl;
        
        line="";                       
        for (j = 0; j < boardSize; j++){     //build first line for row in squares
            if (Board[i][j].getSquareColor() == WHITE_SQUARE ){
                filler = stars;        // fill stars for white squares    ( |*****|)
            }
            else{
                filler = spaces;        // fill spaces for black squares     ( |     |)
            }
            line = line + filler;
        }

        std::cout << line << "|" << std::endl;
      

        line="";
        for (j = 0; j < boardSize; j++){             //build second line for row of square ( | x | )
            if (Board[i][j].getSquareColor() == WHITE_SQUARE ){
               filler = stars;        // fill stars for white squares    ( |*****|)
            }
            else{
               filler = "|  ";                   // fill spaces for blacksquares
               filler += board[i][j].displayChipColor();   // display color of checker
               filler += "  ";          
          }
          line = line + filler;
        }
        std::cout << line << "|" << std::endl;
      
        line="";
        for (j = 0; j < boardSize; j++){              //build third line for row of square 
            if (Board[i][j].getSquareColor() == WHITE_SQUARE ){
                filler = stars;        // fill stars for WHITE squares   ( |*****| )
            }
            else{
                filler = spaces;        // fill spaces for BLACK squares   ( |     | )
            }
            line = line + filler;
        }
        std::cout << line << "|" << std::endl;
   }
   

        line="";  
        for (j=0; j < boardSize; j++){     //build line of cells for boundary on bottom of board   ( +---+ )
            line = line + dashes;
        }
        std::cout << line << "+" << std::endl;
        
        std::cout << std::endl;

}

void Board::displayChecker(int, int)    {
    std::cout << getChipColor;
    }
    
int main()  {
    
    // declares two square objects
    Square firstSquare;
    Square secondSquare(RED, NOTKING, BLACK_SQUARE);
    
    std::cout << firstSquare.getChipColor() << std::endl;
    std::cout << secondSquare.getChipColor() << std::endl;
    
    // code from lab assignment on lab webpage adjusted
    Square thirdSquare;
    
    // test if constructor set rank to NOTKING
    if(thirdSquare.getChipRank() == NOTKING)    {
        std::cout << "Test Passed - ";
        }
    else    {
        std::cout << "Test Failed - ";
        }
    std::cout << "Value of rank for thirdSquare: " << thirdSquare.getChipRank() << std::endl;
    
    
    // test if constructor set rank to EMPTY
    if(thirdSquare.getChipColor() == EMPTY) {
        std::cout << "Test Passed - ";
        }
    else    {
        std::cout << "Test Failed - ";
        }
    std::cout << "Value of color for thirdSquare: " << thirdSquare.getChipColor() << std::endl;
    
    
    // test if square color is BLACK_SQUARE
    // WHITE_SQUARE color is illegal - checkers only played on black squares
    if(thirdSquare.getSquareColor() == BLACK_SQUARE)    {
        std::cout << "Legal move" << std::endl;
        }
    else    {
        std::cout << "Illegal move" << std::endl;
        }
    
    // outputs square information
    Square fourthSquare(BLACK, NOTKING, BLACK_SQUARE);
    std::cout << fourthSquare.getChipColor() << std::endl;
    std::cout << fourthSquare.getChipRank() << std::endl;
    std::cout << fourthSquare.getSquareColor() << std::endl;
    
}
Last edited on
Are you sure the variable you are using is what you think it is?

enums pollute the namespace as well.

Also consider looking at how switch statements work as a bonus.
a simple example

enum e{red,black}; //red is zero, black is 1
...

if(somevariable == black) //same as if somevariable == 1
somevariable can be any sort of number (it should be an integer type) but you generally want them to be of the type of the enum (named e, in this case).

you can put enums in a namespace. I always recommend putting a max in each one for iteration and sizing reasons:
enum e{red,black, emax}; //red is zero, black is 1
eg:
vector<int> oneforeach(emax);
for(i = red; i < emax; i++)


Well I would need to output the color as the enum, so I want it to return something like:
1
2
3
if(piece = EMPTY)   {
   // return piece as enum so it would output as "0"
}

That's what I'm mostly having trouble with I think. I'm not completely sure how to define the variable I guess either now that I'm looking at what I'm trying to do
EMPTY as you defined it IS zero. Think of "EMPTY" as a const int of value zero.

you don't need whatever you think you need here.
piece == zero == EMPTY.
if you need an enum variable, just say

ChipColors cc = piece; //its zero / EMPTY
or make piece a ChipColors type to begin with.

your if statement is wrong. its an assignment, by the way, use == to compare.

now if you wanted to do something totally different if piece is not zero, then you may need an if statement. if you need an enum variable that isn't piece, you need a value for it when it isn't empty.

Finally, you can cast... something( (ChipColors ) piece ); This may fuss at you, and it may crash if piece is outside the range of the enum. Not sure, I haven't messed with going backwards in a long time; typically you make an enum promote to int but its sort of weird to go the other way in most code.

anyway, consider EMPTY as you would a const int, and see if that helps you write what you need, or ask again if you do not understand.
Last edited on
Topic archived. No new replies allowed.