i have this code to make sodoko board with 9*9 cells(button cells ) but i cannot made all Jpanels contain my buttons i made the first Jpanel only

import java.awt.Color;
import java.awt.GridLayout;
import java.lang.reflect.Field;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;

public class Sodoko extends JFrame {

    private Sodoko() {
        int myArray[][] = { { 3, 5, 2 }, { 4, 9, 200 }, { 6, 0, 0 } };
        JPanel[][] panel = new JPanel[3][3];
        JButton[][] GameButton = new JButton[3][3];
        Field field[][] = new Field[9][9] ;
        init_Buttons(GameButton, myArray);
        sodokoPanel(panel, GameButton,field);
    }

    public void init_Buttons(JButton[][] GameButtons, int myArray[][]) {
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++) {
                GameButtons[i][j] = new JButton(myArray[i][j]+"");
            }

    }

    public void sodokoPanel(JPanel[][] panel, JButton[][] GameButtons ,Field field[][]) {
        setLayout(new GridLayout(3,3));
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++) {

                panel[i][j] = new JPanel(new GridLayout(3, 3));
                add(panel[i][j]);
                panel[i][j].setBorder(BorderFactory.createLineBorder(Color.blue));
                panel[i/3][j/3].add(GameButtons[i][j]);
            }

        pack();
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Sodoko().setVisible(true);
            }
        });
    }
}

Recommended Answers

All 8 Replies

Why do you have a 3x3 array of buttons when the board is 9x9?

I guess his idea was to have a board of 3x3 groups of 3x3 buttons each, which would be a somewhat logical first attempt at a sudoku board.
A better implementation is to make a 3x3 array of objects, each containing a 3x3 array of cells.

something like

class Cell {private int value;}
class SuperCell {private Cell[3][3] cells;}
class SudokuBoard {private SuperCell[3][3] board;}

That way the board can defer validation of the 3x3 blocks to the SuperCell.
It'd also need to have methods to validate a row and column of the board itself, which'd need to defer to the rows and columns of each SuperCell to get the data about what's contains in those rows and columns.

IMHO it's a mistake to see it as 9 3x3 blocks. That's no more valid or relevant than 9 x columns or 9 x rows. Treating any one of those as a special case will just lead to duplicate code (one code for the special case, one for the other cases).
I had a go at this some years ago, just for fun, and concluded that the best structure was a 9x9 grid of cells, and 29 instances of a Group class where each instance held a list of 9 cells (blocks, rows, cols or diagonals). That way checking for completeness, correctness etc can be implemented as methods in the Group class that you can call in a simple loop.

I know i should have made array 9X9 but what i want to do is to put the same array 3x3 in each JPanel which means all Jpanel contains the same Buttons as the first one

i have changed myArray to be like this :

int myArray[][] = { { 3, 0, 6, 5, 0, 8, 4, 0, 0 },
                { 5, 2, 0, 0, 0, 0, 0, 0, 0 }, { 0, 8, 7, 0, 0, 0, 0, 3, 1 },
                { 0, 0, 3, 0, 1, 0, 0, 8, 0 }, { 9, 0, 0, 8, 6, 3, 0, 0, 5 },
                { 0, 5, 0, 0, 9, 0, 6, 0, 0 }, { 1, 3, 0, 0, 0, 0, 2, 5, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 7, 4 }, { 0, 0, 5, 2, 0, 6, 3, 0, 0 } };

and changed some of this method :

public void sodokoPanel(JPanel[][] panel, JButton[][] GameButtons,
            Field field[][]) {
        setLayout(new GridLayout(3, 3));
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++) {

                panel[i][j] = new JPanel(new GridLayout(3, 3));
                add(panel[i][j]);
                panel[i][j].setBorder(BorderFactory
                        .createLineBorder(Color.blue));

            }
        for (int i = 0; i < 9; i++)
            for (int j = 0; j < 9; j++) {
                panel[i / 3][j / 3].add(GameButtons[i][j]);
            }
        pack();
        setLocationRelativeTo(null);
    }

so it worked for me and i got something like :
"the output pic was attached"

Yes, that looks right, but I don't understand why you want the same buttons in all 9 blocks when each of the 81 cells can have different contents

It looks like you may be designing your detailed code starting with the GUI. Very often that is a mistake, because when you get to adding the game logic it gets very tangled. In general it's far better to build the logic first, then add the GUI afterwards. Just a thought...

Yes you are right i just thought it worth trying to add the same buttons to all blocks it looks difficult for me somehow .... i'm still trying it

hey man - if it looks difficult then just break it down into small steps and do one at a time. As long as you keep trying things you'll get it right in the end! You never fail until you stop trying... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.