Gostaria de saber se estou fazendo certo para exibir um tabuleiro de jogo da velha.
{
// retorna o tabuleiro em um String, defidamente formatado para apresentacao
String ret = "+----+----+----+\n";
ret += "| " + this.matriz[0][0].toString() + "| " + this.matriz[0][1].toString() + "| " + this.matriz[0][2].toString() +"/n";
ret += "+----+----+----+\n";
ret += "| " + this.matriz[1][0].toString() + "| " + this.matriz[1][1].toString() + "| " + this.matriz[1][2].toString() +"/n";
ret += "+----+----+----+\n";
ret += "| " + this.matriz[1][0].toString() + "| " + this.matriz[1][1].toString() + "| " + this.matriz[1][2].toString() +"/n";
ret += "+----+----+----+\n";
return ret;
Adriana, poderia apresentar com mais detalhes a classe que você está implementando o Tabuleiro?
Posso sim
public class Tabuleiro {
private Marca[][] matriz = new Marca [3][3];
public boolean haMarcaNaPosicao (int lin, int col) throws Exception
{
// verifica se ha marca na posicao indicada por seus parametros, retornando
// true em caso afirmativo, ou false em caso negativo; lanca excecao se seus
// parametros forem invalidos
if(lin<0||lin>2)
throw new Exception("Linha não existe");
if(col<0||col>2)
throw new Exception("Coluna não existe");
for (int i= 0; i<=2; i++)
for (int j= 0 ;j<=2; j++)
if (this.matriz[i][j] != null )
return true;
return false;
}
public Marca getMarcaNaPosicao (int lin, int col) throws Exception
{
// resulta a marca que ha na posicao indicada por seus parametros, retornando-a;
// retorna null, caso ali nao haja nenhuma marca; lanca excecao se seus
// parametros forem invalidos
if(lin<0||lin>2)
throw new Exception("Linha Invalida!");
if(col<0||col>2)
throw new Exception("Coluna Invalida!");
return this.matriz[lin][col];
}
public void setMarcaNaPosicao (Marca mrc, int lin, int col) throws Exception
{
// coloca mrc na posicao indicada por seus parametros; lanca excecao se seus
// parametros forem invalidos
if(lin<0||lin>2)
throw new Exception("Linha Invalida!");
if(col<0||col>2)
throw new Exception("Coluna Invalida!");
this.matriz[lin][col] = mrc;
}
public Marca[][] getMatriz ()
{
return this.matriz;
}
public String toString ()
{
// retorna o tabuleiro em um String, defidamente formatado para apresentacao
String ret = "+----+----+----+\n";
ret+= "| "+this.matriz [0][0].toString() + "| " + this.matriz[0][1].toString() + "| " + this.matriz[0][2].toString() +"/n";
ret+= "+----+----+----+\n";
ret+= "| "+this.matriz[1][0].toString() + "| " + this.matriz[1][1].toString() + "| " + this.matriz[1][2].toString() +"/n";
ret+= "+----+----+----+\n";
ret+= "| "+this.matriz[1][0].toString() + "| " + this.matriz[1][1].toString() + "| " + this.matriz[1][2].toString() +"/n";
ret+= ret + "+----+----+----+\n";
return ret;
}
public boolean equals (Object obj)
{
// verifica se this é igual a obj, retornando true em caso afirmativo,
// ou false em caso negativo
if(this == obj)
return true;
if (obj ==null)
return false;
if(!(obj instanceof Tabuleiro))
return false;
Tabuleiro tab = (Tabuleiro)obj;
for(int i=0; i<this.matriz.length; i++)
if(this.matriz[i]!=tab.matriz[i])
return false;
return true;
}
public int hashCode ()
{
// retorna o hashcode do tabuleiro
int ret=333;
for (int i=0; i<=2;i++)
for (int j=0; j<=2;j++)
ret= ret* 7 + new Character(this.matriz[i][j].getSimbolo()).hashCode();
return ret;
}
}
Olá Adriana,
Teria algumas dicas para te dar, dentre elas:
- Tenta sempre utilizar a classe StringBuilder para concatenação e criação de Strings, ela é mais rápida.
- Tenta sempre deixar seus códigos dinâmicos, se você mudar o tamanho da matriz, o resulto se ajusta ao mesmo.
- Tenta não repetir código
Então com essas dicas cheguei nesse código:
private String criarLinha(String[] conteudo) {
StringBuilder str = new StringBuilder();
for (int j = 0; j < conteudo.length; j++) {
str.append("+-");
str.append(conteudo[j]);
str.append("--");
}
str.append("\n");
return str.toString();
}
private String criarLinhaEmBranco() {
String[] linhaConteudo = new String[this.matriz.length];
for (int i = 0; i < this.matriz.length; i++) {
linhaConteudo[i] = "-";
}
return this.criarLinha(linhaConteudo);
}
@Override
public String toString() {
StringBuilder str = new StringBuilder();
str.append(this.criarLinhaEmBranco());
for (int i = 0; i < this.matriz.length; i++) {
str.append(this.criarLinha(this.matriz[i]));
// str.append("Adicionar algo entre as linhas");
}
str.append(this.criarLinhaEmBranco());
return str.toString();
}
Qualquer coisa só falar.