Skip to content

Commit

Permalink
Gin Rummy: replaced assert with raise exception for Codacy compliance.
Browse files Browse the repository at this point in the history
Former-commit-id: 6a0fff7
  • Loading branch information
billh0420 committed Apr 29, 2020
1 parent 450a69b commit 8b90222
Show file tree
Hide file tree
Showing 18 changed files with 176 additions and 88 deletions.
13 changes: 9 additions & 4 deletions rlcard/agents/gin_rummy_human_agent/gin_rummy_human_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import time

from rlcard.games.gin_rummy.utils.action_event import ActionEvent
from rlcard.games.gin_rummy.utils.gin_rummy_error import GinRummyProgramError


class HumanAgent(object):
Expand Down Expand Up @@ -35,14 +36,18 @@ def step(self, state):
Returns:
action (int): The action decided by human
'''
assert not self.is_choosing_action_id
assert self.state is None
assert self.chosen_action_id is None
if self.is_choosing_action_id:
raise GinRummyProgramError("self.is_choosing_action_id must be False.")
if self.state is not None:
raise GinRummyProgramError("self.state must be None.")
if self.chosen_action_id is not None:
raise GinRummyProgramError("self.chosen_action_id={} must be None.".format(self.chosen_action_id))
self.state = state
self.is_choosing_action_id = True
while not self.chosen_action_id:
time.sleep(0.001)
assert self.chosen_action_id is not None
if self.chosen_action_id is None:
raise GinRummyProgramError("self.chosen_action_id cannot be None.")
chosen_action_event = ActionEvent.decode_action(action_id=self.chosen_action_id)
self.state = None
self.is_choosing_action_id = False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import rlcard.games.gin_rummy.utils.utils as gin_rummy_utils

from rlcard.games.gin_rummy.utils.action_event import KnockAction, GinAction
from rlcard.games.gin_rummy.utils.gin_rummy_error import GinRummyProgramError


class GameCanvas(tk.Canvas):
Expand Down Expand Up @@ -286,7 +287,8 @@ def on_dead_hand(self):
self.after_idle(self.game_canvas_updater.did_perform_actions, [DECLARE_DEAD_HAND_ACTION_ID])

def on_going_out(self):
assert not self.query.is_game_over()
if self.query.is_game_over():
raise GinRummyProgramError("Cannot go out if game is over.")
current_player_id = self.current_player_id
selected_held_pile_item_ids = self.getter.get_selected_held_pile_item_ids(player_id=current_player_id)
legal_actions = self.getter.get_legal_actions(player_id=current_player_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing import List

from rlcard.games.gin_rummy.utils.action_event import *
from rlcard.games.gin_rummy.utils.gin_rummy_error import GinRummyProgramError

import rlcard.games.gin_rummy.judge as judge
import rlcard.games.gin_rummy.utils.utils as gin_rummy_utils
Expand All @@ -28,7 +29,6 @@
from rlcard.games.gin_rummy.utils.settings import Settings

from . import configurations
from . import gin_rummy_error


class GameCanvasGetter(object):
Expand Down Expand Up @@ -60,7 +60,8 @@ def get_current_player_id(self) -> int or None:
mark = game_canvas.game_canvas_updater.mark
moves = game.round.move_sheet[:mark]
if not moves:
assert mark == 0
if not mark == 0:
raise GinRummyProgramError("mark={} must be 0.".format(mark))
if game.round.move_sheet:
first_move = game.round.move_sheet[0]
if isinstance(first_move, DealHandMove):
Expand Down Expand Up @@ -94,7 +95,7 @@ def get_current_player_id(self) -> int or None:
elif isinstance(last_move, ScoreSouthMove):
pass
else:
raise gin_rummy_error.ProgramError('get_current_player_id: unknown last_move={}'.format(last_move))
raise GinRummyProgramError('get_current_player_id: unknown last_move={}'.format(last_move))
return result

def get_legal_actions(self, player_id: int) -> List[ActionEvent]:
Expand Down Expand Up @@ -158,7 +159,7 @@ def get_legal_actions(self, player_id: int) -> List[ActionEvent]:
elif isinstance(last_move, ScoreSouthMove):
pass
else:
raise gin_rummy_error.ProgramError('get_legal_actions: unknown last_move={}'.format(last_move))
raise GinRummyProgramError('get_legal_actions: unknown last_move={}'.format(last_move))
return legal_actions

def get_tags(self, item_id) -> List[str]:
Expand All @@ -169,7 +170,7 @@ def get_card_id(self, card_item_id: int) -> int:
if card_item_id in game_canvas.card_item_ids:
card_id = game_canvas.card_item_ids.index(card_item_id)
else:
assert False
raise GinRummyProgramError("card_item_id={} not found in card_item_ids.".format(card_item_id))
return card_id

def get_top_discard_pile_item_id(self) -> int or None:
Expand All @@ -188,7 +189,8 @@ def get_stock_pile_item_ids(self) -> List[int]:
return stock_pile_item_ids

def get_held_pile_item_ids(self, player_id: int) -> List[int]:
assert player_id is not None
if player_id is None:
raise GinRummyProgramError("player_id must not be None.")
game_canvas = self.game_canvas
ghost_card_item = game_canvas.held_pile_ghost_card_items[player_id]
held_pile_tag = game_canvas.held_pile_tags[player_id]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
from rlcard.games.gin_rummy.utils.move import DrawCardMove, PickupDiscardMove, DeclareDeadHandMove
from rlcard.games.gin_rummy.utils.move import DiscardMove, KnockMove, GinMove
from rlcard.games.gin_rummy.utils.move import ScoreNorthMove, ScoreSouthMove
from rlcard.games.gin_rummy.utils.gin_rummy_error import GinRummyProgramError

from . import configurations
from . import gin_rummy_error
from . import handling_tap_discard_pile
from . import handling_tap_held_pile
from . import starting_new_game
Expand Down Expand Up @@ -95,18 +95,21 @@ def _advance_mark(self):
move = self.moves[self.mark]
thinking_time_in_ms = 0 # type: int
if isinstance(move, DealHandMove):
assert self.mark == 0
if not self.mark == 0:
raise GinRummyProgramError("mark={} must be 0.".format(self.mark))
self.busy_body_id = move.player_dealing.player_id
elif isinstance(move, ScoreNorthMove) or isinstance(move, ScoreSouthMove):
self.busy_body_id = move.player.player_id
elif isinstance(move, PlayerMove):
self.busy_body_id = move.player.player_id
thinking_time_in_ms = 1000 # type: int # simulate the computer thinking
else:
raise gin_rummy_error.ProgramError("GameCanvasUpdater advance_mark: unknown move={move}")
raise GinRummyProgramError("GameCanvasUpdater advance_mark: unknown move={move}")
if self.mark > 0:
assert not self.game_canvas.player_types[self.busy_body_id] == PlayerType.human_player
assert self.busy_body_id == self.game_canvas.getter.get_current_player_id()
if self.game_canvas.player_types[self.busy_body_id] == PlayerType.human_player:
raise GinRummyProgramError("busy_body_id={} must not be human player.".format(self.busy_body_id))
if not self.busy_body_id == self.game_canvas.getter.get_current_player_id():
raise GinRummyProgramError("busy_body_id={} must equal current_player_id={}".format(self.busy_body_id, self.game_canvas.getter.get_current_player_id()))
self._show_prolog_messages_on_computer_turn()
self.game_canvas.after(thinking_time_in_ms, self._advance_mark_for_computer_player)
return
Expand All @@ -117,22 +120,26 @@ def _advance_mark(self):
print("S {}".format(action_event)) # FIXME: South may not always be actor
self.human_agent.chosen_action_id = action_id
return
assert self.mark >= len(self.moves) # FIXME: should be no pending computer moves
if not self.mark >= len(self.moves): # FIXME: should be no pending computer moves
raise GinRummyProgramError("Should be no pending computer moves.")
waiting_player_id = self.env_thread.get_waiting_player_id()
if waiting_player_id is None:
return
# FIXME: should be no pending computer moves
if self.human_agent.chosen_action_id is not None:
assert self.human_agent.chosen_action_id is None
raise GinRummyProgramError("self.human_agent.chosen_action_id must not be None.")
if self.busy_body_id is not None:
assert self.busy_body_id is None
assert waiting_player_id == self.game_canvas.getter.get_current_player_id()
raise GinRummyProgramError("busy_body_id={} must be None.".format(self.busy_body_id))
if not waiting_player_id == self.game_canvas.getter.get_current_player_id():
raise GinRummyProgramError("waiting_player_id={} must be current_player_id.".format(waiting_player_id))
self.busy_body_id = waiting_player_id
assert self.game_canvas.player_types[self.busy_body_id] == PlayerType.human_player
if not self.game_canvas.player_types[self.busy_body_id] == PlayerType.human_player:
raise GinRummyProgramError("busy_body_id={} must be human player.".format(self.busy_body_id))
legal_actions = self.human_agent.state['legal_actions']
if self.game_canvas.query.is_scoring(legal_actions=legal_actions):
# 'boss' performs this, not human
assert len(legal_actions) == 1
if not len(legal_actions) == 1:
raise GinRummyProgramError("len(legal_actions)={} must be 1.".format(len(legal_actions)))
action_id = legal_actions[0]
self._perform_score_action_id(action_id=action_id)
return
Expand All @@ -154,9 +161,11 @@ def _perform_deal_hand_move(self, move: DealHandMove):
def _perform_score_action_id(self, action_id: int):
if utils.is_debug():
if self.busy_body_id == 0:
assert action_id == configurations.SCORE_PLAYER_0_ACTION_ID
if not action_id == configurations.SCORE_PLAYER_0_ACTION_ID:
raise GinRummyProgramError("action_id={} must be SCORE_PLAYER_0_ACTION_ID".format(action_id))
else:
assert action_id == configurations.SCORE_PLAYER_1_ACTION_ID
if not action_id == configurations.SCORE_PLAYER_1_ACTION_ID:
raise GinRummyProgramError("action_id={} must be SCORE_PLAYER_1_ACTION_ID".format(action_id))
self.game_canvas.after_idle(self.did_perform_actions, [action_id])

#
Expand All @@ -180,7 +189,8 @@ def _show_prolog_messages_on_computer_turn(self):
game_canvas=self.game_canvas)

def _advance_mark_for_computer_player(self):
assert self.mark < len(self.moves)
if not self.mark < len(self.moves):
raise GinRummyProgramError("mark={} must be less than len(moves)={}.".format(self.mark, len(self.moves)))
move = self.moves[self.mark]
if isinstance(move, DealHandMove):
self._perform_deal_hand_move(move=move)
Expand Down Expand Up @@ -209,7 +219,8 @@ def _perform_draw_card_move(self, move: DrawCardMove):
card = move.card
source_item_id = self.game_canvas.getter.get_top_stock_pile_item_id()
source_card_item_id = self.game_canvas.getter.get_card_id(card_item_id=source_item_id)
assert source_card_item_id == gin_rummy_utils.get_card_id(card=card)
if not source_card_item_id == gin_rummy_utils.get_card_id(card=card):
raise GinRummyProgramError("source_card_item_id={} doesn't match with card={}.".format(source_card_item_id, card))
self.game_canvas.addtag_withtag(configurations.DRAWN_TAG, source_item_id)
target_item_id = self.game_canvas.getter.get_held_pile_item_ids(player_id=player_id)[-1]
target_item = self.game_canvas.canvas_item_by_item_id[target_item_id]
Expand All @@ -223,7 +234,8 @@ def _perform_pick_up_discard_move(self, move: PickupDiscardMove):
card = move.card
source_item_id = self.game_canvas.getter.get_top_discard_pile_item_id()
source_card_item_id = self.game_canvas.getter.get_card_id(card_item_id=source_item_id)
assert source_card_item_id == gin_rummy_utils.get_card_id(card=card)
if not source_card_item_id == gin_rummy_utils.get_card_id(card=card):
raise GinRummyProgramError("source_card_item_id={} doesn't match with card={}.".format(source_card_item_id, card))
self.game_canvas.addtag_withtag(configurations.DRAWN_TAG, source_item_id)
target_item_id = self.game_canvas.getter.get_held_pile_item_ids(player_id=player_id)[-1]
target_item = self.game_canvas.canvas_item_by_item_id[target_item_id]
Expand All @@ -239,7 +251,8 @@ def _perform_discard_move(self, move: DiscardMove):
if utils.is_debug():
print("{}".format(move))
action_id = move.action.action_id
assert self.busy_body_id is not None
if self.busy_body_id is None:
raise GinRummyProgramError("busy_body_id cannot be None.")
card_id = utils.get_action_card_id(action_id)
source_item_id = self.game_canvas.card_item_ids[card_id]
self.game_canvas.addtag_withtag(configurations.SELECTED_TAG, source_item_id)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
from .handling_tap_held_pile import handle_tap_held_pile
from .handling_tap_player_pane import handle_tap_player_pane

from rlcard.games.gin_rummy.utils.gin_rummy_error import GinRummyProgramError


def on_game_canvas_tap(event):
widget = event.widget
hit_item_ids = widget.find_withtag("current")
if hit_item_ids:
assert len(hit_item_ids) == 1
if not len(hit_item_ids) == 1:
raise GinRummyProgramError("len(hit_item_ids)={} must be 1.".format(len(hit_item_ids)))
hit_item_id = hit_item_ids[0]
hit_item = None
for canvas_item in widget.canvas_items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from . import starting_new_game
from . import utils

from rlcard.games.gin_rummy.utils.gin_rummy_error import GinRummyProgramError


def handle_tap_discard_pile(hit_item: CanvasItem, game_canvas: 'GameCanvas'):
player_id = game_canvas.current_player_id
Expand Down Expand Up @@ -58,9 +60,11 @@ def _handle_can_discard_card(hit_item: CanvasItem, game_canvas: 'GameCanvas'):
# The remaining held_pile_items are fanned.
top_discard_pile_item_id = game_canvas.getter.get_top_discard_pile_item_id()
if top_discard_pile_item_id:
assert hit_item == top_discard_pile_item_id
if not hit_item == top_discard_pile_item_id:
raise GinRummyProgramError("hit_item must be top card of discard_pile.")
else:
assert hit_item == game_canvas.discard_pile_box_item
if not hit_item == game_canvas.discard_pile_box_item:
raise GinRummyProgramError("hit_item must be discard_pile_box_item.")
current_player_id = game_canvas.current_player_id
selected_held_pile_item_ids = game_canvas.getter.get_selected_held_pile_item_ids(player_id=current_player_id)
if len(selected_held_pile_item_ids) == 1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
if TYPE_CHECKING:
from .game_canvas import GameCanvas

from rlcard.games.gin_rummy.utils.gin_rummy_error import GinRummyProgramError

from .player_type import PlayerType
from .canvas_item import CanvasItem

from . import configurations
from . import gin_rummy_error
from . import info_messaging
from . import utils

Expand All @@ -26,7 +27,7 @@ def handle_tap_held_pile(hit_item: CanvasItem, game_canvas: 'GameCanvas'):
elif game_canvas.held_pile_tags[1] in hit_item_tags:
player_id = 1
else:
raise gin_rummy_error.ProgramError("handle_tap_held_pile: unknown held_pile.")
raise GinRummyProgramError("handle_tap_held_pile: unknown held_pile.")
player_is_human = game_canvas.player_types[player_id] is PlayerType.human_player
can_draw_from_stock_pile = game_canvas.query.can_draw_from_stock_pile(player_id=player_id)
can_draw_from_discard_pile = game_canvas.query.can_draw_from_discard_pile(player_id=player_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from . import handling_tap
from . import utils

from rlcard.games.gin_rummy.utils.gin_rummy_error import GinRummyProgramError


def on_tap_to_arrange_held_pile(event):
widget = event.widget
Expand All @@ -25,7 +27,8 @@ def on_tap_to_arrange_held_pile(event):
hit_items_ids = widget.find_withtag("current")
if not hit_items_ids:
return
assert len(hit_items_ids) == 1
if not len(hit_items_ids) == 1:
raise GinRummyProgramError("len(hit_items_ids)={} must be 1.".format(len(hit_items_ids)))
hit_item_id = hit_items_ids[0]
hit_item = None
for canvas_item in widget.canvas_items:
Expand Down
Loading

0 comments on commit 8b90222

Please sign in to comment.