Skip to content

Commit

Permalink
fix(Example): add changes to example
Browse files Browse the repository at this point in the history
  • Loading branch information
Jtmaca9 committed Sep 7, 2023
1 parent e0457a4 commit 3f0429b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example/src/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
GameViewWrapper,
} from 'meepl';
import assets from './Assets';
import { isZoneAvailable } from './Logic';
import { isZoneAvailable } from './gameLogic';

export default function Game(props) {
const { G, ctx, plugins, moves } = props;
Expand Down
14 changes: 13 additions & 1 deletion example/src/gameConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createGameConfig, createGridZones, MOVE_ERROR } from 'meepl';
import ChessPieces, { ChessPieceType } from './ChessPieces';
import { isZoneAvailable } from './Logic';
import { isZoneAvailable } from './gameLogic';

const zones = createGridZones({
rows: 8,
Expand Down Expand Up @@ -44,6 +44,16 @@ const moves = {
const piece = G.pieces.find((p) => p.id === currPlayer.activePiece);
if (!piece) return MOVE_ERROR.INVALID_MOVE;
if (isZoneAvailable(zoneId, currPlayer, { G })) {
const pieceOnZone = G.pieces.find((p) => p.currZoneId === zoneId);
// take piece
if (pieceOnZone) {
G.pieces = G.pieces.filter((p) => p.id !== pieceOnZone.id);
player.set({
...currPlayer,
takenPieces: [...currPlayer.takenPieces, pieceOnZone],
});
}
// move piece
piece.currZoneId = zoneId;
moveSuccessful = true;
}
Expand All @@ -67,7 +77,9 @@ const ChessGame = createGameConfig({
playerView: (players) => players,
playerSetup: (playerID) => ({
name: `Player ${playerID}`,
id: playerID,
activePiece: null,
takenPieces: [],
}),
});

Expand Down
11 changes: 9 additions & 2 deletions example/src/Logic.ts → example/src/gameLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function isZoneAvailable(id: string, player: any, args: any): boolean {

switch (activePiece.type) {
case ChessPieceType.rook:
return isZoneAvailableForRook(currZone, targetZone, G);
return isZoneAvailableForRook(currZone, targetZone, activePlayer, G);
default:
return false;
}
Expand All @@ -22,6 +22,7 @@ export function isZoneAvailable(id: string, player: any, args: any): boolean {
function isZoneAvailableForRook(
currZone: ZoneType,
targetZone: ZoneType,
activePlayer: any,
G: any
): boolean {
const {
Expand Down Expand Up @@ -55,7 +56,13 @@ function isZoneAvailableForRook(
gridX < Math.max(currX, targetX)
);
});
if (!isPieceBetween) return true;
if (!isPieceBetween) {
const pieceOnTarget = pieces.find(
({ currZoneId }) => currZoneId === targetZone.id
);
if (!pieceOnTarget || pieceOnTarget.owner !== activePlayer.id)
return true;
}
}
return false;
}

0 comments on commit 3f0429b

Please sign in to comment.