Skip to content

Commit 828eabd

Browse files
add a test game to the catalog defined directly in code
1 parent d11ada7 commit 828eabd

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

src/pygambit/catalog/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .catalog import PrisonersDilemma, TwoStageMatchingPennies
1+
from .catalog import PrisonersDilemma, PrisonersDilemmaTestgame, TwoStageMatchingPennies
22

3-
__all__ = ["PrisonersDilemma", "TwoStageMatchingPennies"]
3+
__all__ = ["PrisonersDilemma", "TwoStageMatchingPennies", "PrisonersDilemmaTestgame"]

src/pygambit/catalog/catalog.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from pathlib import Path
22

3+
import numpy as np
4+
35
from pygambit import Game, read_efg, read_nfg
46

57
_CATALOG_DIR = Path(__file__).parent
@@ -12,6 +14,17 @@ class CatalogGame:
1214
This class serves as a template for specific games in the catalog.
1315
Calling any subclass will return an instance of the corresponding game.
1416
"""
17+
18+
def __new__(cls) -> Game:
19+
raise NotImplementedError("Subclasses must implement __new__ method")
20+
21+
22+
class CatalogGameFromFile(CatalogGame):
23+
"""
24+
Base class for catalog games loaded from files.
25+
This class serves as a template for specific games in the catalog.
26+
Calling any subclass will return an instance of the corresponding game.
27+
"""
1528
# Subclasses must define these
1629
game_file: str | None = None
1730

@@ -39,11 +52,25 @@ def __init_subclass__(cls, **kwargs):
3952
raise TypeError(f"{cls.__name__} must define 'game_file' class attribute")
4053

4154

42-
class PrisonersDilemma(CatalogGame):
55+
class PrisonersDilemma(CatalogGameFromFile):
4356
"""Prisoner's Dilemma game."""
4457
game_file = "pd.nfg"
4558

4659

47-
class TwoStageMatchingPennies(CatalogGame):
60+
class TwoStageMatchingPennies(CatalogGameFromFile):
4861
"""Two-Stage Matching Pennies game."""
4962
game_file = "2smp.efg"
63+
64+
65+
class PrisonersDilemmaTestgame(CatalogGame):
66+
"""A simple test game based on the Prisoner's Dilemma."""
67+
def __new__(cls) -> Game:
68+
player1_payoffs = np.array([[-1, -3], [0, -2]])
69+
player2_payoffs = np.transpose(player1_payoffs)
70+
71+
g1 = Game.from_arrays(
72+
player1_payoffs,
73+
player2_payoffs,
74+
title="Test Prisoner's Dilemma"
75+
)
76+
return g1

0 commit comments

Comments
 (0)