# Cmput 455 sample code # Play match between several TicTacToe players # Written by Martin Mueller from game_basics import BLACK, WHITE, EMPTY from tic_tac_toe_integer_eval import TicTacToe from perfect_player import PerfectPlayer from random_player import RandomPlayer from simulation_player import SimulationPlayer # Which player should play the next move? # Alternate between even and odd number of moves def selectPlayer(numMoves, player1, player2): if numMoves % 2 == 0: return player1 else: return player2 def playGame(player1, player2): t = TicTacToe() numMoves = 0 while not t.endOfGame(): player = selectPlayer(numMoves, player1, player2) t.play(player.genmove(t)) numMoves += 1 #print("Game winner:", t.winner(), "Moves:", t.moves) return t.winner() def playMatch(player1, player2, numGames): stats = [0] * 3 for _ in range(numGames): winner = playGame(player1, player2) stats[winner] += 1 printstats(stats, player1, player2) return stats def printstats(stats, player1, player2): print("{0} wins for {1}, {2} wins for {3}, {4} draws". format(stats[BLACK], player1.name(), stats[WHITE], player2.name(), stats[EMPTY])) def playMatchBothColors(player1, player2, numGames): # player1 is X stats1 = playMatch(player1, player2, numGames) # player1 is O stats2 = playMatch(player2, player1, numGames) # Compute combined statistics - reversed colors in second match stats1[BLACK] += stats2[WHITE] stats1[WHITE] += stats2[BLACK] stats1[EMPTY] += stats2[EMPTY] print("Total:") printstats(stats1, player1, player2) eval = (stats1[BLACK] + 0.5 * stats1[EMPTY]) / (2 * numGames) print("Percentage for {0} = {1:.2f}".format(player1.name(), 100 * eval)) def RoundRobinMatches(): player1 = SimulationPlayer(10) player2 = PerfectPlayer() player3 = RandomPlayer() playMatchBothColors(player1, player2, 100) playMatchBothColors(player1, player3, 100) playMatchBothColors(player2, player3, 100) def SelfPlayMatches(): player = SimulationPlayer(10) playMatchBothColors(player, player, 100) player = RandomPlayer() playMatchBothColors(player, player, 100) player = PerfectPlayer() playMatchBothColors(player, player, 100) def ScalingMatches(): #player2 = PerfectPlayer() player2 = RandomPlayer() numSim = 1 for _ in range(4): player1 = SimulationPlayer(numSim) playMatchBothColors(player1, player2, 100) numSim *= 10 def Sim1000andPerfectVsRandom(): player1 = SimulationPlayer(1000) player2 = PerfectPlayer() player3 = RandomPlayer() playMatchBothColors(player1, player3, 1000) playMatchBothColors(player2, player3, 1000) RoundRobinMatches() #SelfPlayMatches() #ScalingMatches() #Sim1000andPerfectVsRandom()