Skip to content

Commit

Permalink
Implemented a moverequester that silcences the bot. Closes #80 . Trig…
Browse files Browse the repository at this point in the history
…ger CI
  • Loading branch information
miselico committed Dec 18, 2023
1 parent 2670106 commit a3aacbb
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/schnapsen/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
from __future__ import annotations

from abc import ABC, abstractmethod
import contextlib
from dataclasses import dataclass, field
from enum import Enum
from io import StringIO
from random import Random
from typing import Iterable, Optional, Union, cast, Any
import sys
from typing import Generator, Iterable, Optional, Union, cast, Any
from .deck import CardCollection, OrderedCardCollection, Card, Rank, Suit
import itertools

Expand All @@ -19,6 +22,7 @@ class Bot(ABC):
Besides the get_move method, it is also possible to override notify_trump_exchange and notify_game_end to get notified when these events happen.
"""

def __init__(self, name: Optional[str] = None) -> None:
"""
Args:
Expand Down Expand Up @@ -1226,12 +1230,41 @@ def get_move(self, bot: BotState, perspective: PlayerPerspective, leader_move: O


class SimpleMoveRequester(MoveRequester):
"""The SimplemoveRquester just asks the move, and does nto time out"""
"""The SimplemoveRquester just asks the move, and does not time out"""

def get_move(self, bot: BotState, perspective: PlayerPerspective, leader_move: Optional[Move]) -> Move:
return bot.get_move(perspective, leader_move=leader_move)


class __DummyFile(StringIO):
def write(self, _: str) -> int:
return 0

def flush(self) -> None:
pass


class SilencingMoveRequester(MoveRequester):
"""
This MoveRequester just asks the move, but before doing so it routes stdout to a dummy file
"""

def __init__(self, requester: MoveRequester) -> None:
self.requester = requester

@contextlib.contextmanager
@staticmethod
def __nostdout() -> Generator[None, Any, None]:
save_stdout = sys.stdout
sys.stdout = __DummyFile()
yield
sys.stdout = save_stdout

def get_move(self, bot: BotState, perspective: PlayerPerspective, leader_move: Optional[Move]) -> Move:
with self.__nostdout():
return self.requester.get_move(bot, perspective, leader_move)


class MoveValidator(ABC):
"""
An object of this class can be used to check whether a move is valid.
Expand Down

0 comments on commit a3aacbb

Please sign in to comment.