def run_game(world): new_turn = True while not world.quit: for c in world.companies: if (((not new_turn) and (not c.turn_finished)) or new_turn): c.get_command() a = c.director.last_command.split(' ') cmd = a[0] if len(a) == 2: value = a[1] else: value = 1 if cmd in commands.commands_list.keys(): commands.commands_list[cmd](world, c, int(value)) else: strings.send_text(strings.COMMAND_IS_NOT_EXIST % c.name) if not c.turn_finished: new_turn = False new_turn = True for c in world.companies: if not c.turn_finished: new_turn = False if new_turn: world.ontime() for c in world.companies: c.turn_finished = False
def chklose(self): for c in self.companies: if c.credit > 100: if c.show_messages: strings.send_text(strings.LOSE % c.name) self.winner_is_here = False self.quit = True
def chkwin(self): for c in self.companies: if (c.money > 100) and (c.credit == 0): if (c.show_messages): strings.send_text(strings.WIN % c.name) self.winner_is_here = True self.quit = True
def buy(world, company, value=1): if company.money < (value * world.price): if company.show_messages: strings.send_text(strings.INSUFFICIENT_MONEY % company.name) return company.money -= (value * world.price) company.goody += value company.turn_finished = True
def return_money(world, company, value=1): if company.money < value: if company.show_messages: strings.send_text(strings.INSUFFICIENT_MONEY % company.name) return if company.credit < value: value = company.credit company.money -= value company.credit -= value company.turn_finished = True
def calc(player, nruns, parameters): logfile = open('lastresults.log', 'w') keys = [] current_val = {} for key in parameters.keys(): keys.append(key) top_plays = recur_calc(keys, 0, parameters, current_val, nruns, logfile, player) logfile.write(strings.BEST_AI_RESULT % (str(top_plays), '\n')) strings.send_text(strings.BEST_AI_RESULT % (str(top_plays), '')) logfile.close()
def sell(world, company, value=None): count = 0 sell_price = 0 while (randint(0, 3) != 0) and (company.goody > 0): company.goody -= 1 sell_price = int(world.price * 1.2) + 1 company.money += sell_price count += 1 if company.show_messages: strings.send_text( strings.SUCCESS_TRADE % (company.name, count, sell_price, count * sell_price)) company.turn_finished = True
def info(self, forced=False): if self.show_messages or forced: strings.send_text(strings.MONEY % (self.name, self.money)) strings.send_text(strings.CREDIT % (self.name, self.credit)) strings.send_text(strings.PRODUCTS % (self.name, self.goody)) strings.send_text(strings.LAST_TURN % (self.name, self.director.last_command))
def recur_calc(parameters_list, pos, parameters, current_val, nrun, logfile, player, top_score=[]): top_plays = top_score min_value = parameters[parameters_list[pos]][0] max_value = parameters[parameters_list[pos]][1] for i in range(min_value, max_value): current_val[parameters_list[pos]] = i if len(parameters_list) - 1 != pos: top_plays = recur_calc(parameters_list, pos + 1, parameters, current_val, nrun, logfile, player, top_plays) else: n = nrun wins = 0 loses = 0 while n != 0: world = World([Company(Director(player, current_val))], hideoutput=True) run_game(world) if world.winner_is_here: wins += 1 else: loses += 1 n -= 1 strings.send_text(strings.AI_WIN_RESULT % (wins, nrun, str(current_val), '')) logfile.write(strings.AI_WIN_RESULT % (wins, nrun, str(current_val), '\n')) if top_plays: if wins < top_plays[0]: pass else: if len(top_plays) < 10: top_plays.append(wins) else: top_plays[0] = wins else: top_plays.append(wins) top_plays.sort() return top_plays
def info(self, forced=False): if not self.hideoutput: strings.send_text(strings.DATE % self.date) strings.send_text(strings.CREDIT_PERCENT % self.percent) strings.send_text(strings.PRODUCT_PRICE % self.price) for c in self.companies: c.info(forced)
from game import run_game import players import strings from world import World answer = input(strings.MAINMENU_TEXT) if answer == '1': player_count = input(strings.PLAYER_COUNT) company_list = [] for i in range(0, int(player_count)): strings.send_text(strings.PLAYER_SELECT % (i + 1)) for j in range(0, len(players.players_list)): strings.send_text('%d) %s' % (j + 1, players.players_list[j])) selected_player = int(input(strings.INPUT_NUMBER)) - 1 visible_info = input(strings.SHOW_HIS_TURNS) if visible_info == '1': visible_info = True else: visible_info = False company_list.append( Company(Director(players.func_list[selected_player]), visible_info)) world = World(company_list) run_game(world) elif answer == '2':
def help_game(world, company, value=None): strings.send_text(strings.HELP_TEXT)