From f99639e19cb8d955556bc6c96f504fbb638f16ea Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Thu, 23 Dec 2021 16:53:24 +0100 Subject: Use an enumeration for the game state Signed-off-by: Olivier Gayot --- swiftstory/game.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/swiftstory/game.py b/swiftstory/game.py index 8ed56fc..4a7f009 100644 --- a/swiftstory/game.py +++ b/swiftstory/game.py @@ -1,3 +1,4 @@ +from enum import Enum, auto import random from typing import Optional @@ -7,16 +8,18 @@ from swiftstory.board import Board from swiftstory.status import error, success -class Game: - WAITING_NEW_JUDGE = 0 - WAITING_COLLECTION = 1 - WAITING_DESIGNATION = 2 +class GameState(Enum): + WAITING_NEW_JUDGE = auto(), + WAITING_COLLECTION = auto(), + WAITING_DESIGNATION = auto(), + +class Game: def __init__(self, white_desc, black_desc): white_pick = list(enumerate(white_desc)) black_pick = list(enumerate(black_desc)) - self.state = self.WAITING_NEW_JUDGE + self.state = GameState.WAITING_NEW_JUDGE self.players = [] @@ -59,9 +62,9 @@ class Game: cards = [(idx, desc) for idx, (_, desc) in player.cards.items()] - if self.state is self.WAITING_NEW_JUDGE: + if self.state is GameState.WAITING_NEW_JUDGE: state = 'waiting_judge' - elif self.state is self.WAITING_COLLECTION: + elif self.state is GameState.WAITING_COLLECTION: state = 'waiting_collection' else: state = 'waiting_designation' @@ -70,14 +73,14 @@ class Game: def try_become_judge(self, player): - if self.state is not self.WAITING_NEW_JUDGE: + if self.state is not GameState.WAITING_NEW_JUDGE: # TODO what if the judge has quit ? raise WrongAction('Someone is judge already') self.judge = player self.board.reveal_next_black_card() - self.state = self.WAITING_COLLECTION + self.state = GameState.WAITING_COLLECTION for p in self.players: if p is not player: @@ -87,7 +90,7 @@ class Game: def try_play_card(self, player, card_id): - if self.state is not self.WAITING_COLLECTION: + if self.state is not GameState.WAITING_COLLECTION: raise WrongAction('Who asked you to play now ?!') if self.judge is player: @@ -112,7 +115,7 @@ class Game: def try_collect_cards(self, player): - if self.state is not self.WAITING_COLLECTION: + if self.state is not GameState.WAITING_COLLECTION: raise WrongAction("Do you think it's the moment for collection !?") if self.judge is not player: @@ -121,7 +124,7 @@ class Game: self.board.shuffle_played_cards() # we prevent the others to play - self.state = self.WAITING_DESIGNATION + self.state = GameState.WAITING_DESIGNATION for p in self.players: if p is not player: @@ -131,7 +134,7 @@ class Game: def try_designate_card(self, player, card_id): - if self.state is not self.WAITING_DESIGNATION: + if self.state is not GameState.WAITING_DESIGNATION: raise WrongAction('Not now, moron !') if self.judge is not player: @@ -175,7 +178,7 @@ class Game: if p is not player: p.register_notification({'op': 'judge_needed'}) - self.state = self.WAITING_NEW_JUDGE + self.state = GameState.WAITING_NEW_JUDGE return success(None) @@ -183,7 +186,7 @@ class Game: return success([(idx, desc) for idx, (_, desc) in player.cards.items()]) def try_view_played_cards(self, player): - if self.state is not self.WAITING_DESIGNATION: + if self.state is not GameState.WAITING_DESIGNATION: raise WrongAction('Not now, moron !') return success([desc for (_, desc), _ in self.board.played_cards]) @@ -218,4 +221,4 @@ class Game: p.has_played = False self.board.played_cards = [] - self.state = self.WAITING_NEW_JUDGE + self.state = GameState.WAITING_NEW_JUDGE -- cgit v1.2.3