You are not logged in.
Hello folks !
I am trying to make a game similar to Tic Tac Toe, but in a n by m grid where the point is to align x tokens of the same type.
I have a problem with my diagonal checking algorithms. It starts from the input case, then goes in a direction and then the other and keeps checking for a possible consecutive sequence of the same type. Here is the code;
//x is the vertical array, y the horizontal. char[][] plat is the game board, and char type is the token type
public static int ligneDiagDroite(char[][] plat, int x, int y, char type){
//set all variables.
boolean continuer=true;
int ligne=0;
int k=y;
int i=x;
//keep going until it cant find more of the same type
while (continuer == true) {
//nlignes returns the number of lines in the array, for further reference, ncolonnes is the number of columns (obv)
if ((k-1)<0 || (i+1)>=nlignes(plat))
continuer=false;
else if (plat[i+1][k-1] != type)
continuer=false;
else if (plat[i+1][k-1] == type){
ligne++;
k--;
i--;
}
}
continuer = true;
k=y;
i=x;
while (continuer == true) {
if ((k+1)>=ncolonnes(plat) || (i-1)<0)
continuer=false;
else if (plat[i-1][k+1] != type)
continuer=false;
else if (plat[i-1][k+1] == type){
ligne++;
i++;
k++;
}
}
return ligne+1;
}
public static int ligneDiagGauche(char[][] plat, int x, int y, char type){
boolean continuer=true;
int ligne=0;
int k=y;
int i=x;
while (continuer == true) {
if ((k-1)<0 || (i-1)<0)
continuer=false;
else if (plat[i-1][k-1] != type)
continuer=false;
else if (plat[i-1][k-1] == type){
ligne++;
k--;
i--;
}
}
continuer = true;
k=y;
i=x;
while (continuer == true) {
if ((i+1)>=nlignes(plat) || (k+1)>=ncolonnes(plat))
continuer=false;
else if (plat[i+1][k+1] != type)
continuer=false;
else if (plat[i+1][k+1] == type){
ligne++;
i++;
k++;
}
}
return ligne+1;
}
Half of it works correctly. The ascending check seems the faulty one. It seems to return the double + 1 of consecutive, or some random number at times.
Your thoughs?
Offline