I have made a function which should check if a 2048 game is in a game over situation. This is when there are no zeros and no two adjacent numbers that are equal. When in a game over situation it should return zero and otherwise should be false.
The issue is that the code here always returns false and I do not understand why this is. I would appreciate any help:
bool game_over(const std::vector<int>& v){
int length = v.size();
for (int i = 0; i < length; i++){
if (v[i] == 0){
return false;
}
}
for( int i= 0; i < length ; i++ ){
for( int j = 0; j < length; j++){
int x = v[twod_to_oned(i, j, length)];
if (i < length && x == v[twod_to_oned(i+1, j, length )]){
return false;
}
if (j < length && x == v[twod_to_oned(i, j+1, length)]){
return false;
}
}
}
return true;
}
int twod_to_oned(int row, int col, int rowlen){
return row*rowlen+col;
}
The test I used:
int main(){
std::vector<int> v{1,1,1,1};
bool c = game_over(v);
if(c){
std::cout << "true" << std::endl;
}
if(!c){
std::cout << "false" << std::endl;
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire