Skip to content
This repository was archived by the owner on Jul 8, 2023. It is now read-only.

Commit de7159e

Browse files
committed
Issue #38. Sort players by scores and by their first seat
1 parent e0cb25f commit de7159e

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

project/game/player.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ class PlayerInterface(object):
1616
discards = None
1717
melds = None
1818
in_riichi = None
19+
20+
# current player seat
1921
seat = 0
2022
# where is sitting dealer, based on this information we can calculate player wind
2123
dealer_seat = 0
24+
# it is important to know initial seat for correct players sorting
25+
first_seat = 0
26+
2227
# position based on scores
2328
position = 0
2429
scores = None

project/game/table.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ def init_round(self, round_number, count_of_honba_sticks, count_of_riichi_sticks
6363
# 13 - tiles in each player hand
6464
self.count_of_remaining_tiles = 136 - 14 - self.count_of_players * 13
6565

66+
if round_number == 0 and count_of_honba_sticks == 0:
67+
i = 0
68+
seats = [0, 1, 2, 3]
69+
for player in self.players:
70+
player.first_seat = seats[i - dealer_seat]
71+
i += 1
72+
6673
def add_called_meld(self, player_seat, meld):
6774
# when opponent called meld it is means
6875
# that he discards tile from hand, not from wall
@@ -140,7 +147,7 @@ def get_player(self, player_seat):
140147
return self.players[player_seat]
141148

142149
def get_players_sorted_by_scores(self):
143-
return sorted(self.players, key=lambda x: x.scores or 0, reverse=True)
150+
return sorted(self.players, key=lambda x: (x.scores or 0, -x.first_seat), reverse=True)
144151

145152
@property
146153
def round_wind(self):

project/game/tests/tests_table.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ def test_set_scores_and_recalculate_player_position(self):
7979
table = Table()
8080
table.init_round(0, 0, 0, 0, 0, [])
8181

82+
self.assertEqual(table.get_player(0).first_seat, 0)
83+
self.assertEqual(table.get_player(1).first_seat, 1)
84+
self.assertEqual(table.get_player(2).first_seat, 2)
85+
self.assertEqual(table.get_player(3).first_seat, 3)
86+
8287
scores = [230, 110, 55, 405]
8388
table.set_players_scores(scores)
8489

0 commit comments

Comments
 (0)