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

Create winning condition #169

Merged
merged 4 commits into from
Feb 1, 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
17 changes: 17 additions & 0 deletions development/code/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Piece } from './logic/pieces/Pieces';
import { Queen } from './logic/pieces/Queen';
import { Rook } from './logic/pieces/Rook';
import { RulesManager } from './logic/rules/RulesManager';
import { showWinningAlert } from './ui/Screen';

let rulesManager: RulesManager;
const whitePlayer = new Player(PlayerColors.WHITE);
Expand Down Expand Up @@ -58,6 +59,7 @@ let isCastling = false;
let isFriendlyFire = false;
let isPieceKilled = false;
let fellOffTheBoardPiece: Piece | undefined;
let winner: Player | undefined = undefined;

function initializeGame() {
rulesManager = new RulesManager();
Expand All @@ -79,6 +81,16 @@ function endTurn() {
}

renderScreen();

// element.remove() is scheduled to run in the next event sycle while alert() runs immedietely.
// To make sure the element is removed before displaying the winning alert, we need to add
// a small delay before displaying the alert.
setTimeout(() => {
if (winner !== undefined) {
showWinningAlert(winner.color);
window.location.reload();
}
}, 10);
}

function resetVariables() {
Expand Down Expand Up @@ -180,6 +192,10 @@ function setFellOffTheBoardPiece(_fellOffTheBoardPiece: Piece | undefined) {
fellOffTheBoardPiece = _fellOffTheBoardPiece;
}

function setWinner(_winner: Player) {
winner = _winner;
}

export const game = {
initialize: initializeGame,
endTurn,
Expand All @@ -201,4 +217,5 @@ export const game = {
setIsPieceKilled,
getFellOffTheBoardPiece,
setFellOffTheBoardPiece,
setWinner,
};
6 changes: 5 additions & 1 deletion development/code/LogicAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { game } from './Game';
import { isPlayerAllowedToAct, onPieceFellOffTheBoard, onPlayerAction } from './logic/PieceLogic';
import { PlayerColors } from './logic/Players';
import { Player, PlayerColors } from './logic/Players';
import { comparePositions, convertSquareIdToPosition } from './logic/Utilities';
import { Item } from './logic/items/Items';
import { Piece } from './logic/pieces/Pieces';
Expand Down Expand Up @@ -189,3 +189,7 @@ export function changePieceToAnotherPlayer(piece: Piece) {

piece.player = game.getPlayers().filter(_player => _player !== piece.player)[0];
}

export function winGame(winnerPlayer: Player){
game.setWinner(winnerPlayer);
}
8 changes: 6 additions & 2 deletions development/code/logic/PieceLogic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { game } from '../Game';
import { destroyItemOnBoard, destroyPieceOnBoard, movePieceOnBoard, spawnPieceOnBoard } from '../LogicAdapter';
import { destroyItemOnBoard, destroyPieceOnBoard, movePieceOnBoard, spawnPieceOnBoard, winGame } from '../LogicAdapter';
import { Logger } from '../ui/Logger';
import {
HEAVEN_BOARD_ID,
Expand Down Expand Up @@ -226,7 +226,6 @@ function killPiece(
targetPosition = targetPiece.position,
) {
draggedPiece.hasKilled = true;
commonKillPieceActions(targetPiece);

logKillMessages(targetPiece, draggedPiece);

Expand Down Expand Up @@ -263,6 +262,7 @@ function handleOverworldKill(
targetPiece: Piece,
targetPosition: Position,
) {
commonKillPieceActions(targetPiece);
targetPiece.position = targetPosition;

if (targetPiece.hasKilled || targetPiece instanceof King) {
Expand All @@ -278,6 +278,10 @@ export function permanentlyKillPiece(targetPiece: Piece, draggedPiece: Piece) {
logKillMessages(targetPiece, draggedPiece, true);
game.setPieces(game.getPieces().filter((piece) => piece !== targetPiece));
commonKillPieceActions(targetPiece);

if (targetPiece instanceof King){
winGame(draggedPiece.player);
}
}

function onActionPieceToItem(piece: Piece, item: Item) {
Expand Down
2 changes: 1 addition & 1 deletion development/code/logic/rules/CoupRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ export class CoupRule extends BaseRule {

super(description, isRevealed, condition, onTrigger);
}
}
}
2 changes: 1 addition & 1 deletion development/code/logic/rules/EmptyPocketsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ export class EmptyPocketsRule extends BaseRule {

super(description, isRevealed, condition, onTrigger);
}
}
}
2 changes: 1 addition & 1 deletion development/code/logic/rules/ExperienceOnKillRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export class ExperienceOnKillRule extends BaseRule {

super(description, isRevealed, condition, onTrigger);
}
}
}
2 changes: 1 addition & 1 deletion development/code/logic/rules/FirstBloodRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export class FirstBloodRule extends BaseRule {

super(description, isRevealed, condition, onTrigger);
}
}
}
2 changes: 1 addition & 1 deletion development/code/logic/rules/FriendlyFireRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export class FriendlyFireRule extends BaseRule {

super(description, isRevealed, condition, onTrigger);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export class PiecesCanFallOffTheBoardRule extends BaseRule {

super(description, isRevealed, condition, onTrigger);
}
}
}
2 changes: 1 addition & 1 deletion development/code/logic/rules/WithAgeComesWisdomRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ export class WithAgeComesWisdomRule extends BaseRule {

super(description, isRevealed, condition, onTrigger);
}
}
}
4 changes: 4 additions & 0 deletions development/code/ui/Screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ export function renderNewRule(rule: BaseRule) {
ruleElement.innerHTML = `<b>- ${rule.description}</b>`;
rulesContainer?.appendChild(ruleElement);
}

export function showWinningAlert(winnerColor: string) {
alert(winnerColor + ' wins!');
}
Loading