Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 115 additions & 4 deletions LightsOutGame.java
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;
Copy link

Copilot AI Nov 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field DEFAULT_BOARD_SIZE should be declared as static final instead of just static to ensure it remains a constant and cannot be modified.

Suggested change
static int DEFAULT_BOARD_SIZE = 5;
static final int DEFAULT_BOARD_SIZE = 5;

Copilot uses AI. Check for mistakes.
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";
Expand All @@ -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
Copy link

Copilot AI Nov 1, 2025

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.

Suggested change
// 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 uses AI. Check for mistakes.
Comment on lines +90 to +91
Copy link

Copilot AI Nov 1, 2025

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.

Suggested change
// 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 uses AI. Check for mistakes.
}

/**
* Returns ASCII board display with row/column labels.
* @return String representation of the board
*/
public String display() {
// TODO: Implement display logic (Task #91)
return "";
}

/**
* Gets the state of a cell.
* @param row the row index
* @param column the column index
* @return true if light is on, false if off
*/
boolean getCell(int row, int column) {
Copy link

Copilot AI Nov 1, 2025

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.

Suggested change
boolean getCell(int row, int column) {
public boolean getCell(int row, int column) {

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Nov 1, 2025

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 uses AI. Check for mistakes.
Copy link

Copilot AI Nov 1, 2025

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 uses AI. Check for mistakes.
// TODO: Implement getter (Task #91)
return false;
}

/**
* Sets the state of a cell.
* @param row the row index
* @param column the column index
* @param value true to turn on, false to turn off
*/
void setCell(int row, int column, boolean value) {
Copy link

Copilot AI Nov 1, 2025

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.

Suggested change
void setCell(int row, int column, boolean value) {
public void setCell(int row, int column, boolean value) {

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Nov 1, 2025

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 uses AI. Check for mistakes.
Copy link

Copilot AI Nov 1, 2025

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 uses AI. Check for mistakes.
Copy link

Copilot AI Nov 1, 2025

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 uses AI. Check for mistakes.
// TODO: Implement setter (Task #91)
}

/**
* Checks if all lights are off.
* @return true if all lights are off, false otherwise
*/
boolean allOff() {
Copy link

Copilot AI Nov 1, 2025

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.

Suggested change
boolean allOff() {
public boolean allOff() {

Copilot uses AI. Check for mistakes.
// TODO: Implement win condition check (Task #91)
return false;
}

/**
* Randomizes the board state.
*/
void randomize() {
Copy link

Copilot AI Nov 1, 2025

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.

Suggested change
void randomize() {
public void randomize() {

Copilot uses AI. Check for mistakes.
// TODO: Implement randomization logic (Task #91)
}
}
}