I have an array of coins which are falling downwards and when it comes down (y coordinate <0) I am updating y to a certain random integer which is causing overlap among textures.
inside constructor of GameState Class
for (int i = 0; i < coins.length; i++) {
coins[i]= new Coin(i *(Coin.COIN_SPACING +Coin.COIN_WIDTH ),Game.HEIGHT/2 + i *(Coin.COIN_SPACING_VERTICAL +Coin.COIN_WIDTH));
}
inside override update method
for (int i = 0; i < coins.length; i++) {
coins[i].update(dt,score/SCORE_DIVIDENT);
if(coins[i].collides(delta.getBounds())) {
//coins[i].getTexture().dispose();
score = score + 5;
mScore = "Score:"+score;
coins[i].playDropSound();
coins[i].setPosition(new Vector3(coins[i].getPosition().x, coins[i].getPosition().y+MConstant.GALAXY_GRAND_PRIME_HEIGHT *2,0));
coinCollided = true;
}
}
inside render override method
for (int i = 0; i < coins.length; i++) {
sb.draw(coins[i].getTexture(),coins[i].getPosition().x, coins[i].getPosition().y);
}
Related
sorry I don't know how title this question but I am having an issue with assigning data to a new multidimentional array, do something and then retrieve the old data.
In my case I am trying to loop through JTextFields, assign all current data specifically the colour to a new multidimentional array. Then I want to do a search and change the background colour of found textfields.
Now I have a reset button that I would like the old colours that are now in the new multidimentional array to be assigned back to the fields. The issue I am having is that the new multidimentional array has updated after the search with the new colours. I really appreciate if someone can point me to the right direction.
Here is my code:
public JTextField[][] fields = new JTextField[totalX][totalY];
public JTextField[][] newFields = new JTextField[totalX][totalY];
if (e.getSource() == btnFind || e.getSource() == txtSearch)
{
// Make a copy of fields before selecting everything
for(int t = 0; t < totalX; t++){
for(int r = 0; r < totalY; r++){
newFields[t][r] = fields[t][r];
}
}
findStudentRecord();
// when looping though newFields[x][y] here it is already updated to the current colour
}
if (e.getSource() == btnReset)
{
for(int x = 0; x < totalX; x++){
for(int y = 0; y < totalY; y++){
fields[x][y].setText(newFields[x][y].getText());
fields[x][y].setBackground(newFields[x][y].getBackground());
// have tried this one but doesn't work
if(newFields[x][y].getBackground() == Color.green){
fields[x][y].setBackground(Color.green);
System.out.print(fields[x][y].getText() + "\n");
}
}
}
}
Here is the findStudentRecord()
public void findStudentRecord()
{
boolean found = false;
String strFind = txtSearch.getText();
for(int x = 0; x < totalX; x++){
for(int y = 0; y < totalY; y++){
if(fields[x][y].getText().equalsIgnoreCase(strFind))
{
found = true;
}
}
}
if (found)
{
for (int x = 0; x < totalX; x++)
{
for(int y = 0; y < totalY; y++){
if(fields[x][y].getText().equalsIgnoreCase(strFind))
{
fields[x][y].setBackground(new Color(255,217,200));
}
}
}
txtSearch.setText(txtSearch.getText() + " ...Found.");
}
else
{
txtSearch.setText(txtSearch.getText() + " ...Not Found.");
}
}
You have to create a new JTextField and add the relevant data:
newFields[t][r] = new javax.swing.JTextField();
newFields[t][r].setText(fields[t][r].getText());
newFields[t][r].setColumns(fields[t][r].getColumns());
// any other property you want to transfer
I want to Fill a 2D-Array with Javafx Labels in Which I can change the Text when I click it.
This is my actual Code but it's returning a NullPointer Exception.
Blockquote
`public static Label[][] initWelt() {
Label[][] welt = new Label[DIM1][DIM2];
for (int x = 1; x < welt.length - 1; x++) {
for (int y = 1; y < welt.length - 1; y++) {
if (Math.random() > 0.4) {
welt[x][y].setText("X");
}
else{
welt[x][y].setText(" ");
}
}
}
return welt;
}`
it's returning a NullPointer Exception.
The only thing the below code does is initialise a two-dimensional array, it doesn't populate the two-dimensional array, hence the NullPointerException occurs.
Label[][] welt = new Label[DIM1][DIM2];
Basically you can't call this:
welt[x][y].setText("X");
without populating the two-dimensional array with object references.
to overcome the problem first populate the two dimensional array, something like below:
Label[][] welt = new Label[DIM1][DIM2];
for(int i = 0; i < DIM1; i++){
for(int j = 0; j < DIM2; j++){
welt[i][j] = new Label();
}
}
then you can proceed with your current task at hand.
so now your code becomes like this:
public static Label[][] initWelt() {
Label[][] welt = new Label[DIM1][DIM2];
for(int i = 0; i < DIM1; i++){ //populate the array
for(int j = 0; j < DIM2; j++){
welt[i][j] = new Label();
}
}
for (int x = 0; x < DIM1; x++) {
for (int y = 0; y < DIM2; y++) {
if (Math.random() > 0.4) {
welt[x][y].setText("X");
}
else{
welt[x][y].setText(" ");
}
}
}
return welt;
}
Note - personally I think it would be better to refactor the current method and insert the code that populates the two-dimensional array in a different method.
I get the error ArrayOutOfBoundException: 15 at line 110:
System.out.println(coordinates[k][l]);
When trying to run this code :
import TUIO.*;
TuioProcessing tuioClient;
int cols = 15, rows = 10;
boolean[][] states = new boolean[cols][rows];
String[][] coordinates = new String[cols][rows];
int videoScale = 50;
// these are some helper variables which are used
// to create scalable graphical feedback
int x, y, i, j, k, l;
float cursor_size = 15;
float object_size = 60;
float table_size = 760;
float scale_factor = 1;
PFont font;
boolean verbose = false; // print console debug messages
boolean callback = true; // updates only after callbacks
void setup(){
size(500,500);
noCursor();
noStroke();
fill(0);
// periodic updates
if (!callback) {
frameRate(60); //<>//
loop();
} else noLoop(); // or callback updates
font = createFont("Arial", 18);
scale_factor = height/table_size;
// finally we create an instance of the TuioProcessing client
// since we add "this" class as an argument the TuioProcessing class expects
// an implementation of the TUIO callback methods in this class (see below)
tuioClient = new TuioProcessing(this);
}
void draw(){
// Begin loop for columns
for ( k = 0; k < cols; k++) {
// Begin loop for rows
for ( l = 0; l < rows; l++) {
// Scaling up to draw a rectangle at (x,y)
int x = k*videoScale;
int y = l*videoScale;
fill(255);
stroke(0);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
coordinates[i][j] = String.valueOf((char)(i+65)) + String.valueOf(j).toUpperCase();
}
}
/*
//check if coordinates are within a box (these are mouse x,y but could be fiducial x,y)
//simply look for bounds (left,right,top,bottom)
if( (mouseX >= x && mouseX <= x + videoScale) && //check horzontal
(mouseY >= y && mouseY <= y + videoScale)){
//coordinates are within a box, do something about it
System.out.println(coordinates[k][l]);
//you can keep track of the boxes states (contains x,y or not)
states[k][l] = true;
if(mousePressed) println(k+"/"+l);
}else{
states[k][l] = false;
}
*/
rect(x,y,videoScale,videoScale);
}
}
textFont(font,18*scale_factor);
float obj_size = object_size*scale_factor;
float cur_size = cursor_size*scale_factor;
ArrayList<TuioObject> tuioObjectList = tuioClient.getTuioObjectList();
for (int i=0;i<tuioObjectList.size();i++) {
TuioObject tobj= tuioObjectList.get(i);
stroke(0);
fill(0,0,0);
pushMatrix();
translate(tobj.getScreenX(width),tobj.getScreenY(height));
rotate(tobj.getAngle());
rect(-obj_size/2,-obj_size/2,obj_size,obj_size);
popMatrix();
fill(255);
text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
System.out.println(tobj.getSymbolID ()+ " " + tobj.getX());
if( ( tobj.getX()>= x && tobj.getX() <= x + videoScale) && //check horzontal
(tobj.getY() >= y && tobj.getY() <= y + videoScale)){
//coordinates are within a box, do something about it
System.out.println(coordinates[k][l]);
}
rect(x,y,videoScale,videoScale);
}
}
// --------------------------------------------------------------
// these callback methods are called whenever a TUIO event occurs
// there are three callbacks for add/set/del events for each object/cursor/blob type
// the final refresh callback marks the end of each TUIO frame
// called when an object is added to the scene
/* void addTuioObject(TuioObject tobj) {
if (verbose) println("add obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());
}
// called when an object is moved
void updateTuioObject (TuioObject tobj) {
if (verbose) println("set obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY());
}
// called when an object is removed from the scene
void removeTuioObject(TuioObject tobj) {
if (verbose) println("del obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");
}
*/
// --------------------------------------------------------------
// called at the end of each TUIO frame
void refresh(TuioTime frameTime) {
if (verbose) println("frame #"+frameTime.getFrameID()+" ("+frameTime.getTotalMilliseconds()+")");
if (callback) redraw();
}
Does it mean that it assigns a value at a greater place than the number of elements this array can contain ? How can I modify it ?
I can't find in the code where I specified the size of the array. I created it based on cols and rows (the 15 comes from there since when I modify it, say to 4, the error becomes ArrayOutOfBoundException: 4, but even with 1 there is an error so I don't get it) so I changed these values but I still get the error.
Thanks for your help
You are trying to use the variables k and l outside the following loop -
for ( k = 0; k < cols; k++) {
// Begin loop for rows
for ( l = 0; l < rows; l++) {
Seems like k and l are defined in instance variable scope. After the above loop has ended the values of k is cols and the value of l is rows, and i am assuming that the length of coordinates is (rows, cols) .
Hence, you are getting the issue, when you try to print that within the if condition - if( ( tobj.getX()>= x && tobj.getX() <= x + videoScale) && (tobj.getY() >= y && tobj.getY() <= y + videoScale))
Maybe you want to print it before the loop for k and l`l variables are over. That is before the following lines -
rect(x,y,videoScale,videoScale);
} // <-- Here 'l' loop ends.
} // <---- Here 'k' loop ends.
textFont(font,18*scale_factor);
float obj_size = object_size*scale_factor;
float cur_size = cursor_size*scale_factor;
Or maybe you do not want to end the k and l loop (the ones i mentioned in that start of this post) there? Did you miss commenting that part out?
Value for variable k is 15 at the end of this for loop
for ( k = 0; k < cols; k++) {
// Begin loop for rows
for ( l = 0; l < rows; l++) {
// Scaling up to draw a rectangle at (x,y)
int x = k*videoScale;
int y = l*videoScale;
fill(255);
stroke(0);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
coordinates[i][j] = String.valueOf((char)(i+65)) + String.valueOf(j).toUpperCase();
}
}
rect(x,y,videoScale,videoScale);
}
}
then you trying to access coordinates for index number coordinates[15][11] at this line :
System.out.println(coordinates[k][l]);
that's why you are getting Exception.
Hope this helps.
I am doing a game called 1010! Probably some of you have heard of it. Bascially I encouter some trouble when writing the Algorithm for clearance.
The rule is such that if any row or any column is occupied, then clear row and column respectively.
The scoring is such that each move gains a+10*b points. a is the number of square in the input piece p and b is the total number of row&column cleared.
To start, I create a two dimensional Array board[10][10], poulate each elements in the board[][] with an empty square.
In the class of Square, it has public void method of unset()-> "empty the square" & boolean status() -> "judge if square is empty"In the class of piece, it has int numofSquare -> "return the number of square in each piece for score calculation"
In particular, I don't know how to write it if both row and column are occupied as they are inter-cross each other in an two dimensional array.
It fail the test under some condition, in which some of the squares are not cleared but they should have been cleared and I am pretty sure is the logic problem.
My thinking is that:
Loop through squares in first row and first column, record the number of square that are occupied (using c and r); if both are 10, clear row&column, otherwise clear row or column or do nothing.
reset the c &r to 0, loop through square in the second row, second column…
update score.
Basically the hard part is that if I seperate clear column and clear row algorithm ,I will either judge row or column first then clear them . However, as every column contains at least one square belong to the row, and every row contains at least one square belong to the column, there will be mistake when both row and column are full.
Thanks for help.
import java.util.ArrayList;
public class GameState{
public static final int noOfSquares = 10;
// the extent of the board in both directions
public static final int noOfBoxes = 3;
// the number of boxes in the game
private Square[][] board; // the current state of the board
private Box[] boxes; // the current state of the boxes
private int score; // the current score
// initialise the instance variables for board
// all squares and all boxes are initially empty
public GameState()
{
getboard();
score = 0;
board = new Square[10][10];
for(int i =0;i<board.length;i++){
for(int j =0;j<board[i].length;j++){
board[i][j] = new Square();
}
}
boxes = new Box[3];
for(int k =0;k<boxes.length;k++){
boxes[k] = new Box();
}
}
// return the current state of the board
public Square[][] getBoard()
{
return board;
}
// return the current score
public int getScore()
{
return score;
}
// place p on the board with its (notional) top-left corner at Square x,y
// clear columns and rows as appropriate
int r =0;
int c = 0;
int rowandcolumn = 0;
for (int row=0;row<10;row++){
for (int column=0;column<10;column++) {
if (board[row][column].status() == true){
c = c + 1;
if( c == 10 ) {
rowandcolumn = rowandcolumn + 1;
for(int z=0;z<10;z++){
board[row][z].unset(); //Clear column
}
}
}
if (board[column][row].status() == true){
r = r + 1;
if( r == 10) {
rowandcolumn = rowandcolumn + 1;
for(int q=0;q<10;q++){
board[q][row].unset(); //Clear row
}
}
}
}
r=0; //reset
c=0;
}
score = score + p.numberofBox()+10*rowandcolumn;
}
how about this
void Background::liquidate(int &score){
int arr_flag[2][10]; //0 is row,1 is column。
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 10; j++)
{
arr_flag[i][j] = 1;
}
}
//column
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (arr[i][j].type == 0)
{
arr_flag[0][i] = 0;
break;
}
}
}
//row
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (arr[j][i].type == 0)
{
arr_flag[1][i] = 0;
break;
}
}
}
//clear column
for (int i = 0; i < 10; i++)
{
if (arr_flag[0][i] == 1)
{
for (int j = 0; j < 10; j++)
{
arr[i][j].Clear();
}
}
}
//clear row
for (int i = 0; i < 10; i++)
{
if (arr_flag[1][i] == 1)
{
for (int j = 0; j < 10; j++)
{
arr[j][i].Clear();
}
}
}
}
I tried to write somme code for the idea I posted
// place p on the board with its (notional) top-left corner at Square x,y
// clear columns and rows as appropriate
int r =0;
int c = 0;
int rowandcolumn = 0;
int row=FindFirstRow();
int column=FindFirstColumn();
if(row!=-1 && column!=-1)
{
rowandcolumn++;
//actions here: row found and column found
//clear row and column
clearRow(row);
clearColumn(column);
}
else if(row!=-1)
{
//only row is found
//clear row
clearRow(row);
}
else if(column!=-1)
{
//only column is found
//clear column
clearColumn(column);
}
else
{
//nothing is found
}
public void clearRow(int row)
{
for(int i=0; i<10;i++)
{
board[row][i].unset();
}
}
public void clearColumn(int column)
{
for(int i=0; i<10;i++)
{
board[i][column].unset();
}
}
//this method returns the first matching row index. If nothing is found it returns -1;
public int FindFirstRow()
{
for (int row=0;row<10;row++)
{
int r=0;
for (int column=0;column<10;column++)
{
if (board[row][column].status() == true)
{
r = r + 1;
if( r == 10)
{
//row found
return row;
}
}
}
r=0; //reset
}
//nothing found
return -1;
}
//this method returns the first matching column index. If nothing is found it returns -1;
public int FindFirstColumn()
{
for (int column=0;column<10;column++)
{
int c=0;
for (int row=0;row<10;row++)
{
if (board[row][column].status() == true)
{
c = c + 1;
if( c == 10 )
{
//matching column found
return column;
}
}
}
c=0; //reset
}
//nothing found
return -1;
}
In my last question seen here: Sudoku - Region testing I asked how to check the 3x3 regions and someone was able to give me a satisfactory answer (although it involved a LOT of tinkering to get it working how I wanted to, since they didn't mention what the class table_t was.)
I finished the project and was able to create a sudoku generator, but it feels like it's contrived. And I feel like I've somehow overcomplicated things by taking a very brute-force approach to generating the puzzles.
Essentially my goal is to create a 9x9 grid with 9- 3x3 regions. Each row / col / region must use the numbers 1-9 only once.
The way that I went about solving this was by using a 2-dimensional array to place numbers at random, 3 rows at a time. Once the 3 rows were done it would check the 3 rows, and 3 regions and each vertical col up to the 3rd position. As it iterated through it would do the same until the array was filled, but due to the fact that I was filling with rand, and checking each row / column / region multiple times it felt very inefficient.
Is there an "easier" way to go about doing this with any type of data construct aside from a 2d array? Is there an easier way to check each 3x3 region that might coincide with checking either vert or horizontal better? From a standpoint of computation I can't see too many ways to do it more efficiently without swelling the size of the code dramatically.
I built a sudoku game a while ago and used the dancing links algorithm by Donald Knuth to generate the puzzles. I found these sites very helpful in learning and implementing the algorithm
http://en.wikipedia.org/wiki/Dancing_Links
http://cgi.cse.unsw.edu.au/~xche635/dlx_sodoku/
http://garethrees.org/2007/06/10/zendoku-generation/
import java.util.Random;
import java.util.Scanner;
public class sudoku {
/**
* #antony
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int p = 1;
Random r = new Random();
int i1=r.nextInt(8);
int firstval = i1;
while (p == 1) {
int x = firstval, v = 1;
int a[][] = new int[9][9];
int b[][] = new int[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if ((x + j + v) <= 9)
a[i][j] = j + x + v;
else
a[i][j] = j + x + v - 9;
if (a[i][j] == 10)
a[i][j] = 1;
// System.out.print(a[i][j]+" ");
}
x += 3;
if (x >= 9)
x = x - 9;
// System.out.println();
if (i == 2) {
v = 2;
x = firstval;
}
if (i == 5) {
v = 3;
x = firstval;
}
}
int eorh;
Scanner in = new Scanner(System.in);
System.out
.println("hey lets play a game of sudoku:take down the question and replace the 0's with your digits and complete the game by re entering your answer");
System.out.println("enter your option 1.hard 2.easy");
eorh = in.nextInt();
switch (eorh) {
case 1:
b[0][0] = a[0][0];
b[8][8] = a[8][8];
b[0][3] = a[0][3];
b[0][4] = a[0][4];
b[1][2] = a[1][2];
b[1][3] = a[1][3];
b[1][6] = a[1][6];
b[1][7] = a[1][7];
b[2][0] = a[2][0];
b[2][4] = a[2][4];
b[2][8] = a[2][8];
b[3][2] = a[3][2];
b[3][8] = a[3][8];
b[4][2] = a[4][2];
b[4][3] = a[4][3];
b[4][5] = a[4][5];
b[4][6] = a[4][6];
b[5][0] = a[5][0];
b[5][6] = a[5][6];
b[6][0] = a[6][0];
b[6][4] = a[6][4];
b[6][8] = a[6][8];
b[7][1] = a[7][1];
b[7][2] = a[7][2];
b[7][5] = a[7][5];
b[7][6] = a[7][6];
b[8][4] = a[8][4];
b[8][5] = a[8][5];
b[0][0] = a[0][0];
b[8][8] = a[8][8];
break;
case 2:
b[0][3] = a[0][3];
b[0][4] = a[0][4];
b[1][2] = a[1][2];
b[1][3] = a[1][3];
b[1][6] = a[1][6];
b[1][7] = a[1][7];
b[1][8] = a[1][8];
b[2][0] = a[2][0];
b[2][4] = a[2][4];
b[2][8] = a[2][8];
b[3][2] = a[3][2];
b[3][5] = a[3][5];
b[3][8] = a[3][8];
b[4][0] = a[4][0];
b[4][2] = a[4][2];
b[4][3] = a[4][3];
b[4][4] = a[4][4];
b[4][5] = a[4][5];
b[4][6] = a[4][6];
b[5][0] = a[5][0];
b[5][1] = a[5][1];
b[5][4] = a[5][4];
b[5][6] = a[5][6];
b[6][0] = a[6][0];
b[6][4] = a[6][4];
b[6][6] = a[6][6];
b[6][8] = a[6][8];
b[7][0] = a[7][0];
b[7][1] = a[7][1];
b[7][2] = a[7][2];
b[7][5] = a[7][5];
b[7][6] = a[7][6];
b[8][2] = a[8][2];
b[8][4] = a[8][4];
b[8][5] = a[8][5];
break;
default:
System.out.println("entered option is incorrect");
break;
}
for (int y = 0; y < 9; y++) {
for (int z = 0; z < 9; z++) {
System.out.print(b[y][z] + " ");
}
System.out.println("");
}
System.out.println("enter your answer");
int c[][] = new int[9][9];
for (int y = 0; y < 9; y++) {
for (int z = 0; z < 9; z++) {
c[y][z] = in.nextInt();
}
}
for (int y = 0; y < 9; y++) {
for (int z = 0; z < 9; z++)
System.out.print(c[y][z] + " ");
System.out.println();
}
int q = 0;
for (int y = 0; y < 9; y++) {
for (int z = 0; z < 9; z++)
if (a[y][z] == c[y][z])
continue;
else {
q++;
break;
}
}
if (q == 0)
System.out
.println("the answer you have entered is correct well done");
else
System.out.println("oh wrong answer better luck next time");
System.out
.println("do you want to play a different game of sudoku(1/0)");
p = in.nextInt();
firstval=r.nextInt(8);
/*if (firstval > 8)
firstval -= 9;*/
}
}
}
I think you can use a 1D array, in much the same way a 1D array can model a binary tree. For example, to look at the value below a number, add 9 to the index.
I just made this up, but could something like this work?
private boolean makePuzzle(int [] puzzle, int i)
{
for (int x = 0; x< 10 ; x++)
{
if (//x satisfies all three conditions for the current square i)
{
puzzle[i]=x;
if (i==80) return true //terminal condition, x fits in the last square
else
if makePuzzle(puzzle, i++);//find the next x
return true;
}// even though x fit in this square, an x couldn't be
// found for some future square, try again with a new x
}
return false; //no value for x fit in the current square
}
public static void main(String[] args )
{
int[] puzzle = new int[80];
makePuzzle(puzzle,0);
// print out puzzle here
}
Edit: its been a while since I've used arrays in Java, sorry if I screwed up any syntax. Please consider it pseudo code :)
Here is the code as described below in my comment.
public class Sudoku
{
public int[] puzzle = new int[81];
private void makePuzzle(int[] puzzle, int i)
{
for (int x = 1; x< 10 ; x++)
{
puzzle[i]=x;
if(checkConstraints(puzzle))
{
if (i==80)//terminal condition
{
System.out.println(this);//print out the completed puzzle
puzzle[i]=0;
return;
}
else
makePuzzle(puzzle,i+1);//find a number for the next square
}
puzzle[i]=0;//this try didn't work, delete the evidence
}
}
private boolean checkConstraints(int[] puzzle)
{
int test;
//test that rows have unique values
for (int column=0; column<9; column++)
{
for (int row=0; row<9; row++)
{
test=puzzle[row+column*9];
for (int j=0;j<9;j++)
{
if(test!=0&& row!=j&&test==puzzle[j+column*9])
return false;
}
}
}
//test that columns have unique values
for (int column=0; column<9; column++)
{
for(int row=0; row<9; row++)
{
test=puzzle[column+row*9];
for (int j=0;j<9;j++)
{
if(test!=0&&row!=j&&test==puzzle[column+j*9])
return false;
}
}
}
//implement region test here
int[][] regions = new int[9][9];
int[] regionIndex ={0,3,6,27,30,33,54,57,60};
for (int region=0; region<9;region++) //for each region
{
int j =0;
for (int k=regionIndex[region];k<regionIndex[region]+27; k=(k%3==2?k+7:k+1))
{
regions[region][j]=puzzle[k];
j++;
}
}
for (int i=0;i<9;i++)//region counter
{
for (int j=0;j<9;j++)
{
for (int k=0;k<9;k++)
{
if (regions[i][j]!=0&&j!=k&®ions[i][j]==regions[i][k])
return false;
}
}
}
return true;
}
public String toString()
{
String string= "";
for (int i=0; i <9;i++)
{
for (int j = 0; j<9;j++)
{
string = string+puzzle[i*9+j];
}
string =string +"\n";
}
return string;
}
public static void main(String[] args)
{
Sudoku sudoku=new Sudoku();
sudoku.makePuzzle(sudoku.puzzle, 0);
}
}
Try this code:
package com;
public class Suduku{
public static void main(String[] args ){
int k=0;
int fillCount =1;
int subGrid=1;
int N=3;
int[][] a=new int[N*N][N*N];
for (int i=0;i<N*N;i++){
if(k==N){
k=1;
subGrid++;
fillCount=subGrid;
}else{
k++;
if(i!=0)
fillCount=fillCount+N;
}
for(int j=0;j<N*N;j++){
if(fillCount==N*N){
a[i][j]=fillCount;
fillCount=1;
System.out.print(" "+a[i][j]);
}else{
a[i][j]=fillCount++;
System.out.print(" "+a[i][j]);
}
}
System.out.println();
}
}
}