← All projects

Hearts Card Game

Hearts, in the terminal.

The classic trick-taking card game, played in a terminal against computer opponents. Written in Python, it follows the full rules, from dealing and passing to breaking hearts and shooting the moon, with two AI strategies to play against.

Playing

Three to five players. One of them is you.

Choose how many players and a target score, then play with cards drawn right in the terminal. Your hand is laid out side by side with a number under each card: type one to play it, or three to pass. If a card isn't allowed, the game tells you why and asks again.

Rules

Every rule, enforced.

The two of clubs leads the first trick. You must follow suit if you can, hearts can't lead until they've been broken, and nobody can throw a heart or the queen of spades on the first trick. Each heart you take costs a point and the queen of spades 13. Take all 26 and you shoot the moon: you score nothing and everyone else gets 26.

Opponents

Two ways to play against you.

Player 2 is a basic AI that always plays its lowest legal card. Every other computer player uses a better strategy. It still leads low, but when following suit it plays the highest card that won't take the trick, and when it can't follow suit it gets rid of the queen of spades if it holds it, or otherwise its highest card.

And the details.

How it's built

Python, OOP, AI Heuristics.

Hearts is plain Python using only the standard library, split into classes for the cards, the players, a round and the game.

  1. Cards that compare

    Rank and Suit are enums with their own ordering, and Card compares by suit then rank, so sort(), min() and max() work on a hand directly. The AI players rely on that.

  2. One interface for every player

    A Player base class holds the rule checks. The human and both AI players each implement play_card and pass_cards, so a round treats them all the same.

  3. Rounds and the game

    Round plays tricks until the hands are empty, tracks broken hearts, scores penalties and checks for a shot moon. Hearts deals, checks the deal, passes cards and runs rounds until the target is reached.

Hearts Card Game. Hearts, in the terminal.

NextShepherdYour agents, one keystroke away.