21
The algorithm teacher asked us to write a code to make a simple old game in Java. And I already have everything ready only I’m not very happy with the solution I found to validate who won the game.
I did a series of if and elseifs to check each condition in which a player can win the game. And all right, that covers what the professor asked for, only I don’t think that’s the smartest way to write an algorithm. I was thinking about using embedded repeats (go inside for) but I can’t find a way where I can apply this to the algorithm, or maybe a lot of thinking I can’t think anymore.
I also thought of creating a matrix 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
holds all possible winning combinations, vector positions char tabuleiro[][] = new char[3][3]
where X or O can win. But I couldn’t find a way to go through the two vectors in order to check on the vector board each condition in the vector condicoesVencer
and now I don’t know what to do.
How do I replace the lot of ifs I did with something 'smart'? Please, if you have an answer, 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;
}
How about posting your code even with lots of Ifs? Can help people interested in helping.
– Caffé
I just posted
– axys93
Related: http://answall.com/q/61351/73
– Luiz Vieira