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

Commit 71a2fec

Browse files
committed
Work with basic bots runner #2
1 parent 3f81fd8 commit 71a2fec

File tree

8 files changed

+95
-10
lines changed

8 files changed

+95
-10
lines changed

project/bots_battle.py

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# -*- coding: utf-8 -*-
22
import logging
3+
from time import sleep
4+
5+
from terminaltables import AsciiTable
6+
from tqdm import trange
37

48
from game.game_manager import GameManager
59
from mahjong.client import Client
610

11+
TOTAL_HANCHANS = 3
712

813
def main():
914
# enable it for manual testing
@@ -13,14 +18,71 @@ def main():
1318
clients = [Client() for _ in range(0, 4)]
1419
manager = GameManager(clients)
1520

16-
for x in range(1, 3):
17-
print('Hanchan #{0}'.format(x))
18-
manager.play_game()
19-
players = manager.players_sorted_by_scores()
20-
for player in players:
21-
print(player)
21+
total_results = {}
22+
x = 1
23+
for client in clients:
24+
total_results[client.id] = {
25+
'name': client.player.name,
26+
'version': client.player.ai.version,
27+
'positions': [1, x],
28+
'played_rounds': 0
29+
}
30+
x += 1
31+
32+
for x in trange(TOTAL_HANCHANS):
33+
# yes, I know about tqdm.write
34+
# but it didn't work properly for our case
35+
print('\n')
36+
print('Hanchan #{0}'.format(x + 1))
37+
38+
result = manager.play_game()
39+
sleep(2)
40+
41+
table_data = [
42+
['Position', 'Player', 'AI', 'Scores'],
43+
]
44+
45+
clients = sorted(clients, key=lambda i: i.player.scores, reverse=True)
46+
for client in clients:
47+
player = client.player
48+
table_data.append([player.position,
49+
player.name,
50+
'v{0}'.format(player.ai.version),
51+
int(player.scores)])
52+
53+
total_result_client = total_results[client.id]
54+
total_result_client['positions'].append(player.position)
55+
total_result_client['played_rounds'] = result['played_rounds']
56+
57+
table = AsciiTable(table_data)
58+
print(table.table)
2259
print('')
2360

61+
print('\n')
62+
63+
table_data = [
64+
['Player', 'AI', 'Played rounds', 'Average place'],
65+
]
66+
67+
# recalculate stat values
68+
for item in total_results.values():
69+
played_rounds = item['played_rounds']
70+
item['average_place'] = sum(item['positions']) / len(item['positions'])
71+
72+
calculated_clients = sorted(total_results.values(), key=lambda i: i['average_place'])
73+
74+
for item in calculated_clients:
75+
table_data.append([
76+
item['name'],
77+
item['version'],
78+
item['played_rounds'],
79+
item['average_place']
80+
])
81+
82+
print('Final results:')
83+
table = AsciiTable(table_data)
84+
print(table.table)
85+
2486

2587
if __name__ == '__main__':
2688
main()

project/game/game_manager.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,22 @@ def play_game(self):
166166
is_game_end = False
167167
self.init_game()
168168

169+
played_rounds = 0
170+
169171
while not is_game_end:
170172
self.init_round()
171173
result = self.play_round()
172174
is_game_end = result['is_game_end']
175+
played_rounds += 1
176+
177+
for client in self.clients:
178+
client.table.recalculate_players_position()
173179

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

183+
return {'played_rounds': played_rounds}
184+
177185
def draw_tile(self, client):
178186
tile = self._cut_tiles(1)[0]
179187
client.draw_tile(tile)

project/mahjong/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# -*- coding: utf-8 -*-
22
from mahjong.stat import Statistics
33
from mahjong.table import Table
4+
from utils.general import make_random_letters_and_digit_string
45

56

67
class Client(object):
78
statistics = None
9+
id = ''
810

911
def __init__(self):
1012
self.table = Table()
1113
self.statistics = Statistics()
1214
self.player = self.table.get_main_player()
15+
self.id = make_random_letters_and_digit_string()
1316

1417
def authenticate(self):
1518
pass

project/mahjong/player.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, seat, table):
3434
self.ai = self.ai_class(self)
3535

3636
def __str__(self):
37-
result = u'{0} |v{1}|'.format(self.name, self.ai.version)
37+
result = u'{0}'.format(self.name)
3838
if self.scores:
3939
result += u' ({:,d})'.format(int(self.scores))
4040
if self.uma:

project/mahjong/table.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ def set_players_scores(self, scores, uma=None):
6262
if uma:
6363
self.get_player(i).uma = uma[i]
6464

65-
# recalculate player's positions
65+
self.recalculate_players_position()
66+
67+
def recalculate_players_position(self):
6668
temp_players = self.get_players_sorted_by_scores()
6769
for i in range(0, len(temp_players)):
6870
temp_player = temp_players[i]

project/requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
beautifulsoup4==4.4.1
2-
requests==2.10.0
2+
requests==2.10.0
3+
terminaltables==3.0.0
4+
tqdm==4.7.4

project/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
AI_VERSION = '0.0.3'
3+
AI_VERSION = '0.0.4'
44

55
TENHOU_HOST = '133.242.10.78'
66
TENHOU_PORT = 10080

project/utils/general.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# -*- coding: utf-8 -*-
2+
import string
3+
import random
4+
5+
6+
def make_random_letters_and_digit_string(length=15):
7+
random_chars = string.ascii_lowercase + string.digits
8+
return ''.join(random.choice(random_chars) for _ in range(length))

0 commit comments

Comments
 (0)