Last active
January 26, 2017 20:00
-
-
Save zestone/7947e6460aec1927f221a7130e377b77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import numpy as np | |
import math | |
from matplotlib import pyplot | |
from matplotlib.font_manager import FontProperties | |
def dice(): | |
d = math.ceil(random.random()*6) | |
if d == 0: | |
d = 1 | |
return d | |
class Player: | |
def __init__(self, name): | |
self.__name = name | |
self.__pos = -1 | |
self.__fin = False | |
self.__log = [] | |
def step(self, max_step): | |
if not self.is_finish(): | |
self.__pos += dice() | |
if self.__pos > max_step: | |
self.__pos = 2 * max_step - self.__pos | |
self.__log.append(self.__pos) | |
if self.__pos == max_step: | |
self.__fin = True | |
def is_finish(self): | |
return self.__fin | |
def get_name(self): | |
return self.__name | |
def get_log(self): | |
return self.__log | |
class Sugoroku: | |
def __init__(self, boxes): | |
self.__boxes = boxes | |
self.__unique_boxes = np.unique(boxes) | |
self.__n_boxes = len(boxes) | |
self.__players = [] | |
self.__fin = False | |
self.__turn = 0 | |
self.__fin_turn = None | |
def play(self): | |
self.__fin_turn = np.zeros(len(self.__players)) | |
while True: | |
self.step() | |
if self.is_finish(): | |
break | |
def join(self, player): | |
self.__players.append(player) | |
def step(self): | |
self.__turn += 1 | |
fin = True | |
for i, player in enumerate(self.__players): | |
player.step(max_step=self.__n_boxes) | |
if player.is_finish() and self.__fin_turn[i] == 0: | |
self.__fin_turn[i] = self.__turn | |
fin = fin and player.is_finish() | |
if fin: | |
self.__fin = True | |
def is_finish(self): | |
return self.__fin | |
def get_status(self): | |
idx = np.argsort(self.__fin_turn) | |
sorted_players = np.array(self.__players)[idx] | |
sorted_fin_turn = self.__fin_turn[idx] | |
max_turn = -1 | |
stack = 0 | |
rank = 0 | |
ranks = [] | |
player_names = [] | |
turns = [] | |
for player, turn in zip(sorted_players, sorted_fin_turn): | |
if max_turn < turn: | |
max_turn = turn | |
rank = rank + stack + 1 | |
stack = 0 | |
else: | |
stack += 1 | |
ranks.append(rank) | |
player_names.append(player.get_name()) | |
turns.append(turn) | |
return zip(player_names, ranks, turns) | |
def show_result(self): | |
for name, rank, turn in self.get_status(): | |
print('%s: %2d位(%2dターン)'% (name.ljust(12), rank, turn)) | |
if __name__ == '__main__': | |
fp = FontProperties(fname=r'C:\Windows\Fonts\meiryo.ttc', size=10) | |
foods = ['卵', '鱈', '豆腐', '牡蠣', '鯖缶', '干し芋', 'はんぺん', 'もやし', 'ネギ', 'いちご', | |
'ぬか漬け', 'たくあん', 'えのき', 'ニラ', '牛肉', '豚', '大根おろし', 'しらたき', 'ちくわ部', | |
'しいたけ', '麸', '餅巾着', 'テリヤキバーガー', '豚', 'ちくわ', '人参', 'キムチ', '臓物', '鳥', 'ナス', | |
'うどん', 'みかん缶詰', 'カニ', 'エビ', '焼きSPAM', 'つみれ', 'ナタデココ', '春菊', '白菜', 'パイの実'] | |
unique_foods = np.unique(foods) | |
hist = np.zeros(len(unique_foods)) | |
max_entry = 8 | |
player_names = np.array(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'])[:max_entry] | |
max_iter = 1000 | |
for iter in range(max_iter): | |
# create a board | |
game = Sugoroku(foods) | |
players = [] | |
# create characters | |
for player_name in player_names: | |
player = Player(player_name) | |
players.append(player) | |
game.join(player) | |
# game process | |
game.play() | |
# get winner name | |
winner_name = list(game.get_status())[0][0] | |
# get winner log | |
winner_log = np.array(players)[np.array(player_names) == winner_name][0].get_log()[:-1] | |
winner_log = np.unique(winner_log) | |
# create histogram | |
for square in winner_log: | |
idx = [i for i,x in enumerate(unique_foods) if x == foods[square]] | |
hist[idx] += 1 | |
idx = np.argsort(hist) | |
hist = np.array(hist)[idx] | |
unique_food = np.array(unique_foods)[idx] | |
pyplot.bar(left=range(len(unique_food)), height=hist, tick_label=unique_food) | |
pyplot.xticks(range(len(unique_food)), unique_food, fontproperties=fp, rotation=90) | |
pyplot.title( | |
'闇鍋すごろくを1000回行った場合の食材の獲得頻度('+str(max_entry)+'人)\n闇鍋1回の平均食材数は'+str(sum(hist)/max_iter)+'個', | |
fontproperties=fp) | |
pyplot.xlabel('食材', fontproperties=fp) | |
pyplot.ylabel('獲得頻度', fontproperties=fp) | |
pyplot.tight_layout() | |
pyplot.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment