Example #1
0
class Watcher:
    def __init__(self, game_file):
        self.file = game_file
        FILE = open(self.file, 'r')
        source = FILE.read()
        FILE.close()
        self.turn_list = re.split("\n+", source)[:-1]
        self.current_turn = 0
        self.last_turn = len(self.turn_list)
        self.game = Board(player.Player(), player.Player(), TEMP_GAME_FILE)
        self.has_eval = False
        self.eval = None
        self.show_eval = False
        self.pos_value = 0

    def write_current_turn(self):
        self.game.load_state_from_string(self.turn_list[self.current_turn])
        if self.has_eval:
            self.pos_value = self.eval(str(self.game))

    def set_heuristic(self, heuristic):
        self.eval = heuristic
        self.has_eval = True

    def set_show_eval(self, flag):
        if self.has_eval:
            self.show_eval = flag
        else:
            self.show_eval = False

    def next_turn(self):
        if self.current_turn < self.last_turn - 1:
            self.current_turn += 1
        else:
            self.current_turn = 0
        self.write_current_turn()

    def previous_turn(self):
        if self.current_turn > 0:
            self.current_turn -= 1
        else:
            self.current_turn = self.last_turn - 1
        self.write_current_turn()

    def watch(self):
        done = False
        while not done:
            self.game.opg()
            print "Viewing turn " + str(
                self.current_turn) + " out of " + str(self.last_turn - 1)
            print "press 'q' to quit, 'a' to go to the previous turn, 'd' to go to the next turn."
            if self.show_eval:
                print "This position evaluates to: " + str(self.pos_value)
            input_char = prgm_lib.getch()
            if input_char == 'q':
                done = True
            elif input_char == 'a':
                self.previous_turn()
            elif input_char == 'd':
                self.next_turn()
Example #2
0
class Watcher:
	def __init__(self, game_file):
		self.file = game_file
		FILE = open(self.file, 'r')
		source = FILE.read()
		FILE.close()
		self.turn_list = re.split("\n+", source)[:-1]
		self.current_turn = 0
		self.last_turn = len(self.turn_list)
		self.game = Board(player.Player(),player.Player(),TEMP_GAME_FILE)
		self.has_eval = False
		self.eval = None
		self.show_eval = False
		self.pos_value = 0

	def write_current_turn(self):
		self.game.load_state_from_string(self.turn_list[self.current_turn])
		if self.has_eval:
			self.pos_value = self.eval(str(self.game))
		
	def set_heuristic(self, heuristic):
		self.eval = heuristic
		self.has_eval = True
		
	def set_show_eval(self, flag):
		if self.has_eval:
			self.show_eval = flag	
		else:
			self.show_eval = False
				
	def next_turn(self):
		if self.current_turn < self.last_turn-1:
			self.current_turn += 1
		else:
			self.current_turn = 0
		self.write_current_turn()
	
	def previous_turn(self):
		if self.current_turn > 0:
			self.current_turn -= 1
		else:
			self.current_turn = self.last_turn - 1
		self.write_current_turn()

	def watch(self):
		done = False
		while not done:
			self.game.opg()
			print "Viewing turn " + str(self.current_turn) + " out of " + str(self.last_turn - 1)
			print "press 'q' to quit, 'a' to go to the previous turn, 'd' to go to the next turn."
			if self.show_eval:
				print "This position evaluates to: " + str(self.pos_value)
			input_char = prgm_lib.getch()
			if input_char == 'q':
				done = True
			elif input_char == 'a':
				self.previous_turn()
			elif input_char == 'd':
				self.next_turn()
Example #3
0
 def __init__(self, game_file):
     self.file = game_file
     FILE = open(self.file, 'r')
     source = FILE.read()
     FILE.close()
     self.turn_list = re.split("\n+", source)[:-1]
     self.current_turn = 0
     self.last_turn = len(self.turn_list)
     self.game = Board(player.Player(), player.Player(), TEMP_GAME_FILE)
     self.has_eval = False
     self.eval = None
     self.show_eval = False
     self.pos_value = 0
Example #4
0
	def __init__(self, game_file):
		self.file = game_file
		FILE = open(self.file, 'r')
		source = FILE.read()
		FILE.close()
		self.turn_list = re.split("\n+", source)[:-1]
		self.current_turn = 0
		self.last_turn = len(self.turn_list)
		self.game = Board(player.Player(),player.Player(),TEMP_GAME_FILE)
		self.has_eval = False
		self.eval = None
		self.show_eval = False
		self.pos_value = 0
Example #5
0
	player2 = player.AI_ABPruning(heuristics.game_heuristic1, show_thought_level)
	if random.randint(0,1) == 0:
		player1 = player.RandomAI()
		print "Player 1 is random"
	else:
		player2 = player.RandomAI()
		print "Player 2 is random"

if watch:
	movie = watcher.Watcher(game_file)
	movie.set_heuristic(heuristics.game_heuristic1)
	movie.set_show_eval(True)
	movie.watch()
else:
	if not test:
		game = Board(player1, player2, game_file, history, load, show=show_board)
		game.play()
	else:
		if this_test == "draw1":
			player1 = player.RandomAI()
			player2 = player.RandomAI()
			max_turns = 0
			counter = 0
			while max_turns < 81:
				game = Board(player1, player2, game_file, history, load, True, show_board)
				game.play()
				counter += 1
				if game.get_turn() >= max_turns:
					game.opg()
					max_turns = game.get_turn()
					print str(max_turns) + " ; " + str(counter)
Example #6
0
 def is_terminal_node(self):
     game = Board(player.Player(), player.Player(), DEFAULT_GAME_FILE)
     game.load_state_from_string(self.state)
     return game.is_game_over()
Example #7
0
 def set_children(self):
     game = Board(player.Player(), player.Player(), DEFAULT_GAME_FILE)
     game.load_state_from_string(self.state)
     self.children = sort_into_search_order(game.get_children_states())