Website title

Computing: The Details
Lex's blog of curious computer-related shenanigans.

Blog post

Playing AmongUs with a sat solver

Published: 2020-11-29

This blog post is also available in video form! Watch it on YouTube

Looking for the code? Get the code on GitHub

A satisfyability solver (sat solver) is a tool that finds solutions to a boolean formula. These are normally used for checking mathematical proofs, or for verification of computer programs. I've used them for this purpose, but sometimes it's fun to do unusual things with them. I used one to help me play the popular online video game of social deduction: Among Us.

It's actually quite hard to be good at doing perfect deductions while playing Among Us. There's lots to remember and although the logic you need for deducing the hidden information is simple, it's hard to apply it properly in your mind within the time.

Given how hard this game is, I thought it might be fun to create a tool that helps you play by remembering all the things you've learned so far, and making the deductions for you.

So I wrote a program for doing exactly this. It knows all the constraints that exist on the game state, and it knows the facts you tell it, and then it recommends people to eject after meetings. Assuming you give it only things you know for sure then it will perfectly deduce who the impostors are.

The constraints that it knows at the start are really simple things like: people who were killed cannot be impostors, there's always more crewmates than impostors after an ejection that doesn't end the game, and some other simple things like that.

You can tell it that someone is a crewmate, maybe you saw them do a visual task, you know they're a crewmate. If you see someone murder or vent, then that's an easy one, you know they're an impostor. You also tell it who has been murdered, and who was ejected, of course. But the most useful thing is to tell it when a group of people is known to contain an impostor.

So if you never let green or red out of your sight for the whole round, but somebody was killed, then you know there's at least one impostor amongst all the players that are not you, green, or red.

The UI

The UI for this thing is a webpage. You run the server locally on your PC, and open up your browser to use the solver. It's a really simple UI. At first you simply select which players are present, and how many impostors there are.

A user selects players from a list of colours. They select red, blue, pink, orange, black, and purple. They start a game with two impostors.

Once a game is created, you can input information by clicking the buttons on each player, or by adding them to the selection with the tick/cross icon, and then marking the selected groups as including an impostor.

Each time more information is learned, the sat solver finds all the possible models (assignments of the variables in the boolean expression) that match all of the given knowledge. The UI shows the number of models in which each player is an impostor. The idea is that if you are in more models, then you're more likely to be an impostor. It also recommends an action to take at the meeting.

A user interacts with the UI to inform the solver that the red player is a crewmate, the blue player was murdered, there is at least one impostor among black and purple, there is at least one impostor amont pink and black, black was ejected, purple is a crewmate. As they do this, the number of models shown for each player decreases from the initial 5 models each. Below the controls for information input are two lists. One of players to suspect, and one for innocent players.

Behind the scenes

It's a piece of python code, which serves the UI over http. It uses Z3, which is a sat-solver, to calculate all the possible explanations for the facts you provide. That means I didn't tell it any of the logic for actually making deductions, I just told it the constraints and facts, and the logic doesn't matter because of the way a sat-solver finds solutions to equations. It also means it's really easy to add extra information, if ever an update to the game adds ways to learn things, or adds new game state.

Z3 has some really nice python bindings that allow you to have a python object that refers to a Z3 variable. Perhaps you're interested to see some code? I won't do an in-depth analysis here, but the code is on GitHub and has a lot of documentation comments. If you want a deep understanding of it, have a read of the code.

The first thing that it does when a game starts is set up some basic facts about the game...


class GameSolver:
    def __init__(self, n_players, n_impostors):
        """ Construct a GameSolver with a known number of players and number of impostors """
        self.n_players = n_players
        # Some variables that model the state of each player
        self.alive, self.murdered, self.impostor, self.ejected = ( 
            [z3.Bool('%s-%d' % (varname, player_id)) for player_id in range(self.n_players)]
            for varname in [
                'player-is-alive'
                ,'player-was-murdered'
                ,'player-is-impostor'
                ,'player-was-ejected'
            ]
        )

        # First bit of information known is the number of impostors
        self.n_impostors = z3.IntVal(n_impostors)
        self.n_alive_impostors = z3.Int('alive_impostors')
        self.knowns = [] # for known facts that will never change until the game ends

        # State for building facts about players during the game, when what is known
        # might change (I know blue is not murderd and green is not ejected, but then
        # green murders blue, we figure it out and eject green, now blue's murdered
        # state changed and green's ejected state changed)
        self.murdered_pids = []
        self.ejected_pids = []

The simple constraints are ones such as "player X is a crewmate". These become basic assertions in Z3, which are tracked in the knowns variable on a GameSolver instance.

    def learn_certain_not_impostor(self, colour):
        """ learn that the player with the specified colour is certainly not an impostor """
        p = self.pid(colour)
        self.knowns.append(
            z3.Not(self.impostor[p])
        )

I find the code for specifying the basic constraints of the game to be quite nicely readable, thanks to the Z3 python bindings...

    def basic_constraints(self):
        """ Returns a list of constraints representing basic facts about the possible game states """
        return [
            # Number of impostors is number of impostors
            self.n_impostors == sumbools(self.impostor)
            # A number of impostors can be alive
            ,self.n_alive_impostors == sumbools(
                z3.And(self.alive[i], self.impostor[i])
                for i in range(self.n_players)
            )
            # People that were murdered cannot be impostors
            ,*[
                z3.Implies(self.murdered[i], z3.Not(self.impostor[i]))
                for i in range(self.n_players)
            ]
            # Alive people have not been murdered and have not been ejected
            ,*[
                z3.And(z3.Not(self.murdered[i]), z3.Not(self.ejected[i])) == self.alive[i]
                for i in range(self.n_players)
            ]
            # There is at least one not-dead impostor
            ,self.n_alive_impostors > z3.IntVal(0)
            # Twice the number of alive impostors is less than the number of alive crewmates
            ,sumbools(self.alive) > (self.n_alive_impostors * z3.IntVal(2))
        ]

Normally Z3 will only find one solution to your system of equations. To make Z3 provide all of the possible models, you have to iteratively remove each model that it finds, until it cannot find any more. I do this by adding a constraint that at least one player that is an impostor in this model must not be an impostor. This makes that particular model no longer valid, and I can search for a new one.

    def not_this_model(self, model):
        """ Returns a constraint that prevents the given model from being valid """
        return z3.Or(
            *[self.impostor[pid] != model[self.impostor[pid]]
            for pid in range(self.n_players)]
        )

Of course, this constraint must then be removed before reevaluating the solutions.

There is a lot more supporting code, and I've not included all of the Z3 logic here, so if you are interested, certainly do have a look at the code on GitHub.

Can I use this to cheat?

No, not really, it's not actually that helpful during the game. I did try and found that it was actually harder than normal play. Making this tool was mostly just an academic exercise.