Skip to content

Commit

Permalink
Upgrade WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
asaf-kali committed Jan 11, 2024
1 parent 8172733 commit ed14a12
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 54 deletions.
60 changes: 60 additions & 0 deletions playground/cheaters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import logging
import random

from codenames.game.color import TeamColor
from codenames.game.exceptions import QuitGame
from codenames.game.player import GamePlayers
from codenames.game.runner import GameRunner
from codenames.online.codenames_game.runner import CodenamesGameRunner

from playground.boards.english import * # noqa
from playground.boards.hebrew import * # noqa
from playground.printer import print_results
from solvers.cheater.cheaters import CheaterGuesser, CheaterHinter

random.seed(42)
log = logging.getLogger(__name__)


def get_cheaters() -> GamePlayers:
blue_hinter = CheaterHinter(name="Yoda", team_color=TeamColor.BLUE)
red_hinter = CheaterHinter(name="Einstein", team_color=TeamColor.RED)
blue_guesser = CheaterGuesser(name="Anakin", team_color=TeamColor.BLUE, hinter=blue_hinter)
red_guesser = CheaterGuesser(name="Newton", team_color=TeamColor.RED, hinter=red_hinter)
return GamePlayers.from_collection([blue_hinter, blue_guesser, red_hinter, red_guesser])


def run_offline(board: Board = ENGLISH_BOARDS[2]): # noqa
log.info("Running cheaters game...")
log.setLevel(logging.INFO)
game_runner = None
try:
players = get_cheaters()
game_runner = GameRunner(players=players, board=board)
game_runner.run_game()
except QuitGame:
log.info("Game quit")
except: # noqa
log.exception("Error occurred")
finally:
print_results(game_runner) # type: ignore


def run_online():
log.info("Running online game...")
online_manager = runner = None
try:
players = get_cheaters()
online_manager = CodenamesGameRunner(*players.hinters, *players.guessers, show_host=True)
runner = online_manager.auto_start()
except QuitGame:
log.info("Game quit")
except: # noqa
log.exception("Error occurred")
finally:
print_results(runner)
online_manager.close()


if __name__ == "__main__":
run_online()
21 changes: 15 additions & 6 deletions playground/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import os
import random

from codenames.game.color import TeamColor
from codenames.game.exceptions import QuitGame
from codenames.game.player import GamePlayers
from codenames.game.runner import GameRunner

from playground.boards.english import * # noqa
Expand Down Expand Up @@ -44,18 +46,25 @@ def run_offline(board: Board = ENGLISH_BOARDS[2]): # noqa: F405
game_runner = None
try:
# blue_hinter = GPTHinter(name="Yoda", api_key=GPT_API_KEY)
blue_hinter = NaiveHinter(name="Yoda", model_adapter=adapter, max_group_size=4)
red_hinter = NaiveHinter(name="Einstein", model_identifier=model_id, model_adapter=adapter, max_group_size=3)
blue_hinter = NaiveHinter(name="Yoda", team_color=TeamColor.BLUE, model_adapter=adapter, max_group_size=4)
red_hinter = NaiveHinter(
name="Einstein",
team_color=TeamColor.RED,
model_identifier=model_id,
model_adapter=adapter,
max_group_size=3,
)
# red_hinter = GPTHinter(name="Einstein", api_key=GPT_API_KEY)
# red_hinter = OlympicHinter(name="Yoda", model_adapter=adapter)
blue_guesser = CLIGuesser(name="Anakin")
blue_guesser = CLIGuesser(name="Anakin", team_color=TeamColor.BLUE)
# blue_guesser = GPTGuesser(name="Anakin", api_key=GPT_API_KEY)
# blue_guesser = NaiveGuesser(name="Anakin", model_identifier=model_id, model_adapter=adapter)
red_guesser = CLIGuesser(name="Newton")
red_guesser = CLIGuesser(name="Newton", team_color=TeamColor.RED)
# red_guesser = GPTGuesser(name="Newton", api_key=GPT_API_KEY)
# red_guesser = NaiveGuesser(name="Newton", model_identifier=model_id, model_adapter=adapter)
game_runner = GameRunner(blue_hinter, red_hinter, blue_guesser, red_guesser)
game_runner.run_game(board=board)
players = GamePlayers.from_collection([blue_hinter, blue_guesser, red_hinter, red_guesser])
game_runner = GameRunner(players=players, board=board)
game_runner.run_game()
except QuitGame:
log.info("Game quit")
except: # noqa
Expand Down
23 changes: 16 additions & 7 deletions playground/online_codenames_game.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os

from codenames.game.color import TeamColor
from codenames.game.exceptions import QuitGame
from codenames.online.codenames_game.adapter import CodenamesGameLanguage
from codenames.online.codenames_game.runner import CodenamesGameRunner
Expand Down Expand Up @@ -36,23 +37,31 @@

def run_online():
log.info("Running online game...")
online_manager = None
online_manager = runner = None
try:
# blue_hinter = GPTHinter(name="Einstein", api_key=GPT_API_KEY)
blue_hinter = NaiveHinter("Einstein", model_identifier=model_id, model_adapter=adapter) # noqa
red_hinter = NaiveHinter(name="Yoda", model_identifier=model_id, model_adapter=adapter) # noqa
blue_guesser = NaiveGuesser(name="Newton", model_identifier=model_id, model_adapter=adapter) # noqa
blue_hinter = NaiveHinter(
"Einstein", team_color=TeamColor.BLUE, model_identifier=model_id, model_adapter=adapter
)
red_hinter = NaiveHinter(
name="Yoda", team_color=TeamColor.RED, model_identifier=model_id, model_adapter=adapter
)
blue_guesser = NaiveGuesser(
name="Newton", team_color=TeamColor.BLUE, model_identifier=model_id, model_adapter=adapter
)
# red_guesser = GPTGuesser(name="Anakin", api_key=GPT_API_KEY)
red_guesser = NaiveGuesser(name="Anakin", model_identifier=model_id, model_adapter=adapter) # noqa
red_guesser = NaiveGuesser(
name="Anakin", team_color=TeamColor.RED, model_identifier=model_id, model_adapter=adapter
)
online_manager = CodenamesGameRunner(blue_hinter, red_hinter, blue_guesser, red_guesser, show_host=True)
# online_manager = CodenamesGameGameRunner(blue_hinter, red_hinter, blue_guesser, red_guesser, show_host=False)
online_manager.auto_start()
runner = online_manager.auto_start()
except QuitGame:
log.info("Game quit")
except: # noqa
log.exception("Error occurred")
finally:
print_results(online_manager.game_runner)
print_results(runner)
online_manager.close()


Expand Down
5 changes: 3 additions & 2 deletions playground/printer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Optional

from codenames.game.move import GuessMove, HintMove, Move, PassMove
from codenames.game.player import Player, PlayerRole
Expand All @@ -8,7 +9,7 @@
log = logging.getLogger(__name__)


def print_results(game_runner: GameRunner):
def print_results(game_runner: Optional[GameRunner]):
if game_runner is None or game_runner.state is None:
return
state = game_runner.state
Expand Down Expand Up @@ -42,7 +43,7 @@ def _print_moves(game_runner: GameRunner):

def _get_player(game_runner: GameRunner, move: Move) -> Player:
role = PlayerRole.HINTER if isinstance(move, HintMove) else PlayerRole.GUESSER
return game_runner.get_player(team_color=move.team_color, role=role)
return game_runner.players.get_player(team_color=move.team_color, role=role)


def _print_result(state: GameState):
Expand Down
Loading

0 comments on commit ed14a12

Please sign in to comment.