Package: machiavelli
MachiavelliA command-line solver for the card gameMachiavelli.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 thecomplete 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 aCSPsolver on each turn.
This solver translates the rules and objective of the game into aninteger programand usesCVXPYto find the best move in a fraction of a second.InstallMachiavelli is available on PyPI:python3-mpipinstallmachiavelliUsageThis 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 likehand: 2c,3c,3d,4s,6s,7s,7d,td,jh,kh, no cards are playable.  Draw one.For output likehand: 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).If you drew a card, enter it at theDrawn cardprompt.If you played, enter the cards you played at the prompt.Record all cards played by the other players at theOther playsprompt.Go to step 3 and repeat until you empty your hand.Win.Demo Game$machiavelli--pretty--emoji
Enterstartinghand:kh,jh,10d,7d,3c,4s,7s,3d,2c,6s<-Userinput
Enterotherplays:9c,8c,7c<-Userinput
Currentgamestate:machiavelli7c,8c,9c2c,3c,3d,4s,
6s,7s,7d,td,jh,kh### Before your play ###table:(7♣️8♣️9♣️)### Solve ###---10left---
hand:2♣️3♣️3♦️4♠️6♠️7♠️7♦️10♦️J♥️K♥️<-Solverfoundnoplayablecards
table:(7♣️8♣️9♣️)Enterdrawncard(s)(orblank):jc<-Sodrawonecard
Entermylastplay(orblank):
Enterotherplays:<-Otherplayersonlydrewcards
Currentgamestate:machiavelli7c,8c,9c2c,3c,3d,4s,
6s,7s,7d,td,jc,jh,kh### Before your play ###table:(7♣️8♣️9♣️)### Solve ###---11left---
hand:2♣️3♣️3♦️4♠️6♠️7♠️7♦️10♦️J♣️J♥️K♥️<-Stillnoplayablecards
table:(7♣️8♣️9♣️)Enterdrawncard(s)(orblank):ac<-Sodrawanothercard
Entermylastplay(orblank):
Enterotherplays:
Currentgamestate:machiavelli7c,8c,9c1c,2c,3c,3d,
4s,6s,7s,7d,td,jc,jh,kh### Before your play ###table:(7♣️8♣️9♣️)### Solve ###---9left---
hand:3♦️4♠️6♠️7♠️7♦️10♦️J♣️J♥️K♥️--use:A♣️2♣️3♣️<-Solverfoundplayablecards
table:(A♣️2♣️3♣️)(7♣️8♣️9♣️)<-Thisisavalidplayofthecardsonthetable
Enterdrawncard(s)(orblank):
Entermylastplay(or"best"=1c,2c,3c):best
Enterotherplays:
Currentgamestate:machiavelli1c,2c,3c,7c,8c,9c3d,<-(Youcanrestartwiththese
4s,6s,7s,7d,td,jc,jh,khargumentstorestoregamestate)...
...
...

Currentgamestate:machiavelli1c,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,khkh### 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 ###---0left---
hand:WIN--use:K♥️<-Solverfoundawinningplay!
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♠️)Enterdrawncard(s)(orblank):^D
QuitAcknowledgementsThanks toKonstantinos Ameranisfor help formulating the integer program and thanks to Konstantinos, Kevin, and Kunal for extensive play-testing and debugging.