Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

Can you show me how to keep track of a hearts game using a mongoDB dataset with mongoose

  1. User

    Can you show me how to keep track of a hearts game using a mongoDB dataset with mongoose

  2. Assistant

    Yes, here's an example of how to keep track of a Hearts game using a MongoDB dataset with Mongoose:

    First, we need to design our data schema. A Hearts game consists of players, rounds, and tricks. Each player has a hand, which they use to play cards during a round. A round consists of multiple tricks. Each trick is won by the player who played the highest card of the suit led. Here's how our schema could look like:

    const mongoose = require('mongoose');
    
    const playerSchema = new mongoose.Schema({
      name: String,
      hand: [String], // array of card codes (e.g. "H3" for the Three of Hearts)
    });
    
    const trickSchema = new mongoose.Schema({
      cards: [String], // array of card codes played in the trick
      winner: String, // name of the player who won the trick
    });
    
    const roundSchema = new mongoose.Schema({
      number: Number,
      tricks: [trickSchema],
    });
    
    const gameSchema = new mongoose.Schema({
      players: [playerSchema],
      rounds: [roundSchema],
      currentPlayer: String, // name of the player whose turn it is to play
      deck: [String], // array of remaining card codes in the deck
    });
    

    Next, we need to define methods to interact with our data. Here are some examples:

    // create a new game with the given player names and shuffled deck
    const createGame = async (playerNames) => {
      const deck = shuffleDeck();
      const players = playerNames.map(name => ({ name, hand: [] }));
      const game = new Game({ players, deck, currentPlayer: players[0].name });
      await game.save();
      return game;
    };
    
    // deal a hand of 13 cards to each player
    const dealHands = async (game) => {
      const { players, deck } = game;
      for (let i = 0; i < 13; i++) {
        for (let j = 0; j < players.length; j++) {
          const card = deck.pop();
          players[j].hand.push(card);
        }
      }
      game.deck = deck;
      await game.save();
    };
    
    // play a card from the given player's hand in the current trick
    const playCard = async (game, playerName, cardCode) => {
      const { players, rounds, currentPlayer } = game;
      const round = rounds[rounds.length - 1];
      const trick = round.tricks[round.tricks.length - 1];
      const playerIndex = players.findIndex(p => p.name === playerName);
      const cardIndex = players[playerIndex].hand.findIndex(c => c === cardCode);
      const card = players[playerIndex].hand.splice(cardIndex, 1)[0];
      trick.cards.push(card);
      if (trick.cards.length === players.length) {
        trick.winner = getTrickWinner(trick.cards, currentPlayer);
        game.currentPlayer = trick.winner;
        round.tricks.push({ cards: [], winner: null });
      } else {
        game.currentPlayer = players[(playerIndex + 1) % players.length].name;
      }
      await game.save();
    };
    
    // get the current state of the game (hands, tricks, etc.)
    const getGameState = async (game) => {
      const { players, rounds } = game;
      const gameState = { players: [], rounds: [] };
      for (let i = 0; i < players.length; i++) {
        gameState.players.push({ name: players[i].name, hand: players[i].hand });
      }
      for (let i = 0; i < rounds.length; i++) {
        const roundState = { number: rounds[i].number, tricks: [] };
        for (let j = 0; j < rounds[i].tricks.length; j++) {
          const trickState = { cards: rounds[i].tricks[j].cards, winner: rounds[i].tricks[j].winner };
          roundState.tricks.push(trickState);
        }
        gameState.rounds.push(roundState);
      }
      return gameState;
    };
    

    Finally, we can use these methods to play a game of Hearts:

    // connect to MongoDB and create a new game with four players
    mongoose.connect('mongodb://localhost/hearts');
    const Game = mongoose.model('Game', gameSchema);
    const game = await createGame(['Alice', 'Bob', 'Charlie', 'David']);
    
    // deal hands to the players and start the first round
    await dealHands(game);
    game.rounds.push({ number: 1, tricks: [{ cards: [], winner: null }] });
    await game.save();
    
    // play the first trick
    await playCard(game, 'Alice', 'H3');
    await playCard(game, 'Bob', 'H9');
    await playCard(game, 'Charlie', 'HJ');
    await playCard(game, 'David', 'HK');
    
    // play the rest of the round
    // ...
    
    // get the current state of the game
    const gameState = await getGameState(game);
    console.log(gameState);
    
    // disconnect from MongoDB
    await mongoose.disconnect();
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.