0

Estoy creando el videojuego "3 en raya" y soy novato en java. Tengo creado todo y no me da error de sintaxis.Alguien me puede decir por que no me aparece ningun mensaje cuando pongo 3 cruces o circulos seguidos? Dejo aqui mi codigo

public class TicTacToes {

    private JFrame frame;
    private String startGame = "X";
    private int oCount = 0;
    private int xCount = 0;
    private JLabel btn1;
    private JLabel btn2;
    private JLabel btn3;
    private JLabel btn4;
    private JLabel btn5;
    private JLabel btn6;
    private JLabel btn7;
    private JLabel btn8;
    private JLabel btn9;
    private JTextField txtCountX;
    private JTextField txtCountO;



    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TicTacToes window = new TicTacToes();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public TicTacToes() {
        initialize();

    }

    private void GameScore() {
        txtCountX.setText(String.valueOf(xCount++));
        txtCountO.setText(String.valueOf(oCount++));
    }

    private void choosePlayer() {
        if (startGame.equalsIgnoreCase("X")){
            startGame = "O";
        }
        else {
            startGame = "X";
        }
    }

    private void WinningGame() {
        String b1 = btn1.getText();
        String b2 = btn2.getText();
        String b3 = btn3.getText();
        String b4 = btn4.getText();
        String b5 = btn5.getText();
        String b6 = btn6.getText();
        String b7 = btn7.getText();
        String b8 = btn8.getText();
        String b9 = btn9.getText();

        //COMBINACIONES PARA QUE X GANA

    if (b1 == "X" && b2 == "X" && b3 == "X") {
        JOptionPane.showMessageDialog(null, "Jugador con las X gana");

    xCount ++;
    GameScore();

    }

    if (b1 == ("X") && b4 == ("X") && b7 == ("X")) {
        JOptionPane.showMessageDialog(null, "Jugador con las X gana",
                "Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);

    xCount ++;
    GameScore();

    }

    if (b1 == ("X") && b5 == ("X") && b9 == ("X")) {
        JOptionPane.showMessageDialog(null, "Jugador con las X gana",
                "Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);

    xCount ++;
    GameScore();

    }

    //COMBINACIONES PARA QUE O GANA

        if (b1 == ("O") && b2 == ("O") && b3 == ("O")) {
            JOptionPane.showMessageDialog(null, "Jugador con las O gana",
                    "Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);

        xCount ++;
        GameScore();}

        if (b1 == ("O") && b4 == ("O") && b7 == ("O")) {
            JOptionPane.showMessageDialog(null, "Jugador con las O gana",
                    "Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);

        xCount ++;
        GameScore();}

        if (b1 == ("O") && b5 == ("O") && b9 == ("O")) {
            JOptionPane.showMessageDialog(null, "Jugador con las O gana",
                    "Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);

        xCount ++;
        GameScore();

        }



    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 1200, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout(0, 0));

        JPanel panel = new JPanel();
        panel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(new GridLayout(3, 5, 2, 2));

        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_1);
        panel_1.setLayout(new BorderLayout(0, 0));

        JButton btn1 = new JButton("");
        btn1.setFont(new Font("Tahoma", Font.BOLD, 95));
        btn1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                btn1.setText(startGame);
                if (startGame.equalsIgnoreCase("X")) {
                    btn1.setForeground(Color.RED);
                }
                else {
                    btn1.setForeground(Color.BLUE);
                }
                choosePlayer();
                WinningGame();

            }
        });
        panel_1.add(btn1, BorderLayout.CENTER);

        JPanel panel_2 = new JPanel();
        panel_2.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_2);
        panel_2.setLayout(new BorderLayout(0, 0));

        JButton btn2 = new JButton("");
        btn2.setFont(new Font("Tahoma", Font.BOLD, 95));
        btn2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                btn2.setText(startGame);
                if (startGame.equalsIgnoreCase("X")) {
                    btn2.setForeground(Color.RED);
                }
                else {
                    btn2.setForeground(Color.BLUE);
                }
                choosePlayer();
                WinningGame();

            }
        });
        panel_2.add(btn2, BorderLayout.CENTER);

        JPanel panel_3 = new JPanel();
        panel_3.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_3);
        panel_3.setLayout(new BorderLayout(0, 0));

        JButton btn3 = new JButton("");
        btn3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                btn3.setText(startGame);
                if (startGame.equalsIgnoreCase("X")) {
                    btn3.setForeground(Color.RED);
                }
                else {
                    btn3.setForeground(Color.BLUE);
                }
                choosePlayer();
                WinningGame();

            }
        });
        btn3.setFont(new Font("Tahoma", Font.BOLD, 95));
        panel_3.add(btn3, BorderLayout.CENTER);

        JPanel panel_4 = new JPanel();
        panel_4.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_4);
        panel_4.setLayout(new BorderLayout(0, 0));

        JLabel gh = new JLabel("Jugador X:");
        gh.setHorizontalAlignment(SwingConstants.CENTER);
        gh.setFont(new Font("Tahoma", Font.BOLD, 40));
        panel_4.add(gh, BorderLayout.CENTER);

        JPanel panel_5 = new JPanel();
        panel_5.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_5);
        panel_5.setLayout(new BorderLayout(0, 0));

        txtCountX = new JTextField();
        txtCountX.setHorizontalAlignment(SwingConstants.CENTER);
        txtCountX.setFont(new Font("Tahoma", Font.BOLD, 75));
        txtCountX.setText("0");
        panel_5.add(txtCountX, BorderLayout.CENTER);
        txtCountX.setColumns(10);

        JPanel panel_6 = new JPanel();
        panel_6.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_6);
        panel_6.setLayout(new BorderLayout(0, 0));

        JButton btn4 = new JButton("");
        btn4.setFont(new Font("Tahoma", Font.BOLD, 95));
        btn4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                btn4.setText(startGame);
                if (startGame.equalsIgnoreCase("X")) {
                    btn4.setForeground(Color.RED);
                }
                else {
                    btn4.setForeground(Color.BLUE);
                }
                choosePlayer();
                WinningGame();

            }
        });
        panel_6.add(btn4, BorderLayout.CENTER);

        JPanel panel_7 = new JPanel();
        panel_7.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_7);
        panel_7.setLayout(new BorderLayout(0, 0));

        JButton btn5 = new JButton("");
        btn5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                btn5.setText(startGame);
                if (startGame.equalsIgnoreCase("X")) {
                    btn5.setForeground(Color.RED);
                }
                else {
                    btn5.setForeground(Color.BLUE);
                }
                choosePlayer();
                WinningGame();

            }
        });
        btn5.setFont(new Font("Tahoma", Font.BOLD, 95));
        panel_7.add(btn5, BorderLayout.CENTER);

        JPanel panel_8 = new JPanel();
        panel_8.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_8);
        panel_8.setLayout(new BorderLayout(0, 0));

        JButton btn6 = new JButton("");
        btn6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                btn6.setText(startGame);
                if (startGame.equalsIgnoreCase("X")) {
                    btn6.setForeground(Color.RED);
                }
                else {
                    btn6.setForeground(Color.BLUE);
                }
                choosePlayer();
                WinningGame();

            }
        });
        btn6.setFont(new Font("Tahoma", Font.BOLD, 95));
        panel_8.add(btn6, BorderLayout.CENTER);

        JPanel panel_9 = new JPanel();
        panel_9.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_9);
        panel_9.setLayout(new BorderLayout(0, 0));

        JLabel lbPlayerO = new JLabel("Jugador O:");
        lbPlayerO.setHorizontalAlignment(SwingConstants.CENTER);
        lbPlayerO.setFont(new Font("Tahoma", Font.BOLD, 40));
        panel_9.add(lbPlayerO, BorderLayout.CENTER);

        JPanel panel_10 = new JPanel();
        panel_10.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_10);
        panel_10.setLayout(new BorderLayout(0, 0));

        txtCountO = new JTextField();
        txtCountO.setHorizontalAlignment(SwingConstants.CENTER);
        txtCountO.setText("0");
        txtCountO.setFont(new Font("Tahoma", Font.BOLD, 75));
        panel_10.add(txtCountO, BorderLayout.CENTER);

        txtCountO = new JTextField();
        txtCountO.setText("0");
        txtCountO.setHorizontalAlignment(SwingConstants.CENTER);
        txtCountO.setFont(new Font("Tahoma", Font.BOLD, 75));
        panel_10.add(txtCountO, BorderLayout.CENTER);
        txtCountO.setColumns(10);
        txtCountO.setColumns(10);

        JPanel panel_11 = new JPanel();
        panel_11.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_11);
        panel_11.setLayout(new BorderLayout(0, 0));

        JButton btn7 = new JButton("");
        btn7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                btn7.setText(startGame);
                if (startGame.equalsIgnoreCase("X")) {
                    btn7.setForeground(Color.RED);
                }
                else {
                    btn7.setForeground(Color.BLUE);
                }
                choosePlayer();
                WinningGame();

            }
        });
        btn7.setFont(new Font("Tahoma", Font.BOLD, 95));
        panel_11.add(btn7, BorderLayout.CENTER);

        JPanel panel_12 = new JPanel();
        panel_12.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_12);
        panel_12.setLayout(new BorderLayout(0, 0));

        JButton btn8 = new JButton("");
        btn8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                btn8.setText(startGame);
                if (startGame.equalsIgnoreCase("X")) {
                    btn8.setForeground(Color.RED);
                }
                else {
                    btn8.setForeground(Color.BLUE);
                }
                choosePlayer();
                WinningGame();
            }
        });
        btn8.setFont(new Font("Tahoma", Font.BOLD, 95));
        panel_12.add(btn8, BorderLayout.CENTER);

        JPanel panel_13 = new JPanel();
        panel_13.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_13);
        panel_13.setLayout(new BorderLayout(0, 0));

        JButton btn9 = new JButton("");
        btn9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                btn9.setText(startGame);
                if (startGame.equalsIgnoreCase("X")) {
                    btn9.setForeground(Color.RED);
                }
                else {
                    btn9.setForeground(Color.BLUE);
                }
                choosePlayer();
                WinningGame();
            }
        });
        btn9.setFont(new Font("Tahoma", Font.BOLD, 95));
        panel_13.add(btn9, BorderLayout.CENTER);

        JPanel panel_14 = new JPanel();
        panel_14.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_14);
        panel_14.setLayout(new BorderLayout(0, 0));

        JButton btnReset = new JButton("Reset");
        btnReset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn1.setText(null);
                btn2.setText(null);
                btn3.setText(null);
                btn4.setText(null);
                btn5.setText(null);
                btn6.setText(null);
                btn7.setText(null);
                btn8.setText(null);
                btn9.setText(null);
            }
        });
        btnReset.setFont(new Font("Tahoma", Font.BOLD, 50));
        panel_14.add(btnReset, BorderLayout.CENTER);

        JPanel panel_15 = new JPanel();
        panel_15.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_15);
        panel_15.setLayout(new BorderLayout(0, 0));

        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame = new JFrame("Exit");
                if (JOptionPane.showConfirmDialog(frame, "¿Quieres salir del juego?", "Tic Tac Toe",
                        JOptionPane.YES_NO_OPTION)== JOptionPane.YES_NO_OPTION) {

                    System.exit(0);
                }

            }
        });
        btnExit.setFont(new Font("Tahoma", Font.BOLD, 50));
        panel_15.add(btnExit, BorderLayout.CENTER);
    }

}
4
  • que mensaje deberia aparecer??? en donde de todo este codigo esta eso?
    – gbianchi
    Commented el 16 mar. 2020 a las 19:01
  • Cuando se pone 3 x seguidas, deberia aparecer el mensaje "Jugador con las X gana ", no? lo pone cuando pongo las combinaciones b1 == "X", b3 == "X"...
    – Gonzalo
    Commented el 16 mar. 2020 a las 19:08
  • Fijate que de todo tu codigo, no se entiende como va. trata de dejar solo lo importante del mismo.. y otra cosa, las condiciones para ganar en ta te ti no son 3.. son 8...
    – gbianchi
    Commented el 16 mar. 2020 a las 19:12
  • Lo importante del codigo es el metodo WinningGame, con el quiero que si pulso 3 botones en linea poniendo en ellos la X me salga un mensaje donde diga que has ganado. Sin embargo parece que nunca se llega a cumplir la condicion y por tanto no aparece ningun mensaje.
    – Gonzalo
    Commented el 16 mar. 2020 a las 19:22

1 respuesta 1

0
  1. Es tremendamente confuso y para nada recomendado cuando propones unos JLabel y JButton con el mismo nombre! (btn1 ... btn9).
  2. Siempre las comparaciones de igualdad del tipo de dato String se efectúan a través del método equals() no con ==.

    if(b1 == ("O") && b5 == ("O") && b9 == ("O"))//Incorrecto!
    
    if(b1.equals("O") && b5.equals("O") && b9.equals("O"))//Manera correcta
    

Ocurre que los JLabel en ningún momento estan siendo instanciados, no existen, por ende en el método WinningGame() se obtiene el NullPointerException.

Ya que usas JButton para registrar los clicks pues entonces trabajemos con ellos cambiándoles el scope para que sean globales y no que solo queden a nivel del método initialize():

/*private JLabel btn1;
private JLabel btn2;
private JLabel btn3;
private JLabel btn4;
private JLabel btn5;
private JLabel btn6;
private JLabel btn7;
private JLabel btn8;
private JLabel btn9;*/
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
private JButton btn5;
private JButton btn6;
private JButton btn7;
private JButton btn8;
private JButton btn9;
private JTextField txtCountX;
private JTextField txtCountO;
//...
//...
private void initialize() {
 //...
 btn1=new JButton("");
 btn1.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
     btn1.setText(startGame);
     if (startGame.equalsIgnoreCase("X")) {
       btn1.setForeground(Color.RED);
     } else {
       btn1.setForeground(Color.BLUE);
     }
     choosePlayer();
     WinningGame();
   }
  });
  panel_1.add(btn1, BorderLayout.CENTER);
  //demas botones demas atributos...
}

He probado y camina bien; de resto solo queda agregar las condiciones que hacen falta para determinar si gana X u O.

2
  • Muchisimas gracuas johnnyTB, soy principiante en java y me cuesta un poquillo. Un saludo :D
    – Gonzalo
    Commented el 17 mar. 2020 a las 8:14
  • Muchisimas gracuas johnnyTB, soy principiante en java y me cuesta un poquillo. Un saludo :D
    – Gonzalo
    Commented el 17 mar. 2020 a las 8:14

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.