Skip to content

Instantly share code, notes, and snippets.

@torjusti
Last active June 4, 2024 20:12
Show Gist options
  • Save torjusti/a02646c6624830bb2c5a69958321079f to your computer and use it in GitHub Desktop.
Save torjusti/a02646c6624830bb2c5a69958321079f to your computer and use it in GitHub Desktop.
public class LightsOut {
private List<Integer> board;
public LightsOut() {
board = generateBoard();
while (!validateBoard()) {
board = generateBoard();
}
}
public List<Integer> getBoard() {
return board;
}
private List<Integer> generateBoard() {
int random = ThreadLocalRandom.current().nextInt(0, (int) Math.pow(2, 25));
List<Integer> vector = new ArrayList();
for (int i = 0; i < 25; i++) {
vector.add(0, random % 2);
random /= 2;
}
return vector;
}
private Integer dot(List<Integer> a, List<Integer> b) {
int product = 0;
for (int i = 0; i < a.size(); i++) {
product += a.get(i) * b.get(i);
}
return product;
}
private Boolean validateBoard() {
List<Integer> a = List.of(0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0);
List<Integer> b = List.of(1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1);
return dot(a, board) == 0 && dot(b, board) == 0;
}
public static void main(String[] args) {
LightsOut game = new LightsOut();
System.out.println(game.getBoard());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment