-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightsOut.java
182 lines (168 loc) · 6.21 KB
/
LightsOut.java
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
/**
* Class for playing with Lights Out puzzles.
*/
public class LightsOut {
/** Default number of rows in a puzzle. */
public static final int DEFAULT_ROWS = 5;
/** Default number of columns in a puzzle. */
public static final int DEFAULT_COLS = 5;
/** Character for light on in string representation. */
public static final char LIGHT_ON = 'X';
/** Character for light off in string representation. */
public static final char LIGHT_OFF = '.';
/**
* Create a lights out puzzle grid of given dimensions with all lights off.
* @param rows Number of rows
* @param cols Number of columns
* @return 2D array of booleans with all values set to false.
*/
public static boolean[][] makeEmptyGrid(int rows, int cols) {
// TODO: implement this
boolean[][] grid = new boolean[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
grid[i][j] = false;
}
}
return grid;
}
/**
* Given a 2D array of booleans, how many rows does it have?
* @param arr The array in question
* @return The number of rows in the array.
*/
public static int numRows(boolean[][] arr) {
// TODO: implement this
return arr.length;
}
/**
* Given a 2D array of booleans, how many columns does it have?
* @param arr The array in question
* @return The number of columns in the array.
*/
public static int numCols(boolean[][] arr) {
// TODO: implement this
return arr[0].length;
}
/**
* Creates a string representation of the game board.
* Rows end with a newline character.
* @param grid The grid of lights
* @return String representation of the board
*/
public static String gridToString(boolean[][] grid) {
// TODO: implement this
StringBuilder sb = new StringBuilder();
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
sb.append(grid[i][j] ? LIGHT_ON : LIGHT_OFF);
}
sb.append('\n');
}
return sb.toString();
}
/**
* Toggle the status of a single light, if possible.
* @param grid The game board
* @param row The row of the light location
* @param col The column of the light location
* @return Turns light at location on if it was off and off it was on and
* returns true. Returns false without changing the board if the
* location was invalid.
*/
public static boolean toggleSingleLight(boolean[][] grid, int row, int col) {
// TODO: implement this
int rows = numRows(grid);
int cols = numCols(grid);
if (row >= 0 && row < rows && col >= 0 && col < cols) {
grid[row][col] = !grid[row][col];
return true;
}
return false;
}
/**
* Toggle the status of a light and its neighbors, if possible, and
* return how many lights were toggled.
* @param grid The game board
* @param row The row of the light location
* @param col The column of the light location
* @return Toggles on/off status of light and its vertical and horizontal
* neighbors and returns number of lights toggled. Returns 0 without
* changing the board if the location was invalid.
*/
public static int toggleLightWithNeighbors(boolean[][] grid, int row, int col) {
// TODO: implement this
int toggled = 0;
if (toggleSingleLight(grid, row, col)) {
toggled++;
}
if (toggleSingleLight(grid, row - 1, col)) {
toggled++;
}
if (toggleSingleLight(grid, row + 1, col)) {
toggled++;
}
if (toggleSingleLight(grid, row, col - 1)) {
toggled++;
}
if (toggleSingleLight(grid, row, col + 1)) {
toggled++;
}
return toggled;
}
/**
* Selects a random location on the board and toggles it with its neighbors.
* @param grid The game board.
*/
public static void toggleRandomLocation(boolean[][] grid) {
// TODO: implement this
int rows = numRows(grid);
int cols = numCols(grid);
int row = (int) (Math.random() * rows);
int col = (int) (Math.random() * cols);
toggleLightWithNeighbors(grid, row, col);
}
/**
* Create a random solvable puzzle of desired size.
*
* To make a solvable puzzle, turn on lights by toggling a random location
* with its neighbors. (The solution will be to toggle those same positions.)
* Toggle roughly half the locations. (It's okay if you toggle the same place
* more than once, undoing your earlier random toggle.)
*
* @param rows Number of rows
* @param cols Number of columns
* @return Game board with some lights turned on.
*/
public static boolean[][] makeRandomPuzzle(int rows, int cols) {
// TODO: implement this
boolean[][] grid = makeEmptyGrid(rows, cols);
int totalToggles = (rows * cols) / 2; // Approximately half of the lights should be toggled.
for (int i = 0; i < totalToggles; i++) {
toggleRandomLocation(grid);
}
return grid;
}
/**
* Is this puzzle solved?
* In other words, are all the lights off?
* @param grid The puzzle grid
* @return True if all lights are off, false if any are on.
*/
public static boolean isSolved(boolean[][] grid) {
// TODO: implement this
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j]) {
return false; // If any light is on, the puzzle is not solved.
}
}
}
return true;
}
/** Creates puzzle array and starts game GUI. */
public static void main(String[] args) {
boolean[][] b = makeRandomPuzzle(DEFAULT_ROWS, DEFAULT_COLS);
LightsOutGUI.showGUI(b);
}
}