Great question! Here's an implementation of the Love Letter game with all 8 cards:
// Define constants for each card
const GUARD = 1;
const PRIEST = 2;
const BARON = 3;
const HANDMAID = 4;
const PRINCE = 5;
const KING = 6;
const COUNTESS = 7;
const PRINCESS = 8;
// Initialize the deck of cards
const deck = [
GUARD, GUARD, GUARD, GUARD, GUARD,
PRIEST, PRIEST, PRIEST,
BARON, BARON,
HANDMAID, HANDMAID,
PRINCE, PRINCE,
KING,
COUNTNESS,
PRINCESS
];
// Initialize the game state
let players = [
{id: "player1", cards: [], score: 0, isActive: true},
{id: "player2", cards: [], score: 0, isActive: false}
];
let discardedCards = [];
// Shuffle function to randomly re-arrange the cards in the deck
function shuffle(deck) {
for (let i = deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[deck[i], deck[j]] = [deck[j], deck[i]];
}
}
// Deal one card to each player
function dealCards(players, deck) {
for (let i = 0; i < players.length; i++) {
let card = deck.pop();
players[i].cards.push(card);
}
}
// Draw a card from the deck or discard pile
function drawCard(players, deck, discardedCards, playerId) {
let card;
if (discardedCards.length > 0) {
card = discardedCards.pop();
} else {
card = deck.pop();
}
for (let i = 0; i < players.length; i++) {
if (players[i].id === playerId) {
players[i].cards.push(card);
break;
}
}
}
// Discard a card from the player's hand
function discardCard(players, discardedCards, playerId, card) {
for (let i = 0; i < players.length; i++) {
if (players[i].id === playerId) {
let index = players[i].cards.indexOf(card);
discardedCards.push(players[i].cards.splice(index, 1)[0]);
break;
}
}
}
// Play the Guard card against a target player
function playGuard(players, discardedCards, playerId, targetPlayerId, guess) {
let targetCard;
for (let i = 0; i < players.length; i++) {
if (players[i].id === targetPlayerId) {
targetCard = players[i].cards[0];
break;
}
}
if (targetCard === guess) {
for (let i = 0; i < players.length; i++) {
if (players[i].id === playerId) {
players[i].score++;
break;
}
}
} else {
for (let i = 0; i < players.length; i++) {
if (players[i].id === targetPlayerId) {
discardCard(players, discardedCards, targetPlayerId, targetCard);
break;
}
}
}
}
// Play the Priest card against a target player
function playPriest(players, playerId, targetPlayerId) {
let targetCard;
for (let i = 0; i < players.length; i++) {
if (players[i].id === targetPlayerId) {
targetCard = players[i].cards[0];
break;
}
}
for (let i = 0; i < players.length; i++) {
if (players[i].id === playerId) {
players[i].score++;
console.log(`Player ${targetPlayerId} has a ${targetCard} card.`);
break;
}
}
}
// Play the Baron card against a target player
function playBaron(players, discardedCards, playerId, targetPlayerId) {
let playerCard, targetCard;
for (let i = 0; i < players.length; i++) {
if (players[i].id === playerId) {
playerCard = players[i].cards[0];
} else if (players[i].id === targetPlayerId) {
targetCard = players[i].cards[0];
}
}
if (playerCard > targetCard) {
for (let i = 0; i < players.length; i++) {
if (players[i].id === playerId) {
players[i].score++;
break;
}
}
} else if (playerCard < targetCard) {
for (let i = 0; i < players.length; i++) {
if (players[i].id === targetPlayerId) {
players[i].score++;
break;
}
}
}
}
// Play the Handmaid card to become immune to other players' attacks
function playHandmaid(players, playerId) {
for (let i = 0; i < players.length; i++) {
if (players[i].id === playerId) {
players[i].isActive = false;
break;
}
}
}
// Play the Prince card to force a target player to discard their hand and draw a new card
function playPrince(players, discardedCards, playerId, targetPlayerId) {
let targetCard;
for (let i = 0; i < players.length; i++) {
if (players[i].id === targetPlayerId) {
targetCard = players[i].cards[0];
if (targetCard === PRINCESS) {
for (let i = 0; i < players.length; i++) {
if (players[i].id === playerId) {
players[i].score++;
break;
}
}
} else {
discardCard(players, discardedCards, targetPlayerId, targetCard);
drawCard(players, deck, discardedCards, targetPlayerId);
}
break;
}
}
}
// Play the King card to swap hands with a target player
function playKing(players, playerId, targetPlayerId) {
let playerCard, targetCard;
for (let i = 0; i < players.length; i++) {
if (players[i].id === playerId) {
playerCard = players[i].cards[0];
} else if (players[i].id === targetPlayerId) {
targetCard = players[i].cards[0];
players[i].cards[0] = playerCard;
}
}
}
// Play the Countess card
function playCountess(players, playerId) {
// The Countess card must be played if the player has either the King or Princess card in hand
// Otherwise, playing the Countess card is optional and doesn't affect gameplay
}
// Play the Princess card and lose the game
function playPrincess(players, discardedCards, playerId) {
for (let i = 0; i < players.length; i++) {
if (players[i].id === playerId) {
discardCard(players, discardedCards, playerId, PRINCESS);
break;
}
}
}
// Call the shuffle function to randomize the deck
shuffle(deck);
// Deal one card to each player
dealCards(players, deck);
// Test the game logic by playing some cards
playPriest(players, "player1", "player2");
playGuard(players, discardedCards, "player2", "player1", KING);
playBaron(players, discardedCards, "player1", "player2");
playPrince(players, discardedCards, "player2", "player1");
playKing(players, "player1", "player2");
playPrincess(players, discardedCards, "player1");
This implementation includes functions for each card, with parameters for the player IDs and any additional information required by the card. The game state is tracked using an array of player objects, each with an ID, an array of cards in their hand, a score, and an isActive flag to indicate whether they are immune to other players' attacks. The game logic is tested by playing some sample cards, which update the game state according to the rules of the Love Letter game.