Skip to content

Commit

Permalink
structurize the core mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Dorapower committed Jan 26, 2024
1 parent 1143e2e commit bc24ad8
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .idea/Uno-Game.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ keywords = [
"game",
]
classifiers = [
"Development Status :: 1 - Planning",
"Development Status :: 2 - Pre-Alpha",
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
"License :: OSI Approved :: MIT License",
Expand Down
Empty file added src/uno/engine/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions src/uno/engine/context.py
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
19 changes: 19 additions & 0 deletions src/uno/engine/game.py
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))
26 changes: 26 additions & 0 deletions src/uno/engine/player.py
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

38 changes: 38 additions & 0 deletions src/uno/engine/rule.py
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)

0 comments on commit bc24ad8

Please # to comment.