From e850391056adceee545b8f654d055ca394f0fe6c Mon Sep 17 00:00:00 2001 From: martin Date: Wed, 23 Aug 2023 20:11:31 +0200 Subject: [PATCH] Removed TODOs and moved some logic in algo to it's own method --- pac-man-board-game/ClientApp/src/App.tsx | 1 - .../ClientApp/src/components/gameBoard.tsx | 2 +- .../ClientApp/src/game/character.ts | 2 +- .../src/game/possibleMovesAlgorithm.ts | 22 ++++++++++--------- .../ClientApp/src/utils/actions.ts | 2 +- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/pac-man-board-game/ClientApp/src/App.tsx b/pac-man-board-game/ClientApp/src/App.tsx index b421d3d..7c8175b 100644 --- a/pac-man-board-game/ClientApp/src/App.tsx +++ b/pac-man-board-game/ClientApp/src/App.tsx @@ -23,7 +23,6 @@ export const App: FC = () => ( * @param secured Whether or not the page is secured. * @constructor The Secured component. */ -// TODO only works on first render after refresh const Secured: FC<{ secured: boolean } & ChildProps> = ({children, secured}) => { const player = useAtomValue(thisPlayerAtom); const navigate = useNavigate(); diff --git a/pac-man-board-game/ClientApp/src/components/gameBoard.tsx b/pac-man-board-game/ClientApp/src/components/gameBoard.tsx index 890de9d..1832975 100644 --- a/pac-man-board-game/ClientApp/src/components/gameBoard.tsx +++ b/pac-man-board-game/ClientApp/src/components/gameBoard.tsx @@ -25,7 +25,7 @@ const Board: FC = ( const characters = useAtomValue(allCharactersAtom); const selectedDice = useAtomValue(selectedDiceAtom); const [selectedCharacter, setSelectedCharacter] = useState(); - const [possiblePositions, setPossiblePositions] = useState([]); // TODO reset when other client moves a character + const [possiblePositions, setPossiblePositions] = useState([]); const [hoveredPosition, setHoveredPosition] = useState(); const setModalOpen = useSetAtom(modalOpenAtom); diff --git a/pac-man-board-game/ClientApp/src/game/character.ts b/pac-man-board-game/ClientApp/src/game/character.ts index 9855618..61ec36b 100644 --- a/pac-man-board-game/ClientApp/src/game/character.ts +++ b/pac-man-board-game/ClientApp/src/game/character.ts @@ -83,7 +83,7 @@ export class Ghost extends Character implements CharacterProps { export class Dummy extends Character implements CharacterProps { - public constructor(position: Path) { // TODO see-through + public constructor(position: Path) { super({ colour: Colour.grey, position: position, diff --git a/pac-man-board-game/ClientApp/src/game/possibleMovesAlgorithm.ts b/pac-man-board-game/ClientApp/src/game/possibleMovesAlgorithm.ts index 92000f5..61238f5 100644 --- a/pac-man-board-game/ClientApp/src/game/possibleMovesAlgorithm.ts +++ b/pac-man-board-game/ClientApp/src/game/possibleMovesAlgorithm.ts @@ -2,8 +2,6 @@ import {TileType} from "./tileType"; import {Character} from "./character"; import {Direction, getDirections} from "./direction"; -// TODO if a Pac-Man with a powerpellet hits a ghost, the Pac-Man can't walk further - /** * Finds all the possible positions for the character to move to * @param board The board the character is on @@ -24,7 +22,7 @@ export default function findPossiblePositions(board: GameMap, character: Charact * @param steps The number of steps the character can move * @param character The current character * @param characters All the characters on the map - * @returns An array of paths the character can move to + * @returns {Path[]} An array of paths the character can move to */ function findPossibleRecursive(map: GameMap, currentPath: Path, steps: number, character: Character, characters: Character[]): Path[] { @@ -64,7 +62,7 @@ function findPossibleRecursive(map: GameMap, currentPath: Path, steps: number, c * Checks if the current character is on its spawn * @param currentPath The current path the character is on * @param character The current character - * @returns True if the character is on its spawn, otherwise false + * @returns {boolean} True if the character is on its spawn, otherwise false */ function isCharactersSpawn(currentPath: Path, character: Character): boolean { return character.spawnPosition?.at.x === currentPath.end.x && character.spawnPosition.at.y === currentPath.end.y; @@ -75,7 +73,7 @@ function isCharactersSpawn(currentPath: Path, character: Character): boolean { * @param character The current character * @param currentPath The current path the character is on * @param characters All the characters on the board - * @returns True if the character is a ghost and hits Pac-Man, otherwise false + * @returns {boolean} True if the character is a ghost and hits Pac-Man, otherwise false */ function ghostHitsPacMan(character: Character, currentPath: Path, characters: Character[]): Character | undefined | false { return character.isGhost() && characters.find(c => c.isPacMan() && c.isAt(currentPath.end)); @@ -86,7 +84,7 @@ function ghostHitsPacMan(character: Character, currentPath: Path, characters: Ch * @param character The current character * @param currentPath The current path the character is on * @param characters All the characters on the board - * @returns True if the character hits another character, otherwise false + * @returns {boolean} True if the character hits another character, otherwise false */ function characterHitsAnotherCharacter(character: Character, currentPath: Path, characters: Character[]): boolean { return characters.find(c => c !== character && c.isAt(currentPath.end)) !== undefined; @@ -143,7 +141,6 @@ function tryMove(board: GameMap, path: Path, direction: Direction, steps: number } if (path.direction !== (direction + 2) % 4) { - // TODO getNewPosition() and check if a character is on the new position return findPossibleRecursive(board, { end: getNewPosition(), direction: direction, path: path.path }, steps, character, characters); @@ -158,13 +155,18 @@ function tryMove(board: GameMap, path: Path, direction: Direction, steps: number * @param steps The number of steps the character can move * @param character The current character * @param characters All the characters on the map + * @returns {Path[]} An array of paths the character can move to */ function addTeleportationTiles(board: GameMap, currentPath: Path, steps: number, character: Character, characters: Character[]): Path[] { const possiblePositions = findTeleportationTiles(board); const paths: Path[] = []; for (const pos of possiblePositions) { - if (pos.end.x !== interval(0, board.length - 1, currentPath.end.x) || - pos.end.y !== interval(0, board.length - 1, currentPath.end.y)) { + + function inInterval(coordinate: "x" | "y"): boolean { + return pos.end[coordinate] !== interval(0, board.length - 1, currentPath.end[coordinate]) + } + + if (inInterval("x") || inInterval("y")) { pos.path = currentPath.path; paths.push(...findPossibleRecursive(board, pos, steps, character, characters)); @@ -178,7 +180,7 @@ function addTeleportationTiles(board: GameMap, currentPath: Path, steps: number, * @param lower The lower bound * @param upper The upper bound * @param value The value to check - * @returns The value if it's between the lower and upper bounds, otherwise it returns the lower or upper bound + * @returns {number} The value if it's between the lower and upper bounds, otherwise it returns the lower or upper bound */ function interval(lower: number, upper: number, value: number): number { return Math.max(Math.min(value, upper), lower); diff --git a/pac-man-board-game/ClientApp/src/utils/actions.ts b/pac-man-board-game/ClientApp/src/utils/actions.ts index e444f9a..b5e4d57 100644 --- a/pac-man-board-game/ClientApp/src/utils/actions.ts +++ b/pac-man-board-game/ClientApp/src/utils/actions.ts @@ -39,7 +39,7 @@ export const doAction: MessageEventFunction = (event): void => { switch (message.action as GameAction) { case GameAction.error: - console.error("Error:", message.data); // TODO show error to user + console.error("Error:", message.data); break; case GameAction.rollDice: setDice(message.data);