Constant Enumerations

Hi, i am making a checkers game..and i have used array of enumerations to declare the states of the board..


enum state {state_black,state_white,state_board_m,state_board_im,state_black_k,state_white_k}; // shows states of pieces on board
// ( white, white_king, black, black_king and the board surface )

state arr[8][8]; // 8 x 8 state type array it takes values : (black,white,board)



My other checkers are declared on the board by the following code..

for(int i=0;i<8;i++) // loop to declare the states in the array (black,white,board)
{
for(int j=0;j<8;j++)
{
if( // conditions to declare black piece
(i==0 && (j==1 || j==3 || j==5 || j==7)) ||
(i==1 && (j==0 || j==2 || j==4 || j==6)) ||
(i==2 && (j==1 || j==3 || j==5 || j==7))
)
{
arr[i][j]=state_black;

}

else if ( // conditions to declare white pieces
(i==5 && (j==0 || j==2 || j==4 || j==6)) ||
(i==6 && (j==1 || j==3 || j==5 || j==7)) ||
(i==7 && (j==0 || j==2 || j==4 || j==6))
)
{
arr[i][j]=state_white;
}

else if (
(i==0 && (j==0 || j==2 || j==4 || j==6)) ||
(i==1 && (j==1 || j==3 || j==5 || j==7)) ||
(i==2 && (j==0 || j==2 || j==4 || j==6)) ||
(i==3 && (j==1 || j==3 || j==5 || j==7)) ||
(i==4 && (j==0 || j==2 || j==4 || j==6)) ||
(i==5 && (j==1 || j==3 || j==5 || j==7)) ||
(i==6 && (j==0 || j==2 || j==4 || j==6)) ||
(i==7 && (j==1 || j==3 || j==5 || j==7))
)
{
arr[i][j]=state_board_im;
}
else // declaring board pieces
arr[i][j]=state_board_m;
}

}



When my checker reaches the opponents baseline it becomes king....the code for that is...



void king_print()
{

for(int i=0;i<1;i++)
for(int j=0;j<8;j++)
if(arr[i][j]==state_white)
{
arr[i][j]=state_white_k;
}

for(int i=7;i<8;i++)
for(int j=0;j<8;j++)
if(arr[i][j]==state_black)
{
arr[i][j]=state_black_k;
}

}





When i move the king checker back to the board it turns back into normal checker....how can i make it a constant king..so that once it becomes king it does not change back no matter where i move it....
When i move the king checker back to the board it turns back into normal checker....how can i make it a constant king..so that once it becomes king it does not change back no matter where i move it....

Your move routine is probably incorrect. I'd guess you're setting the state on the destination field to e.g. state_black instead of the value on the source field.

And your initialization can be done with much less code:
1
2
3
4
5
6
7
8
 for (int i=0;i<8;i++)
 for (int j=0;j<8;j++)
 {
    if (j>=5 && (i+j)%2)arr[i][j]=state_white;
    else if (j<3 && (i+j)%2)arr[i][j]=state_black;
    else if ((i+j)%2==0)arr[i][j]=state_board_im;
    else arr[i][j]=state_board_m;
 }
thanks for the initialization ..... and yes i am changing the state of the field on the basis of the destination ....

what should i do with it if i want to change the state on the basis of value of field ?
arr[destX][destY]=arr[originX][originY]; ?
sorry but i am not getting you ?? i am a newbie so can u please explain it a little in detail ..please!
Just assign the value from the field the piece is currently on to the field you want it to move to.
What's the problem?
i can move it....the problem is that when a normal checker turns to a king it should remain a king ...but when it becomes king when reaching the baseline of the opponent...then after that when i move it back to the other rows of the board it changes back to normal checker which should not happen... i want to know how it should retain being in the king state after being transformed to king...
Topic archived. No new replies allowed.