Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented "Coup" rule logic #163

Merged
merged 5 commits into from
Jan 26, 2024
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
13 changes: 13 additions & 0 deletions development/code/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function endTurn() {
});

resetVariables();
updatePlayerDetails();

currentPlayerIndex = currentPlayerIndex + 1 < players.length ? currentPlayerIndex + 1 : 0;
turnCounter++;
Expand All @@ -95,6 +96,18 @@ function resetVariables() {
});
}

function updatePlayerDetails() {
game.getPlayers().forEach(player => {
if (player === getCurrentPlayer()) {
if (player.gold < 0) {
player.inDebtForTurns++;
} else {
player.inDebtForTurns = 0;
}
}
});
}

function getCurrentPlayer() {
return players[currentPlayerIndex];
}
Expand Down
15 changes: 15 additions & 0 deletions development/code/LogicAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { game } from './Game';
import { isPlayerAllowedToAct, onPieceFellOffTheBoard, onPlayerAction } from './logic/PieceLogic';
import { PlayerColors } from './logic/Players';
import { comparePositions, convertSquareIdToPosition } from './logic/Utilities';
import { Item } from './logic/items/Items';
import { Piece } from './logic/pieces/Pieces';
Expand All @@ -14,6 +15,7 @@ import {
spawnItemElementOnBoard,
spawnPieceElementOnBoard,
highlightLastMove,
getPieceElementBySquareId,
} from './ui/BoardManager';
import { renderPlayersInformation, renderNewRule } from './ui/Screen';

Expand Down Expand Up @@ -174,3 +176,16 @@ export function spawnItemOnBoard(item: Item) {

spawnItemElementOnBoard(item, squareId);
}

export function changePieceToAnotherPlayer(piece: Piece) {
const squareId = piece.position.coordinates.join(',');
const boadrId = piece.position.boardId;
const pieceElement = getPieceElementBySquareId(squareId, boadrId);
if (pieceElement) {
pieceElement.classList.remove(piece.player.color.toLowerCase());
const enemyPlayerColor = piece.player.color === PlayerColors.WHITE ? PlayerColors.BLACK : PlayerColors.WHITE;
pieceElement.classList.add(enemyPlayerColor.toLowerCase());
}

piece.player = game.getPlayers().filter(_player => _player !== piece.player)[0];
}
2 changes: 2 additions & 0 deletions development/code/logic/Players.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ export class Player implements PlayerType {
color: string;
xp: number;
gold: number;
inDebtForTurns: number;
inventory: Inventory;

constructor(color: string) {
this.color = color;
this.xp = 0;
this.gold = 0;
this.inDebtForTurns = 0;
this.inventory = new Inventory();
}
}
6 changes: 1 addition & 5 deletions development/code/logic/rules/BaseRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { game } from '../../Game';
import { renderRules } from '../../LogicAdapter';
import { Logger } from '../../ui/Logger';
export interface Rule {
id: number;
description: string;
isRevealed: boolean;
condition: () => boolean;
Expand All @@ -11,20 +10,17 @@ export interface Rule {
}

export class BaseRule implements Rule {
id: number;
description: string;
isRevealed: boolean;
condition: () => boolean;
onTrigger: () => void;

constructor(
id: number,
description: string,
isRevealed: boolean,
condition: () => boolean,
onTrigger: () => void,
) {
this.id = id;
this.description = description;
this.isRevealed = isRevealed;
this.condition = condition;
Expand All @@ -44,4 +40,4 @@ export class BaseRule implements Rule {
}
}
}
}
}
44 changes: 44 additions & 0 deletions development/code/logic/rules/CoupRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { game } from '../../Game';
import { changePieceToAnotherPlayer } from '../../LogicAdapter';
import { Logger } from '../../ui/Logger';
import { King } from '../pieces/King';
import { BaseRule } from './BaseRule';

export class CoupRule extends BaseRule {
constructor(isRevealed = false) {
const description = 'Coup.';
const condition = () => {
let result = false;
game.getPlayers().forEach((player) => {
if (player.inDebtForTurns === 2 && player === game.getCurrentPlayer()) {
result = true;
}
});
return result;
};

const onTrigger = () => {
game.getPlayers().forEach((player) => {
if (player.inDebtForTurns === 2 && player === game.getCurrentPlayer()) {
player.inDebtForTurns = -1;
const playerPieces = game.getPieces().filter(piece => piece.player === player);
const randomAmountOfPieces = Math.floor(Math.random() * (playerPieces.length - 1) / 2) + 1;

Logger.logRule(`${player.color} is deep in debt. ${randomAmountOfPieces} of their pieces desert.`);

let desertedPiecesCounter = 0;
while (desertedPiecesCounter < randomAmountOfPieces) {
const randomPieceIndex = Math.floor(Math.random() * (playerPieces.length - 1)) + 1;
const piece = playerPieces[randomPieceIndex];
if (piece instanceof King) continue;

changePieceToAnotherPlayer(piece);
desertedPiecesCounter++;
}
}
});
};

super(description, isRevealed, condition, onTrigger);
}
}
18 changes: 10 additions & 8 deletions development/code/logic/rules/EmptyPocketsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import { BaseRule } from './BaseRule';

export class EmptyPocketsRule extends BaseRule {
constructor(isRevealed = false) {
const index = 5;
const description = 'Empty pockets.';
let condition = () => false;
game.getPlayers().forEach((player) => {
if (player === game.getCurrentPlayer() && player.gold < 0) {
condition = () => true;
}
});
const condition = () => {
let result = false;
game.getPlayers().forEach((player) => {
if (player === game.getCurrentPlayer() && player.gold < 0) {
result = true;
}
});
return result;
};
const onTrigger = () => {
game.getPlayers().forEach((player) => {
if (player === game.getCurrentPlayer() && player.gold < 0) {
Expand All @@ -22,6 +24,6 @@ export class EmptyPocketsRule extends BaseRule {
});
};

super(index, description, isRevealed, condition, onTrigger);
super(description, isRevealed, condition, onTrigger);
}
}
3 changes: 1 addition & 2 deletions development/code/logic/rules/ExperienceOnKillRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BaseRule } from './BaseRule';

export class ExperienceOnKillRule extends BaseRule {
constructor(isRevealed = false) {
const index = 2;
const description = 'Players gain XP on a kill.';
const condition = () => game.getIsPieceKilled();
const onTrigger = () => {
Expand All @@ -13,6 +12,6 @@ export class ExperienceOnKillRule extends BaseRule {
player.xp++;
};

super(index, description, isRevealed, condition, onTrigger);
super(description, isRevealed, condition, onTrigger);
}
}
3 changes: 1 addition & 2 deletions development/code/logic/rules/FirstBloodRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BaseRule } from './BaseRule';

export class FirstBloodRule extends BaseRule {
constructor(isRevealed = false) {
const index = 1;
const description = 'First Blood Bonus: The first to kill gets an extra XP.';
const condition = () => game.getDeathCounter() == 1 && game.getIsPieceKilled();
const onTrigger = () => {
Expand All @@ -13,6 +12,6 @@ export class FirstBloodRule extends BaseRule {
player.xp++;
};

super(index, description, isRevealed, condition, onTrigger);
super(description, isRevealed, condition, onTrigger);
}
}
3 changes: 1 addition & 2 deletions development/code/logic/rules/FriendlyFireRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BaseRule } from './BaseRule';

export class FriendlyFireRule extends BaseRule {
constructor(isRevealed = false) {
const index = 3;
const description = 'Friendly Fire! Players can attack their own pieces (for a price).';
const condition = () => game.getIsFriendlyFire();
const onTrigger = () => {
Expand All @@ -13,6 +12,6 @@ export class FriendlyFireRule extends BaseRule {
player.gold--;
};

super(index, description, isRevealed, condition, onTrigger);
super(description, isRevealed, condition, onTrigger);
}
}
3 changes: 1 addition & 2 deletions development/code/logic/rules/PiecesCanFallOffTheBoardRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BaseRule } from './BaseRule';

export class PiecesCanFallOffTheBoardRule extends BaseRule {
constructor(isRevealed = false) {
const index = 0;
const description = 'Pieces can fall off the board.';
const condition = () => !!game.getFellOffTheBoardPiece();
const onTrigger = () => {
Expand All @@ -13,6 +12,6 @@ export class PiecesCanFallOffTheBoardRule extends BaseRule {
`);
};

super(index, description, isRevealed, condition, onTrigger);
super(description, isRevealed, condition, onTrigger);
}
}
2 changes: 2 additions & 0 deletions development/code/logic/rules/RulesManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BaseRule } from './BaseRule';
import { CoupRule } from './CoupRule';
import { EmptyPocketsRule } from './EmptyPocketsRule';
import { ExperienceOnKillRule } from './ExperienceOnKillRule';
import { FirstBloodRule } from './FirstBloodRule';
Expand All @@ -19,6 +20,7 @@ export class RulesManager {
new FriendlyFireRule(),
new WithAgeComesWisdomRule(),
new EmptyPocketsRule(),
new CoupRule(),
];
}
}
3 changes: 1 addition & 2 deletions development/code/logic/rules/WithAgeComesWisdomRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BaseRule } from './BaseRule';

export class WithAgeComesWisdomRule extends BaseRule {
constructor(isRevealed = false) {
const index = 4;
const description = 'With age comes wisdom.';
const condition = () => game.getRoundCounter() === 20;
const onTrigger = () => {
Expand All @@ -15,6 +14,6 @@ export class WithAgeComesWisdomRule extends BaseRule {
});
};

super(index, description, isRevealed, condition, onTrigger);
super(description, isRevealed, condition, onTrigger);
}
}
11 changes: 11 additions & 0 deletions development/code/ui/BoardManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ export function getSquareElementById(
return board.boardElement.querySelector(`[square-id="${squareId}"]`) as HTMLElement;
}

export function getPieceElementBySquareId(
squareId: string,
boardId: string,
): HTMLElement | undefined {
const squareElement = getSquareElementById(squareId, boardId);
const pieceElement = squareElement?.firstElementChild as HTMLElement;
if (pieceElement.classList.contains('piece')) {
return pieceElement;
}
}

export function moveElementOnBoard(
boardId: string,
originSquareId: string,
Expand Down
2 changes: 1 addition & 1 deletion development/code/ui/Screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ export function renderPlayersInformation() {

export function renderNewRule(rule: BaseRule) {
const ruleElement = document.createElement('p');
ruleElement.innerHTML = `<b>${rule.id + 1}) ${rule.description}</b>`;
ruleElement.innerHTML = `<b>- ${rule.description}</b>`;
rulesContainer?.appendChild(ruleElement);
}
Loading