The algorithm teacher asked us to write a code to play a simple old game in Java. And I already have everything ready, but I'm not too happy with the solution I've got to validate who won the game.
I've done a series of if and elseifs to check each condition in which a player can win the game. And okay, that covers what the teacher asked, but I do not think that's the smartest way to write an algorithm. I was thinking of using embedded reps (for inside for) but I can not find a way where I can apply this in the algorithm, or maybe from thinking so much I can no longer think straight.
I also thought about creating an array with all possible possibilities:
final int[][][] condicoesVencer = {
//COLUNAS
{{ 0, 0 }, { 1, 0 }, { 2, 0 }},
{{ 0, 1 }, { 1, 1 }, { 2, 1 }},
{{ 0, 2 }, { 1, 2 }, { 2, 2 }},
//LINHAS
{{ 0, 0 }, { 0, 1 }, { 0, 2 }},
{{ 1, 0 }, { 1, 1 }, { 1, 2 }},
{{ 2, 0 }, { 2, 1 }, { 2, 2 }},
//DIAGONAIS
{{ 0, 0 }, { 1, 1 }, { 2, 2 }},
{{ 2, 0 }, { 1, 1 }, { 0, 2 }}
};
condicoesVencer
saves all possible combinations of wins, the positions of the char tabuleiro[][] = new char[3][3]
vector where X or O can win. But I did not get a way to traverse the two vectors so as to check in the vector tray each condition in the condicoesVencer
vector and now I do not know what to do.
How to replace the mount of ifs I've done in something more 'smart'? Please, if you have an answer, please send the code and explain.
EDIT: Here's the method I created, with all ifs:
public boolean haGanhador(){
//Checa X verticalmente
if(tabuleiro[0][0] == 'X' && tabuleiro[1][0] == 'X' && tabuleiro [2][0] == 'X'){
System.out.println("'X' VENCEU");
return true;
}
else if(tabuleiro[0][1] == 'X' && tabuleiro[1][1] == 'X' && tabuleiro[2][1] == 'X'){
System.out.println("'X' VENCEU");
return true;
}
else if(tabuleiro[0][2] == 'X' && tabuleiro[1][2] == 'X' && tabuleiro[2][2] == 'X'){
System.out.println("'X' VENCEU");
return true;
}
//Checa X horizontalmente
else if(tabuleiro[0][0] == 'X' && tabuleiro[0][1] == 'X' && tabuleiro[0][2] == 'X'){
System.out.println("'X' VENCEU");
return true;
}
else if(tabuleiro[1][0] == 'X' && tabuleiro[1][1] == 'X' && tabuleiro[1][2] == 'X'){
System.out.println("'X' VENCEU");
return true;
}
else if(tabuleiro[2][0] == 'X' && tabuleiro[2][1] == 'X' && tabuleiro[2][2] == 'X'){
System.out.println("'X' VENCEU");
return true;
}
//Checa X diagonalmente
else if(tabuleiro[0][0] == 'X' && tabuleiro[1][1] == 'X' && tabuleiro[2][2] == 'X'){
System.out.println("'X' VENCEU");
return true;
}
else if(tabuleiro[0][2] == 'X' && tabuleiro[1][1] == 'X' && tabuleiro[2][0] == 'X'){
System.out.println("'X' VENCEU");
return true;
}
//Checa O verticalmente
if(tabuleiro[0][0] == 'O' && tabuleiro[1][0] == 'O' && tabuleiro [2][0] == 'O'){
System.out.println("'O' VENCEU");
return true;
}
else if(tabuleiro[0][1] == 'O' && tabuleiro[1][1] == 'O' && tabuleiro[2][1] == 'O'){
System.out.println("'O' VENCEU");
return true;
}
else if(tabuleiro[0][2] == 'O' && tabuleiro[1][2] == 'O' && tabuleiro[2][2] == 'O'){
System.out.println("'O' VENCEU");
return true;
}
//Checa O horizontalmente
if(tabuleiro[0][0] == 'O' && tabuleiro[0][1] == 'O' && tabuleiro[0][2] == 'O'){
System.out.println("'O' VENCEU");
return true;
}
else if(tabuleiro[1][0] == 'O' && tabuleiro[1][1] == 'O' && tabuleiro[1][2] == 'O'){
System.out.println("'O' VENCEU");
return true;
}
else if(tabuleiro[2][0] == 'O' && tabuleiro[2][1] == 'O' && tabuleiro[2][2] == 'O'){
System.out.println("'O' VENCEU");
return true;
}
//Checa O diagonalmente
if(tabuleiro[0][0] == 'O' && tabuleiro[1][1] == 'O' && tabuleiro[2][2] == 'O'){
System.out.println("'O' VENCEU");
return true;
}
else if(tabuleiro[0][2] == 'O' && tabuleiro[1][1] == 'O' && tabuleiro[2][0] == 'O'){
System.out.println("'O' VENCEU");
return true;
}
return false;
}