def trial(home, num=500): '''Tests the home player versus num Critters.''' wins = 0 draws = 0 print "Beginning trials..." for _ in range(num): crit = Critter() game = play_game(home, crit, False) if game == Board.X: wins += 1 elif game == Board.draw: draws += 1 game = play_game(crit, home, False) if game == Board.O: wins += 1 elif game == Board.draw: draws += 1 return (100. * wins / (num * 2.), 100. * draws / (num * 2.))
def gauntlet(data): '''Determines the fitness of Critters in a Population by having them play a tournament of tic-tac-toe.''' i, sz = data _, critters = restore(sz, False) x = critters[i] ret = [play_game(x, o, False) for o in critters] print "evaluated %i of %i"% (i+1, sz) return ret
prompt = "%s's move? "% brd.active_symbol() try: move = int(raw_input(prompt))-1 except ValueError: pass # keep trying on nonsense return move def trial(home, num=500): '''Tests the home player versus num Critters.''' wins = 0 draws = 0 print "Beginning trials..." for _ in range(num): crit = Critter() game = play_game(home, crit, False) if game == Board.X: wins += 1 elif game == Board.draw: draws += 1 game = play_game(crit, home, False) if game == Board.O: wins += 1 elif game == Board.draw: draws += 1 return (100. * wins / (num * 2.), 100. * draws / (num * 2.)) if __name__ == "__main__": if len(sys.argv)<3: print "Usage %s pop_size gens"% sys.argv[0] p = Population(int(sys.argv[1]), int(sys.argv[2])) print "%f%% wins, %f%% draws"% trial(p) x, o = p, Human() while True: print "---starting game---" play_game(x, o) x, o = o, x