Olá, estou fazendo um jogo da velha simples pra uma tarefa de casa de Java. A única coisa que está faltando para termina-lo é algo fundamental no jogo, não consigo diferenciar quem foi o primeiro jogador a dar o primeiro clique, sabendo disso ele será o ‘X’ e o segundo jogador será o ‘O’. Por favor se alguém poder me dar uma ideia mais ou menos de como isso pode funcionar, me ajude!
package questao3;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class JogoDaVelha extends JFrame implements ActionListener{
private JButton
botao1,botao2,botao3,botao4,botao5,botao6,botao7,botao8,botao9,botaoNovoJogo;
public JogoDaVelha() {
setTitle("Jogo da Velha");
setSize(330, 420);
setLocationRelativeTo(null);
setLayout(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
botoes();
setVisible(true);
}
public void botoes() {
botao1 = new JButton();
botao1.setFont(new Font("Arial", Font.BOLD, 50));
botao1.setBounds(20, 20, 80, 80);
botao2 = new JButton();
botao2.setFont(new Font("Arial", Font.BOLD, 50));
botao2.setBounds(120, 20, 80, 80);
botao3 = new JButton();
botao3.setFont(new Font("Arial", Font.BOLD, 50));
botao3.setBounds(220, 20, 80, 80);
botao4 = new JButton();
botao4.setFont(new Font("Arial", Font.BOLD, 50));
botao4.setBounds(20, 120, 80, 80);
botao5 = new JButton();
botao5.setFont(new Font("Arial", Font.BOLD, 50));
botao5.setBounds(120, 120, 80, 80);
botao6 = new JButton();
botao6.setFont(new Font("Arial", Font.BOLD, 50));
botao6.setBounds(220, 120, 80, 80);
botao7 = new JButton();
botao7.setFont(new Font("Arial", Font.BOLD, 50));
botao7.setBounds(20, 220, 80, 80);
botao8 = new JButton();
botao8.setFont(new Font("Arial", Font.BOLD, 50));
botao8.setBounds(120, 220, 80, 80);
botao9 = new JButton();
botao9.setFont(new Font("Arial", Font.BOLD, 50));
botao9.setBounds(220, 220, 80, 80);
botaoNovoJogo = new JButton("Novo Jogo");
botaoNovoJogo.setFont(new Font("Arial", Font.BOLD, 20));
botaoNovoJogo.setBounds(80, 320, 150, 40);
add(botao1);
add(botao2);
add(botao3);
add(botao4);
add(botao5);
add(botao6);
add(botao7);
add(botao8);
add(botao9);
add(botaoNovoJogo);
botao1.addActionListener(this);
botao2.addActionListener(this);
botao3.addActionListener(this);
botao4.addActionListener(this);
botao5.addActionListener(this);
botao6.addActionListener(this);
botao7.addActionListener(this);
botao8.addActionListener(this);
botao9.addActionListener(this);
String erro = "Essa posição já foi escolhida! Escolha outra.";
botao1.addActionListener(new ActionListener() {
int cliques = 0;
public void actionPerformed(ActionEvent e) {
cliques ++;
if(cliques > 1) {
JOptionPane.showMessageDialog(null, erro);
}
}
});
botao2.addActionListener(new ActionListener() {
int cliques = 0;
public void actionPerformed(ActionEvent e) {
cliques ++;
if(cliques > 1) {
JOptionPane.showMessageDialog(null, erro);
}
}
});
botao3.addActionListener(new ActionListener() {
int cliques = 0;
public void actionPerformed(ActionEvent e) {
cliques ++;
if(cliques > 1) {
JOptionPane.showMessageDialog(null, erro);
}
}
});
botao4.addActionListener(new ActionListener() {
int cliques = 0;
public void actionPerformed(ActionEvent e) {
cliques ++;
if(cliques > 1) {
JOptionPane.showMessageDialog(null, erro);
}
}
});
botao5.addActionListener(new ActionListener() {
int cliques = 0;
public void actionPerformed(ActionEvent e) {
cliques ++;
if(cliques > 1) {
JOptionPane.showMessageDialog(null, erro);
}
}
});
botao6.addActionListener(new ActionListener() {
int cliques = 0;
public void actionPerformed(ActionEvent e) {
cliques ++;
if(cliques > 1) {
JOptionPane.showMessageDialog(null, erro);
}
}
});
botao7.addActionListener(new ActionListener() {
int cliques = 0;
public void actionPerformed(ActionEvent e) {
cliques ++;
if(cliques > 1) {
JOptionPane.showMessageDialog(null, erro);
}
}
});
botao8.addActionListener(new ActionListener() {
int cliques = 0;
public void actionPerformed(ActionEvent e) {
cliques ++;
if(cliques > 1) {
JOptionPane.showMessageDialog(null, erro);
}
}
});
botao9.addActionListener(new ActionListener() {
int cliques = 0;
public void actionPerformed(ActionEvent e) {
cliques ++;
if(cliques > 1) {
JOptionPane.showMessageDialog(null, erro);
}
}
});
botaoNovoJogo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
new JogoDaVelha();
}
}
);
}
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
String x = "X";
String o = "O";
//for(int i = 0; i < 9; i++) {
//setText(x);
//}
if(obj == botao2) {
botao2.setText("X");
}/*if(obj == botao3) {
botao3.setText("O");
}if(obj == botao4) {
botao4.setText("X");
}if(obj == botao5) {
botao5.setText("X");
}if(obj == botao6) {
botao6.setText("X");
}if(obj == botao7) {
botao7.setText("X");
}if(obj == botao8) {
botao8.setText("X");
}if(obj == botao9) {
botao9.setText("X");
}*/
}
public static void main(String[] args) {
new JogoDaVelha();
}
}
Crie um campo para armazenar o próximo jogador.
Ex.:
char proximoJogador = ‘X’;
Assim que ele jogar, você inverte o valor da variável e repete o processo.
Mantenha uma variável apontando para o jogador atual, após realizar uma jogada você troca o jogador atual.
Exemplo:
import java.awt.Container;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
public class JogoDaVelha extends JFrame {
private static final String JOGADOR_1 = "X";
private static final String JOGADOR_2 = "O";
public static void main(String[] args) {
try {
JogoDaVelha tela = new JogoDaVelha();
tela.setVisible(true);
} catch (Throwable t) {
t.printStackTrace();
}
}
private JButton botao1;
private JButton botao2;
private JButton botao3;
private JButton botao4;
private JButton botao5;
private JButton botao6;
private JButton botao7;
private JButton botao8;
private JButton botao9;
private JButton botaoNovoJogo;
private String jogadorAtual;
public JogoDaVelha() {
setTitle("Jogo da Velha");
setSize(330, 420);
setLocationRelativeTo(null);
setLayout(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inicializarJogo();
}
private void clicouBotao(JButton button, int numero) {
button.setText(jogadorAtual);
button.setEnabled(false);
System.out.println("Clicou no botão " + numero);
trocarJogador();
}
private void inicializarJogo() {
trocarJogador();
Container container = getContentPane();
container.removeAll();
botao1 = new JButton();
container.add(botao1);
botao1.setFont(new Font("Arial", Font.BOLD, 50));
botao1.setBounds(20, 20, 80, 80);
botao2 = new JButton();
container.add(botao2);
botao2.setFont(new Font("Arial", Font.BOLD, 50));
botao2.setBounds(120, 20, 80, 80);
botao3 = new JButton();
container.add(botao3);
botao3.setFont(new Font("Arial", Font.BOLD, 50));
botao3.setBounds(220, 20, 80, 80);
botao4 = new JButton();
container.add(botao4);
botao4.setFont(new Font("Arial", Font.BOLD, 50));
botao4.setBounds(20, 120, 80, 80);
botao5 = new JButton();
container.add(botao5);
botao5.setFont(new Font("Arial", Font.BOLD, 50));
botao5.setBounds(120, 120, 80, 80);
botao6 = new JButton();
container.add(botao6);
botao6.setFont(new Font("Arial", Font.BOLD, 50));
botao6.setBounds(220, 120, 80, 80);
botao7 = new JButton();
container.add(botao7);
botao7.setFont(new Font("Arial", Font.BOLD, 50));
botao7.setBounds(20, 220, 80, 80);
botao8 = new JButton();
container.add(botao8);
botao8.setFont(new Font("Arial", Font.BOLD, 50));
botao8.setBounds(120, 220, 80, 80);
botao9 = new JButton();
container.add(botao9);
botao9.setFont(new Font("Arial", Font.BOLD, 50));
botao9.setBounds(220, 220, 80, 80);
botaoNovoJogo = new JButton("Novo Jogo");
container.add(botaoNovoJogo);
botaoNovoJogo.setFont(new Font("Arial", Font.BOLD, 20));
botaoNovoJogo.setBounds(80, 320, 150, 40);
botao1.addActionListener(event -> clicouBotao((JButton) event.getSource(), 1));
botao2.addActionListener(event -> clicouBotao((JButton) event.getSource(), 2));
botao3.addActionListener(event -> clicouBotao((JButton) event.getSource(), 3));
botao4.addActionListener(event -> clicouBotao((JButton) event.getSource(), 4));
botao5.addActionListener(event -> clicouBotao((JButton) event.getSource(), 5));
botao6.addActionListener(event -> clicouBotao((JButton) event.getSource(), 6));
botao7.addActionListener(event -> clicouBotao((JButton) event.getSource(), 7));
botao8.addActionListener(event -> clicouBotao((JButton) event.getSource(), 8));
botao9.addActionListener(event -> clicouBotao((JButton) event.getSource(), 9));
botaoNovoJogo.addActionListener(event -> inicializarJogo());
}
private void trocarJogador() {
if (jogadorAtual == JOGADOR_2) {
jogadorAtual = JOGADOR_1;
} else {
jogadorAtual = JOGADOR_2;
}
}
}
Obrigado, meu amigo!