def move(self, state): print("{0}'s turn. {0} is {1}, {2}".format(self.name, self.color, self.ctr)) self.ctr += 1 m = Minimax(state) best_move, value = m.bestMove(self.difficulty, state, self.color) return best_move
class AIPlayer(Player): """ AIPlayer object that extends Player The AI algorithm is minimax, the difficulty parameter is the depth to which the search tree is expanded. """ difficulty = None def __init__(self, name, color, difficulty=5): self.type = "AI" self.name = name self.color = color self.difficulty = difficulty self.m = Minimax() def move(self, state): print "{0}'s turn. {0} is {1}".format(self.name, self.color) # sleeping for about 1 second makes it looks like he's thinking #time.sleep(random.randrange(8, 17, 1)/10.0) #return random.randint(0, 6) self.m.set_board(state) best_move, value = self.m.bestMove(self.difficulty, state) print best_move return best_move
def move(self, state): print("{0}'s turn. {0} is {1}".format(self.name, self.color)) # sleeping for about 1 second makes it looks like he's thinking #time.sleep(random.randrange(8, 17, 1)/10.0) #return random.randint(0, 6) m = Minimax(state) best_move, value = m.bestMove(self.difficulty, state, self.color) return best_move
def move(self, state): print("{0}'s turn. {0} is {1}".format(self.name, self.color)) m = Minimax(state) best_move, _ = m.bestMove(self.difficulty, state, self.color, self.heuristic) #SetMoveMi(best_move) print(self.name + ": " + str(best_move)) return best_move
def make_move(self): changed_board = self.change_board(settings.board) m = Minimax(changed_board) x = m.bestMove(self.depth, changed_board, 'x' if self.player == 1 else 'o') x = x[0] output = self.name + " played in column " + str(x) + "." print(output) return x
def randomai(self, r): print("---------------") g = deepcopy(self.stanje) g = transponiraj(g) g = obrni(g) m = Minimax(g) p = m.bestMove(self.tezavnost, g, 2)[0] if self.preverjanje1(self.stanje)[0] != -1 or self.preverjanje1(self.stanje)[1] != -1: if self.preverjanje1(self.stanje)[0] != -1: p = self.preverjanje1(self.stanje)[0] else: p = self.preverjanje1(self.stanje)[1] v = [i for i in range(len(self.stanje[p][:])) if self.stanje[p][i]!=0] v.append(6) m = min(v)-1 self.steviloklikov[p] += 1 self.stanje[p][m] = 2 self.risanje(2, p, m) self.platno.bind("<Button-1>", self.klik) if self.zmaga(self.stanje): self.aiscore += 1 self.risanjezmaga() m1=tkinter.messagebox.askyesno("Jaesus","You lost! New game?") if m1: self.zacetek() return else: return if sum(self.steviloklikov)==42: self.neodloceno+=1 m1=tkinter.messagebox.askyesno("Tie","It's a tie. New game?") if m1: self.zacetek() return else: quit()
def move(self, state): print("{0}'s turn. {0} is {1}".format(self.name, self.color)) # sleeping for about 1 second makes it looks like he's thinking #time.sleep(random.randrange(8, 17, 1)/10.0) #return random.randint(0, 6) port = serial.Serial("COM12", 9600) port.isOpen() m = Minimax(state) #m = Minimax(super_board) best_move, value = m.bestMove(self.difficulty, state, self.color) #print(state) #input(best_move + 1) print(best_move + 1) inp = 7 - best_move k = str.encode(str(inp)) #time.sleep(2) time.sleep(1) #k = bytes(inp) port.write(k) port.write(b'1') #print(k) i = 0 while True: response = port.read() if response == b'9': print(response) i = 1 if (i == 1): break port.close() return best_move
def intelligentFunction2(turn, board): m = Minimax() best_move, value = m.bestMove(4, board, 2) return best_move
class C4(object): """ the Connect 4 game, with computer vision for understanding the game board and whose turn it is, minimax for planning its own moves, and basic control system to make those moves and other interactions """ def __init__(self, render=True, ros_node=None): self.board = C4Board(render) self.max = Minimax() if not ros_node: rospy.init_node("connect4", anonymous=True) self.ur5_commander = rospy.Publisher("behaviors_cmd", String, queue_size=10) self.token_grabber = rospy.Publisher("gripper", Int16, queue_size=10) self.computer_vision = rospy.Publisher("c4_ready", Int16, queue_size=10) rospy.Subscriber("arm_status", String, self.status_callback) rospy.Subscriber("opponent_move", Int16, self.player_move) self.status = True self.turn = "Start" self.player_action = None def player_move(self, data): """ callback from Hannah's CV module, gives back an integer indicating which column the player moved to """ self.player_action = data.data self.turn = "HUMAN" def status_callback(self, data): """ callback for arm status """ if data.data == "free": self.status = True else: self.status = False def check_for_completion(self): """ checks to make sure that the arm is done with the previous command before continuing """ time.sleep(3) while not self.status: pass def ur5_publish(self, command): """ publishes a command, then checks for completion of said command before continuing the script """ self.ur5_commander.publish(command) self.check_for_completion() def control_mouth(self): """ controls the mouth, opens (0), waits a bit, then closes (1) """ self.token_grabber.publish(0) time.sleep(3) self.token_grabber.publish(1) def make_move(self, move): """ makes a physical move with the physical board, which includes getting a token, then moving to drop it into the chosen column, then returning to home position """ # looks for token self.ur5_publish("c4_load_token") self.control_mouth() # makes a move and drops token self.ur5_publish("c4_move_" + str(move)) self.control_mouth() self.ur5_publish("c4_home") def play_game(self): """ plays a single game of connect 4 """ self.board.reset() while True: # waits for opponent move from rostopic print "WAITING FOR OPPONENT'S MOVE" stuff = raw_input("Move made?") self.computer_vision.publish(1) while self.turn != "HUMAN": pass # self.computer_vision.publish(0) # updates its own digital board observation_, done = self.board.step(self.player_action, 1) # break if human wins if done: print "\n" * 2 print "HUMAN WINS" return "HUMAN" elif done == "Draw": print "\n" * 2 print "DRAW" return "HUMAN" # AI makes moves and updates digital board self.turn = "AI" move, value = self.max.bestMove(5, observation_) _, done = self.board.step(move, 2) print "I MOVED TO COLUMN", str(move + 1) # AI physically moves self.make_move(move + 1) self.computer_vision.publish(1) # break if ai wins if done: print "\n" * 2 print "AI WINS" return "AI" elif done == "Draw": print "\n" * 2 print "DRAW" return "HUMAN" def run(self): """ main interaction sequence """ print "Starting up Connect 4 game" time.sleep(3) self.ur5_publish("c4_start") self.ur5_publish("c4_home") self.computer_vision.publish(1) time.sleep(1) result = self.play_game() if result == "AI": command = "c4_win" else: command = "c4_loss" self.ur5_publish(command) self.ur5_publish("c4_end")
def move(self, state): print("{0}'s turn. {0} is {1}, {2}".format(self.name, self.color, self.ctr)) self.ctr+=1 m = Minimax(state) best_move, value = m.bestMove(self.difficulty, state, self.color) return best_move
def move(self, state): print("Computer turn") m = Minimax(state) best_move, value = m.bestMove(self.difficulty, state, self.color) return best_move
def move(self, board): time.sleep(1) minimax = Minimax(board) x = minimax.bestMove(board, self.depth, self.piece) return x[0]
)) if difficulty not in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: print("Invalid choice...Goodbye!") quit() algo = Minimax() # Pass in algo for AI move selection game_board = Board(starting_player) while not game_board.isOver(): if game_board.player == 1: game_board.display() print( "Choose an open edge to connect.\nType 'e' in row or col to end the game" ) r = int(input("row: ")) c = int(input("col: ")) game_board.makeMove(r, c) elif game_board.player == -1: game_board.display() r, c = algo.bestMove(root=game_board, search_depth=difficulty) game_board.makeMove(r, c) print("AI chose ({}, {})".format(r, c)) if game_board.score > 0: game_board.display() print("Human wins! ... Wait what?") elif game_board.score < 0: game_board.display() print("AI wins! Better luck net time...") else: game_board.display() print("It's a draw!")
def getComputerMove(board,secondBoard): m = Minimax(secondBoard) best_move, value = m.bestMove(4,secondBoard,YELLOW) return best_move