def count_minmax(board, player, level) possible moves = get_legal_moves(board,player) if possible moves length==0 return -1 # pass else max_score = -1000 min_score = 1000 boards = [] for square in possible moves temp_board = clone of current board make the move to square in temp_board, color in the surrounded players add this temp_board to the list of possible board states resulting from each of the possible moves end Look at each of the possible boards if the recursive level >= $maxlevel evaluate the current board based on the current player if level % 2 == 0 If player is "max" if score > max_score max_square is the square leading to this current board state max_score is score of the evaluated board (for example, player1 - player2) end else if score < min_score If player is "min" max_square is the square leading to this current board state max_score and min_score are each the score of the evaluated board min_score = score end end else If you are not at the deepest recursive level, get a list of scores generated by a recursive call: recursive call to find the score of next board generated... score = count_minmax(board, nextplayer, level+1) look at the score... if level % 2 == 0 <--- If you are "max" if score != nil and score > max_score max_score = score max_square is the square leading to this board state new_max = max_score end else If you are "min" if score != nil and score < min_score min_score and max_score = score max_square is the square leading to this board state new_max=max_score end end end end return the max_score or a list with the score and max_square end end def pick_minmax(board, player) possible moves = get_legal_moves(board,player) level = 0 if the list of possible moves has length 0 return -1 # pass else max_score = -1000 for each square in the list of possible moves temp_board = clone of the current board bracket(temp_board[index], player, square) end for each of the possible boards generated from the possible moves call the recursive function, get the score of the possible board score = count_minmax(el, nextplayer, level+1) if score != nil and score > max_score max_score = score max_square = square used to get this current board end end return max_square end end