Sure, here is an example of how you can implement the card effects for the Love Letter game in Node.js:
class Card {
constructor(id, name, effect) {
this.id = id;
this.name = name;
this.effect = effect;
}
applyEffect(gameState, targetPlayer) {
this.effect(gameState, targetPlayer);
}
}
// Card effects implementation
const cardEffects = {
// Guard - Guess a player's hand and if you are correct, that player is out of the round.
guard: (gameState, targetPlayer, guessedCard) => {
if (targetPlayer.hasCard(guessedCard)) {
targetPlayer.discardCard(targetPlayer.getCardIndex(guessedCard));
gameState.discardPile.push(guessedCard);
targetPlayer.isKnockedOut = true;
}
},
// Priest - Look at a player's hand.
priest: (gameState, targetPlayer) => {
const card = targetPlayer.cards[0];
const playerName = targetPlayer.name;
console.log(`${playerName}'s card is a ${card.name}.`);
},
// Baron - Compare hands with another player. The player with the lower hand is out of the round.
baron: (gameState, targetPlayer) => {
const currentPlayer = gameState.currentPlayer;
const currentCard = currentPlayer.cards[0];
const targetCard = targetPlayer.cards[0];
if (currentCard.value > targetCard.value) {
targetPlayer.discardCard(0);
gameState.discardPile.push(targetCard);
targetPlayer.isKnockedOut = true;
} else if (currentCard.value < targetCard.value) {
currentPlayer.discardCard(0);
gameState.discardPile.push(currentCard);
currentPlayer.isKnockedOut = true;
}
},
// Handmaid - Protection until your next turn.
handmaid: (gameState, targetPlayer) => {
targetPlayer.hasProtection = true;
},
// Prince - Choose a player to discard their card and draw a new one.
prince: (gameState, targetPlayer) => {
targetPlayer.discardCard(0);
gameState.discardPile.push(targetPlayer.cards[0]);
targetPlayer.drawCard(gameState.deck);
},
// King - Trade hands with another player.
king: (gameState, targetPlayer) => {
const currentPlayer = gameState.currentPlayer;
const currentCard = currentPlayer.cards[0];
const targetCard = targetPlayer.cards[0];
currentPlayer.cards[0] = targetCard;
targetPlayer.cards[0] = currentCard;
},
// Countess - Must be played if the player has a King or Prince in their hand.
countess: (gameState, targetPlayer) => {
// If the player has a King or Prince, they must play the Countess card instead.
const hasKingOrPrince = targetPlayer.cards.some(card => card.id === 'king' || card.id === 'prince');
if (hasKingOrPrince) {
console.log(`${targetPlayer.name} must play the Countess card.`);
}
},
// Princess - If played, the player is out of the round.
princess: (gameState, targetPlayer) => {
targetPlayer.discardCard(0);
gameState.discardPile.push(targetPlayer.cards[0]);
targetPlayer.isKnockedOut = true;
},
};
// Example usage of Card class and its effects:
const guard = new Card('guard', 'Guard', cardEffects.guard);
const princess = new Card('princess', 'Princess', cardEffects.princess);
const gameState = {
players: [
{
name: 'John',
cards: [guard],
},
{
name: 'Mary',
cards: [princess],
},
],
currentPlayerIndex: 0,
deck: [
{
id: 'prince',
name: 'Prince',
value: 5,
},
// other cards omitted for brevity
],
discardPile: [],
};
guard.applyEffect(gameState, gameState.players[1], gameState.players[1].cards[0]);
// This will try to guess Mary's card and knock her out of the round if the guess is correct.
princess.applyEffect(gameState, gameState.players[0], gameState.players[0].cards[0]);
// This will discard John's card and knock him out of the round.
In this example, we define a Card class that has an applyEffect method that takes in the game state and a target player as parameters, and applies the effect of the card by calling the appropriate function from the cardEffects object.
Each function in the cardEffects object represents the effect of a single card. The function takes in the game state, the target player, and any other parameters needed for that particular card, and modifies the game state accordingly.
In the example usage code, we create instances of the Guard and Princess cards, and apply their effects to the game state by calling their applyEffect methods. This shows how you can use the Card class to simulate the gameplay of Love Letter.