How to organize a single array into a two-dimensional one? - java

I am trying to split a one dimensional array of display modes into a 2-dimensional string array, though I have encountered trouble where I got to the point of splitting the sorted array of display modes. My question: How can I split the single array, which is already sorted, into a two dimensional array? The code (Sorry about the weird variable names):
public static String[][] OrganizeDisplayModes (DisplayMode[] modes) {
int iter = 0;
int deltaIter = 0;
int rows = 0;
int columns = 0;
String[][] tobe;
//bubble sorting
for (int a = 0; a < modes.length - 1; a++) {
for(int i = 0; i < modes.length - 1; i++) {
if (modes[i].getWidth() < modes[i+1].getWidth()) {
DisplayMode change = modes[i];
modes[i] = modes[i+1];
modes[i+1] = change;
}
}
}
for(int i = 0; i < modes.length - 1; i++) {
if ((modes[i].getWidth() == modes[i+1].getWidth()) && (modes[i].getBitsPerPixel() < modes[i+1].getBitsPerPixel())) {
DisplayMode change = modes[i];
modes[i] = modes[i+1];
modes[i+1] = change;
}
}
for(int i = 0; i < modes.length - 1; i++) {
if ((modes[i].getWidth() == modes[i+1].getWidth()) && (modes[i].getFrequency() < modes[i+1].getFrequency())) {
DisplayMode change = modes[i];
modes[i] = modes[i+1];
modes[i+1] = change;
}
}
for(int i = 0; i < modes.length; i++) {
DisplayMode current = modes[i];
System.out.println(i + ". " + current.getWidth() + "x" + current.getHeight() + "x" +
current.getBitsPerPixel() + " " + current.getFrequency() + "Hz");
}
//fit into string array
for (int i = 0; i < modes.length - 1; i++) {
if (!(modes[i].getWidth() == modes[i+1].getWidth())) {
rows += 1;
deltaIter = i - deltaIter;
if (deltaIter > columns)
columns = deltaIter;
}
}
//split the displaymode array into the two-dimensional string one here
tobe = new String[rows][columns];
for (int i = 0; i < rows; i++) {
for (int a = 0; a < columns; a++) {
if((modes[iter].getWidth() == modes[iter+1].getWidth())) {
tobe[i][a] = iter + ". " + modes[iter].toString() + " ";
}
else
break;
if (!(iter >= 68))
iter += 1;
}
if (iter >= 68)
break;
}
tobe[rows-1][columns-1] = (iter + 1) + ". " + modes[iter].toString() + " ";
//test to see that it works
for (int i = 0; i < rows; i++) {
for (int a = 0; a < columns; a++) {
if(tobe[i][a] != null)
System.out.print(tobe[i][a]);
else
break;
}
System.out.println("");
}
System.exit(0);
return null;
}
The output looks like this:
0. 1440x900x32 75Hz
1. 1440x900x16 75Hz
2. 1440x900x32 60Hz
3. 1440x900x16 60Hz
with all of the different possible resolutions. Basically, what I'm trying to do is make a readable list of the different resolutions, so a user can select their desired one. Thank you.

How about storing the information in a different manner? When I made the transition from C to Java I used to use multi-dimensional arrays quite a bit, but it's a pain.
public class ScreenType implements Comparable {
int width;
int height;
int color; // not sure what this one was for, assuming 32/16 bit color
int hertz;
public ScreenType(int w, int h, int c, int h) {
width = w;
height = h;
color = t;
hertz = h;
}
// remember to maintain transitive property,
// see JDK documentation for Comparable
#Override
public int compareTo(Tuple other) {
}
// getters and setters and whatnot
#Override
public String toString() {
return width + "X" + height + "X" + color+ " " + hertz + "hz";
}
}
Then store your screen data like this and use an ArrayList of ScreenTypes which, if I remember right, will sort based on your overriding of compareTo()

Related

Checking to see if two 2D boolean arrays are equal at a given interval: Java

I have two 2d boolean arrays, the smaller array (shape) is going over the larger array (world).
I am having trouble to find a method to find out when the smaller array can "fit" into the larger one.
When I run the code it either just goes through the larger array, never stopping, or stops after one step (incorrectly).
public void solve() {
ArrayList<Boolean> worldList=new ArrayList<>();
ArrayList<Boolean> shapeList=new ArrayList<>();
for (int i = 0; i < world.length; i++) {
for (int k = 0; k < world[i].length; k++) {
worldList.add(world[i][k]);
display(i, k, Orientation.ROTATE_NONE);
for (int j = 0; j < shape.length; j++) {
for (int l = 0; l < shape[j].length; l++) {
shapeList.add(shape[j][l]);
if(shapeList.equals(worldList)) {
return;
}
}
}
}
}
}
A good place to start with a problem like this is brute force for the simplest case. So, for each index in the world list, just check to see if every following index of world and shapes match.
Notice we only iterate to world.size()-shapes.size(), because naturally if shapes is longer than the portion of world we haven't checked, it won't fit.
import java.util.ArrayList;
public class Test {
ArrayList<Boolean> world = new ArrayList<>();
ArrayList<Boolean> shapes = new ArrayList<>();
public static void main(String[] args) {
new Work();
}
public Test() {
world.add(true);
world.add(false);
world.add(false);
world.add(true);
shapes.add(false);
shapes.add(true);
// Arraylists initialized to these values:
// world: T F F T
// shapes: F T
System.out.println(getFitIndex());
}
/**
* Get the index of the fit, -1 if it won't fit.
* #return
*/
public int getFitIndex() {
for (int w = 0; w <= world.size()-shapes.size(); w++) {
boolean fits = true;
for (int s = 0; s < shapes.size(); s++) {
System.out.println("Compare shapes[" + s + "] and world["+ (w+s) + "]: " +
shapes.get(s).equals(world.get(w+s)));
if (!shapes.get(s).equals(world.get(w+s))) fits = false;
}
System.out.println();
if (fits) return w;
}
return -1;
}
}
When we run this code, we get a value of 2 printed to the console, since shapes does indeed fit inside world, starting at world[2].
You can find the row and column of fitting like this
public void fit() {
int h = world.length - shape.length;
int w = world[0].length - shape[0].length;
for (int i = 0; i <= h; i++) {
for (int k = 0; k <= w; k++) {
boolean found = true;
for (int j = 0; j < shape.length && found; j++) {
for (int l = 0; l < shape[j].length && found; l++) {
if (shape[j][l] != world[i + j][k + l])
found = false;
}
}
if (found) {
//Your shape list fit the world list at starting index (i, k)
//You can for example save the i, k variable in instance variable
//Or return then as an object for further use
return;
}
}
}

Find all common substrings with a minimum length of 2 characters

Task's content:
Find all (starting from the longest, the shortest ending) common substrings with a minimum length of 2 characters, which do not overlap with previously found.
My task is to remake below code or write new one. I don't even know how to start with it :/
public static void main(String[] args) {
String text1 = "AABABB";
String text2 = "BAABAB";
int len1 = text1.length();
int len2 = text2.length();
int max = 0;
int position_w1 = -1;
int position_w2 = -1;
for (int i = 0; i < len1 - max; i++)
{
for (int k = len2 - 1; k >= 0; k--)
{
int counter = 0;
int limit = Math.min(len2, (len1 -i + k) );
for (int j = k; j < limit; j++)
{
if (text1.charAt(i+j-k) == text2.charAt(j))
{
counter++;
if (max < counter)
{
max = counter;
position_w1 = i + j - k - max + 1;
position_w2 = j - max + 1;
}
}
else counter = 0;
}
}
}
System.out.println("Position of text1: " + position_w1 + ", position of text2: " + position_w2 + ", length: " + max);
System.out.println(text1.substring(0, position_w1) + "\u001B[31m"
+ text1.substring(position_w1, max + position_w1) + "\u001B[0m" + text1.substring(max + position_w1) );
System.out.println(text2.substring(0, position_w2) + "\u001B[31m"
+ text2.substring(position_w2, max + position_w2) + "\u001B[0m" + text2.substring(max + position_w2) );
}
Maybe try something like this :
public static void main(String[] args){
String text1 = "AABABB";
String text2 = "BAABAB";
int maxLen = 3;
for (int len = 2; len < maxLen; len++) {
ArrayList<Pair<Integer, Integer>> intervalsText1 = new ArrayList<>();
ArrayList<Pair<Integer, Integer>> intervalsText2 = new ArrayList<>();
for (int i = 0; i < text1.length() - len; i++) {
String sub1 = text1.substring(i, i + len);
for (int j = 0; j < text2.length() - len; i++) {
String sub2 = text2.substring(j, j + len);
if(sub1.equals(sub2)){
//check if overlapping
for(Pair<Integer, Integer> inter : intervalsText1){
for(Pair<Integer, Integer> inter : intervalsText1){
if(!isInsideInterval(inter, i) && !isInsideInterval(inter, i + len)){
if(!isInsideInterval(inter, j) && !isInsideInterval(inter, j + len)){
//the strings are equal and outside previous strings
intervalsText1.add(Pair.of(i, i + len));
intervalsText2.add(Pair.of(j, j + len));
}
}
}
}
}
}
}
}
}
public static boolean isInsideInterval(Pair<Integer, Integer> interval, int toCheck){
if(interval.getLeft() < toCheck && interval.getRight() > toCheck)
return true;
return false;
}
*Please note that I haven't checked it working
As this is home work, just some mental help.
Immediately diving into for-i like loops is hard to read and/or change.
Consider how one would do the task oneself, and see what the original writer did.
Starting with the maximal subsequence, would be:
int max = Math.min(len1, len2);
...
--max;
or even
for (int max = Math.min(len1, len2); may > 0; --max) {
for (int i = 0; i + max <= len1; ++i) {
if (match found) {
print match;
i += max - 1;
}
}
}
The for-k loop does a --k; a bit needlessly IMHO.
Matching a substring and when matched skip by i += max; or i += max - 1; would benefit from the String.substring or your own function for that.
By the way, when ready try to consider things like skipping a second occurrence of a sought substring: "AB" appears twice in text1. Good names important, comments might help. Otherwise demerits for "circumstantial code" might follow.
int notEarlierPos = text1.indexOf(text1.subtring(i, i + max));
if (notEarlierPos == i) { // Not already treated earlier.
int matchPos = text2.indexOf(text1.subtring(i, i + max));
if (matchPos != -1) {
print match;
i += max - 1;
}
}

drawing Diamond of numbers with 2D array in java

So I need to make a diamond_look of numbers using 2D array in Java. I got my results but with null before the diamond. For drawNumDiamond(9) I have to get a diamond look that goes until 5 and back. I know I can make it without using array, but I want to learn more about 2D arrays :this is how it should look like and what are my results
public class Example1{
private static void drawNumDiamond(int h) {
if(h%2 != 0) {
int size = h/2 +1;
int count = 1;
int loop = 1;
String[][] dijamant = new String[h][];
for(int row = 0; row < dijamant.length; row++) {
dijamant[row] = new String[row+1];
for(int kolona=0; kolona<=row; kolona++) {
dijamant[0][0] = "1";
for(int i=0; i< loop;i++) {
dijamant[row][kolona]+= count;
}
}
count++;
loop+=2;
}
for (int k = 0; k < size; k++) {
System.out.printf("%" + h + "s", dijamant[k]);
h++;
System.out.println();
}
h--;
for (int q = size - 2; q>=0; q--) {
h--;
System.out.printf("%" + h + "s", dijamant[q]);
System.out.println();
}
}
}
public static void main(String[] args) {
drawNumDiamond(9);
}
}
The issue is in this line :
dijamant[row][kolona] += count;
if dijamant[row][kolona] is null and count is 2, the result of the string concatenation will be "null2". Try adding the following if statement before to initialize with an empty string :
if (dijamant[row][kolona] == null) {
dijamant[row][kolona] = "";
}
This will get your code working, but there are still things to think about. E.g. you keep setting dijamant[0][0] = "1"; in the loop.

print 2 dimentional array center aligned

Hi I am trying to print a two dimensional array that is center aligned but the numbers point to the memory cell if im correct. How would I go about getting them to print the actual numbers, Ive tried creating a display method and that didnt work. Here is my code so far. I am also going to be finding the min, max and avg after I figure this out.
import java.util.Scanner;
public class Print2DArray {
public static void main(String[] args) {
Scanner print2d = new Scanner(System.in);
System.out.println("Please enter the number of rows: ");
int rows = print2d.nextInt();
System.out.println("Please enter the number of columns: ");
int columns = print2d.nextInt();
int array[][] = new int[rows][columns];
System.out.println("\nVictor - 002017044\n");
for (int x = 0; x < array.length; x++) {
for (int y = 0; y < array[x].length; y++) {
int value = (int) (Math.random() * 10000);
value = (int) (Math.round((value * 100)) / 100.0);
array[x][y] = value;
}
}
for (int x = 0; x < array.length; x++) {
for (int y = 0; y < array[x].length; y++) {
printArray(array);
}
System.out.println();
}
int max = 0;
int avg = 0;
int min = 0;
System.out.println("\nMaximum: " + max + "\nAverage: " + avg
+ "\nMinimum: " + min);
}
private static void printArray(int [][] array){
int width = 6;
int leftSP = (width - array.length)/2;
int rightSP = width - array.length - leftSP;
for (int i = 0; i < leftSP; i++) {
System.out.print(" ");
}
System.out.print(array);
for (int i =0; i < rightSP; i++) {
System.out.print(" ");
}
}
}
I'm not quite sure what you mean by center aligned in this context. Centering something requires you to know how much space you are allowed to print to and then evenly distributing what you are displaying to both sides of the width/2. For example, by default, in cmd.exe you are limited to 80 characters across, but this can be changed. However, I think the core of this answer is here:
Can I find the console width with Java?
Basically, you can't center it. The best you can hope for is to left align it (or attempt to center it based on some arbitrary pre-determined width).
Based on what you wrote though, and what I see in printArray, your other issue is that you don't know how to print out a value at an index of an array. Before I address that, I must address something you wrote
but the numbers point to the memory cell if im correct
This is actually incorrect. This is the default functionality of the toString method, per the java.lang.Object#toString method:
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `#', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '#' + Integer.toHexString(hashCode())
http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString%28%29
Your print method should probably look like:
private static void printArray(int [][] array){
if(array == null || array.length < 1 || array[0].length < 1)
throw new IllegalArgumentException("array must be non-null, and must have a size of at least \"new int[1][1]\"");
for (int i = 0; i < array.length; i++) {
for(int j = 0; j < array[0].length; j++)
System.out.print("[" + array[i][j] + "]");
System.out.println();
}
}
EDIT:
I saw a comment you made in which you specify what you mean by center aligned. Basically you will want to record the maximum length of any int you are placing into the array, like the following:
//global max value
public static int maxLength = 0;
...
//inside of Print2DArray.main(String [] args)
for (int x = 0; x < array.length; x++) {
for (int y = 0; y < array[x].length; y++) {
value = (int) (Math.round((value * 100)) / 100.0);
int numberOfDigits = String.valueOf(value).length();
if(numberOfDigits > Print2DArray.maxLength)
Print2DArray.maxLength = numberOfDigits;
array[x][y] = value;
}
}
Then you will want to adjust your printArray function's print from
System.out.print("[" + array[i][j] + "]");
to
System.out.printf("[%" + Print2DArray.maxLength + "d]", array[i][j]);
first fix bug
for (int y = 0; y < array[x].length; y++) {
printArray(array[x][y]);//chage printArray(array) to printArray(array[x][y])
}
second modify printlnArray
private static void printArray(int v){
System.out.format("%-8s",String.valueOf(v));
}
center-align
private static int[] widthes = {9,99,999,9999,99999};
private static int numberWidth(int v) {
for (int i = 0; i < widthes.length; i++) {
if(widthes[i] > v) return (i+1);
}
return -1;
}
private static void printArray(int v){
int width = 8;
int numberWidth = numberWidth(v);
int left = (width-numberWidth)/2;
int right = width - numberWidth - left;
for (int i = 0; i < left; i++) {
System.out.print(" ");
}
System.out.print(v);
for (int i = 0; i < right; i++) {
System.out.print(" ");
}
}
more reference
How to align String on console output

Java Sudoku Generator(easiest solution)

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&&regions[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();
}
}
}

Categories