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

Commit

Permalink
Work with basic bots runner #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Nihisil committed Jun 24, 2016
1 parent 3f81fd8 commit 71a2fec
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 10 deletions.
74 changes: 68 additions & 6 deletions project/bots_battle.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# -*- coding: utf-8 -*-
import logging
from time import sleep

from terminaltables import AsciiTable
from tqdm import trange

from game.game_manager import GameManager
from mahjong.client import Client

TOTAL_HANCHANS = 3

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

for x in range(1, 3):
print('Hanchan #{0}'.format(x))
manager.play_game()
players = manager.players_sorted_by_scores()
for player in players:
print(player)
total_results = {}
x = 1
for client in clients:
total_results[client.id] = {
'name': client.player.name,
'version': client.player.ai.version,
'positions': [1, x],
'played_rounds': 0
}
x += 1

for x in trange(TOTAL_HANCHANS):
# yes, I know about tqdm.write
# but it didn't work properly for our case
print('\n')
print('Hanchan #{0}'.format(x + 1))

result = manager.play_game()
sleep(2)

table_data = [
['Position', 'Player', 'AI', 'Scores'],
]

clients = sorted(clients, key=lambda i: i.player.scores, reverse=True)
for client in clients:
player = client.player
table_data.append([player.position,
player.name,
'v{0}'.format(player.ai.version),
int(player.scores)])

total_result_client = total_results[client.id]
total_result_client['positions'].append(player.position)
total_result_client['played_rounds'] = result['played_rounds']

table = AsciiTable(table_data)
print(table.table)
print('')

print('\n')

table_data = [
['Player', 'AI', 'Played rounds', 'Average place'],
]

# recalculate stat values
for item in total_results.values():
played_rounds = item['played_rounds']
item['average_place'] = sum(item['positions']) / len(item['positions'])

calculated_clients = sorted(total_results.values(), key=lambda i: i['average_place'])

for item in calculated_clients:
table_data.append([
item['name'],
item['version'],
item['played_rounds'],
item['average_place']
])

print('Final results:')
table = AsciiTable(table_data)
print(table.table)


if __name__ == '__main__':
main()
8 changes: 8 additions & 0 deletions project/game/game_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,22 @@ def play_game(self):
is_game_end = False
self.init_game()

played_rounds = 0

while not is_game_end:
self.init_round()
result = self.play_round()
is_game_end = result['is_game_end']
played_rounds += 1

for client in self.clients:
client.table.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 draw_tile(self, client):
tile = self._cut_tiles(1)[0]
client.draw_tile(tile)
Expand Down
3 changes: 3 additions & 0 deletions project/mahjong/client.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# -*- coding: utf-8 -*-
from mahjong.stat import Statistics
from mahjong.table import Table
from utils.general import make_random_letters_and_digit_string


class Client(object):
statistics = None
id = ''

def __init__(self):
self.table = Table()
self.statistics = Statistics()
self.player = self.table.get_main_player()
self.id = make_random_letters_and_digit_string()

def authenticate(self):
pass
Expand Down
2 changes: 1 addition & 1 deletion project/mahjong/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, seat, table):
self.ai = self.ai_class(self)

def __str__(self):
result = u'{0} |v{1}|'.format(self.name, self.ai.version)
result = u'{0}'.format(self.name)
if self.scores:
result += u' ({:,d})'.format(int(self.scores))
if self.uma:
Expand Down
4 changes: 3 additions & 1 deletion project/mahjong/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def set_players_scores(self, scores, uma=None):
if uma:
self.get_player(i).uma = uma[i]

# recalculate player's positions
self.recalculate_players_position()

def recalculate_players_position(self):
temp_players = self.get_players_sorted_by_scores()
for i in range(0, len(temp_players)):
temp_player = temp_players[i]
Expand Down
4 changes: 3 additions & 1 deletion project/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
beautifulsoup4==4.4.1
requests==2.10.0
requests==2.10.0
terminaltables==3.0.0
tqdm==4.7.4
2 changes: 1 addition & 1 deletion project/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

AI_VERSION = '0.0.3'
AI_VERSION = '0.0.4'

TENHOU_HOST = '133.242.10.78'
TENHOU_PORT = 10080
Expand Down
8 changes: 8 additions & 0 deletions project/utils/general.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
import string
import random


def make_random_letters_and_digit_string(length=15):
random_chars = string.ascii_lowercase + string.digits
return ''.join(random.choice(random_chars) for _ in range(length))

0 comments on commit 71a2fec

Please sign in to comment.