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

Commit

Permalink
Fix a bug with wrong position calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nihisil committed Jun 25, 2016
1 parent 5f1b4cd commit e9acd8f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
5 changes: 3 additions & 2 deletions project/bots_battle.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from game.game_manager import GameManager
from mahjong.client import Client

TOTAL_HANCHANS = 3
TOTAL_HANCHANS = 10

def main():
# enable it for manual testing
Expand Down Expand Up @@ -50,7 +50,8 @@ def main():
table_data.append([player.position,
player.name,
'v{0}'.format(player.ai.version),
int(player.scores)])
'{0:,d}'.format(int(player.scores))
])

total_result_client = total_results[client.id]
total_result_client['positions'].append(player.position)
Expand Down
16 changes: 14 additions & 2 deletions project/game/game_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,26 @@ def play_game(self, total_results):

played_rounds += 1

for client in self.clients:
client.table.recalculate_players_position()
self.recalculate_players_position()

logger.info('Final Scores: {0}'.format(self.players_sorted_by_scores()))
logger.info('The end of the game')

return {'played_rounds': played_rounds}

def recalculate_players_position(self):
"""
For players with same count of scores we need
to set position based on their initial seat on the table
"""
temp_clients = sorted(self.clients, key=lambda x: x.player.scores, reverse=True)
for i in range(0, len(temp_clients)):
temp_client = temp_clients[i]

for client in self.clients:
if client.id == temp_client.id:
client.player.position = i + 1

def can_call_ron(self, client, win_tile):
if not client.player.in_tempai or not client.player.in_riichi:
return False
Expand Down
25 changes: 23 additions & 2 deletions project/game/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,43 @@ def test_init_dealer(self):
self.assertFalse(manager.clients[1].player.is_dealer)
self.assertFalse(manager.clients[2].player.is_dealer)

def test_init_scores(self):
def test_init_scores_and_recalculate_position(self):
clients = [Client() for _ in range(0, 4)]
manager = GameManager(clients)
manager.init_game()
manager.set_dealer(3)

clients[0].player.scores = 24000
clients[1].player.scores = 23000
clients[2].player.scores = 22000
clients[3].player.scores = 21000

manager.init_round()
manager.recalculate_players_position()

self.assertEqual(clients[0].player.scores, 24000)
self.assertEqual(clients[0].player.position, 1)
self.assertEqual(clients[1].player.scores, 23000)
self.assertEqual(clients[1].player.position, 2)
self.assertEqual(clients[2].player.scores, 22000)
self.assertEqual(clients[2].player.position, 3)
self.assertEqual(clients[3].player.scores, 21000)
self.assertEqual(clients[3].player.position, 4)

clients[0].player.scores = 24000
clients[1].player.scores = 24000
clients[2].player.scores = 22000
clients[3].player.scores = 22000

manager.recalculate_players_position()

self.assertEqual(clients[0].player.scores, 24000)
self.assertEqual(clients[0].player.position, 1)
self.assertEqual(clients[1].player.scores, 24000)
self.assertEqual(clients[1].player.position, 2)
self.assertEqual(clients[2].player.scores, 22000)
self.assertEqual(clients[2].player.position, 3)
self.assertEqual(clients[3].player.scores, 22000)
self.assertEqual(clients[3].player.position, 4)

def test_call_riichi(self):
game.game_manager.shuffle_seed = lambda : 0.33
Expand Down
10 changes: 9 additions & 1 deletion project/mahjong/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,23 @@ def test_set_scores_and_uma(self):
def test_set_scores_and_recalculate_player_position(self):
table = Table()
table.init_round(0, 0, 0, 0, 0, [])
scores = [230, 110, 55, 405]

scores = [230, 110, 55, 405]
table.set_players_scores(scores)

self.assertEqual(table.get_player(0).position, 2)
self.assertEqual(table.get_player(1).position, 3)
self.assertEqual(table.get_player(2).position, 4)
self.assertEqual(table.get_player(3).position, 1)

scores = [110, 110, 405, 405]
table.set_players_scores(scores)

self.assertEqual(table.get_player(0).position, 3)
self.assertEqual(table.get_player(1).position, 4)
self.assertEqual(table.get_player(2).position, 1)
self.assertEqual(table.get_player(3).position, 2)

def test_set_names_and_ranks(self):
table = Table()
table.init_round(0, 0, 0, 0, 0, [])
Expand Down

0 comments on commit e9acd8f

Please sign in to comment.