Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- public class JogoDaVelha {
- 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 JPanel painel;
- private JFrame frame;
- private JLabel labelResultado;
- private JPanel painelResultado;
- private String marcador = "X";
- private int numJogadas = 0;
- public JogoDaVelha() {
- botao1 = new JButton();
- botao2 = new JButton();
- botao3 = new JButton();
- botao4 = new JButton();
- botao5 = new JButton();
- botao6 = new JButton();
- botao7 = new JButton();
- botao8 = new JButton();
- botao9 = new JButton();
- painel = new JPanel(new GridLayout(3, 3));
- painel.add(botao1);
- painel.add(botao2);
- painel.add(botao3);
- painel.add(botao4);
- painel.add(botao5);
- painel.add(botao6);
- painel.add(botao7);
- painel.add(botao8);
- painel.add(botao9);
- frame = new JFrame("Jogo da Velha");
- frame.getContentPane().add(painel);
- frame.setSize(300, 300);
- frame.setVisible(true);
- botao1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- botao1.setText(marcador);
- verificaFimPartida();
- mudaMarcador();
- }
- });
- botao2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- botao2.setText(marcador);
- verificaFimPartida();
- mudaMarcador();
- }
- });
- botao3.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- botao3.setText(marcador);
- verificaFimPartida();
- mudaMarcador();
- }
- });
- botao4.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- botao4.setText(marcador);
- verificaFimPartida();
- mudaMarcador();
- }
- });
- botao5.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- botao5.setText(marcador);
- verificaFimPartida();
- mudaMarcador();
- }
- });
- botao6.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- botao6.setText(marcador);
- verificaFimPartida();
- mudaMarcador();
- }
- });
- botao7.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- botao7.setText(marcador);
- verificaFimPartida();
- mudaMarcador();
- }
- });
- botao8.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- botao8.setText(marcador);
- verificaFimPartida();
- mudaMarcador();
- }
- });
- botao9.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- botao9.setText(marcador);
- verificaFimPartida();
- mudaMarcador();
- }
- });
- }
- private void mudaMarcador() {
- if (marcador.equals("X")) {
- marcador = "O";
- } else {
- marcador = "X";
- }
- }
- private void verificaFimPartida(){
- String b1 = botao1.getText();
- String b2 = botao2.getText();
- String b3 = botao3.getText();
- String b4 = botao4.getText();
- String b5 = botao5.getText();
- String b6 = botao6.getText();
- String b7 = botao7.getText();
- String b8 = botao8.getText();
- String b9 = botao9.getText();
- numJogadas++;
- if (b1.equals(b2) && b1.equals(b3) && b1.equals(marcador)){
- apresentaVitoria();
- }
- else if (b4.equals(b5) && b4.equals(b6) && b4.equals(marcador)){
- apresentaVitoria();
- }
- else if (b7.equals(b8) && b7.equals(b9) && b7.equals(marcador)){
- apresentaVitoria();
- }
- else if (b1.equals(b4) && b1.equals(b7) && b1.equals(marcador)){
- apresentaVitoria();
- }
- else if (b2.equals(b5) && b2.equals(b8) && b2.equals(marcador)){
- apresentaVitoria();
- }
- else if (b3.equals(b6) && b3.equals(b9) && b3.equals(marcador)){
- apresentaVitoria();
- }
- else if (b1.equals(b5) && b1.equals(b9) && b1.equals(marcador)){
- apresentaVitoria();
- }
- else if (b7.equals(b5) && b7.equals(b3) && b7.equals(marcador)){
- apresentaVitoria();
- }
- else if (numJogadas == 9){
- apresentaEmpate();
- }
- }
- private void apresentaVitoria(){
- labelResultado = new JLabel("Fim de Jogo. Vitória do Jogador [ " + marcador + " ]!");
- painelResultado = new JPanel();
- painelResultado.add(labelResultado);
- frame.getContentPane().removeAll();
- frame.getContentPane().add(painelResultado);
- }
- private void apresentaEmpate(){
- labelResultado = new JLabel("Fim de Jogo. Empate!");
- painelResultado = new JPanel();
- painelResultado.add(labelResultado);
- frame.getContentPane().removeAll();
- frame.getContentPane().add(painelResultado);
- }
- public static void main(String[] args) {
- new JogoDaVelha();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement