Skip to content
Julen Somet García
FRENES
← All missions

Software LOG-01 · Jun 2024

Catan, the card game: a Monte Carlo AI in C

A two-player card game inspired by Catan, in C with SDL2, against an MCTS AI that does not cheat. The second game of our 1st-year project, left unfinished and then taken back and completed.

Role
1st-year project at ISIMA, team of 3: game rules and structures, then took over the whole project to finish it
Stack
  • C
  • SDL2
  • MCTS
  • Makefile
Screenshot of Catan, the card game: a Monte Carlo AI in C

AThe context

After Quantik, we started a second game: a card duel inspired by Catan, with resources, a market, a draw pile and buildings that earn points. At the end of the project, it did not compile: two competing SDL interfaces, an AI reduced to a skeleton, and incomplete rules.

BWhat I did

  • A single game state: each player kept their own copy of the draw pile, market and discard pile, which had to be synchronised at every action
  • Rules completed from our code and the card texts: sides A and B of roads and knights, city that refreshes the market, end of game when the draw pile is empty
  • MCTS AI: the tree covers the actions of a turn (trades, construction), each branch is evaluated by simulating games to the end
  • SDL interface rebuilt on Valentin's images: market, hand, buildings with their cost, possible actions highlighted, two-player game with a hand-over screen
  • Tests: none of the 62 cards is lost over hundreds of games, and the AI beats a greedy strategy about three times out of four

CThe challenge

The game relies on the luck of the draw and on hidden information: the opponent's hand. A classic minimax does not fit, hence the choice of MCTS. So that the AI does not cheat, each simulation starts by randomly dealing the opponent's hand from the cards it does not know. And since the same move can lead to different draws, the tree stores only sequences of actions: the game state is replayed at each simulation.

DIn pictures

End of a game against the AI, with both players' scores.
End of a game against the AI, with both players' scores.