mbardoe / cgt

Combinatorial game theory evaluator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Combinatorial game theory

Tests

Generalized deterministic recursive/dynamic combinatorial game theory calculator for evaluating the state of any two-person game that has a corresponding number. Only uses vanilla python and pydantic (for type-checking).

Rationale:

The best way to understand a process is to write a program automating it.

Example game (Push)

from cgt.game import GameTree
from cgt.pushpin import PushPinGame

case = PushPinGame(["", "L", "R"])
print(GameTree(case).value())

Output:

>>> -1.75

Implementing your own game

class AbstractGame():
    """
    Example of required methods and functions that must be implemented
    """
    def __init__(self, state: AbstractState = AbstractState()): # some state to pass to the game
        raise NotImplementedError

    def moves(self) -> List[Set[AbstractState]]: # return a list of a set of moves for each player
        """
        Returns the possible moves left. If there are no moves left, return empty lists.
        """
        raise NotImplementedError

    def apply(self, state: AbstractState): # takes a state and applies it to the game
        """
        Apply a move or "state" return by moves.
        """
        raise NotImplementedError

    @staticmethod
    def prune_states(self, state: AbstractState) -> AbstractState:
        """
        Normalizes the state
        """
        raise NotImplementedError

For a real-live working example, see cgt/pushpin.py

About

Combinatorial game theory evaluator


Languages

Language:Python 100.0%