Conditions de victoire pour un jeu semblable à Connect-4

J’ai un tableau 5×10 qui est rempli avec des valeurs aléatoires 1-5. Je veux être capable de vérifier quand 3 nombres, horizontalement ou verticalement, correspondent. Je ne peux pas trouver un moyen de faire cela sans écrire une tonne de déclarations if.

Voici le code pour le tableau rempli aléatoirement

int i; int rowincrement = 10; int row = 0; int col = 5; int board[10][5]; int randomnum = 5; int main(int argc, char * argv[]) { srand(time(NULL)); cout << "============\n"; while(row < rowincrement) { for(i = 0; i < 5; i++) { board[row][col] = rand()%5 + 1; cout << board[row][col] << " "; } cout << endl; cout << "============\n"; row++; } cout << endl; return 0; } 

Supposons que vous ayez un sharepoint départ particulier (x, y) et que vous soyez curieux de savoir s’il y a trois nombres égaux dans une ligne qui commencent à ce point. Prenons juste le cas où vous regardez dans la direction horizontale. Ensuite, une façon de faire ceci (en ignorant la vérification des liens) serait la suivante:

 bool IsHorizontalMatch(int x, int y) { /* Get the value of the start position. */ const int startValue = board[x][y]; /* Confirm the two values after it match. */ for (int i = 1; i < 3; ++i) if (board[x + i][y] != startValue) return false; /* If we got here, then they all match! */ return true; } 

Vous pouvez également écrire une fonction comme celle-ci pour vérifier verticalement:

 bool IsVerticalMatch(int x, int y) { /* Get the value of the start position. */ const int startValue = board[x][y]; /* Confirm the two values after it match. */ for (int i = 1; i < 3; ++i) if (board[x][y + i] != startValue) return false; /* If we got here, then they all match! */ return true; } 

Et enfin, une pour les diagonales:

 bool IsDiagonalDownMatch(int x, int y) { /* Get the value of the start position. */ const int startValue = board[x][y]; /* Confirm the two values after it match. */ for (int i = 1; i < 3; ++i) if (board[x + i][y + i] != startValue) return false; /* If we got here, then they all match! */ return true; } bool IsDiagonalUpMatch(int x, int y) { /* Get the value of the start position. */ const int startValue = board[x][y]; /* Confirm the two values after it match. */ for (int i = 1; i < 3; ++i) if (board[x + i][y - i] != startValue) return false; /* If we got here, then they all match! */ return true; } 

Cela fonctionne, mais ce n'est tout simplement pas très élégant. ces trois fonctions se ressemblent beaucoup! Heureusement, vous pouvez tous les réécrire en une seule fonction unificasortingce. L'idée est la suivante: si vous remarquez, les trois fonctions fonctionnent en définissant une "taille de pas" indiquant la direction dans laquelle vous vous déplacez. Dans le cas horizontal, le pas est (+1, +0), dans le cas vertical, il est (+0, +1) et dans la diagonale, il est (+1, +1) ou (+1, -1). Compte tenu de cela, vous pouvez écrire une fonction pour vérifier si trois valeurs correspondent dans une ligne:

 bool IsLinearMatch(int x, int y, int stepX, int stepY) { /* Get the value of the start position. */ const int startValue = board[x][y]; /* Confirm the two values after it match. */ for (int i = 1; i < 3; ++i) if (board[x + i * stepX][y + i * stepY] != startValue) return false; /* If we got here, then they all match! */ return true; } 

Vous pouvez alors écrire

 bool IsLineStartingAt(int x, int y) { return (IsLinearMatch(x, y, 1, 0) || // Horizontal IsLinearMatch(x, y, 0, 1) || // Vertical IsLinearMatch(x, y, 1, 1) || // Diagonal Down IsLinearMatch(x, y, 1, -1)); // Diagonal Up } 

Étant donné cette primitive, vous pouvez vérifier tous les correspondances possibles en itérant simplement tous les points de départ possibles.

J'espère que cela t'aides!

EDIT: Merci aux commentateurs d’avoir aidé à corriger mes bugs stupides. 🙂

pour le compte rendu:

je pense que vous voulez dire

 for(i = 0; i < 5; i++) { board[row][i] = rand()%5 + 1; cout << board[row][i] << " "; } 

Et depuis que d'autres ont posté du code, voici comment je le ferais:

 for(int i = 0 ; i < 8 ; i++) { for(int j = 0 ; j < 3 ; j++) { if( ((board[i][j] == board[i][j+1]) && (board[i][j+1] == board[i][j+2]))) std::cout << "Found a horizontal match on " << i << " " << j << std::endl; if((board[i][j] == board[i+1][j]) && (board[i+1][j] == board[i+2][j])) std::cout << "Found a vertical match on " << i << " " << j << std::endl; } } 

Je pense que cela devrait fonctionner. Si quelqu’un signale l’erreur, je serais heureux de corriger.

 for( int row = 0; row<8 ; ++row ) { bool outerLoopBreakFlag = false ; for( int col=0 ; col<3; ++col ) { // check for the winning conditions // ie, board[row][col] == board[row][col+1] == board[row][col+2] // board[row][col] == board[row+1][col] == board[row+2][col] // if any one is satisfied, set the outerLoopBreakFlag to true else break ; } if( outerLoopBreakFlag == true ) break ; }