Partage
  • Partager sur Facebook
  • Partager sur Twitter

Exception in thread "AWT-EventQueue-0" java.lang.N

    30 mars 2020 à 8:58:08

    Bonjour,

    j'ai réaliser un tic tac toe mais j'ai une erreur que je n'arrive pas a corriger pouvez vous m'aidez s'il vous plait.

    P.S : j'ai cherché sur internet mais je n'ai rien trouver qui ai pu solutionner mon probleme.

    voici le code 

    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import java.awt.BorderLayout;
    import javax.swing.JPanel;
    import javax.swing.border.LineBorder;
    import java.awt.Color;
    import java.awt.GridLayout;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Font;
    import javax.swing.SwingConstants;
    import javax.swing.JTextField;
    
    public class TicTacToe {
    
    	private JFrame frame;
    	private String startGame = "X";
    	private int xCount = 0;
    	private int oCount = 0;
    	
    	private JLabel button1;
    	private JLabel button2;
    	private JLabel button3;
    	private JLabel button4;
    	private JLabel button5;
    	private JLabel button6;
    	private JLabel button7;
    	private JLabel button8;
    	private JLabel button9;
    	private JTextField txtCountX;
    	private JTextField txtCountO;
    	
    	
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					TicTacToe window = new TicTacToe();
    					window.frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the application.
    	 */
    	public TicTacToe() {
    		initialize();
    		
    		
    	}
    	////////////////////////////////METHODES/////////////////////////////////////////////////////
    	/**
    	 * 
    	 */
    	
    	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 = button1.getText();
    		String b2 = button2.getText();
    		String b3 = button3.getText();
    		String b4 = button4.getText();
    		String b5 = button5.getText();
    		String b6 = button6.getText();
    		String b7 = button7.getText();
    		String b8 = button8.getText();
    		String b9 = button9.getText();
    		
    //////////////////////////////////////////Player X conditions wins/////////////////////////////////////////////////
    		
    		if (b1 ==("X") && b2 ==("X") && b3 ==("X")) {
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER X wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			xCount++;
    			gameScore();
    		}
    		
    		if (b4 ==("X") && b5 ==("X") && b6 ==("X")) {
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER X wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			xCount++;
    			gameScore();
    		}
    		
    		if (b7 ==("X") && b8 ==("X") && b9 ==("X")) {
    			
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER X wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			xCount++;
    			gameScore();
    		}
    		
    		if (b1 ==("X") && b4 ==("X") && b7 ==("X")) {
    			
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER X wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			xCount++;
    			gameScore();
    		}
    		
    		if (b2 ==("X") && b5 ==("X") && b8 ==("X")) {
    			
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER X wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			xCount++;
    			gameScore();
    		}
    		
    		if (b3 ==("X") && b6 ==("X") && b9 ==("X")) {
    			
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER X wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			xCount++;
    			gameScore();
    		}
    		
    		if (b1 ==("X") && b5 ==("X") && b9 ==("X")) {
    			
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER X wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			xCount++;
    			gameScore();
    		}
    		
    		if (b3 ==("X") && b5 ==("X") && b7 ==("X")) {
    			
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER X wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			xCount++;
    			gameScore();
    		}
    		
    		//////////////////////////////////////////Player O condition de wins/////////////////////////////////////////////////
    		
    		
    		if (b1 ==("O") && b2 ==("O") && b3 ==("O")) {
    			
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER O wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			oCount++;
    			gameScore();
    		}
    		
    		if (b4 ==("O") && b5 ==("O") && b6 ==("O")) {
    			
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER O wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			oCount++;
    			gameScore();
    		}
    		
    		if (b7 ==("O") && b8 ==("O") && b9 ==("O")) {
    			
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER O wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			oCount++;
    			gameScore();
    		}
    		
    		if (b1 ==("O") && b4 ==("O") && b7 ==("O")) {
    			
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER O wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			oCount++;
    			gameScore();
    		}
    		
    		if (b2 ==("O") && b5 ==("O") && b8 ==("O")) {
    			
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER O wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			oCount++;
    			gameScore();
    		}
    		
    		if (b3 ==("O") && b6 ==("O") && b9 ==("O")) {
    			
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER O wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			oCount++;
    			gameScore();
    		}
    		
    		if (b1 ==("O") && b5 ==("O") && b9 ==("O")) {
    			
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER O wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			oCount++;
    			gameScore();
    		}
    		
    		if (b3 ==("O") && b5 ==("O") && b7 ==("O")) {
    			
    			
    			JOptionPane.showMessageDialog(frame,"PLAYER O wins", "Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			oCount++;
    			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 button1 = new JButton("");
    		button1.setFont(new Font("Tahoma", Font.BOLD, 40));
    		button1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				button1.setText(startGame);
    				if (startGame.equalsIgnoreCase("X"))
    				{
    					button1.setForeground(Color.blue);
    				}
    				else
    				{
    					button1.setForeground(Color.red);
    				}
    				
    				choosePlayer();
    				winningGame();
    			}
    		});
    		panel_1.add(button1, 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 button2 = new JButton("");
    		button2.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				
    				
    				button2.setText(startGame);
    				if (startGame.equalsIgnoreCase("X"))
    				{
    					button2.setForeground(Color.blue);
    				}
    				else
    				{
    					button2.setForeground(Color.red);
    				}
    				
    				choosePlayer();
    				winningGame();
    			}
    		});
    		
    		
    		button2.setFont(new Font("Tahoma", Font.BOLD, 40));
    		panel_2.add(button2, 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 button3 = new JButton("");
    		button3.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				
    				button3.setText(startGame);
    				if (startGame.equalsIgnoreCase("X"))
    				{
    					button3.setForeground(Color.blue);
    				}
    				else
    				{
    					button3.setForeground(Color.red);
    				}
    				
    				choosePlayer();
    				winningGame();
    			}
    		});
    		button3.setFont(new Font("Tahoma", Font.BOLD, 40));
    		panel_3.add(button3, 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 lblPlayerX = new JLabel("Player X:");
    		lblPlayerX.setFont(new Font("Tahoma", Font.BOLD, 40));
    		panel_4.add(lblPlayerX, 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, 40));
    		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 button4 = new JButton("");
    		button4.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				button4.setText(startGame);
    				if (startGame.equalsIgnoreCase("X"))
    				{
    					button4.setForeground(Color.blue);
    				}
    				else
    				{
    					button4.setForeground(Color.red);
    				}
    				
    				choosePlayer();
    				winningGame();
    			}
    		});
    		button4.setFont(new Font("Tahoma", Font.BOLD, 40));
    		panel_6.add(button4, 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 button5 = new JButton("");
    		button5.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				
    				button5.setText(startGame);
    				if (startGame.equalsIgnoreCase("X"))
    				{
    					button5.setForeground(Color.blue);
    				}
    				else
    				{
    					button5.setForeground(Color.red);
    				}
    				
    				choosePlayer();
    				winningGame();
    			}
    		});
    		button5.setFont(new Font("Tahoma", Font.BOLD, 40));
    		panel_7.add(button5, 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 button6 = new JButton("");
    		button6.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				
    				
    				button6.setText(startGame);
    				if (startGame.equalsIgnoreCase("X"))
    				{
    					button6.setForeground(Color.blue);
    				}
    				else
    				{
    					button6.setForeground(Color.red);
    				}
    				
    				choosePlayer();
    				winningGame();
    			}
    		});
    		button6.setFont(new Font("Tahoma", Font.BOLD, 40));
    		panel_8.add(button6, 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 lblPlayerO = new JLabel("Player O:");
    		lblPlayerO.setFont(new Font("Tahoma", Font.BOLD, 40));
    		panel_9.add(lblPlayerO, 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.setText("0");
    		txtCountO.setHorizontalAlignment(SwingConstants.CENTER);
    		txtCountO.setFont(new Font("Tahoma", Font.BOLD, 40));
    		panel_10.add(txtCountO, BorderLayout.CENTER);
    		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 button7 = new JButton("");
    		button7.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				
    				button7.setText(startGame);
    				if (startGame.equalsIgnoreCase("X"))
    				{
    					button7.setForeground(Color.blue);
    				}
    				else
    				{
    					button7.setForeground(Color.red);
    				}
    				
    				choosePlayer();
    				winningGame();
    			}
    		});
    		button7.setFont(new Font("Tahoma", Font.BOLD, 40));
    		panel_11.add(button7, 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 button8 = new JButton("");
    		button8.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				
    				
    				button8.setText(startGame);
    				if (startGame.equalsIgnoreCase("X"))
    				{
    					button8.setForeground(Color.blue);
    				}
    				else
    				{
    					button8.setForeground(Color.red);
    				}
    				
    				choosePlayer();
    				winningGame();
    			}
    		});
    		button8.setFont(new Font("Tahoma", Font.BOLD, 40));
    		panel_12.add(button8, 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 button9 = new JButton("");
    		button9.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				
    				
    				button9.setText(startGame);
    				if (startGame.equalsIgnoreCase("X"))
    				{
    					button9.setForeground(Color.blue);
    				}
    				else
    				{
    					button9.setForeground(Color.red);
    				}
    				
    				choosePlayer();
    				winningGame();
    			}
    		});
    		button9.setFont(new Font("Tahoma", Font.BOLD, 40));
    		panel_13.add(button9, 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) {
    				button1.setText(null);
    				button2.setText(null);
    				button3.setText(null);
    				button4.setText(null);
    				button5.setText(null);
    				button6.setText(null);
    				button7.setText(null);
    				button8.setText(null);
    				button9.setText(null);
    				
    				
    			}
    		});
    		btnReset.setFont(new Font("Tahoma", Font.PLAIN, 40));
    		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,"voulez vous vraiment quittez ?","Tic Tac Toe",
    						JOptionPane.YES_NO_OPTION)== JOptionPane.YES_NO_OPTION) {
    					System.exit(0);
    				}
    			}
    		});
    		btnExit.setFont(new Font("Tahoma", Font.PLAIN, 40));
    		panel_15.add(btnExit, BorderLayout.CENTER);
    	}
    
    }
    

    -
    Edité par VictorHuguet 31 mars 2020 à 6:11:46

    • Partager sur Facebook
    • Partager sur Twitter
      30 mars 2020 à 17:49:57

      C'est une erreur classique, ca veut dire que tu es sortie de ton tableau

      Exemple:

      String [] tab = {"A","B","C"};
      for (int i=0; i<=tab.length;i++){
       System.out.println(tab[i]);
      }

      Ca afficheras :

      A

      B

      C

      error car tu regardes la 4eme case de ton tableau qui n'existe pas

      • Partager sur Facebook
      • Partager sur Twitter

      On n'aime pas ce qu'on ne comprend pas 

        31 mars 2020 à 5:13:30

        Smooki a écrit:

        C'est une erreur classique, ca veut dire que tu es sortie de ton tableau

        Exemple:

        String [] tab = {"A","B","C"};
        for (int i=0; i<=tab.length;i++){
         System.out.println(tab[i]);
        }

        Ca afficheras :

        A

        B

        C

        error car tu regardes la 4eme case de ton tableau qui n'existe pas

        ha d'accord, merci. 

        mais c'est bizarre que j'ai cette erreur une n'importe qu'elles buttons que je clique pour placer soit un O soit un X. ça viendrais de panel du coup ?



        • Partager sur Facebook
        • Partager sur Twitter
          31 mars 2020 à 14:23:14

          Je crois que le probleme vient de "txtCountX". Essaye de l'initialiser au début.

          PS: pour un tictactoe, tu as fait un très mauvais code, imagine que je veux qu'il y ai 100 cases...

          • Partager sur Facebook
          • Partager sur Twitter

          On n'aime pas ce qu'on ne comprend pas 

            1 avril 2020 à 6:25:00

            Smooki a écrit:

            Je crois que le probleme vient de "txtCountX". Essaye de l'initialiser au début.

            PS: pour un tictactoe, tu as fait un très mauvais code, imagine que je veux qu'il y ai 100 cases...

            le txtCountX est initialiser au debut avec ça non ?

            private JTextField txtCountX;
            private JTextField txtCountO;
            	

            un Tic tac toe avec 100 cases mdr. 

            • Partager sur Facebook
            • Partager sur Twitter
              1 avril 2020 à 9:01:27

              initialiser 

              private JTextField txtCountX = new JTextField();
              

              Et pourtant avec un bon code qu'il y a 9 ou 100 cases, la taille du programme sera le même, en plus il sera 3fois plus petit

              • Partager sur Facebook
              • Partager sur Twitter

              On n'aime pas ce qu'on ne comprend pas 

                3 avril 2020 à 7:00:14

                Smooki a écrit:

                initialiser 

                private JTextField txtCountX = new JTextField();
                

                Et pourtant avec un bon code qu'il y a 9 ou 100 cases, la taille du programme sera le même, en plus il sera 3fois plus petit


                ha d'accord merci xD. 

                c'est moi qui est mis cette taille la je sais que c'est overkill mais je prefere

                l'erreur viens de winninggame apparemment comment je peux faire différemment ça ?

                -
                Edité par VictorHuguet 3 avril 2020 à 9:22:34

                • Partager sur Facebook
                • Partager sur Twitter
                  4 avril 2020 à 0:16:21

                          JButton button1 = new JButton("");
                  
                  essaye en mettant un chaine non vide 
                  et sérieusement avec un code plus simple tu verras les erreurs plus facilement :p
                  • Partager sur Facebook
                  • Partager sur Twitter

                  On n'aime pas ce qu'on ne comprend pas 

                    4 avril 2020 à 8:43:04

                    oui mais par exemple si je fais avec ceux code la : 
                    import java.awt.GridLayout;
                    import java.awt.event.ActionEvent;
                    import java.awt.event.ActionListener;
                    import javax.swing.*;
                    
                    public class TicTacToe extends JPanel
                    {
                        JButton buttons[] = new JButton[9]; 
                        int alternate = 0;//if this number is a even, then put a X. If it's odd, then put an O
                        
                        public TicTacToe()
                        {
                          setLayout(new GridLayout(3,3));
                          initializebuttons(); 
                        }
                        
                        public static void main(String[] args) 
                        {
                            JFrame window = new JFrame("Tic-Tac-Toe");
                            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                            window.getContentPane().add(new TicTacToe());
                            window.setBounds(300,200,300,300);
                            window.setVisible(true);
                        }
                        
                        public boolean checkForWin()
                        {
                            /**   Reference: the button array is arranged like this as the board
                             *      0 | 1 | 2
                             *      3 | 4 | 5
                             *      6 | 7 | 8
                             */
                            //horizontal win check
                            if( checkAdjacent(0,1) && checkAdjacent(1,2) ) //no need to put " == true" because the default check is for true
                                return true;
                            else if( checkAdjacent(3,4) && checkAdjacent(4,5) )
                                return true;
                            else if ( checkAdjacent(6,7) && checkAdjacent(7,8))
                                return true;
                            
                            //vertical win check
                            else if ( checkAdjacent(0,3) && checkAdjacent(3,6))
                                return true;  
                            else if ( checkAdjacent(1,4) && checkAdjacent(4,7))
                                return true;
                            else if ( checkAdjacent(2,5) && checkAdjacent(5,8))
                                return true;
                            
                            //diagonal win check
                            else if ( checkAdjacent(0,4) && checkAdjacent(4,8))
                                return true;  
                            else if ( checkAdjacent(2,4) && checkAdjacent(4,6))
                                return true;
                            else 
                                return false;
                            
                            
                        }
                        
                        public boolean checkAdjacent(int a, int b)
                        {
                            if ( buttons[a].getText().equals(buttons[b].getText()) && !buttons[a].getText().equals("") )
                                return true;
                            else
                                return false;
                        }
                     // when a button is clicked, it generates an ActionEvent. Thus, each button needs an ActionListener. When it is clicked, it goes to this listener class that I have created and goes to the actionPerformed method. There (and in this class), we decide what we want to do.
                        private class buttonListener implements ActionListener
                        {
                           
                            public void actionPerformed(ActionEvent e) 
                            {
                                
                                JButton buttonClicked = (JButton)e.getSource(); //get the particular button that was clicked
                                if(alternate%2 == 0)
                                    buttonClicked.setText("X");
                                else
                                    buttonClicked.setText("O");
                                
                                if(checkForWin() == true)
                                {
                                    JOptionPane.showConfirmDialog(null, "Game Over.");
                                    resetButtons();
                                }  
                                alternate++;
                            }  
                        }
                        public void initializebuttons()
                        {
                            for(int i = 0; i <= 8; i++)
                            {
                                buttons[i] = new JButton();
                                buttons[i].setText("");
                                buttons[i].addActionListener(new buttonListener());
                                
                                add(buttons[i]); //adds this button to JPanel (note: no need for JPanel.add(...)
                                                    //because this whole class is a JPanel already           
                            }
                        }
                        public void resetButtons()
                        {
                            for(int i = 0; i <= 8; i++)
                            {
                                buttons[i].setText("");
                            }
                        }
                        
                    
                    }
                    
                    qui est tres simple je n'arrive pas a faire la meme présentation que sur le code compliqué d'au dessus, avec le scord bord un bouton reset un bouton quitter et un message qui indique qui a wins la game.
                    • Partager sur Facebook
                    • Partager sur Twitter
                      4 avril 2020 à 13:41:00

                      tu peux faire la chose suivante:

                      tu stocks ta grille dans un tableau bidimensionnel 

                      int nbLigne;
                      int nbCol = nbLigne = 3;
                      String [][] grille = new String[nbLigne][nbCol];

                      Ensuite tu crées un JPanel, dedans tu va mettres deux JPanels, un en haut et un au centre (avec un BorderLayout North et Center).

                      respectivement paneN et paneC.

                      Dans le paneN tu mets les scores, ensuite dans paneC tu lui mets un BorderLayout et tu lui rajoute un JPanel au centre qui va être la grille (soit paneG) avec un GridLayout.

                      Pour afficher la grille tu fais:

                      private JPanel afficheGrille(){
                        Jpanel panel = new JPanel();
                        panel.setLayout(new GridLayout(nbLigne,nbCol));
                        for (int i=0; i<nbLigne; i++){
                          for {int j=0; j<nbCol; j++){
                            panel.add(new JLabel(grille[i][j]);
                          }
                        }
                        return panel
                      }


                      Je te laisse te débrouiller, n'hésite pas si tu bloques 

                      -
                      Edité par Smooki 4 avril 2020 à 13:44:19

                      • Partager sur Facebook
                      • Partager sur Twitter

                      On n'aime pas ce qu'on ne comprend pas 

                        6 avril 2020 à 7:35:43

                        Smooki a écrit:

                        tu peux faire la chose suivante:

                        tu stocks ta grille dans un tableau bidimensionnel 

                        int nbLigne;
                        int nbCol = nbLigne = 3;
                        String [][] grille = new String[nbLigne][nbCol];

                        Ensuite tu crées un JPanel, dedans tu va mettres deux JPanels, un en haut et un au centre (avec un BorderLayout North et Center).

                        respectivement paneN et paneC.

                        Dans le paneN tu mets les scores, ensuite dans paneC tu lui mets un BorderLayout et tu lui rajoute un JPanel au centre qui va être la grille (soit paneG) avec un GridLayout.

                        Pour afficher la grille tu fais:

                        private JPanel afficheGrille(){
                          Jpanel panel = new JPanel();
                          panel.setLayout(new GridLayout(nbLigne,nbCol));
                          for (int i=0; i<nbLigne; i++){
                            for {int j=0; j<nbCol; j++){
                              panel.add(new JLabel(grille[i][j]);
                            }
                          }
                          return panel
                        }


                        Je te laisse te débrouiller, n'hésite pas si tu bloques 

                        -
                        Edité par Smooki 4 avril 2020 à 13:44:19


                        bonjour,

                        peut tu me dire ou je devrais le placer dans le code  et comment faire ce que tu ma dis de faire.

                        peut tu donner plus de detail car quand j'essaie d'integrer a mon code en rajoutant des truc bin ça fonctionne pas xD

                        -
                        Edité par VictorHuguet 6 avril 2020 à 7:38:00

                        • Partager sur Facebook
                        • Partager sur Twitter
                          6 avril 2020 à 18:12:59

                          Je que je disais c'est de refaire complètement ton code de A à Z, et non de l'integerer
                          • Partager sur Facebook
                          • Partager sur Twitter

                          On n'aime pas ce qu'on ne comprend pas 

                          Exception in thread "AWT-EventQueue-0" java.lang.N

                          × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                          × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                          • Editeur
                          • Markdown