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
Changes from 3 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
@@ -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);
@@ -58,6 +59,7 @@ let isCastling = false;
let isFriendlyFire = false;
let isPieceKilled = false;
let fellOffTheBoardPiece: Piece | undefined;
let winner = '';
Monkey-bored marked this conversation as resolved.
Show resolved Hide resolved

function initializeGame() {
rulesManager = new RulesManager();
@@ -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 !== '') {
showWinningAlert(winner);
window.location.reload();
}
}, 10);
}

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

function setWinner(winnerColor: string) {
winner = winnerColor;
}

export const game = {
initialize: initializeGame,
endTurn,
@@ -201,4 +217,5 @@ export const game = {
setIsPieceKilled,
getFellOffTheBoardPiece,
setFellOffTheBoardPiece,
setWinner,
};
11 changes: 10 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';
@@ -189,3 +189,12 @@ export function changePieceToAnotherPlayer(piece: Piece) {

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

export function winGame(winnerPlayer: Player){
const winnerColor =
winnerPlayer.color === PlayerColors.WHITE
? 'White'
: 'Black';

game.setWinner(winnerColor);
}
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,
@@ -226,7 +226,6 @@ function killPiece(
targetPosition = targetPiece.position,
) {
draggedPiece.hasKilled = true;
commonKillPieceActions(targetPiece);

logKillMessages(targetPiece, draggedPiece);

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

if (targetPiece.hasKilled || targetPiece instanceof King) {
@@ -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) {
2 changes: 1 addition & 1 deletion development/code/logic/rules/CoupRule.ts
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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
@@ -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
@@ -14,4 +14,4 @@ export class FriendlyFireRule extends BaseRule {

super(description, isRevealed, condition, onTrigger);
}
}
}
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -39,3 +39,7 @@ export function renderNewRule(rule: BaseRule) {
ruleElement.innerHTML = `<b>- ${rule.description}</b>`;
rulesContainer?.appendChild(ruleElement);
}

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