-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPuzzleGenerator.java
More file actions
185 lines (168 loc) · 4.93 KB
/
Copy pathPuzzleGenerator.java
File metadata and controls
185 lines (168 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import java.util.ArrayList;
import java.util.Random;
public class PuzzleGenerator {
/* The following code was taken from the following link.
* I did not author any of this code.
* https://github.com/SomeKittens/Sudoku-Project/blob/master/SudokuGenerator.java
* @author: SomeKittens
*/
public static final int BOARD_WIDTH = 9;
public static final int BOARD_HEIGHT = 9;
/**
*Constructor. Resets board to zeros
*/
public PuzzleGenerator() {
board = new int[BOARD_WIDTH][BOARD_HEIGHT];
}
/**
*Driver method for nextBoard.
*@param difficult the number of blank spaces to insert
*@return board, a partially completed 9x9 Sudoku board
*/
public int[][] nextBoard()
{
board = new int[BOARD_WIDTH][BOARD_HEIGHT];
nextCell(0,0);
//makeHoles(difficulty);
return board;
}
// This is the only method I authored in this file. Simple getter.
public int[][] getBoard(int difficulty) {
makeHoles(difficulty);
return board;
}
/**
*Recursive method that attempts to place every number in a cell.
*
*@param x x value of the current cell
*@param y y value of the current cell
*@return true if the board completed legally, false if this cell
*has no legal solutions.
*/
public boolean nextCell(int x, int y)
{
int nextX = x;
int nextY = y;
int[] toCheck = {1,2,3,4,5,6,7,8,9};
Random r = new Random();
int tmp = 0;
int current = 0;
int top = toCheck.length;
for(int i=top-1;i>0;i--)
{
current = r.nextInt(i);
tmp = toCheck[current];
toCheck[current] = toCheck[i];
toCheck[i] = tmp;
}
for(int i=0;i<toCheck.length;i++)
{
if(legalMove(x, y, toCheck[i]))
{
board[x][y] = toCheck[i];
if(x == 8)
{
if(y == 8)
return true;//We're done! Yay!
else
{
nextX = 0;
nextY = y + 1;
}
}
else
{
nextX = x + 1;
}
if(nextCell(nextX, nextY))
return true;
}
}
board[x][y] = 0;
return false;
}
/**
*Given a cell's coordinates and a possible number for that cell,
*determine if that number can be inserted into said cell legally.
*
*@param x x value of cell
*@param y y value of cell
*@param current The value to check in said cell.
*@return True if current is legal, false otherwise.
*/
private boolean legalMove(int x, int y, int current) {
for(int i=0;i<9;i++) {
if(current == board[x][i])
return false;
}
for(int i=0;i<9;i++) {
if(current == board[i][y])
return false;
}
int cornerX = 0;
int cornerY = 0;
if(x > 2)
if(x > 5)
cornerX = 6;
else
cornerX = 3;
if(y > 2)
if(y > 5)
cornerY = 6;
else
cornerY = 3;
for(int i=cornerX;i<10 && i<cornerX+3;i++)
for(int j=cornerY;j<10 && j<cornerY+3;j++)
if(current == board[i][j])
return false;
return true;
}
/**
*Given a completed board, replace a given amount of cells with 0s
*(to represent blanks)
*@param holesToMake How many 0s to put in the board.
*/
public void makeHoles(int holesToMake)
{
/* We define difficulty as follows:
Easy: 32+ clues (49 or fewer holes)
Medium: 27-31 clues (50-54 holes)
Hard: 26 or fewer clues (54+ holes)
This is human difficulty, not algorighmically (though there is some correlation)
*/
double remainingSquares = 81;
double remainingHoles = (double)holesToMake;
for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
{
double holeChance = remainingHoles/remainingSquares;
if(Math.random() <= holeChance)
{
board[i][j] = 0;
remainingHoles--;
}
remainingSquares--;
}
}
/**
*Prints a representation of board on stdout
*/
public void print()
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
System.out.print(board[i][j] + " ");
System.out.println();
}
System.out.println();
}
public static void main(String[] args)
{
PuzzleGenerator sg = new PuzzleGenerator();
sg.nextBoard();
sg.print();
}
int[][] board;
private int operations;
}