def test_iters(): # I want to know how many iteractions are good. # I'll play a player that does only 10 iterations, then keep pushing mine up to see how many wins/losses # Play this many games at each iter value NUM_GAMES = 100 for iterations in (1, 10, 100, 1000, 10000): win_counts = [0,0] for game_num in range(NUM_GAMES): state = PresidentGameState() state._deal() with term.location(0, term.height - 1): print("(%s) Game number %s/%s" % (iterations, game_num, NUM_GAMES)), sys.stdout.flush() while state.get_moves(): # Use different numbers of iterations (simulations, tree nodes) for different players if state.player_to_move == 0: m = ismcts(rootstate=state, itermax=iterations, verbose=False, quiet=True) else: m = ismcts(rootstate=state, itermax=1, verbose=False, quiet=True) # print "Best Move: " + str(m) + "\n" state.do_move(m) # print "Results: P0: %s P1: %s" % (state.get_result(0), state.get_result(1)) # if (state.get_result(0) and state.get_result(1)) or not (state.get_result(0) or state.get_result(1)): # # Nobody won or both players won # print state for p in (0,1): win_counts[p] += state.get_result(p) term.clear_eol() print "Iteration %s - %s" % (iterations, win_counts)
def play_self(): """ Play a sample game between two ismcts players. """ state = PresidentGameState() state._deal() while state.get_moves(): print str(state) # Use different numbers of iterations (simulations, tree nodes) for different players if state.player_to_move == 0: m = ismcts(rootstate=state, itermax=1000, verbose=False) else: m = ismcts(rootstate=state, itermax=100, verbose=False) print "Best Move: " + str(m) + "\n" state.do_move(m) for p in (0,1): if state.get_result(p) > 0: print "Player " + str(p) + " wins!"
def play_game(): """ Play a sample game between two ismcts players. """ state = KnockoutWhistState(4) while state.get_moves(): print str(state) # Use different numbers of iterations (simulations, tree nodes) for different players if state.player_to_move == 1: m = ismcts(rootstate=state, itermax=1000, verbose=False) else: m = ismcts(rootstate=state, itermax=100, verbose=False) print "Best Move: " + str(m) + "\n" state.do_move(m) someone_won = False for p in xrange(1, state.number_of_players + 1): if state.get_result(p) > 0: print "Player " + str(p) + " wins!" someone_won = True if not someone_won: print "Nobody wins!"
def play_game(): """ Play a game between one human and one AI """ state = PresidentGameState() # state.player_hands[0] = [ # Card('3H'), # Card('4H'), # Card('5C'), # Card('5D'), # Card('6H'), # Card('7H'), # Card('8C'), # Card('9H'), # Card('JH'), # Card('QH'), # Card('KC'), # Card('AH'), # Card('2H'), # Card('QD'), # Card('KS'), # Card('AD'), # Card('2D')] state.player_hands[0] = [ Card('9d'), Card('js'), Card('qc'), Card('3d'), Card('as'), Card('2s'), Card('ts'), Card('jd'), Card('6s'), Card('9s'), Card('5h'), Card('7d'), Card('8s'), Card('4d'), Card('qs'), Card('tc'), Card('7s')] TOTAL_CARDS = 17 while len(state.player_hands[0]) < TOTAL_CARDS: card = get_card("Enter card %s/%s: " % (len(state.player_hands[0]) + 1, TOTAL_CARDS)) if card not in state.player_hands[0]: state.player_hands[0].append(card) else: print "Duplicate card - ignoring" print "All done" print state.player_hands[0] while True: print str(state) # Use different numbers of iterations (simulations, tree nodes) for different players if state.player_to_move == 0: m = ismcts(rootstate=state, itermax=10000, verbose=False) print "Best Move: " + str(m) + "\n" state.do_move(m) if not state.player_hands[0]: # No cards left - the end break else: confirmed_move = False move = [] while not confirmed_move: num_cards = get_int("How many cards did the other player play?") move = [] if num_cards == 0: move = "PASS" else: for i in range(num_cards): card = get_card("What was the card?") move.append(card) user_input = raw_input("Did the player play this (y/n)? %s" % move) if user_input is "y" or user_input is "Y": confirmed_move = True else: print "Let's try again..." if move is not "PASS": state.player_hands[1].extend(move) state.do_move(move) if move is not "PASS": state.player_to_move = state.get_next_player(state.player_to_move) for p in (0,1): if state.get_result(p) > 0: print "Player " + str(p) + " wins!"