Meu nome é Bruno e estou tentando fazer esse exercicio como teste para começar a trabalhar com java
nao sou muito experiente com a linguagem e cheguei num ponto que nao sei como fazer gostaria de ver se vc pode me ajudar
peço por gentileza para me ajudar o projeto "jogo" até executa mas ele não está agrupando as característica de um animal
por exemplo na primeira vez que roda o jogo
ele pergunta se vive na agua
se não pergunta qual é o animal?
digamos que vc responde "gato" na proxima tela aparece um complete o gato ______"mia" o macaco nao e volta para o inicio do jogo
caso vc escolhe novamente a opção nao vive na agua
vai aparecer uma pergunto O animal mia? se sim pergunta se é gato se nao pede para informar o animal e sua caracteristica
na terceira vez que executa o programa se optar por nao viver na agua e o animal mia pergunta a caractertica do animal que escolheu anteriormente .... e assim sucessivamente
porem eles vao agrupando as cararcteristicas por exemplo se o animal mia e nao for gato num segundo momento ele aparece só se a opão mia for selecionada ....
falta na verdade agrupar todos os animais com a mesma caracteristica.
mas nao sei o q fazer
package animalarvore;
/**
*
* @author Bruno
*/
public class Animal {
private String nome, caracteristica, Pai;
@Override
public String toString() {
return getNome();
}
@Override
public boolean equals(Object obj) {
return ((Animal) obj).Pai == this.Pai;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getCaracteristica() {
return caracteristica;
}
public void setCaracteristica(String caracteristica) {
this.caracteristica = caracteristica;
}
public String getPai() {
return Pai;
}
public void setPai(String Pai) {
this.Pai = Pai;
}
}
package animalarvore;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
/**
*
* @author Bruno
*/
public class Jogo extends JFrame implements ActionListener {
boolean chaveTerra, ChaveAgua = false;
Jogo() {
JLabel pergunta;
JButton ok;
setTitle("Animais");
setSize(200, 100);
pergunta = new JLabel("Pense em um animal!", JLabel.CENTER);
pergunta.setForeground(Color.GRAY);
ok = new JButton();
ok.setText("Ok");
ok.addActionListener(this);
setLocationRelativeTo(null);
setResizable(false);
getContentPane().setLayout(new GridLayout(2, 1));
getContentPane().setBackground(Color.white);
getContentPane().add(pergunta);
getContentPane().add(ok);
}
public static void main(String[] args) {
JFrame janela = new Jogo();
janela.setVisible(true);
WindowListener x = new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
janela.addWindowListener((x));
}
@Override
public void actionPerformed(ActionEvent e) {
inicio();
}
Animal animal = new Animal();
String TUBARAO = "Tubarão";
String MACACO = "Macaco";
String AGUA = "AGUA", TERRA = "TERRA";
String resposta;
int quantidadeAgua, quantidadeAguaTerra = 0;
ArrayList ListAnimalPaiTerra = new ArrayList();
ArrayList ListaAnimaisTerra = new ArrayList();
ArrayList ListAnimalPaiAgua = new ArrayList();
ArrayList ListAnimalAgua = new ArrayList();
String tipo = "";
String venceu, AnimalEscolhido = "";
String resp, resp1 = "";
public void inicio() {
venceu = "";
resposta = Pergunta("O Animal que você pensou vive na água?");
if (resposta.equals("sim")) {
tipo = AGUA;
AnimalEscolhido = TUBARAO;
ListarAnimais();
tubarao();
} else {
tipo = TERRA;
ListarAnimais();
macaco();
}
}
public void tubarao() {
if (!(ChaveAgua == true)) {
resposta = Pergunta("O Animal que você pensou é um Tubarão?");
if (resposta.equals("sim")) {
vitoria();
ChaveAgua = false;
} else {
Desisto(tipo, TUBARAO);
}
}
}
public void macaco() {
if (!(chaveTerra == true)) {
resposta = Pergunta("O Animal que você pensou é um Macaco?");
if (resposta.equals("sim")) {
vitoria();
chaveTerra = false;
} else {
AnimalEscolhido = MACACO;
Desisto(tipo, MACACO);
}
}
}
public String Pergunta(String Pergunta) {
resposta = null;
int opcoes;
Object[] botoes = {"Sim", "Não"};
opcoes = JOptionPane.showOptionDialog(null, Pergunta, "Confirma",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, botoes, botoes[0]);
if (opcoes == JOptionPane.YES_OPTION) {
resposta = "sim";
} else {
resposta = "não";
}
return resposta;
}
public void Desisto(String tipo, String Animal) {
animal = new Animal();
animal.setNome(JOptionPane.showInputDialog(null, "Qual Animal que você Pensou", "Desisto", 1));
animal.setCaracteristica(JOptionPane.showInputDialog(null, "Um(a) " + animal.getNome() + " ___________ mas um(a) " + Animal + " não?", "Complete!", 1));
animal.setAnimalPai(Animal);
ListAnimalPaiTerra.add(animal);
AnimalEscolhido = animal.getNome();
if (tipo.equals(TERRA)) {
ListAnimalPaiTerra.add(animal);
AnimalEscolhido = animal.getNome();
} else if (tipo.equals(AGUA)) {
ListAnimalPaiAgua.add(animal);
AnimalEscolhido = animal.getNome();
}
}
public void ListarAnimais() {
if (tipo.equals(TERRA)) {
ListaAnimaisTerra = new ArrayList();
animal.setNome(AnimalEscolhido);
for (int i = 0; i < ListaAnimaisTerra.size(); i++) {
if (ListAnimalPaiTerra.equals(ListaAnimaisTerra.get(i)));
System.out.print(ListAnimalPaiTerra.get(i).toString());
}
}
for (int i = 0; i < ListAnimalPaiTerra.size(); i++) {
Animal animalcaracteristica;
animalcaracteristica = (Animal) ListAnimalPaiTerra.get(i);
String Caracteristica = animalcaracteristica.getCaracteristica();
resp = Pergunta("O Animal que você pensou " + Caracteristica + "?");
if (resp.equals("sim")) {
Animal animalnome;
animalnome = (Animal) ListAnimalPaiTerra.get(i);
String nome = animalnome.getNome();
resp1 = Pergunta("O Animal que você pensou é " + nome + "?");
if (resp1.equals("sim")) {
chaveTerra = true;
vitoria();
break;
} else {
Desisto(tipo, nome);
chaveTerra = true;
break;
}
} else {
chaveTerra = false;
}
}
}
public void vitoria() {
JOptionPane.showMessageDialog(null, "Acertei de Novo", "Acertei!", JOptionPane.INFORMATION_MESSAGE);
}
}

