fix: allow anchor moves to reach exit with key
Update findAnchorTarget to accept optional hasKey parameter. When player has the key and moving to an anchor (gg, G, ^, or $), if an exit is positioned at the edge, allow movement to reach it. This matches behavior of counted moves like 5k that can reach edge-positioned exits.
This commit is contained in:
@@ -27,6 +27,6 @@ export type BoardManager = {
|
|||||||
|
|
||||||
export type MazeNavigator = {
|
export type MazeNavigator = {
|
||||||
isValidMove: (from: Coord, to: Coord) => boolean;
|
isValidMove: (from: Coord, to: Coord) => boolean;
|
||||||
findAnchorTarget: (from: Coord, direction: 'left' | 'right' | 'top' | 'bottom') => Coord | null;
|
findAnchorTarget: (from: Coord, direction: 'left' | 'right' | 'top' | 'bottom', hasKey?: boolean) => Coord | null;
|
||||||
validatePath: (from: Coord, direction: Coord, steps: number) => boolean;
|
validatePath: (from: Coord, direction: Coord, steps: number) => boolean;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ export function useMazeNavigation(maze: MazeCell[][]): MazeNavigator {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function findAnchorTarget(from: Coord, direction: 'left'|'right'|'top'|'bottom'): Coord | null {
|
function findAnchorTarget(from: Coord, direction: 'left'|'right'|'top'|'bottom', hasKey: boolean = false): Coord | null {
|
||||||
if (!maze.length) return null;
|
if (!maze.length) return null;
|
||||||
|
|
||||||
const rowsCount = maze.length;
|
const rowsCount = maze.length;
|
||||||
@@ -22,6 +22,7 @@ export function useMazeNavigation(maze: MazeCell[][]): MazeNavigator {
|
|||||||
if (direction === 'left' || direction === 'right') {
|
if (direction === 'left' || direction === 'right') {
|
||||||
const step = direction === 'left' ? -1 : 1;
|
const step = direction === 'left' ? -1 : 1;
|
||||||
let cc = c;
|
let cc = c;
|
||||||
|
let edgeCell: Coord | null = null;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const next = cc + step;
|
const next = cc + step;
|
||||||
@@ -35,13 +36,23 @@ export function useMazeNavigation(maze: MazeCell[][]): MazeNavigator {
|
|||||||
if (cc !== expectedCellIdx) return null;
|
if (cc !== expectedCellIdx) return null;
|
||||||
if (cc === c) return null;
|
if (cc === c) return null;
|
||||||
|
|
||||||
return { row: r, col: cc };
|
edgeCell = { row: r, col: cc };
|
||||||
|
|
||||||
|
if (hasKey && wallIdx === expectedWallIdx) {
|
||||||
|
const wallPos = { row: r, col: wallIdx };
|
||||||
|
if (maze[wallPos.row]?.[wallPos.col]?.includes('exit')) {
|
||||||
|
return wallPos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return edgeCell;
|
||||||
}
|
}
|
||||||
cc = next;
|
cc = next;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const step = direction === 'top' ? -1 : 1;
|
const step = direction === 'top' ? -1 : 1;
|
||||||
let rr = r;
|
let rr = r;
|
||||||
|
let edgeCell: Coord | null = null;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const next = rr + step;
|
const next = rr + step;
|
||||||
@@ -55,7 +66,16 @@ export function useMazeNavigation(maze: MazeCell[][]): MazeNavigator {
|
|||||||
if (rr !== expectedCellIdx) return null;
|
if (rr !== expectedCellIdx) return null;
|
||||||
if (rr === r) return null;
|
if (rr === r) return null;
|
||||||
|
|
||||||
return { row: rr, col: c };
|
edgeCell = { row: rr, col: c };
|
||||||
|
|
||||||
|
if (hasKey && wallIdx === expectedWallIdx) {
|
||||||
|
const wallPos = { row: wallIdx, col: c };
|
||||||
|
if (maze[wallPos.row]?.[wallPos.col]?.includes('exit')) {
|
||||||
|
return wallPos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return edgeCell;
|
||||||
}
|
}
|
||||||
rr = next;
|
rr = next;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,7 +67,8 @@ export function useCursor(board: BoardManager, gameStatusManager: GameStatusMana
|
|||||||
target === 'end' ? 'right' :
|
target === 'end' ? 'right' :
|
||||||
target === 'top' ? 'top' : 'bottom';
|
target === 'top' ? 'top' : 'bottom';
|
||||||
|
|
||||||
const anchorTarget = navigator.findAnchorTarget(currentPos, direction);
|
const hasKey = gameStatusManager.gameStatus === 'has-key';
|
||||||
|
const anchorTarget = navigator.findAnchorTarget(currentPos, direction, hasKey);
|
||||||
if (!anchorTarget) return;
|
if (!anchorTarget) return;
|
||||||
|
|
||||||
const dr = anchorTarget.row > currentPos.row ? 1 :
|
const dr = anchorTarget.row > currentPos.row ? 1 :
|
||||||
|
|||||||
Reference in New Issue
Block a user