-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from dataclasses import dataclass | ||
|
||
from .player import Player | ||
|
||
|
||
@dataclass(slots=True) | ||
class Round: | ||
deck: list[str] | ||
discard: list[str] | ||
hands: list[list[str]] | ||
|
||
|
||
@dataclass(slots=True) | ||
class Context: | ||
players: list[Player] | ||
rounds: int | ||
current_round: Round | None = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from .player import Player | ||
from .rule import Rule | ||
from .context import Context, Round | ||
|
||
|
||
class Game: | ||
context: Context | ||
rule: Rule | ||
history: list | ||
|
||
def __init__(self, rule: Rule, n_players: int = 4): | ||
self.context = Context([Player()] * n_players, 0) | ||
self.history = [] | ||
|
||
self.rule = rule | ||
|
||
def start(self): | ||
while not self.rule.is_over(self.context): | ||
self.history.append(self.rule.step(self.context)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Request: | ||
hand: list[str] | ||
hand_sizes: list[int] | ||
scores: list[int] | ||
latest_moves: list[tuple[int, str]] | ||
order: str | ||
|
||
|
||
class Player: | ||
name: str | ||
|
||
def __init__(self, name: str): | ||
self.name = name | ||
|
||
def act(self, request: Request) -> str: | ||
""" | ||
Player should return a string of the card to play | ||
:param request: | ||
:return: | ||
""" | ||
raise NotImplementedError | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import random | ||
|
||
from .context import Context | ||
|
||
|
||
class Rule: | ||
def init_deck(self, ctx: Context): | ||
raise NotImplementedError | ||
|
||
def step(self, ctx: Context): | ||
raise NotImplementedError | ||
|
||
def is_over(self, ctx: Context): | ||
raise NotImplementedError | ||
|
||
|
||
class ExampleRule(Rule): | ||
cards: tuple[str] = ('red', 'blue', 'green', 'yellow', 'wild', 'wild_draw_four') | ||
|
||
def init_deck(self, ctx: Context): | ||
return [(color, number) for color in self.cards for number in range(10)] | ||
|
||
def step(self, ctx: Context): | ||
assert ctx.current_round is not None | ||
|
||
round_ = ctx.current_round | ||
if len(round_.deck) == 0: | ||
round_.deck.extend(round_.discard) | ||
random.shuffle(round_.deck) | ||
round_.discard.clear() | ||
return round_.deck.pop() | ||
|
||
def is_over(self, scoreboard): | ||
return any(score >= 500 for score in scoreboard) | ||
|
||
@staticmethod | ||
def is_round_over(ctx: Context): | ||
return any(len(hand) == 0 for hand in ctx.current_round.hands) |