Solver for the card game Machiavelli
Project description
Machiavelli
A command-line solver for the card game Machiavelli.
In Machiavelli, cards are played and arranged in groups on the table. You either draw one card or play any number of cards from your hand. The only constraint is that at the end of your turn, all the cards on the table are grouped in valid sets or sequences of at least three each. See the complete rules here.
Machiavelli is particularly nice to solve with a computer because the only game state that matters is the list of cards on the table and in your hand. A valid grouping of cards can simply be recomputed from scratch with a CSP solver on each turn. This solver translates the rules and objective of the game into an integer program and uses CVXPY to find the best move in a fraction of a second.
Install
Machiavelli is available on PyPI:
python3 -m pip install machiavelli
Usage
This package provides a command line tool to solve the card game Machiavelli. The tool keeps track of the game state by prompting for cards drawn by you and cards played by anyone. It will then tell you the "best" play (the play that uses the most cards from your hand).
- Enter your initial hand of cards at the prompt e.g.
kh,td,jh,7d,3c,4s,7s,3d,2c,6s
(king of hearts, 10 of diamonds, ...). - Enter all cards played by others before your first turn e.g.
7c,8c,9c
(a sequence of clubs from 7 to 9). - Check the solution printout.
- For output like
hand: 2c,3c,3d,4s,6s,7s,7d,td,jh,kh
, no cards are playable. Draw one. - For output like
hand: 3d,7d,td,jc,jh,kh -- use: 1d,4s,5c,6s,7s
, the cards on the right are playable. Rearrange the cards on the table and add yours as indicated e.g.table: (1c,[1d],1h) ([5c],5d,5h) (2c,3c,4c) (2h,3h,4h) ([4s],5s,[6s],[7s]) (7c,8c,9c)
.
- For output like
- If you drew a card, enter it at the
Drawn card
prompt. - If you played, enter the cards you played at the prompt.
- Record all cards played by the other players at the
Other plays
prompt. - Go to step 3 and repeat until you empty your hand.
- Win.
Demo Game
$ machiavelli --pretty --emoji
Enter starting hand: kh,jh,10d,7d,3c,4s,7s,3d,2c,6s <- User input
Enter other plays: 9c,8c,7c <- User input
Current game state: machiavelli 7c,8c,9c 2c,3c,3d,4s,
6s,7s,7d,td,jh,kh
### Before your play ###
table: (7♣️ 8♣️ 9♣️)
### Solve ###
--- 10 left ---
hand: 2♣️ 3♣️ 3♦️ 4♠️ 6♠️ 7♠️ 7♦️ 10♦️ J♥️ K♥️ <- Solver found no playable cards
table: (7♣️ 8♣️ 9♣️)
Enter drawn card(s) (or blank): jc <- So draw one card
Enter my last play (or blank):
Enter other plays: <- Other players only drew cards
Current game state: machiavelli 7c,8c,9c 2c,3c,3d,4s,
6s,7s,7d,td,jc,jh,kh
### Before your play ###
table: (7♣️ 8♣️ 9♣️)
### Solve ###
--- 11 left ---
hand: 2♣️ 3♣️ 3♦️ 4♠️ 6♠️ 7♠️ 7♦️ 10♦️ J♣️ J♥️ K♥️ <- Still no playable cards
table: (7♣️ 8♣️ 9♣️)
Enter drawn card(s) (or blank): ac <- So draw another card
Enter my last play (or blank):
Enter other plays:
Current game state: machiavelli 7c,8c,9c 1c,2c,3c,3d,
4s,6s,7s,7d,td,jc,jh,kh
### Before your play ###
table: (7♣️ 8♣️ 9♣️)
### Solve ###
--- 9 left ---
hand: 3♦️ 4♠️ 6♠️ 7♠️ 7♦️ 10♦️ J♣️ J♥️ K♥️
-- use: A♣️ 2♣️ 3♣️ <- Solver found playable cards
table: (A♣️ 2♣️ 3♣️) (7♣️ 8♣️ 9♣️) <- This is a valid play of
the cards on the table
Enter drawn card(s) (or blank):
Enter my last play (or "best"=1c,2c,3c): best
Enter other plays:
Current game state: machiavelli 1c,2c,3c,7c,8c,9c 3d, <- (You can restart with these
4s,6s,7s,7d,td,jc,jh,kh arguments to restore game state)
...
...
...
Current game state: machiavelli 1c,1d,1d,1h,2s,2c,2d,
2d,2h,3s,3c,3d,3h,4s,4s,4c,4d,4h,5s,5c,5d,5h,6s,6c,6d,
6d,6h,7s,7s,7c,7c,7d,7d,7h,8s,8s,8c,8d,8d,8h,8h,9s,9s,
9c,9d,9h,ts,ts,tc,tc,td,th,th,js,js,jc,jc,jd,jd,jh,jh,
qs,qd,qh,ks,ks,kc,kh kh
### Before your play ###
table: (A♣️ A♦️ A♥️) (2♣️ 2♦️ 2♥️) (10♠️ 10♣️ 10♥️)
(J♠️ J♣️ J♦️ J♥️) (K♠️ K♣️ K♥️) (A♦️ 2♦️ 3♦️ 4♦️ 5♦️
6♦️ 7♦️ 8♦️ 9♦️ 10♦️ J♦️ Q♦️) (2♠️ 3♠️ 4♠️) (3♣️ 4♣️
5♣️ 6♣️ 7♣️) (3♥️ 4♥️ 5♥️ 6♥️ 7♥️ 8♥️) (4♠️ 5♠️ 6♠️
7♠️ 8♠️ 9♠️) (6♦️ 7♦️ 8♦️) (7♠️ 8♠️ 9♠️ 10♠️ J♠️ Q♠️
K♠️) (7♣️ 8♣️ 9♣️ 10♣️ J♣️) (8♥️ 9♥️ 10♥️ J♥️ Q♥️)
### Solve ###
--- 0 left ---
hand: WIN -- use: K♥️ <- Solver found a winning play!
table: (A♣️ A♦️ A♥️) (2♣️ 2♦️ 2♥️) (7♠️ 7♣️ 7♦️) (8♠️
8♦️ 8♥️) (10♠️ 10♣️ 10♥️) (J♠️ J♣️ J♦️ J♥️) (K♠️ K♣️
K♥️) (A♦️ 2♦️ 3♦️ 4♦️ 5♦️ 6♦️) (2♠️ 3♠️ 4♠️) (3♣️ 4♣️
5♣️ 6♣️ 7♣️ 8♣️ 9♣️ 10♣️ J♣️) (3♥️ 4♥️ 5♥️ 6♥️ 7♥️ 8♥️
9♥️ 10♥️ J♥️ Q♥️ K♥️) (4♠️ 5♠️ 6♠️ 7♠️ 8♠️ 9♠️) (6♦️
7♦️ 8♦️ 9♦️ 10♦️ J♦️ Q♦️) (9♠️ 10♠️ J♠️ Q♠️ K♠️)
Enter drawn card(s) (or blank): ^D
Quit
Acknowledgements
Thanks to Konstantinos Ameranis for help formulating the integer program and thanks to Konstantinos, Kevin, and Kunal for extensive play-testing and debugging.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file machiavelli-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: machiavelli-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.0 setuptools/57.4.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2761b7728f09e3bf06d56514a79ceb11b42c3dcbcaa3093967ecdb2d73b62f99 |
|
MD5 | 3845218d4a263dc5743b9db2dad7a018 |
|
BLAKE2b-256 | 1342c6177df6dc39d7ffb02a6e3fd18a941b4534d080e376db382b11478d4421 |