-
Notifications
You must be signed in to change notification settings - Fork 27
created stubs for Lights Out game task 89 #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,16 +1,34 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.io.InputStream; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Optional; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * A logic puzzle where each cell on a grid can be either "on" or "off". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Selecting a cell toggles it and its immediate neighbors. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * The goal is to turn all the lights off in as few moves as possible. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * <pre> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Consider a model using a grid of booleans. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Implement toggling logic on that grid. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * </pre> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @version 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class LightsOutGame implements Game { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static int DEFAULT_BOARD_SIZE = 5; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private LightsOutBoard board; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private InputStream inputStream; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Default constructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public LightsOutGame() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this(System.in); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Constructor with InputStream injection for testing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public LightsOutGame(InputStream in) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.inputStream = in; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.board = new LightsOutBoard(DEFAULT_BOARD_SIZE, DEFAULT_BOARD_SIZE); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Constructor with InputStream and board injection for deterministic testing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public LightsOutGame(InputStream in, LightsOutBoard board) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.inputStream = in; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.board = board; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public String getName() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "Lights Out"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -25,6 +43,99 @@ public Optional<Integer> play() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Turning them 'off' or 'on' as well."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Grid must be lit in as few turns as possible."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Good luck!"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Implement game loop (Task #91) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // - Randomize board | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // - Display board | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // - Get user input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // - Toggle cells | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // - Check win condition | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // - Handle quit/restart | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Optional.empty(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Nested inner class representing the Lights Out game board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Holds the grid and toggling logic. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static class LightsOutBoard { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private boolean[][] grid; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private int numRows; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private int numColumns; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Constructor with dimensions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public LightsOutBoard(int numRows, int numColumns) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.numRows = numRows; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.numColumns = numColumns; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.grid = new boolean[numRows][numColumns]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Initialize grid (all off by default) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Copy constructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public LightsOutBoard(LightsOutBoard other) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.numRows = other.numRows; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.numColumns = other.numColumns; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.grid = new boolean[numRows][numColumns]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Deep copy the grid from other | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Toggles specified cell and its neighbors. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param row the row index (0-based) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param column the column index (0-based) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return true if toggle was successful, false otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public boolean toggle(int row, int column) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Implement toggle logic (Task #91) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+90
to
+91
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Implement toggle logic (Task #91) | |
| return false; | |
| // Check bounds | |
| if (row < 0 || row >= numRows || column < 0 || column >= numColumns) { | |
| return false; | |
| } | |
| // Helper to toggle a cell if in bounds | |
| java.util.function.BiConsumer<Integer, Integer> toggleCell = (r, c) -> { | |
| if (r >= 0 && r < numRows && c >= 0 && c < numColumns) { | |
| grid[r][c] = !grid[r][c]; | |
| } | |
| }; | |
| // Toggle the selected cell and its neighbors | |
| toggleCell.accept(row, column); // Center | |
| toggleCell.accept(row - 1, column); // Up | |
| toggleCell.accept(row + 1, column); // Down | |
| toggleCell.accept(row, column - 1); // Left | |
| toggleCell.accept(row, column + 1); // Right | |
| return true; |
Copilot
AI
Nov 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter 'column' is never used.
| // TODO: Implement toggle logic (Task #91) | |
| return false; | |
| // Check bounds | |
| if (row < 0 || row >= numRows || column < 0 || column >= numColumns) { | |
| return false; | |
| } | |
| // Toggle the specified cell and its immediate neighbors | |
| int[][] directions = { {0,0}, {-1,0}, {1,0}, {0,-1}, {0,1} }; | |
| for (int[] dir : directions) { | |
| int r = row + dir[0]; | |
| int c = column + dir[1]; | |
| if (r >= 0 && r < numRows && c >= 0 && c < numColumns) { | |
| grid[r][c] = !grid[r][c]; | |
| } | |
| } | |
| return true; |
Copilot
AI
Nov 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The access modifier for getCell is missing. It should be explicitly declared as public for consistency with other getter methods and to match typical getter visibility patterns.
| boolean getCell(int row, int column) { | |
| public boolean getCell(int row, int column) { |
Copilot
AI
Nov 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter 'row' is never used.
Copilot
AI
Nov 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter 'column' is never used.
Copilot
AI
Nov 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The access modifier for setCell is missing. It should be explicitly declared as public for consistency with other setter methods and to match typical setter visibility patterns.
| void setCell(int row, int column, boolean value) { | |
| public void setCell(int row, int column, boolean value) { |
Copilot
AI
Nov 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter 'row' is never used.
Copilot
AI
Nov 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter 'column' is never used.
Copilot
AI
Nov 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter 'value' is never used.
Copilot
AI
Nov 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The access modifier for allOff is missing. It should be explicitly declared as public to maintain consistency with other public methods in the class.
| boolean allOff() { | |
| public boolean allOff() { |
Copilot
AI
Nov 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The access modifier for randomize is missing. It should be explicitly declared as public to maintain consistency with other public methods in the class.
| void randomize() { | |
| public void randomize() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The field
DEFAULT_BOARD_SIZEshould be declared asstatic finalinstead of juststaticto ensure it remains a constant and cannot be modified.