def choose_options(depth, board, myTurn=True): """ INPUT: depth(integer), board(matrix), myTurn(boolean) creates a dict with score s OUTPUT: column (integer) were we should place our piece >>> choose_options(depth, board) """ rows = board.shape[0] columns = board.shape[1] possible_moves = {} # possible moves (key) and their scores (value) for column in range(columns): # check if column i is a possible if isLegalMove(column, board): # make the move in column for curr_player temp = makeMove(column, board, myTurn) # print(column) # print(temp) if winning(temp)[1] == 2: # print('winning') return str(column + 1) if depth == 0: possible_moves[column] = scoring(temp, True) #### assign overall score (value, recurs function) to every column (key) possible_moves[column] = search(depth - 1, temp, False) print(possible_moves) # return the key(column) for the best score return best_option(possible_moves)
def search(depth, board, my_turn): """ Search the tree until depth 'depth' By default, the is the board, and curr_player is whomever called this search Return score >>> search(0, board, True) """ columns = board.shape[1] # enumerate all possible moves from this board legal_moves = [] for column in range(columns): # if column i is a legal move if isLegalMove(column, board): # make the move in column i for curr_player temp = make_move(column, board, my_turn) # print(temp) # create list of matrix legal_moves.append(temp) # BASECASE (if depth == 0, game tied or someone wins) if depth == 0 or not legal_moves or winning(board)[0]: # return value(board, curr_player) from winning return scoring(board, my_turn) # RECURSION elif my_turn: # Maximizing Player score = -99999999 for child in legal_moves: val = search(depth - 1, child, False) score = max(score, val) # print(score) # You can also do: # return max(search(depth - 1, child, False) for child in legal_moves) # I'm going to hold off on suggesting list comprehensions in general, # since they're a matter of taste anyway. In this particular case they're # handy since you don't have to figure out an initial score – *unless* # the code should work when there aren't any legal moves, in which case # you need the -99999999 somewhere anyway, or else return None and # modify the caller to handle this. return score elif not my_turn: # Minimizing Player score = 99999999 for child in legal_moves: val = search(depth - 1, child, True) score = min(score, val) return score
def search(depth, board, myTurn): """ Search the tree until depth 'depth' By default, the is the board, and curr_player is whomever called this search Return score >>> search(0, board, True) """ columns = board.shape[1] # enumerate all possible moves from this board legal_moves = [] for column in range(columns): # if column i is a legal move if isLegalMove(column, board): # make the move in column i for curr_player temp = makeMove(column, board, myTurn) # print(temp) # create list of matrix legal_moves.append(temp) ### BASECASE (if depth == 0, game tied or someone wins) if depth == 0 or len(legal_moves) == 0 or winning(board)[0]: # return value(board, curr_player) from winning return scoring(board, myTurn) #*(depth+1) #### RECURSION elif myTurn: #Maximizing Player score = -99999999 for child in legal_moves: val = search(depth - 1, child, False) score = max(score, val) # print(score) return score elif not myTurn: #Minimizing Player score = 99999999 for child in legal_moves: val = search(depth - 1, child, True) score = min(score, val) return score
pygame.draw.circle(screen, color, pos, 40) # Initializing and printing board gameboard = Board(7, 6) print(gameboard.matrix) # Set colors and dimensions of board background_color = (255, 255, 255) # white width, height = 1000, 600 # screen dimensions for connect 4 # width, height = 500, 400 #screen dimensions for connect 3 black = (0, 0, 0) player = 1 # Player 1 ges to start first Time = 0 # Timer Variable for delay # winning function to check if game has been won win = winning.winning(gameboard.matrix) # Initializing game environment pygame.init() screen = pygame.display.set_mode((width, height)) pygame.display.set_caption('Connect 4') # screen.fill(background_color) intro = True # Intro screen running = False # Main game screen endscreen = False # End game screen "Intro display renders the text center of page" while intro: screen.fill(black) font = pygame.font.SysFont("Lucida Sans Typewriter", 13)
continue pygame.draw.circle(screen, color, pos, 40) ### Initializing and printing board gameboard = Board(7, 6) print(gameboard.matrix) ### Set colors and dimensions of board background_color = (255, 255, 255) #white width, height = 1000, 600 #screen dimensions for connect 4 #width, height = 500, 400 #screen dimensions for connect 3 black = (0, 0, 0) player = 1 #Player 1 ges to start first Time = 0 #Timer Variable for delay win = winning.winning( gameboard.matrix) #winning function to check if game has been won ### Initializing game environment pygame.init() screen = pygame.display.set_mode((width, height)) pygame.display.set_caption('Connect 4') #screen.fill(background_color) intro = True #Intro screen running = False #Main game screen endscreen = False #End game screen "Intro display renders the text center of page" while intro: screen.fill(black) font = pygame.font.SysFont("Lucida Sans Typewriter", 13)