/terraforming-mars-bot

A proof-of-concept Terraforming Mars AI meant to play https://github.com/bafolts/terraforming-mars/ automatically

Primary LanguageJavaScriptMIT LicenseMIT

terraforming-mars-bot

Gitpod ready-to-code

A proof-of-concept Terraforming Mars AI meant to play https://github.com/bafolts/terraforming-mars/ automatically.

How to run this

Open this repository in Gitpod, a free online development environment:

Open in Gitpod

This will give you a cloud workspace containing this repository already set-up and running:

terraforming-mars-bot-in-gitpod

How to implement your own bot

  1. Feel free to fork this repository, or to contribute your own bot here (Pull Requests welcome!)

  2. Create your own bot script under the bots/ directory, for example by copying one of the existing bots:

cd bots/
cp random.js my-bot.js
  1. Implement or modify the required functions in your bot script, like for example exports.playInitialResearchPhase:
exports.playInitialResearchPhase = async (game, availableCorporations, availableCards) => {
  const corporation = // TODO
  const initialCards = // TODO
  return [[ corporation ], initialCards];
}

💡 Feel free to inspect the game object, for example with console.log(game), as it holds all the interesting details about the game in progress.

  1. Test your bot by starting a new game of Terraforming Mars, and make your bot play it:
  • node start-game: Start a new game, and outputs a player link
  • node play-bot --bot=bots/my-bot PLAYER_LINK: Makes your bot play the game using the player link from above

Here is what such a test run may look like in yout Terminal:

$ node start-game
Started new game. Player links:
  - Bot (red): http://localhost:8080/player?id=6d9796440d25

$ node play-bot --bot=bots/my-bot http://localhost:8080/player?id=6d9796440d25
Bot plays: [ [ 'CrediCor' ], [ 'Solar Wind Power' ] ]
...
Game ended!
Game state (1p): gen=14, temp=-20, oxy=1, oceans=2, phase=end
Final scores:
  - Bot (red): 22 points

💡 This should make your bot play until the end of the game. However, if an error occurs (for example, if your bot tries to play an illegal move) you can debug it like so:

  • Notice in the logs what the game was waiting for, and what your bot attempted to play
  • Then, open the bot's player link in your browser
  • Open your browser's Developer Tools, then open the Network panel, and clear all previous entries
  • Manually play what your bot should have played
  • Click on the first item that appeared in your Network panel (looks like /input), and display the request's JSON payload
  • Then, compare what your bot tried to play with what you manually played -- this should tell you what your bot should do differently

Helper scripts

This repository comes with a few useful scripts:

node start-game [SERVER]

# Start a new local game of Terraforming Mars (outputs a player link)
node start-game

# Start a new game on any Terraforming Mars server
node start-game https://my-tm-server.com

node play-bot PLAYER_LINK

# Make a bot play Terraforming Mars (accepts a regular player link)
node play-bot https://my-tm-server.com/player?id=123456789

# Make a specific bot play Terraforming Mars
node play-bot --bot=bots/random.js https://my-tm-server.com/player?id=123456789