Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
tool: linera-service@0.15.3

- run: |
ln -s /opt/hostedtoolcache/linera-storage-service/bin/linera-storage-server /opt/hostedtoolcache/linera-service/bin/
ln -sf /opt/hostedtoolcache/linera-storage-service/bin/linera-storage-server /opt/hostedtoolcache/linera-service/bin/

- name: Run clippy
run: cargo clippy --locked --all-targets --all-features
Expand Down
15 changes: 6 additions & 9 deletions frontend/src/lib/game-of-life/domain/entities/Board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,15 @@ export class Board {
}
});

// If we have a valid history position, replace it
if (this.currentHistoryIndex >= 0 && this.currentHistoryIndex < this.history.length) {
this.history[this.currentHistoryIndex] = state;
} else if (this.currentHistoryIndex === -1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This damn zero-indexing :) we could also treat index === 0 as the initial, empty state. Then the if above changes to this.currentHistoryIndex > 0.

Not sure if we need to do this change right now, maybe too risky, but it'd simplify the logic.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, if its ok I can change it in a refactoring after the launch to not modify the logic this close :)

// No history yet, save as initial state
this.history = [state];
this.currentHistoryIndex = 0;
} else {
// Shouldn't happen, but save state as fallback
this.saveState();
}
}
Expand All @@ -104,9 +110,6 @@ export class Board {
return this.currentHistoryIndex > 0;
}

canRedo(): boolean {
return this.currentHistoryIndex < this.history.length - 1;
}

hasInitialState(): boolean {
return this.currentHistoryIndex > -1;
Expand Down Expand Up @@ -137,12 +140,6 @@ export class Board {
this.restoreState(this.history[this.currentHistoryIndex]);
}

redo(): void {
if (!this.canRedo()) return;

this.currentHistoryIndex++;
this.restoreState(this.history[this.currentHistoryIndex]);
}

private restoreState(state: Map<string, Cell>): void {
this.cells.clear();
Expand Down
42 changes: 13 additions & 29 deletions frontend/src/lib/game-of-life/hooks/useGameOfLife.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,43 +50,28 @@ export function useGameOfLife(options: UseGameOfLifeOptions) {
const toggleCell = useCallback(
(x: number, y: number) => {
board.toggleCell(x, y);
// Save or update state after toggling at generation 0
if (generation === 0) {
if (!board.hasInitialState()) {
board.saveState();
} else {
// Replace the current state instead of adding new one
board.replaceCurrentState();
}
}
// Always save/replace the current state after modification
board.replaceCurrentState();
updateCells();
},
[board, updateCells, generation]
[board, updateCells]
);

const next = useCallback(() => {
if (!engine || !board) return;

// Only compute new generation if we're at the end of history
if (board.isAtEndOfHistory()) {
// Make sure we have an initial state saved
if (generation === 0 && !board.hasInitialState()) {
board.saveState(); // Save generation 0 if not already saved
}
// Make sure we have an initial state saved to prevent bugs where you would reset to generation 0
if (generation === 0 && !board.hasInitialState()) {
board.saveState(); // Save generation 0 if not already saved
}

// Compute next generation
engine.nextGeneration();
setGeneration((g) => g + 1);
// Always compute fresh generation from current board state
engine.nextGeneration();
setGeneration((g) => g + 1);

// Save the NEW state after computing
board.saveState();
updateCells();
} else if (board.canRedo()) {
// If we're in the middle of history, just move forward
board.redo();
setGeneration((g) => g + 1);
updateCells();
}
// Save the newly computed state
board.saveState();
updateCells();
}, [engine, board, updateCells, generation]);

const previous = useCallback(() => {
Expand Down Expand Up @@ -183,7 +168,6 @@ export function useGameOfLife(options: UseGameOfLifeOptions) {
isPlaying,
speed,
canUndo: board.canUndo(),
canRedo: board.canRedo(),
toggleCell,
next,
previous,
Expand Down