def __init__(self, player_number): """Initializes the Connect4Player. DON'T CHANGE THIS!""" self.player_number = player_number self.board = Board(player_number) self.hard_coded_moves = True self.their_move_stack = []
class AIConnect4Player(object): def __init__(self, player_number): """Initializes the Connect4Player. DON'T CHANGE THIS!""" self.player_number = player_number self.board = Board(player_number) self.hard_coded_moves = True self.their_move_stack = [] @Connect4Timer def move(self, board): """Returns the next move of the Connect4Player. The value returned is an integer from 0-6, specifying which column the player's piece should be dropped in Arguments: board -- this is a 6x7 array containing the contents of the current game board. Each entry will be either 0 (empty), 1 (occupied by Player 1) or 2 (occupied by Player 2) """ their_move = None for row, move in zip(self.board.array(), board): for index, item in enumerate(move): if item != row[index]: their_move = index self.board.move(index) move = self.move_logic(their_move) self.board.move(move) return move def move_logic(self, their_move): """Returns a single move or array of moves to take for a given board""" # Check for hard coded preconditions if self.hard_coded_moves: hard_coded_move = self.check_hard_coded_moves(their_move) if hard_coded_move != -1: alpha_beta(self.board, 4, self.player_number) return hard_coded_move self.hard_coded_moves = False if self.board.move_count < 12: # We need to build up our move cache. Not like it matters at this # depth anyway. move = alpha_beta(self.board, 5, self.player_number) else: move = alpha_beta(self.board, 6, self.player_number) return move def check_hard_coded_moves(self, their_move): if self.player_number == 1 and self.board.move_count == 0: return 3 self.their_move_stack.append(their_move) if self.player_number == 1: return self.player_one_hardcoded_moves(their_move) else: return self.player_two_hardcoded_moves(their_move) return -1 def player_one_hardcoded_moves(self, their_move): if self.board.move_count == 0: return 3 if self.board.move_count == 2: if their_move == 3: return 3 elif their_move == 1: return 5 elif their_move == 4: return 0 else: return -1 elif self.board.move_count == 4: if self.their_move_stack == [3, 3]: return 3 elif self.their_move_stack == [3, 2]: return 2 elif self.their_move_stack == [1, 4]: return 5 elif self.their_move_stack == [4, 3]: return 3 elif self.their_move_stack == [4, 0]: return 1 elif self.board.move_count == 6: if self.their_move_stack == [3, 3, 4]: return 4 elif self.their_move_stack == [3, 2, 2]: return 3 elif self.their_move_stack == [4, 3, 3]: return 1 return -1 def player_two_hardcoded_moves(self, their_move): if self.board.move_count == 1: return 4 elif self.board.move_count == 3: if their_move == 1: return 0 elif their_move == 0: return 2 elif self.board.move_count == 5: if self.their_move_stack == [3, 0, 3]: return 3 elif self.their_move_stack == [3, 1, 1]: return 1 return -1