Skip to content

Commit

Permalink
Gin Rummy: simple changes for Codacy compliance.
Browse files Browse the repository at this point in the history
Former-commit-id: 1a08535
  • Loading branch information
billh0420 committed Apr 29, 2020
1 parent 65f1f00 commit 450a69b
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
The Gin Rummy app is a major refactoring of the code for a chess program by Bhaskar Chaudhary.
The chess program is explained in the video course "Tkinter GUI Application Development Projects".
You can do a google search on "Bhaskar Chaudhary tkinter packt" for more information.
The code for the chess program is available at https://github.com/PacktPublishing/Tkinter-GUI-Application-Development-Projects.
The code for the chess program is available at <https://github.com/PacktPublishing/Tkinter-GUI-Application-Development-Projects>.

## GameFrame
The GameFrame class uses the following parameters:

* window_title: str
* base_height: int
* base_width: int
* scale_factor_multiplier: float
* window_title: str
* base_height: int
* base_width: int
* scale_factor_multiplier: float

The base_height and base_width only determine the aspect ratio for the frame size.
The actual size of the window is calculated from these values and the 'window size factor' preference.
Expand All @@ -25,8 +25,8 @@ The program uses a value of 1.5 for the scale_factor_multiplier.
The window is centered in the screen.
Its origin is determined by the following;

* window_x: int
* window_y: int
* window_x: int
* window_y: int

A new game is started when the GameFrame is initialized.

Expand All @@ -35,12 +35,12 @@ The GameCanvas class specifies and layouts the widgets and canvas items for the

The GameFrame provides the initializer of the GameCanvas with the following value:

* scale_factor: float
* scale_factor: float

The GameCanvas needs to scale the layout values of its widgets by multiplying by this value.
For example, for a margin of 10, it would use 10 * scale_factor.
For example, for a margin of 10, it would use 10 times scale_factor.
It also calculate the scaled card size and uses the width and height of a card to do layout.
For example, it specifies that the discard pile x origin to be card_width * 1.5
For example, it specifies that the discard pile x origin to be card_width times 1.5
away from the stock pile x origin.
It does not need to worry about the scale_factor in this case since it is built into the card_width.

Expand Down Expand Up @@ -296,12 +296,12 @@ A new gin_rummy_env is created.
A new env_thread is created and started.

## To Do
* Implement drag-and-drop for cards.
* Increase white margin space for face of cards.
* Implement reverse fan of a card pile.
* Implement highlighting to select cards rather than jogging them.
* Provide preference to show/hide discard_pile_box.
* Find a cross platform method to play a beep sound.
* Add alerts for warning errors.
* Add alerts for program logic errors.
* Provide "Help" document in "Help" menu.
* Implement drag-and-drop for cards.
* Increase white margin space for face of cards.
* Implement reverse fan of a card pile.
* Implement highlighting to select cards rather than jogging them.
* Provide preference to show/hide discard_pile_box.
* Find a cross platform method to play a beep sound.
* Add alerts for warning errors.
* Add alerts for program logic errors.
* Provide "Help" document in "Help" menu.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ def loop_completion():
# show meld piles for both players
self._show_meld_piles()
# submit action to game_canvas_updater
card_id = game_canvas.card_item_ids.index(selected_held_pile_item_id)
action = GIN_ACTION_ID
game_canvas.after_idle(game_canvas.game_canvas_updater.did_perform_actions, [action])

Expand Down Expand Up @@ -229,8 +228,7 @@ def put_down_meld_cluster(self, meld_cluster, player_id: int):
y_tab = int(card_height * 0.15)
anchor_x = int(card_width * 0.5) # type: int
anchor_y = int(game_canvas.coords(player_pane.item_id)[1]) + y_tab # type: int
for index in range(len(meld_cluster)):
meld_pile = meld_cluster[index]
for meld_pile in meld_cluster:
self.put_down_meld_pile(meld_pile, anchor=(anchor_x, anchor_y), player_id=player_id)
utils.fan_held_pile(player_id=player_id, game_canvas=game_canvas)
anchor_x += len(meld_pile) * game_canvas.held_pile_tab
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from .canvas_item import CanvasItem

from . import configurations
from . import gin_rummy_error
from . import info_messaging
from . import starting_new_game
from . import utils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .game_canvas import GameCanvas

from .player_type import PlayerType
from .canvas_item import CanvasItem, CardItem
from .canvas_item import CanvasItem

from . import configurations
from . import gin_rummy_error
Expand Down
21 changes: 11 additions & 10 deletions rlcard/games/gin_rummy/utils/action_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@

from . import utils as utils

''' Action_ids:
0 -> score_player_0_id
1 -> score_player_1_id
2 -> draw_card_id
3 -> pick_up_discard_id
4 -> declare_dead_hand_id
5 -> gin_id
6 to 57 -> discard_id card_id
58 to 109 -> knock_id card_id
'''
# ====================================
# Action_ids:
# 0 -> score_player_0_id
# 1 -> score_player_1_id
# 2 -> draw_card_id
# 3 -> pick_up_discard_id
# 4 -> declare_dead_hand_id
# 5 -> gin_id
# 6 to 57 -> discard_id card_id
# 58 to 109 -> knock_id card_id
# ====================================

score_player_0_action_id = 0
score_player_1_action_id = 1
Expand Down
22 changes: 11 additions & 11 deletions rlcard/games/gin_rummy/utils/melding.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

from rlcard.games.gin_rummy.utils import utils

"""
Terminology:
run_meld - three or more cards of same suit in sequence
set_meld - three or more cards of same rank
meld_pile - a run_meld or a set_meld
meld_piles - a list of meld_pile
meld_cluster - same as meld_piles, but usually with the piles being mutually disjoint
meld_clusters - a list of meld_cluster
"""
# ===============================================================
# Terminology:
# run_meld - three or more cards of same suit in sequence
# set_meld - three or more cards of same rank
# meld_pile - a run_meld or a set_meld
# meld_piles - a list of meld_pile
# meld_cluster - same as meld_piles, but usually with the piles being mutually disjoint
# meld_clusters - a list of meld_cluster
# ===============================================================


def get_meld_clusters(hand: List[Card]) -> List[List[List[Card]]]:
Expand Down Expand Up @@ -66,7 +66,7 @@ def get_best_meld_clusters(hand: List[Card]) -> List[List[List[Card]]]:

def get_all_run_melds(hand: List[Card]) -> List[List[Card]]:
card_count = len(hand)
hand_by_suit = sorted(hand, key=lambda card: utils.get_card_id(card))
hand_by_suit = sorted(hand, key=utils.get_card_id)
max_run_melds = []

i = 0
Expand Down Expand Up @@ -121,7 +121,7 @@ def get_all_set_melds(hand: List[Card]) -> List[List[Card]]:
def get_all_run_melds_for_suit(cards: List[Card], suit: str) -> List[List[Card]]:
cards_for_suit = [card for card in cards if card.suit == suit]
cards_for_suit_count = len(cards_for_suit)
cards_for_suit = sorted(cards_for_suit, key=lambda card: utils.get_card_id(card))
cards_for_suit = sorted(cards_for_suit, key=utils.get_card_id)
max_run_melds = []

i = 0
Expand Down
25 changes: 12 additions & 13 deletions tests/games/test_gin_rummy_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ def test_step(self):
opponent_player = (current_player + 1) % 2
action = np.random.choice(game.judge.get_legal_actions())
self.assertIn(action.action_id, put_action_ids) # should be a put action
next_state, next_player = game.step(action)
_, next_player = game.step(action)
if not game.is_over():
self.assertEqual(next_player, opponent_player)
if not game.is_over():
action = np.random.choice(game.judge.get_legal_actions())
self.assertIn(action.action_id, get_action_ids) # should be a get action
next_state, next_player = game.step(action)
_, next_player = game.step(action)
self.assertEqual(next_player, opponent_player) # keep turn to put card

def test_proceed_game(self):
Expand All @@ -76,12 +76,12 @@ def test_proceed_game(self):
while not game.is_over():
legal_actions = game.judge.get_legal_actions()
action = np.random.choice(legal_actions)
state, _ = game.step(action)
_, _ = game.step(action)
self.assertEqual(game.actions[-1].action_id, score_player_1_action_id)

def test_get_state(self):
game = Game()
state, current_player = game.init_game()
state, _ = game.init_game()
self.assertEqual(len(state), 6)

def test_gin_rummy_dealer(self):
Expand Down Expand Up @@ -143,11 +143,10 @@ def test_melding_2(self): # 2020-Apr-19
alpha, beta = judge._get_going_out_cards(meld_clusters=meld_clusters,
hand=hand,
going_out_deadwood_count=going_out_deadwood_count)
assert set(alpha) == set(knock_cards)
assert beta == []

assert knock_cards == [utils.card_from_text('8D')]
assert gin_cards == []
self.assertEqual(set(alpha), set(knock_cards))
self.assertEqual(beta, [])
self.assertEqual(knock_cards, [utils.card_from_text('8D')])
self.assertEqual(gin_cards, [])

def test_melding_3(self): # 2020-Apr-19
hand_text = ['7H', '6H', '5H', '4S', '4H', '3H', '2S', 'AS', 'AH', 'AD', 'AC']
Expand All @@ -162,12 +161,12 @@ def test_melding_3(self): # 2020-Apr-19
alpha, beta = judge._get_going_out_cards(meld_clusters=meld_clusters,
hand=hand,
going_out_deadwood_count=going_out_deadwood_count)
assert set(alpha) == set(knock_cards)
assert beta == []
self.assertEqual(set(alpha), set(knock_cards))
self.assertEqual(beta, [])

correct_knock_cards = [utils.card_from_text(x) for x in ['7H', '4S', '4H', '3H', '2S', 'AS', 'AH', 'AD', 'AC']]
assert set(knock_cards) == set(correct_knock_cards)
assert gin_cards == []
self.assertEqual(set(knock_cards), set(correct_knock_cards))
self.assertEqual(gin_cards, [])

def test_corrected_settings(self):
default_setting = Setting.default_setting()
Expand Down

0 comments on commit 450a69b

Please sign in to comment.