### Thread: Simple Sudoku Game with Processing

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
4
Rep Power
0

#### Simple Sudoku Game with Processing

Hi All,

Got this assignment that requires building a 4x4 grid sudoku game using processing, which is a simplified java-based, that can then be easily changed to a 9x9 one, so starting off with one that can take an n sized algorithm to begin with would be best.

Super stuck tho! Have managed the following so far, but I can't seem to manage to amalgamate it all to create my end result, and I know this is close to go by:

//simple snap-to-grid sketch
float gridSize = 16;
int finalX1, finalY1, finalX2, finalY2;
int currentMouseY, currentMouseX;
void setup(){
//size of screen
size(512,512);
background(0);
stroke(255);
strokeWeight(5);
//change framrate for less points??
frameRate(8);
}
void draw(){
if (mousePressed){
//set current mouseX and mouseY to some value (initially set to 0)
currentMouseX = mouseX;
currentMouseY = mouseY;
//calculate the new position of point 1
float x1 = currentMouseX/gridSize;
int roundedX1 = round(x1);
int finalX1 = (int)(roundedX1*gridSize);
float y1 = currentMouseY/gridSize;
int roundedY1 = round(y1);
int finalY1 = (int)(roundedY1*gridSize);
//calculate the new position of point 2
if (finalX2 == 0 && finalY2 == 0){
finalX2 = finalX1;
finalY2 = finalY1;
}
else if (finalX2 != finalX1 || finalY2 != finalY1){
line(finalX1, finalY1, finalX2, finalY2);
finalY2 = finalY1;
finalX2 = finalX1;
}
println(finalX2);
println(finalY2);
println(" ");
}
}

//pseudo algorithm
SubGridSize = 3;
WholeGridSize = SubGridSize * SubGridSize;
Grid = array : [WholeGridSize by WholeGridSize];
for (int i = 0; i < WholeGridSize; i++)
for (int j = 0; j < WholeGridSize; j++)
Grid[i][j] = (i*SubGridSize + i/SubGridSize + j) % (WholeGridSize) + 1;

Would super appreciate it if anyone could help get me out of this rut, please?
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
165
Rep Power
22
Well i dont code in JAVA but i have written a simple sudoku application in C++ back in school. Basically i just used a 2D array to hold the values. After the user filled out the sudoku table i would execute a series of loops to check validity and completion.

pseudo code
Code:
```int Dimension = 9;
int Grid[Dimension, Dimension];
bool Numbers[9];
bool FailFlag=false;

for(x=0, x < Dimension, X++){
for(y=0,y < Dimension, Y++){
if(Grid[X,Y]<10){
if (Numbers[Grid[X,Y]]){
FailFlag=True;
Break;
}else{
Numbers[Grid[X,Y]]=true;
}
}else{
FailFlag=True;
}
}
Numbers=[false,false,false,false,false,false,false,false,false];
}
for(y=0, y < Dimension, y++){
for(x=0,x < Dimension, x++){
if(Grid[X,Y]<10){
if (Numbers[Grid[X,Y]]){
FailFlag=True;
Break;
}else{
Numbers[Grid[X,Y]]=true;
}
}else{
FailFlag=True;
}
}
Numbers=[false,false,false,false,false,false,false,false,false];
}

for(i=0,i<(Dimension/3),i++){
for(j=0,j<(Dimension/3),j++){
for(x=j*3, x<3+j*3, x++){
for(y=i*3,x<3+i*3,y++){
if(Grid[X,Y]<10){
if (Numbers[Grid[X,Y]]){
FailFlag=True;
Break;
}else{
Numbers[Grid[X,Y]]=true;
}
}else{
FailFlag=True;
}
}
Numbers=[false,false,false,false,false,false,false,false,false];
}
}
}```

Originally Posted by ir-Rax
Hi All,

Got this assignment that requires building a 4x4 grid sudoku game using processing, which is a simplified java-based, that can then be easily changed to a 9x9 one, so starting off with one that can take an n sized algorithm to begin with would be best.

Super stuck tho! Have managed the following so far, but I can't seem to manage to amalgamate it all to create my end result, and I know this is close to go by:

//simple snap-to-grid sketch
float gridSize = 16;
int finalX1, finalY1, finalX2, finalY2;
int currentMouseY, currentMouseX;
void setup(){
//size of screen
size(512,512);
background(0);
stroke(255);
strokeWeight(5);
//change framrate for less points??
frameRate(8);
}
void draw(){
if (mousePressed){
//set current mouseX and mouseY to some value (initially set to 0)
currentMouseX = mouseX;
currentMouseY = mouseY;
//calculate the new position of point 1
float x1 = currentMouseX/gridSize;
int roundedX1 = round(x1);
int finalX1 = (int)(roundedX1*gridSize);
float y1 = currentMouseY/gridSize;
int roundedY1 = round(y1);
int finalY1 = (int)(roundedY1*gridSize);
//calculate the new position of point 2
if (finalX2 == 0 && finalY2 == 0){
finalX2 = finalX1;
finalY2 = finalY1;
}
else if (finalX2 != finalX1 || finalY2 != finalY1){
line(finalX1, finalY1, finalX2, finalY2);
finalY2 = finalY1;
finalX2 = finalX1;
}
println(finalX2);
println(finalY2);
println(" ");
}
}

//pseudo algorithm
SubGridSize = 3;
WholeGridSize = SubGridSize * SubGridSize;
Grid = array : [WholeGridSize by WholeGridSize];
for (int i = 0; i < WholeGridSize; i++)
for (int j = 0; j < WholeGridSize; j++)
Grid[i][j] = (i*SubGridSize + i/SubGridSize + j) % (WholeGridSize) + 1;

Would super appreciate it if anyone could help get me out of this rut, please?
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
165
Rep Power
22
With the above pseudo code off the top of my head is how i rendered mine in C++. Our instructor had given us the GUI already compiled and it had passed

Int Dimension and int Grid[Dimension, Dimension]

and the application returned bool failflag back to the original application

We used this for 3X3 6X6 9X9 12X12 and 15X15 grids
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
4
Rep Power
0
Hi,

Thanks for this! You don't really use java you said? Do you think you could make anything out of this:

void setup() {
String theProblem = "018 034 052 076 113 124 169 171 209 216 278 284 332 341 356 533 545 557 608 614 677 685 712 726 761 773 819 837 851 874";

int[][] matrix = parseProblem(split(theProblem, " "));
writeMatrix(matrix);
if (solve(0, 0, matrix)) // solves in place
writeMatrix(matrix);
else
println("NONE");
}

boolean solve(int i, int j, int[][] cells) {
if (i == 9) {
i = 0;
if (++j == 9)
return true;
}
if (cells[i][j] != 0) // skip filled cells
return solve(i+1, j, cells);

for (int val = 1; val <= 9; ++val) {
if (legal(i, j, val, cells)) {
cells[i][j] = val;
if (solve(i+1, j, cells))
return true;
}
}
cells[i][j] = 0; // reset on backtrack
return false;
}

boolean legal(int i, int j, int val, int[][] cells) {
for (int k = 0; k < 9; ++k) // row
if (val == cells[k][j])
return false;

for (int k = 0; k < 9; ++k) // col
if (val == cells[i][k])
return false;

int boxRowOffset = (i / 3)*3;
int boxColOffset = (j / 3)*3;
for (int k = 0; k < 3; ++k) // box
for (int m = 0; m < 3; ++m)
if (val == cells[boxRowOffset+k][boxColOffset+m])
return false;

return true; // no violations, so it's legal
}

int[][] parseProblem(String[] args) {
int[][] problem = new int[9][9]; // default 0 vals
for (int n = 0; n < args.length; ++n) {
int i = int(args[n].substring(0, 1));
int j = int(args[n].substring(1, 2));
int val = int(args[n].substring(2, 3));
problem[i][j] = val;
}
return problem;
}

void writeMatrix(int[][] solution) {
for (int i = 0; i < 9; ++i) {
if (i % 3 == 0)
println(" -----------------------");
for (int j = 0; j < 9; ++j) {
if (j % 3 == 0) print("| ");
if (solution[i][j] == 0) {
print(" ");
}
else {
print(solution[i][j]);
}
print(' ');
}
println("|");
}
println(" -----------------------");
}

It's basically pretty much what I'm after, since:

1. There's no need for the game to be a random one each time. Your version is not so hard-coded.

2. Also, the result should be displayed in the sketch window that processing generates, rather than in the console itself. The snap-to-grid code I provided in my first post should help as a basis to create the look and feel of it.

3. Finally, the result must be kept as a basis of comparison for the cpu to tell the user whether they won the game or not.