def play(self): """Simulates a game of Hog by calling hog.play with the GUI strategies. If the player destroys the window prematurely (i.e. in the middle of a game), a HogGUIException is raised to exit out of play's loop. Otherwise, the widget will be destroyed, but the strategy will continue waiting. """ self.turn = 1 - self.turn self.switch(0) self.s_labels[0].text = "0" self.s_labels[1].text = "0" self.status_label.text = "" try: commentary = hog.both( hog.announce_highest(0), hog.both(hog.announce_highest(1), hog.announce_lead_changes())) score, opponent_score = hog.play(self.strategy, self.strategy, dice=self.make_dice(6), say=commentary) except HogGUIException: pass else: self.s_labels[0].text = score self.s_labels[1].text = opponent_score winner = 0 if score > opponent_score else 1 self.status_label.text = "Game over! {} wins!".format(name(winner))
def play(self): """Симулирует игру в Свинью, вызывая hog.play с GUI-стратегиями. Если игрок внезапно закрывает окно (например в середине игры), то поднимается HogGUIException для выхода из игрового цикла. В противном случае при уничтожении виджета стратегия продолжила бы ожидание. """ self.turn = 1 - self.turn self.switch(0) self.s_labels[0].text = '0' self.s_labels[1].text = '0' self.status_label.text = '' try: commentary = hog.both(hog.announce_highest(0), hog.both(hog.announce_highest(1), hog.announce_lead_changes())) score, opponent_score, _ = trace_play(hog.play, self.strategy, self.strategy, score0=0, score1=0, dice=self.make_dice(6), goal=100, say=commentary, feral_hogs=True) except HogGUIException: pass else: self.s_labels[0].text = score self.s_labels[1].text = opponent_score winner = 0 if score > opponent_score else 1 self.status_label.text = 'Игра окончена! {} победил!'.format( name(winner))
def play(self): """Simulates a game of Hog by calling hog.play with the GUI strategies. If the player destroys the window prematurely (i.e. in the middle of a game), a HogGUIException is raised to exit out of play's loop. Otherwise, the widget will be destroyed, but the strategy will continue waiting. """ self.turn = 1 - self.turn self.switch(0) self.s_labels[0].text = '0' self.s_labels[1].text = '0' self.status_label.text = '' try: commentary = hog.both(hog.announce_highest(0), hog.both(hog.announce_highest(1), hog.announce_lead_changes())) score, opponent_score = hog.play(self.strategy, self.strategy, dice=self.make_dice(6), say=commentary) except HogGUIException: pass else: self.s_labels[0].text = score self.s_labels[1].text = opponent_score winner = 0 if score > opponent_score else 1 self.status_label.text = 'Game over! {} wins!'.format( name(winner))
def take_turn(prev_rolls, move_history, goal, game_rules): fair_dice = dice.make_fair_dice(6) dice_results = [] feral_hogs = game_rules["Feral Hogs"] swine_swap = game_rules["Swine Swap"] if not swine_swap: old_is_swap, hog.is_swap = hog.is_swap, lambda score0, score1: False def logged_dice(): if len(dice_results) < len(prev_rolls): out = prev_rolls[len(dice_results)] else: out = fair_dice() dice_results.append(out) return out final_scores = None final_message = None commentary = hog.both( hog.announce_highest(0), hog.both(hog.announce_highest(1), hog.announce_lead_changes()), ) def log(*logged_scores): nonlocal final_message, commentary f = io.StringIO() with redirect_stdout(f): # commentary = commentary(*logged_scores) print("OO") final_message = f.getvalue() return log move_cnt = 0 def strategy(*scores): nonlocal final_scores, move_cnt final_scores = scores if move_cnt % 2: final_scores = final_scores[::-1] if move_cnt == len(move_history): raise HogLoggingException() move = move_history[move_cnt] move_cnt += 1 return move game_over = False try: final_scores = trace_play(hog.play, strategy, strategy, 0, 0, dice=logged_dice, say=log, goal=goal, feral_hogs=feral_hogs)[:2] except HogLoggingException: pass else: game_over = True if not swine_swap: hog.is_swap = old_is_swap return { "rolls": dice_results, "finalScores": final_scores, "message": final_message, "gameOver": game_over, }
def take_turn(prev_rolls, move_history, goal, game_rules): """Simulate the whole game up to the current turn.""" fair_dice = dice.make_fair_dice(6) dice_results = [] swine_align = game_rules["Swine Align"] pig_pass = game_rules["Pig Pass"] try: if not swine_align: old_swine_align, hog.swine_align = hog.swine_align, lambda score0, score1: False if not pig_pass: old_pig_pass, hog.pig_pass = hog.pig_pass, lambda score0, score1: False def logged_dice(): if len(dice_results) < len(prev_rolls): out = prev_rolls[len(dice_results)] else: out = fair_dice() dice_results.append(out) return out final_scores = None final_message = None who = 0 commentary = hog.both( hog.announce_highest(0), hog.both(hog.announce_highest(1), hog.announce_lead_changes()), ) def log(*logged_scores): nonlocal final_message, commentary f = io.StringIO() with redirect_stdout(f): commentary = commentary(*logged_scores) final_message = f.getvalue() return log move_cnt = 0 def strategy_for(player): def strategy(*scores): nonlocal final_scores, move_cnt, who final_scores = scores if player: final_scores = final_scores[::-1] who = player if move_cnt == len(move_history): raise HogLoggingException() move = move_history[move_cnt] move_cnt += 1 return move return strategy game_over = False try: final_scores = trace_play(hog.play, strategy_for(0), strategy_for(1), 0, 0, dice=logged_dice, say=log, goal=goal)[:2] except HogLoggingException: pass else: game_over = True finally: if not swine_align: hog.swine_align = old_swine_align if not pig_pass: hog.pig_pass = old_pig_pass return { "rolls": dice_results, "finalScores": final_scores, "message": final_message, "gameOver": game_over, "who": who, }
def take_turn(prev_rolls, move_history, goal): fair_dice = dice.make_fair_dice(6) dice_results = [] def logged_dice(): if len(dice_results) < len(prev_rolls): out = prev_rolls[len(dice_results)] else: out = fair_dice() dice_results.append(out) return out final_scores = None final_message = None commentary = hog.both( hog.announce_highest(0), hog.both(hog.announce_highest(1), hog.announce_lead_changes()), ) def log(*logged_scores): nonlocal final_scores, final_message, commentary final_scores = logged_scores f = io.StringIO() with redirect_stdout(f): commentary = commentary(*logged_scores) final_message = f.getvalue() return log move_cnt = 0 def strategy(*args): nonlocal move_cnt if move_cnt == len(move_history): raise HogLoggingException() move = move_history[move_cnt] move_cnt += 1 return move game_over = False try: trace_play(hog.play, strategy, strategy, 0, 0, dice=logged_dice, say=log, goal=goal, feral_hogs=True) except HogLoggingException: pass else: game_over = True return { "rolls": dice_results, "finalScores": final_scores, "message": final_message, "gameOver": game_over, }