Finished the possibleMovesAlgorithm
This commit is contained in:
parent
89df373d45
commit
c5d8ecc362
6
.idea/.idea.pac-man-board-game/.idea/jsLibraryMappings.xml
generated
Normal file
6
.idea/.idea.pac-man-board-game/.idea/jsLibraryMappings.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="JavaScriptLibraryMappings">
|
||||
<includedPredefinedLibrary name="Node.js Core" />
|
||||
</component>
|
||||
</project>
|
@ -1,6 +1,7 @@
|
||||
import React, {useEffect, useState} from "react";
|
||||
import {Character, PacMan} from "../game/character";
|
||||
import {findPossiblePositions} from "../utils/game";
|
||||
import findPossiblePositions from "../game/possibleMovesAlgorithm";
|
||||
import {TileType} from "../game/tileType";
|
||||
|
||||
/**
|
||||
* 0 = empty
|
||||
@ -24,15 +25,6 @@ const map: number[][] = [
|
||||
[1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1],
|
||||
];
|
||||
|
||||
export enum TileType {
|
||||
empty,
|
||||
wall,
|
||||
pellet,
|
||||
powerPellet,
|
||||
ghostSpawn,
|
||||
pacmanSpawn,
|
||||
}
|
||||
|
||||
interface BoardProps extends ComponentProps {
|
||||
characters: Character[],
|
||||
selectedDice?: SelectedDice,
|
||||
@ -49,7 +41,7 @@ const Board: Component<BoardProps> = (
|
||||
const [selectedCharacter, setSelectedCharacter] = useState<Character>();
|
||||
const [possiblePositions, setPossiblePositions] = useState<CharacterPosition[]>([]);
|
||||
|
||||
function handleSelectCharacter(character: Character) {
|
||||
function handleSelectCharacter(character: Character): void {
|
||||
setSelectedCharacter(character);
|
||||
}
|
||||
|
||||
@ -57,6 +49,8 @@ const Board: Component<BoardProps> = (
|
||||
if (selectedCharacter && selectedDice) {
|
||||
const possiblePositions = findPossiblePositions(map, selectedCharacter.position, selectedDice.value);
|
||||
setPossiblePositions(possiblePositions);
|
||||
} else {
|
||||
setPossiblePositions([]);
|
||||
}
|
||||
}, [selectedCharacter, selectedDice]);
|
||||
|
||||
@ -88,7 +82,8 @@ const Board: Component<BoardProps> = (
|
||||
<div key={rowIndex} className={"flex"}>
|
||||
{
|
||||
row.map((tile, colIndex) =>
|
||||
<Tile className={`${possiblePositions.find(p => p.x === colIndex && p.y === rowIndex) ? "rounded-full" : ""}`}
|
||||
<Tile className={`${possiblePositions.find(p => p.x === colIndex && p.y === rowIndex) ?
|
||||
"border-4 border-white" : ""}`}
|
||||
characterClass={`${selectedCharacter?.isAt({x: colIndex, y: rowIndex}) ? "animate-bounce" : ""}`}
|
||||
key={colIndex + rowIndex * colIndex}
|
||||
type={tile}
|
||||
|
@ -0,0 +1,92 @@
|
||||
import {TileType} from "./tileType";
|
||||
|
||||
/**
|
||||
* Finds all the possible positions for the character to move to
|
||||
* @param board The board the character is on
|
||||
* @param currentPos The current position of the character
|
||||
* @param steps The number of steps the character can move
|
||||
*/
|
||||
export default function findPossiblePositions(board: number[][], currentPos: CharacterPosition, steps: number): CharacterPosition[] {
|
||||
const possiblePositions: CharacterPosition[] = [];
|
||||
findPossibleRecursive(board, currentPos, steps, possiblePositions, []);
|
||||
return possiblePositions;
|
||||
}
|
||||
|
||||
function findPossibleRecursive(board: number[][], currentPos: CharacterPosition, steps: number,
|
||||
possibleList: CharacterPosition[], visitedTiles: CharacterPosition[]): CharacterPosition | null {
|
||||
if (isOutsideBoard(currentPos)) {
|
||||
addTeleportationTiles(board, currentPos, steps, possibleList, visitedTiles);
|
||||
} else if (visitedTiles.find(tile => tile.x === currentPos.x && tile.y === currentPos.y)) {
|
||||
return null;
|
||||
} else {
|
||||
if (isWall(board, currentPos)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
visitedTiles.push(currentPos);
|
||||
if (steps === 0) return currentPos;
|
||||
|
||||
const nextStep = steps - 1;
|
||||
const result = {
|
||||
up: findPossibleRecursive(board, {x: currentPos.x, y: currentPos.y + 1}, nextStep, possibleList, visitedTiles),
|
||||
right: findPossibleRecursive(board, {x: currentPos.x + 1, y: currentPos.y}, nextStep, possibleList, visitedTiles),
|
||||
down: findPossibleRecursive(board, {x: currentPos.x, y: currentPos.y - 1}, nextStep, possibleList, visitedTiles),
|
||||
left: findPossibleRecursive(board, {x: currentPos.x - 1, y: currentPos.y}, nextStep, possibleList, visitedTiles),
|
||||
};
|
||||
|
||||
pushToList(board, possibleList, Object.values(result));
|
||||
return null;
|
||||
}
|
||||
|
||||
function addTeleportationTiles(board: number[][], currentPos: CharacterPosition, steps: number,
|
||||
possibleList: CharacterPosition[], visitedTiles: CharacterPosition[]): void {
|
||||
const newPositons: (CharacterPosition | null)[] = [];
|
||||
const possiblePositions = findTeleportationTiles(board);
|
||||
for (const pos of possiblePositions) {
|
||||
if (pos.x !== Math.max(currentPos.x, 0) || pos.y !== Math.max(currentPos.y, 0)) {
|
||||
newPositons.push(findPossibleRecursive(board, pos, steps, possibleList, visitedTiles));
|
||||
}
|
||||
}
|
||||
pushToList(board, possibleList, newPositons);
|
||||
}
|
||||
|
||||
function pushToList(board: number[][], list: CharacterPosition[], newEntries: (CharacterPosition | null)[]): void {
|
||||
for (const entry of newEntries) {
|
||||
if (entry !== null && !list.find(p => p.x === entry.x && p.y === entry.y) && !isSpawn(board, entry)) {
|
||||
list.push(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function findTeleportationTiles(board: number[][]): CharacterPosition[] {
|
||||
const possiblePositions: CharacterPosition[] = [];
|
||||
const edge = [0, board.length - 1];
|
||||
|
||||
for (const e of edge) {
|
||||
for (let i = 0; i < board[e].length; i++) {
|
||||
|
||||
if (board[e][i] !== TileType.wall) {
|
||||
possiblePositions.push({x: i, y: e});
|
||||
}
|
||||
if (board[i][e] !== TileType.wall) {
|
||||
possiblePositions.push({x: e, y: i});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return possiblePositions;
|
||||
}
|
||||
|
||||
function isOutsideBoard(currentPos: CharacterPosition): boolean {
|
||||
return currentPos.x < 0 || currentPos.y < 0;
|
||||
}
|
||||
|
||||
function isWall(board: number[][], currentPos: CharacterPosition): boolean {
|
||||
return board[currentPos.y][currentPos.x] === TileType.wall; // TODO shouldn't work, but it does
|
||||
}
|
||||
|
||||
function isSpawn(board: number[][], currentPos: CharacterPosition): boolean {
|
||||
return board[currentPos.x][currentPos.y] === TileType.pacmanSpawn ||
|
||||
board[currentPos.x][currentPos.y] === TileType.ghostSpawn;
|
||||
}
|
||||
|
8
pac-man-board-game/ClientApp/src/game/tileType.ts
Normal file
8
pac-man-board-game/ClientApp/src/game/tileType.ts
Normal file
@ -0,0 +1,8 @@
|
||||
export enum TileType {
|
||||
empty,
|
||||
wall,
|
||||
pellet,
|
||||
powerPellet,
|
||||
ghostSpawn,
|
||||
pacmanSpawn,
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
import {TileType} from "../components/gameBoard";
|
||||
|
||||
/**
|
||||
* Finds all the possible positions for the character to move to
|
||||
* @param board The board the character is on
|
||||
* @param currentPos The current position of the character
|
||||
* @param steps The number of steps the character can move
|
||||
*/
|
||||
export function findPossiblePositions(board: number[][], currentPos: CharacterPosition, steps: number): CharacterPosition[] {
|
||||
const possiblePositions: CharacterPosition[] = [];
|
||||
findPossibleRecursive(board, currentPos, steps, possiblePositions);
|
||||
return possiblePositions;
|
||||
}
|
||||
|
||||
// TODO character phasing through wall next to spawn
|
||||
function findPossibleRecursive(board: number[][], currentPos: CharacterPosition, steps: number,
|
||||
possibleList: CharacterPosition[]): CharacterPosition | null {
|
||||
if (isWall(board, currentPos)) return null;
|
||||
// TODO handle teleportation
|
||||
if (steps === 0) return currentPos;
|
||||
|
||||
const result = {
|
||||
up: findPossibleRecursive(board, {x: currentPos.x, y: currentPos.y + 1}, steps - 1, possibleList),
|
||||
right: findPossibleRecursive(board, {x: currentPos.x + 1, y: currentPos.y}, steps - 1, possibleList),
|
||||
down: findPossibleRecursive(board, {x: currentPos.x, y: currentPos.y - 1}, steps - 1, possibleList),
|
||||
left: findPossibleRecursive(board, {x: currentPos.x - 1, y: currentPos.y}, steps - 1, possibleList),
|
||||
};
|
||||
|
||||
for (const [_, value] of Object.entries(result)) {
|
||||
if (value !== null && !possibleList.find(p => p.x === value.x && p.y === value.y) && !isSpawn(board, value)) {
|
||||
possibleList.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function isWall(board: number[][], currentPos: CharacterPosition): boolean {
|
||||
return board[currentPos.x][currentPos.y] === TileType.wall;
|
||||
}
|
||||
|
||||
function isSpawn(board: number[][], currentPos: CharacterPosition): boolean {
|
||||
return board[currentPos.x][currentPos.y] === TileType.pacmanSpawn ||
|
||||
board[currentPos.x][currentPos.y] === TileType.ghostSpawn;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user