0
So guys were here trying to do an exercise of old game in android studio and I can not find where is the error, I would like some help.
package com.example.francisco.jogodavelha;
import android.app.AlertDialog; import
android.content.DialogInterface; import
android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.view.Menu; import android.view.MenuItem; import
android.view.View; import android.widget.Button; import
android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public final int BOLA = 1;
public final int CRUZ = 2;
int turno;
int rodada;
public void VerificarFim() {
int vencedor = 0;
if (bot[0].getValor() == bot[1].getValor() && bot[1].getValor() == bot[2].getValor()) {
vencedor = bot[0].getValor();
} else if (bot[3].getValor() == bot[4].getValor() && bot[4].getValor() == bot[5].getValor()) {
vencedor = bot[3].getValor();
} else if (bot[6].getValor() == bot[7].getValor() && bot[7].getValor() == bot[8].getValor()) {
vencedor = bot[6].getValor();
} else if (bot[0].getValor() == bot[3].getValor() && bot[3].getValor() == bot[6].getValor()) {
vencedor = bot[0].getValor();
} else if (bot[1].getValor() == bot[4].getValor() && bot[4].getValor() == bot[7].getValor()) {
vencedor = bot[1].getValor();
} else if (bot[2].getValor() == bot[4].getValor() && bot[4].getValor() == bot[8].getValor()) {
vencedor = bot[2].getValor();
} else if (bot[0].getValor() == bot[4].getValor() && bot[1].getValor() == bot[2].getValor()) {
vencedor = bot[0].getValor();
} else if (bot[2].getValor() == bot[4].getValor() && bot[4].getValor() == bot[6].getValor()) {
vencedor = bot[2].getValor();
}
if (rodada > 9 && vencedor == 0) {
vencedor = -1;
}
if (vencedor != 0) {
String textovit = "";
if (vencedor == BOLA) {
textovit = "Bola Venceu, ";
} else if (vencedor == CRUZ) {
textovit = "Cruz Venceu, ";
} else {
textovit = "Houve um empate";
}
textovit += "Deseja jogar novamente?";
}
AlertDialog.Buider buider = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Fim de jogo");
buider.setMessage(textovit);
builder.setCancelable(false);
buider.setPositiveButton("Jogar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i<9; i++){
bot[i].getBot().setEnabled(true);
bot[i].getBot().setText("");
bot[i].setValor(0);
}
}
});
builder.setNegativeButton("sair", new DialogInterface.OnClickListener(){
public void onClick (DialogInterface dialog,int which){
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
class Botao {
private Button bot;
private int valor;
public void setValor(int valor) {
this.valor = valor;
}
public Button getBot() {
return this.bot;
}
public int getValor() {
return this.valor;
}
public Botao(Button bot) {
this.bot = bot;
this.valor = 0;
this.bot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getBot().setEnabled(false);
setValor(turno);
if (turno == BOLA) {
getBot().setText("0");
turno = CRUZ;
} else {
getBot().setText("X");
turno = BOLA;
}
rodada++;
VerificarFim();
}
});
}
}
Botao[] bot;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
turno = BOLA;
rodada = 1;
bot = new Botao[9];
bot[0] = new Botao ( (Button) findViewById(R.id.bot1));
bot[1] = new Botao ( (Button) findViewById(R.id.bot2));
bot[2] = new Botao ( (Button) findViewById(R.id.bot3));
bot[3] = new Botao ( (Button) findViewById(R.id.bot4));
bot[4] = new Botao ( (Button) findViewById(R.id.bot5));
bot[5] = new Botao ( (Button) findViewById(R.id.bot6));
bot[6] = new Botao ( (Button) findViewById(R.id.bot7));
bot[7] = new Botao ( (Button) findViewById(R.id.bot8));
bot[8] = new Botao ( (Button) findViewById(R.id.bot9));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
Could you add the full stacktrace? It will be easier to help you.
– Jorge Campos