Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Naviary2 committed Nov 27, 2024
2 parents 8a9f8e8 + 0aee32a commit 26a3ceb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/client/scripts/esm/chess/logic/checkdetection.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function appendAttackerToList(attackers, attacker) {
// Auto disable this when the win condition is NOT checkmate!
function removeMovesThatPutYouInCheck(gamefile, moves, pieceSelected, color) { // moves: { individual: [], horizontal: [], ... }
if (color === colorutil.colorOfNeutrals) return; // Neutral pieces can't be in check
if (!gamefileutility.isOpponentUsingWinCondition(gamefile, 'checkmate')) return;
if (!gamefileutility.isOpponentUsingWinCondition(gamefile, color, 'checkmate')) return;

// There's a couple type of moves that put you in check:

Expand Down
4 changes: 2 additions & 2 deletions src/client/scripts/esm/chess/logic/checkmate.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function detectCheckmateOrDraw(gamefile) {
// We made it through every single piece without finding a single move.
// So is this draw or checkmate? Depends on whether the current state is check!
// Also make sure that checkmate can't happen if the winCondition is NOT checkmate!
const usingCheckmate = gamefileutility.isOpponentUsingWinCondition(gamefile, 'checkmate');
const usingCheckmate = gamefileutility.isOpponentUsingWinCondition(gamefile, gamefile.whosTurn, 'checkmate');
if (gamefile.inCheck && usingCheckmate) {
const colorThatWon = moveutil.getColorThatPlayedMoveIndex(gamefile, gamefile.moves.length - 1);
return `${colorThatWon} checkmate`;
Expand Down Expand Up @@ -160,7 +160,7 @@ function detectCheckmateOrDraw(gamefile) {
// // We made it through every single piece without finding a single move.
// // So is this draw or checkmate? Depends on whether the current state is check!
// // Also make sure that checkmate can't happen if the winCondition is NOT checkmate!
// const usingCheckmate = gamefileutility.isOpponentUsingWinCondition(gamefile, 'checkmate')
// const usingCheckmate = gamefileutility.isOpponentUsingWinCondition(gamefile, gamefile.whosTurn, 'checkmate')
// if (gamefile.inCheck && usingCheckmate) {

// if (whosTurn === 'white') return 'black checkmate' // Black wins
Expand Down
8 changes: 4 additions & 4 deletions src/client/scripts/esm/chess/logic/wincondition.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function getGameConclusion(gamefile) {
}

function detectRoyalCapture(gamefile) {
if (!gamefileutility.isOpponentUsingWinCondition(gamefile, 'royalcapture')) return false; // Not using this gamerule
if (!gamefileutility.isOpponentUsingWinCondition(gamefile, gamefile.whosTurn, 'royalcapture')) return false; // Not using this gamerule

// Was the last move capturing a royal piece?
if (wasLastMoveARoyalCapture(gamefile)) {
Expand All @@ -58,7 +58,7 @@ function detectRoyalCapture(gamefile) {
}

function detectAllroyalscaptured(gamefile) {
if (!gamefileutility.isOpponentUsingWinCondition(gamefile, 'allroyalscaptured')) return false; // Not using this gamerule
if (!gamefileutility.isOpponentUsingWinCondition(gamefile, gamefile.whosTurn, 'allroyalscaptured')) return false; // Not using this gamerule
if (!wasLastMoveARoyalCapture(gamefile)) return false; // Last move wasn't a royal capture.

// Are there any royal pieces remaining?
Expand All @@ -74,7 +74,7 @@ function detectAllroyalscaptured(gamefile) {
}

function detectAllpiecescaptured(gamefile) {
if (!gamefileutility.isOpponentUsingWinCondition(gamefile, 'allpiecescaptured')) return false; // Not using this gamerule
if (!gamefileutility.isOpponentUsingWinCondition(gamefile, gamefile.whosTurn, 'allpiecescaptured')) return false; // Not using this gamerule

// If the player who's turn it is now has zero pieces left, win!
const count = gamefileutility.getPieceCountOfColorFromPiecesByType(gamefile.ourPieces, gamefile.whosTurn);
Expand All @@ -88,7 +88,7 @@ function detectAllpiecescaptured(gamefile) {
}

function detectKoth(gamefile) {
if (!gamefileutility.isOpponentUsingWinCondition(gamefile, 'koth')) return false; // Not using this gamerule
if (!gamefileutility.isOpponentUsingWinCondition(gamefile, gamefile.whosTurn, 'koth')) return false; // Not using this gamerule

// Was the last move a king move?
const lastMove = moveutil.getLastMove(gamefile.moves);
Expand Down
10 changes: 6 additions & 4 deletions src/client/scripts/esm/chess/util/gamefileutility.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,15 @@ function getRoyalCountOfColor(piecesByKey, color) {
}

/**
* Tests if the player who JUST played a move can win from the specified win condition.
* @param {gamefile} gamefile - The gamefile containing game data.
* Tests if the color's opponent can win from the specified win condition.
* @param {gamefile} gamefile - The gamefile.
* @param {string} friendlyColor - The color of friendlies.
* @param {string} winCondition - The win condition to check against.
* @returns {boolean} True if the opponent can win from the specified win condition, otherwise false.
*/
function isOpponentUsingWinCondition(gamefile, winCondition) {
const oppositeColor = colorutil.getOppositeColor(gamefile.whosTurn);
function isOpponentUsingWinCondition(gamefile, friendlyColor, winCondition) {
if (!winconutil.isWinConditionValid(winCondition)) throw new Error(`Cannot test if opponent of color "${friendlyColor}" is using invalid win condition "${winCondition}"!`);
const oppositeColor = colorutil.getOppositeColor(friendlyColor);
return gamerules.doesColorHaveWinCondition(gamefile.gameRules, oppositeColor, winCondition);
}

Expand Down

0 comments on commit 26a3ceb

Please sign in to comment.