8 Replies Latest reply: Jan 16, 2011 10:20 AM by 827569 RSS

    tic-tac-toe [help]

    827569
      I have a java project to make a tic-tac-toe game with variable dimensions and variable number of players.
      For example, if the grid is 9x9. Players will play normally trying to make 3 consecutive X or 0. Each player will have a score which will be incremented if he manages to make 3 consecutive X or 0.


      So far I made this: http://img202.imageshack.us/img202/2197/formk.th.jpg

      i want that buttons grid to be modified by those radiobuttons and I don't know how to do that.
      When i press the radio button 5x5 I want the buttton grid to be modified by that dimension.

      Here I gave that grid variable the value 3 so i could test it.
          public Xsi0View(SingleFrameApplication app) {
              super(app);
              [COLOR="Red"]grid=3[/COLOR];
              [COLOR="Red"]int dimensiune = (grid*grid)[/COLOR];
              
              initComponents();
              
              for(int i=1; i<=dimensiune; i++){
                  buttons[i] = new JButton();
                  tabla.add(buttons);
      buttons[i].addActionListener(this);
      }

      }

      This is initComponents with the GUI generated by Net Beans:
      private void initComponents() {
      
              mainPanel = new javax.swing.JPanel();
              tabla = new javax.swing.JPanel();
              dimensiuni = new javax.swing.JPanel();
              dimensiune3 = new javax.swing.JRadioButton();
              dimensiune5 = new javax.swing.JRadioButton();
              dimensiune7 = new javax.swing.JRadioButton();
              dimensiune9 = new javax.swing.JRadioButton();
              grupbutoane = new javax.swing.ButtonGroup();
      
              mainPanel.setName("mainPanel"); // NOI18N
              mainPanel.setPreferredSize(new java.awt.Dimension(988, 873));
      
              tabla.setAutoscrolls(true);
              tabla.setName("tabla"); // NOI18N
              tabla.setLayout(new java.awt.GridLayout([COLOR="Red"]grid, grid[/COLOR]
              ));
      
              dimensiuni.setName("dimensiuni"); // NOI18N
              dimensiuni.setLayout(new java.awt.GridLayout(3, 3));
      
              grupbutoane.add(dimensiune3);
              org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(xsi0.Xsi0App.class).getContext().getResourceMap(Xsi0View.class);
              dimensiune3.setText(resourceMap.getString("dimensiune3.text")); // NOI18N
              dimensiune3.setName("dimensiune3"); // NOI18N
              dimensiuni.add(dimensiune3);
      
              grupbutoane.add(dimensiune5);
              dimensiune5.setText(resourceMap.getString("dimensiune5.text")); // NOI18N
              dimensiune5.setName("dimensiune5"); // NOI18N
              dimensiuni.add(dimensiune5);
      
              grupbutoane.add(dimensiune7);
              dimensiune7.setText(resourceMap.getString("dimensiune7.text")); // NOI18N
              dimensiune7.setName("dimensiune7"); // NOI18N
              dimensiuni.add(dimensiune7);
      
              grupbutoane.add(dimensiune9);
              dimensiune9.setText(resourceMap.getString("dimensiune9.text")); // NOI18N
              dimensiune9.setName("dimensiune9"); // NOI18N
              dimensiuni.add(dimensiune9);
      
              javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
              mainPanel.setLayout(mainPanelLayout);
              mainPanelLayout.setHorizontalGroup(
                  mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(mainPanelLayout.createSequentialGroup()
                      .addGap(66, 66, 66)
                      .addComponent(dimensiuni, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                      .addComponent(tabla, javax.swing.GroupLayout.PREFERRED_SIZE, 708, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addContainerGap())
              );
              mainPanelLayout.setVerticalGroup(
                  mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(mainPanelLayout.createSequentialGroup()
                      .addContainerGap(114, Short.MAX_VALUE)
                      .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                              .addComponent(dimensiuni, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                              .addGap(32, 32, 32))
                          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                              .addComponent(tabla, javax.swing.GroupLayout.PREFERRED_SIZE, 471, javax.swing.GroupLayout.PREFERRED_SIZE)
                              .addGap(19, 19, 19))))
              );
      
              setComponent(mainPanel);
          }// </editor-fold>
          // <editor-fold defaultstate="collapsed" desc="comment">
      
          // Variables declaration - do not modify
          private javax.swing.JRadioButton dimensiune3;
          private javax.swing.JRadioButton dimensiune5;
          private javax.swing.JRadioButton dimensiune7;
          private javax.swing.JRadioButton dimensiune9;
          private javax.swing.JPanel dimensiuni;
          private javax.swing.ButtonGroup grupbutoane;
          private javax.swing.JPanel mainPanel;
          private javax.swing.JPanel tabla;
          // End of variables declaration
      This is the full source code:
      /*
       * Xsi0View.java
       */
      
      package xsi0;
      
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import javax.swing.JButton;
      import org.jdesktop.application.SingleFrameApplication;
      import org.jdesktop.application.FrameView;
      
      /**
       * The application's main frame.
       */
      public class Xsi0View extends FrameView implements ActionListener{
      private JButton buttons[] = new JButton[100];
      private int count = 0;
          private int grid;
            private String letter = "";
          private boolean win = false;
              private int[][] winCombinations = new int[][] {
                  {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
                  {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //virticle wins
                  {0, 4, 8}, {2, 4, 6}             //diagonal wins
              };
      
          public Xsi0View(SingleFrameApplication app) {
              super(app);
              grid=3;
              int dimensiune = (grid*grid);
              
              initComponents();
              
              for(int i=1; i<=dimensiune; i++){
                  buttons[i] = new JButton();
                  tabla.add(buttons);
      buttons[i].addActionListener(this);
      }

      }




      /** This method is called from within the constructor to
      * initialize the form.
      * WARNING: Do NOT modify this code. The content of this method is
      * always regenerated by the Form Editor.
      */
      @SuppressWarnings("unchecked")
      // <editor-fold defaultstate="collapsed" desc="Generated Code">
      private void initComponents() {

      mainPanel = new javax.swing.JPanel();
      tabla = new javax.swing.JPanel();
      dimensiuni = new javax.swing.JPanel();
      dimensiune3 = new javax.swing.JRadioButton();
      dimensiune5 = new javax.swing.JRadioButton();
      dimensiune7 = new javax.swing.JRadioButton();
      dimensiune9 = new javax.swing.JRadioButton();
      grupbutoane = new javax.swing.ButtonGroup();

      mainPanel.setName("mainPanel"); // NOI18N
      mainPanel.setPreferredSize(new java.awt.Dimension(988, 873));

      tabla.setAutoscrolls(true);
      tabla.setName("tabla"); // NOI18N
      tabla.setLayout(new java.awt.GridLayout(grid, grid
      ));

      dimensiuni.setName("dimensiuni"); // NOI18N
      dimensiuni.setLayout(new java.awt.GridLayout(3, 3));

      grupbutoane.add(dimensiune3);
      org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(xsi0.Xsi0App.class).getContext().getResourceMap(Xsi0View.class);
      dimensiune3.setText(resourceMap.getString("dimensiune3.text")); // NOI18N
      dimensiune3.setName("dimensiune3"); // NOI18N
      dimensiuni.add(dimensiune3);

      grupbutoane.add(dimensiune5);
      dimensiune5.setText(resourceMap.getString("dimensiune5.text")); // NOI18N
      dimensiune5.setName("dimensiune5"); // NOI18N
      dimensiuni.add(dimensiune5);

      grupbutoane.add(dimensiune7);
      dimensiune7.setText(resourceMap.getString("dimensiune7.text")); // NOI18N
      dimensiune7.setName("dimensiune7"); // NOI18N
      dimensiuni.add(dimensiune7);

      grupbutoane.add(dimensiune9);
      dimensiune9.setText(resourceMap.getString("dimensiune9.text")); // NOI18N
      dimensiune9.setName("dimensiune9"); // NOI18N
      dimensiuni.add(dimensiune9);

      javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
      mainPanel.setLayout(mainPanelLayout);
      mainPanelLayout.setHorizontalGroup(
      mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(mainPanelLayout.createSequentialGroup()
      .addGap(66, 66, 66)
      .addComponent(dimensiuni, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
      .addComponent(tabla, javax.swing.GroupLayout.PREFERRED_SIZE, 708, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addContainerGap())
      );
      mainPanelLayout.setVerticalGroup(
      mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(mainPanelLayout.createSequentialGroup()
      .addContainerGap(114, Short.MAX_VALUE)
      .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
      .addComponent(dimensiuni, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addGap(32, 32, 32))
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
      .addComponent(tabla, javax.swing.GroupLayout.PREFERRED_SIZE, 471, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addGap(19, 19, 19))))
      );

      setComponent(mainPanel);
      }// </editor-fold>
      // <editor-fold defaultstate="collapsed" desc="comment">

      // Variables declaration - do not modify
      private javax.swing.JRadioButton dimensiune3;
      private javax.swing.JRadioButton dimensiune5;
      private javax.swing.JRadioButton dimensiune7;
      private javax.swing.JRadioButton dimensiune9;
      private javax.swing.JPanel dimensiuni;
      private javax.swing.ButtonGroup grupbutoane;
      private javax.swing.JPanel mainPanel;
      private javax.swing.JPanel tabla;
      // End of variables declaration

      public void actionPerformed(ActionEvent e) {


      count++;

      /*Calculate whose turn it is*/
      if(count % 2 == 0){
      letter = "O";
      } else {
      letter = "X";
      }

      /*Write the letter to the button and deactivate it*/
      JButton pressedButton = (JButton)e.getSource();
      pressedButton.setText(letter);
      pressedButton.setEnabled(false);
      /*Determine who won*/
      for(int i=0; i<=7; i++){
      if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) &&
      buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) &&
      (buttons[winCombinations[i][0]].getText() == null ? "" != null : !buttons[winCombinations[i][0]].getText().equals(""))){
      win = true;
      }
      }

      /*Show a dialog when game is over*/
      /* if(win == true){
      JOptionPane.showMessageDialog(null, letter + " wins the game!");
      System.exit(0);
      } else if(count == 9 && win == false){
      JOptionPane.showMessageDialog(null, "The game was tie!");
      System.exit(0);}*/
      }
      }





      How can I make this work?
        • 1. Re: tic-tac-toe [help]
          gimbal2
          not much to do to help you in your current state. Before you can be helped you'll need to take a step back, and that is too:

          - not use a GUI editor at all
          - learn to use java Swing layout managers
          - build the GUI completely in code (IE you do it yourself in stead of letting a tool do the work for you)

          Because then you know what the code is doing and you have full control over the dynamic part of it. When you understand how to do this, only then will it be possible to help you. But probably you'll already know enough to be able to implement this yourself.


          In any case to adjust a GUI during runtime, you'll need to:

          - remove the old components from the frame or panel they are in
          - add the new components
          - cause a revalidate of the frame or panel so that it actually rebuilds and repaints the GUI. In some rare cases I cannot identify you even need to trigger the repaint manually.

          When adding more components than you had before you may run into the issue of the window being too small for all the components to fit. But that is a problem I would worry about later, when you already have something working.


          Lucky for you Swing has an awesome and complete guide that you can follow.

          http://download.oracle.com/javase/tutorial/uiswing/

          Good luck!
          • 2. Re: tic-tac-toe [help]
            827569
            Thank you very much for your response! I will code it myself without that design view and I will use the swing tutorial!
            • 3. Re: tic-tac-toe [help]
              gimbal2
              No no, thank you for being a good sport about it. Refreshing to see someone actually wanting to do the effort himself for a change...
              • 4. Re: tic-tac-toe [help]
                DarrylBurke
                Cross posted
                http://www.java-forums.org/awt-swing/36734-gui-help.html

                db

                edit
                http://forum.codecall.net/java-tutorials/2141-java-tutorial-tic-tac-toe-5.html

                Edited by: Darryl Burke
                • 5. Re: tic-tac-toe [help]
                  827569
                  I'm sorry about that cross post. I thought i would have more chanses to get a faster answer or maybe multiple ideas.
                  I managed to code it using the java tutorials and some google searching.
                  Thank you gimbal2 for your response, all i need to do now is to implement the logic of that tic-tac-toe game so that it can actually work.
                  So far i managed to make only the GUI.

                  Here is how i made it:
                  import java.awt.*;
                  import java.awt.event.*;
                  import javax.swing.*;
                  
                  public class GUI extends JFrame{
                      static final String dimensiune[] = {"3", "5", "7", "9"};
                      private int dimbuton = 3;
                      JComboBox Dimensiunea;
                          
                      private JButton buttons[]= new JButton[90];
                      JButton okButton = new JButton("OK");
                      GridLayout tablaLayout = new GridLayout();
                      
                      public GUI(String name) {
                          super(name);
                          setResizable(true);
                      }
                      
                      public void addComponentsToPane(final Container pane) {
                          
                          Dimensiunea = new JComboBox(dimensiune);
                          final JPanel tabla = new JPanel();
                          tabla.setLayout(tablaLayout);
                          JPanel control = new JPanel();
                          control.setLayout(new GridLayout(1,1));
                          
                  
                          
                          //Adaugare butoane
                         
                          
                          //adaugare controale
                          control.add(new Label("Selectati Dimensiunea:"));
                          control.add(Dimensiunea);
                          control.add(okButton);
                          
                          //Actiuni asupra bunotnului OK
                          okButton.addActionListener(new ActionListener(){
                              public void actionPerformed(ActionEvent e){
                                  //setare dimensiuni
                                  String dim = (String)Dimensiunea.getSelectedItem();
                                  tabla.removeAll();
                                 dimbuton=Integer.parseInt(dim);
                  
                                  for(int i=1; i<=dimbuton*dimbuton; i++){
                          buttons[i] = new JButton();
                          tabla.add(buttons);

                  }
                  //Setare coloane si randuri |revalidare si repaint|
                  tablaLayout.setColumns(dimbuton);
                  tablaLayout.setRows(dimbuton);

                  tabla.revalidate();
                  tabla.repaint();
                  //setare layout
                  tablaLayout.layoutContainer(tabla);/////////////////
                  }
                  });
                  pane.add(control, BorderLayout.SOUTH);
                  pane.add(tabla, BorderLayout.CENTER);
                  //pane.add(new JSeparator(), BorderLayout.CENTER);

                  }


                  private static void createAndShowGUI() {
                  //creaza fereastra.
                  GUI frame = new GUI("X-0");
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  //Seteaza continutul
                  frame.addComponentsToPane(frame.getContentPane());
                  //Afiseaza fereastra si seteaza dimensiunile.
                  frame.setBounds(400, 20, 700, 700);
                  //frame.pack();
                  frame.setVisible(true);
                  }

                  public static void main(String[] args) {


                  javax.swing.SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                  createAndShowGUI();
                  }
                  });
                  }
                  }


                  Edited by: 824566 on Jan 5, 2011 6:20 AM
                  • 6. Re: tic-tac-toe [help]
                    gimbal2
                    Seems like you're on the right track. At least you didn't fall for the trap of directly going for GridbagLayout and in stead you're combining layout managers to get what you want. Excellent.
                    • 7. Re: tic-tac-toe [help]
                      827569
                      //code removed

                      I am trying to put "x" and "0" on those buttons when i click them but i don't really know how to do that.
                      I posted my code above.
                      Could you please help me solve this? I have to finish and give this school project until 11. 01. 2011 and i don't have enough time since I have one other project involving databases and php.
                      I need more time to do this myself, please give me some ideas on how to implement what i want and help me finish this until Tuesday.
                      I think i need to call that actionPerformed in main but i don't seem to know how.

                      //LE
                      I made changes and i made 2 classes instead of one:

                      This is GUI class:
                      import java.awt.*;
                      import java.awt.event.*;
                      import javax.swing.*;
                      
                      public class GUI extends JFrame implements ActionListener{
                          static final String dimensiune[] = {"3", "5", "7", "9"};
                          private int dimbuton = 3;
                          JComboBox Dimensiunea;
                          private int count = 0;
                          private String letter = "";
                          private JFrame fereastra = new JFrame("X_0");
                      
                          public JButton buttons[][]= new JButton[10][10];
                          JButton okButton = new JButton("OK");
                          GridLayout tablaLayout = new GridLayout();
                      
                          public GUI() {
                      
                      
                              Dimensiunea = new JComboBox(dimensiune);
                              final JPanel tabla = new JPanel();
                              tabla.setLayout(tablaLayout);
                              JPanel control = new JPanel();
                              control.setLayout(new GridLayout(1,1));
                      
                      
                      
                              //Adaugare butoane
                      
                      
                              //adaugare controale
                              control.add(new Label("Selectati Dimensiunea:"));
                              control.add(Dimensiunea);
                              control.add(okButton);
                      
                              //Actiuni asupra butonului OK
                              okButton.addActionListener(new ActionListener(){
                                  public void actionPerformed(ActionEvent e){
                                      //setare dimensiuni
                                      String dim = (String)Dimensiunea.getSelectedItem();
                      
                                      tabla.removeAll();
                                     dimbuton=Integer.parseInt(dim);
                      
                                      for(int i=1; i<=dimbuton ; i++){
                                          for (int j=1; j<=dimbuton; j++){
                              buttons[i][j] = new JButton();
                              tabla.add(buttons[i][j]);
                               buttons[i][j].addActionListener(this);
                                          }
                              }
                                      //Setare coloane si randuri |revalidare si repaint|
                                      tablaLayout.setColumns(dimbuton);
                                      tablaLayout.setRows(dimbuton);
                      
                                      tabla.revalidate();
                                      tabla.repaint();
                                      //setare layout
                                     tablaLayout.layoutContainer(tabla);/////////////////
                                  }
                              });
                      
                              //caracteristici fereastra
                              fereastra.add(control, BorderLayout.SOUTH);
                              fereastra.add(tabla, BorderLayout.CENTER);
                             //pane.add(new JSeparator(), BorderLayout.CENTER);
                              fereastra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                              fereastra.setBounds(400, 20, 700, 700);
                              fereastra.setVisible(true);
                              setResizable(true);
                              fereastra.getContentPane();
                      
                      
                          }
                      
                      
                            public void actionPerformed(ActionEvent e) {
                          count++;
                               if(count % 2 == 0){
                                  letter = "O";
                              } else {
                                  letter = "X";
                              }
                      
                                for(int i=1; i<=dimbuton ; i++){
                                          for (int j=1; j<=dimbuton; j++){
                                              if (e.getSource() == buttons[i][j]){
                      
                                buttons[i][j].setText(letter);
                                buttons[i][j].setEnabled(false);
                                              }
                                    }
                            }
                          }
                      
                      }
                      This is Principala class:
                      public class Principala
                      {
                      
                      
                          public static void main(String[] args) {
                      
                      GUI starter = new GUI();
                      
                          }
                      
                      }
                      What am I missing?

                      Edited by: 824566 on Jan 8, 2011 12:14 PM

                      //LLE Solved the problem by modifing modyfing GUI class like this:
                      import java.awt.*;
                      import java.awt.event.*;
                      import javax.swing.*;
                      
                      public class GUI extends JFrame {
                          static final String dimensiune[] = {"3", "5", "7", "9"};
                          private int dimbuton = 3;
                          JComboBox Dimensiunea;
                          private int count = 0;
                          private String letter = "";
                          private JFrame fereastra = new JFrame("X_0");
                      
                          public JButton buttons[][]= new JButton[10][10];
                          JButton okButton = new JButton("OK");
                          GridLayout tablaLayout = new GridLayout();
                      
                          public GUI() {
                      
                      
                              Dimensiunea = new JComboBox(dimensiune);
                              final JPanel tabla = new JPanel();
                              tabla.setLayout(tablaLayout);
                              JPanel control = new JPanel();
                              control.setLayout(new GridLayout(1,1));
                      
                      
                              //adaugare controale
                              control.add(new Label("Selectati Dimensiunea:"));
                              control.add(Dimensiunea);
                              control.add(okButton);
                      
                              //Actiuni asupra butoanelor
                              okButton.addActionListener(new ActionListener(){
                                  public void actionPerformed(ActionEvent e){
                                      //setare dimensiuni
                                      String dim = (String)Dimensiunea.getSelectedItem();
                      
                                      tabla.removeAll();
                                     dimbuton=Integer.parseInt(dim);
                      
                                      for(int i=1; i<=dimbuton ; i++){
                                          for (int j=1; j<=dimbuton; j++){
                              buttons[i][j] = new JButton();
                              tabla.add(buttons[i][j]);
                               buttons[i][j].addActionListener(new ActionListener() {
                      
                                  public void actionPerformed(ActionEvent e)
                                  {
                                          count++;
                               if(count % 2 == 0){
                                  letter = "O";
                              } else {
                                  letter = "X";
                              }
                      
                                for(int i=1; i<=dimbuton ; i++){
                                          for (int j=1; j<=dimbuton; j++){
                                              if (e.getSource() == buttons[i][j]){
                      
                                buttons[i][j].setText(letter);
                                buttons[i][j].setEnabled(false);
                                              }
                                    }
                                  }
                               }
                           });
                          }
                        }
                                      //Setare coloane si randuri |revalidare si repaint|
                                      tablaLayout.setColumns(dimbuton);
                                      tablaLayout.setRows(dimbuton);
                      
                                      tabla.revalidate();
                                      tabla.repaint();
                                      //setare layout
                                     tablaLayout.layoutContainer(tabla);/////////////////
                                  }
                              });
                      
                              //caracteristici fereastra
                              fereastra.add(control, BorderLayout.SOUTH);
                              fereastra.add(tabla, BorderLayout.CENTER);
                             //pane.add(new JSeparator(), BorderLayout.CENTER);
                              fereastra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                              fereastra.setBounds(400, 20, 700, 700);
                              fereastra.setVisible(true);
                              setResizable(true);
                              fereastra.getContentPane();
                              
                      
                          }
                      
                      }
                      Edited by: 824566 on Jan 8, 2011 1:04 PM
                      • 8. Re: tic-tac-toe [help]
                        827569
                        Thanks for your support!
                        This is how my final code looks like:
                        import java.awt.*;
                        import java.awt.event.*;
                        import javax.swing.*;
                        
                        public class GUI extends JFrame {
                            //declaratii variabile, butoane, frame...
                            static final String dimensiune[] = {"3", "5", "7", "9"};
                            public int dimbuton = 3;
                            JComboBox Dimensiunea;
                            JTextField scorx;
                            JTextField scor0;
                            private int count, xwins, owins = 0;
                            private String letter = "";
                            private JFrame fereastra = new JFrame("X_0");
                        
                            public JButton buttons[][]= new JButton[10][10];
                            JButton okButton = new JButton("OK");
                            JButton verificaButon = new JButton("Calculeaza Scor");
                            GridLayout tablaLayout = new GridLayout();
                        
                            public GUI() {
                                //setare ComboBox
                                Dimensiunea = new JComboBox(dimensiune);
                                final JPanel tabla = new JPanel();
                                tabla.setLayout(tablaLayout);
                                //setare layout pentru afisarea scorului
                                JPanel scor = new JPanel();
                                scor.setLayout(new GridLayout(2,1));
                                 scorx= new JTextField(5);
                                 scor0= new JTextField(5);
                                //adaugare componente pe layoutul scor
                                scor.add(new Label("Player X:"));
                                scor.add(scorx);
                                scor.add(new Label("Player 0:"));
                                scor.add(scor0);
                                //setare layout controale
                                JPanel control = new JPanel();
                                control.setLayout(new GridLayout(1,1));
                                //adaugare controale
                                control.add(new Label("Selectati Dimensiunea:"));
                                control.add(Dimensiunea);
                                control.add(okButton);
                                control.add(verificaButon);
                                
                                //Actiuni asupra butonului "OK"
                                okButton.addActionListener(new ActionListener(){
                                    public void actionPerformed(ActionEvent e){
                                        //setare dimensiuni
                                        String dim = (String)Dimensiunea.getSelectedItem();
                                        //stergerea butonelor pentru a putea fi adaugate altele
                                        tabla.removeAll();
                                        //initializarea scorului cu 0 pentru ambii jucatori
                                        String zerox = Integer.toString(xwins);
                                       scorx.setText(zerox);
                                       String zeroy = Integer.toString(xwins);
                                       scor0.setText(zeroy);
                                       dimbuton=Integer.parseInt(dim);
                                        //inserare butoane pe tabla in functie de valoarea aleasa din Combo box
                                        for(int i=1; i<=dimbuton ; i++){
                                            for (int j=1; j<=dimbuton; j++){
                                                buttons[i][j] = new JButton();
                                tabla.add(buttons[i][j]);
                                //adaugare "ascultatori" pentru butoanele create
                                 buttons[i][j].addActionListener(new ActionListener() {
                                 //setare actiuni la apasarea unui buton de pe tabla
                                    public void actionPerformed(ActionEvent e)
                                    {
                                        //setarea ordinii jucatorilor
                                            count++;
                                        if(count % 2 == 0){
                                            letter = "0";
                                        } else {
                                            letter = "X";
                                        }
                        
                                  for(int i=1; i<=dimbuton ; i++){
                                            for (int j=1; j<=dimbuton; j++){
                                                if (e.getSource() == buttons[i][j]){
                                  //pentru fiecare buton de pe tabla apasat butonul se seteaza cu textul playerului respectiv si se blocheaza
                                  buttons[i][j].setText(letter);
                                  buttons[i][j].setEnabled(false);
                                  
                        
                                      }
                                   }
                                      
                                 }
                                       
                                     }
                                    
                             });
                            }
                          }
                                        //Setare coloane si randuri |revalidare si repaint|
                                        tablaLayout.setColumns(dimbuton);
                                        tablaLayout.setRows(dimbuton);
                        
                                        tabla.revalidate();
                                        tabla.repaint();
                                        //setare layout
                                       tablaLayout.layoutContainer(tabla);
                                    }
                                });
                                    //actiuni asupra butonului care verifica scorul
                                        verificaButon.addActionListener(new ActionListener(){
                                    public void actionPerformed(ActionEvent e){
                                        //aduce scor la 0 pe urma verifica noul scor
                                        xwins=0;
                                        owins=0;
                                       String zerox = Integer.toString(xwins);
                                       scorx.setText(zerox);
                                       String zeroy = Integer.toString(xwins);
                                       scor0.setText(zeroy);
                        
                                        verifiaScor();
                                        
                                        
                                    }
                                        });
                        
                        
                                //adaugare elemente in fereastra si setare dimensiuni
                                fereastra.add(control, BorderLayout.SOUTH);
                                fereastra.add(tabla, BorderLayout.CENTER);
                                fereastra.add(scor, BorderLayout.NORTH);
                                fereastra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                fereastra.setBounds(400, 20, 700, 700);
                                fereastra.setVisible(true);
                                setResizable(true);
                                fereastra.getContentPane();
                                
                        
                            }
                        
                        //metoda care verifica combinatiile castigatoare
                         public void verifiaScor(){
                             //verificare pe orizontala
                                  for (int i=1;i<=dimbuton;i++){
                                      for(int j=1;j<=dimbuton-2;j++){
                                 //verificare pe orizontala pentru "X"
                                 if(  buttons[i][j].getText().equals("X") && buttons[i][j+1].getText().equals("X") && buttons[i][j+2].getText().equals("X"))
                                 {
                                     xwins++;
                                     String x = Integer.toString(xwins);
                                     scorx.setText(x);
                                 }
                                 //verificare pe orizontala pentru "0"
                                 else if (buttons[i][j].getText().equals("0") && buttons[i][j + 1].getText().equals("0") && buttons[i][j + 2].getText().equals("0"))
                                 {
                                     owins++;
                                     String o = Integer.toString(owins);
                                     scor0.setText(o);
                                 }
                                      }
                           }
                                  //verificare pe verticala
                        for (int i=1;i<=dimbuton-2;i++){
                            for(int j=1;j<=dimbuton;j++){
                                //verificare pe verticala pentru "X"
                                if (buttons[i][j].getText().equals("X") && buttons[i+1][j].getText().equals("X") && buttons[i+2][j].getText().equals("X"))
                                 {
                                     xwins++;
                                     String x = Integer.toString(xwins);
                                     scorx.setText(x);
                                 }
                                //verificare pe verticala pentru "0"
                                if (buttons[i][j].getText().equals("0") && buttons[i+1][j].getText().equals("0") && buttons[i+2][j].getText().equals("0"))
                                {
                                    owins++;
                                    String o = Integer.toString(owins);
                                    scor0.setText(o);
                                }
                            }
                             }
                                  //verificare diagonala principala
                                  for (int i=1;i<=dimbuton-2;i++){
                            for(int j=1;j<=dimbuton-2;j++){
                                //verificare diagonala pentru "X"
                                    if (buttons[i][j].getText().equals("X") && buttons[i+1][j+1].getText().equals("X") && buttons[i+2][j+2].getText().equals("X"))
                                   {
                                       xwins++;
                                       String x = Integer.toString(xwins);
                                       scorx.setText(x);
                                   }
                                    if (buttons[i][j].getText().equals("0") && buttons[i+1][j+1].getText().equals("0") && buttons[i+2][j+2].getText().equals("0"))
                                {
                                    owins++;
                                    String o = Integer.toString(owins);
                                    scor0.setText(o);
                                }
                                      }
                            }
                                  //verificare diagonala secundara
                             for (int i = 2; i <= dimbuton; i++) {
                                 for (int j = 1 ;j <=dimbuton - 2; j++) {
                                         if (buttons[i][j].getText().equals("X") && buttons[i-1][j+1].getText().equals("X") && buttons[i-2][j+2].getText().equals("X"))
                                   {
                                       xwins++;
                                       String x = Integer.toString(xwins);
                                       scorx.setText(x);
                                   }
                                          if (buttons[i][j].getText().equals("0") && buttons[i-1][j+1].getText().equals("0") && buttons[i-2][j+2].getText().equals("0"))
                                     {
                                         owins++;
                                         String o = Integer.toString(owins);
                                         scor0.setText(o);
                                     }
                                 }
                             }
                        
                            }
                        }