currentplayer = 1 #initalize a player 1 ai if no human if human == 0: ai1 = ai.ai(1) #ai1.set_SELF_SCORE(10) #initalize player 2 ai #ai2 = ai.ai(2) #ai2.set_OPPONENT_SCORE(10) #while no winner and there are still moves available while winner == False and len(connect4.open_columns()) > 0: connect4.print_board() #Human move if currentplayer == 1 and human > 0: open_columns = connect4.open_columns() print "Open columns: " + str(open_columns) column = input("Player " + str(currentplayer) + " choose an open column to drop your piece in: \n") while column not in open_columns: print "ERROR: " + str(column) + " is not a valid open column"
def analyze_choices(self, depth, player, alpha, beta): if depth == 0: return [self.evaluate(), -1] elif player == self.ai: v = -sys.maxint maxcolumn = -1 columns = connect4.open_columns() if len(columns) == 0: return [self.TIE_SCORE, -1] for column in columns: win = connect4.drop_piece(column, player) if win == True: connect4.undo_piece(column) return [self.FOUR_SCORE * self.SELF_SCORE, column] score = self.analyze_choices(depth-1,self.opponent, alpha, beta)[0] connect4.undo_piece(column) if score > v: v = score maxcolumn = column if v > alpha: alpha = v if beta <= alpha: break return [v, maxcolumn] else: v = sys.maxint mincolumn = -1 columns = connect4.open_columns() if len(columns) == 0: return [self.TIE_SCORE, -1] for column in columns: win = connect4.drop_piece(column, player) if win == True: connect4.undo_piece(column) return [self.FOUR_SCORE * self.OPPONENT_SCORE * -1, column] score = self.analyze_choices(depth-1,self.ai, alpha, beta)[0] connect4.undo_piece(column) if score < v: v = score mincolumn = column if v < beta: beta = v if beta <= alpha: break return [v, mincolumn]