溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

minimax算法怎么在python中使用

發(fā)布時(shí)間:2021-03-24 16:00:41 來(lái)源:億速云 閱讀:367 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)minimax算法怎么在python中使用,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

完整代碼

from functools import wraps
import time
import csv


#1.初始化棋盤
#------------
def init_board():
  '''
  初始化棋盤
  
  棋盤規(guī)格 15*15
  
  如下所示:
  board = [[. . . . . . . . . . . . . . .],
       [. . . . . . . . . . . . . . .],
       [. . . . . . . . . . . . . . .],
       [. . . . . . . . . . . . . . .],
       [. . . . . . . . . . . . . . .],
       [. . . . . . . . . . . . . . .],
       [. . . . . . . . . . . . . . .],
       [. . . . . . . . . . . . . . .],
       [. . . . . . . . . . . . . . .],
       [. . . . . . . . . . . . . . .],
       [. . . . . . . . . . . . . . .],
       [. . . . . . . . . . . . . . .],
       [. . . . . . . . . . . . . . .],
       [. . . . . . . . . . . . . . .],
       [. . . . . . . . . . . . . . .]]

  其中: 
    . – 未被占用 
    X – 被黑棋占用 
    O – 被白棋占用
  '''
  print('Init board...')
  time.sleep(0.5)
  n = 15
  board = [['.' for _ in range(n)] for _ in range(n)]
  
  return board

#2.確定玩家,執(zhí)黑先走
#--------------------
def get_player():
  '''
  人類玩家選擇棋子顏色(黑'X'先走)
  '''
  humancolor = input("Enter your color. (ex. 'X' or 'O'):").upper()
  computercolor = ['X', 'O'][humancolor == 'X']
  
  return computercolor, humancolor

#3.進(jìn)入循環(huán)
#----------

#4.打印棋盤、提示走子
#------------------------------
def print_board(board): #ok
  '''
  打印棋盤、比分
  
  開(kāi)局:
   1 2 3 4 5 6 7 8 9 a b c d e f
  1 . . . . . . . . . . . . . . .
  2 . . . . . . . . . . . . . . .
  3 . . . . . . . . . . . . . . .
  4 . . . . . . . . . . . . . . .
  5 . . . . . . . . . . . . . . .
  6 . . . . . . . . . . . . . . .
  7 . . . . . . . . . . . . . . .
  8 . . . . . . . . . . . . . . .
  9 . . . . . . . . . . . . . . .
  a . . . . . . . . . . . . . . .
  b . . . . . . . . . . . . . . .
  c . . . . . . . . . . . . . . .
  d . . . . . . . . . . . . . . .
  e . . . . . . . . . . . . . . .
  f . . . . . . . . . . . . . . .
  '''
  axises = list('123456789abcdef')
  print(' ', ' '.join(axises))
  for i, v in enumerate(axises): 
    print(v, ' '.join(board[i]))

#5.思考走法、放棄終止
#--------------------
def get_human_move(board, color): #ok
  '''
  取人類玩家走法
  '''
  giveup = True # 放棄標(biāo)志
  
  legal_moves = _get_legal_moves(board, color)
  
  #print(','.join([translate_move(move) for move in legal_moves]), len(legal_moves))
  
  while True:
    _move = input("Enter your move.(ex.'cd' means row=c col=d): ").lower()
    
    move = translate_move(_move)
    
    if move in legal_moves:
      giveup = False # 不放棄
      break
  
  return move, giveup

def _get_all_lianxin(board, move, color): #ok
  '''
  取當(dāng)前點(diǎn)位落子后連星
  
  1.按照棋盤兩連、三連、四連的個(gè)數(shù) double triple quadra penta
  '''
  n = len(board)
  uncolor = ['X', 'O'][color == 'X'] # 反色
  
  lianxin = [] # 連星數(shù),len(lianxin) == 4
  
  directions = ((0,1),(1,0),(1,1),(1,-1)) # 東, 南, 東南, 西南
  for direction in directions:
    dr, dc = direction   # 步幅
    #r, c = move # 起點(diǎn)
    
    count = 1         # 連星數(shù),算上起點(diǎn)(落子位置)
    jump_count = [0, 0]    # 順、反方向跳開(kāi)一個(gè)空格之后的連星數(shù)
    jump_flag = [False, False] # 順、反方向跳開(kāi)一個(gè)空格的標(biāo)志
    block = [False, False]   # 順、反方向是否堵死
    #name = ['','']
    
    for i,v in enumerate([1, -1]): # 順、反方向分別用1、-1表示
      dr, dc = v*dr, v*dc      # 步幅
      r, c = move[0]+dr, move[1]+dc # 先走一步
      while True:
        if not _is_on_board(board, [r, c]) or board[r][c] == uncolor: # 不在棋盤內(nèi),或?qū)Ψ狡遄?
          block[i] = True # 被堵死
          break
        if board[r][c] == '.':                           # 為空
          if not _is_on_board(board, [r+dr, c+dc]) or board[r+dr][c+dc] != color: # 且下一格,不在棋盤內(nèi)、或者非己方棋子
            break
          if jump_flag[i] == True: # 前面已經(jīng)跳了一格了,則終止
            break        # 能力所限,不考慮又跳一格的情況!?。?
          else:
            jump_flag[i] = True
        elif board[r][c] == color:
          if jump_flag[i] == True:
            jump_count[i] += 1
          else:
            count += 1
        
        r, c = r + dr, c + dc # 步進(jìn)
        
      
    
    lianxin.append([count, jump_count, block])
  
  return lianxin
  
def _move_score(board, move): #ok
  '''
  對(duì)該落子位置“打分”
  
  這個(gè)邏輯太復(fù)雜了,代碼又長(zhǎng)又臭!!暫時(shí)不考慮簡(jiǎn)化
  
  棋型分值:
  0.活五   +100000
  1.死五 +100000
  2.活四   +10000
  3.死四   +1000
  4.活三   +1000
  5.死三   +100
  6.活二   +100
  7.死二   +10
  8.活一   +10
  9.死一 +2
  
  特別說(shuō)明:
  10.跳N  兩邊棋型分相加 * 上一級(jí)分值的20% ?商榷
  
  
  lianxin == [[2,[0,0],[True,False]],
        [1,[0,0],[True,False]],
        [3,[1,0],[False,False]],
        [3,[2,1],[True,False]]]
  '''
  #      死一, 活一, 死二, 活二, 死三, 活三, 死四, 活四, 死五, 活五
  scores = [   2,  10,  10,  100,  100, 1000, 1000, 10000,100000,100000]
  sum_score = 0
  for color in ['X','O']:
    for lianxin in _get_all_lianxin(board, move, color):
      count, jump_count, block = lianxin
      if jump_count[0] > 0 and jump_count[1] > 0: # 情況一:兩邊跳
        if block[0] == True and block[1] == True:
          if count + jump_count[0] + jump_count[1] + 2 < 5: continue
        else:
          # 這邊跳了
          if block[0] == True: # 有跳的,先把分?jǐn)?shù)加了再說(shuō)(查表加分)
            sum_score += scores[jump_count[0]*2-2] # 加死的分
            sum_score += min(scores[(jump_count[0]+count)*2-2] * 0.2, 200) # 上一級(jí)的20%
          else:
            sum_score += scores[jump_count[0]*2-1] # 加活的分
            sum_score += min(scores[(jump_count[0]+count)*2-1] * 0.2, 200) # 上一級(jí)的20%
          
          # 這邊也跳了
          if block[1] == True: # 有跳的,先把分?jǐn)?shù)加了再說(shuō)(查表加分)
            sum_score += scores[jump_count[1]*2-2] # 加死的分
            sum_score += min(scores[(jump_count[1]+count)*2-2] * 0.2, 200) # 上一級(jí)的20%
          else:
            sum_score += scores[jump_count[1]*2-1] # 加活的分
            sum_score += min(scores[(jump_count[1]+count)*2-1] * 0.2, 200) # 上一級(jí)的20%
          
          # 中間
          sum_score += scores[count*2-1] # 中間加活的分
        
      elif jump_count[0] > 0 and jump_count[1] == 0: # 情況二:有一邊跳
        if block[0] == True and block[1] == True:
          if count + jump_count[0] + jump_count[1] + 1 < 5: continue
        else:
          # 跳的這邊
          if block[0] == True: # 先把跳那邊的分?jǐn)?shù)加了再說(shuō)(查表加分)
            sum_score += scores[jump_count[0]*2-2] # 加死的分
            sum_score += min(scores[(jump_count[0]+count)*2-2] * 0.2, 200) # 上一級(jí)的20%
          else:
            sum_score += scores[jump_count[0]*2-1] # 加活的分
            sum_score += min(scores[(jump_count[0]+count)*2-1] * 0.2, 200) # 上一級(jí)的20%
          
          # 沒(méi)跳的那邊
          if block[1] == True:
            sum_score += scores[count*2-2] # 加死的分
          else:
            sum_score += scores[count*2-1] # 加活的分
          
      elif jump_count[1] > 0 and jump_count[0] == 0: # 情況三:另一邊跳
        if block[0] == True and block[1] == True:
          if count + jump_count[0] + jump_count[1] + 1 < 5: continue
        else:
          # 跳的這邊
          if block[1] == True: # 先把跳那邊的分?jǐn)?shù)加了再說(shuō)(查表加分)
            sum_score += scores[jump_count[1]*2-2] # 加死的分
            sum_score += min(scores[(jump_count[1]+count)*2-2] * 0.2, 200) # 上一級(jí)的20%
          else:
            sum_score += scores[jump_count[1]*2-1] # 加活的分
            sum_score += min(scores[(jump_count[1]+count)*2-1] * 0.2, 200) # 上一級(jí)的20%
          
          # 沒(méi)跳的那邊
          if block[0] == True:
            sum_score += scores[count*2-2] # 加死的分
          else:
            sum_score += scores[count*2-1] # 加活的分
          
      elif jump_count[0] == 0 and jump_count[1] == 0: # 情況四:兩邊都沒(méi)跳
        if block[0] and block[1]: # 兩邊都堵死了
          if count == 5: # 等于5才加,否則不加
            sum_score += scores[count*2-2] # -1,-2一樣
        elif block[0] or block[1]: # 只堵死一邊
          sum_score += scores[count*2-2] # 加死的分
        else:
          sum_score += scores[count*2-1] # 加活的分

  return sum_score
  
def _get_center_enmpty_points(board): #ok
  '''
  取中心點(diǎn)附近的空位
  
  從中心點(diǎn)逐圈順時(shí)針掃描,若連續(xù)兩圈未有棋子,則停止
  '''
  n = len(board)
  
  center_point = [n//2, n//2] # 中心點(diǎn)[7,7],即'88'
  
  c1 = 0   # 空圈計(jì)數(shù)
  legal_moves = [] # 保存空位
  for i in range(8): #從內(nèi)到外掃描8圈
    c2 = True  # 空圈標(biāo)志
    
    if i == 0:
      points = [[n//2, n//2]]
    else:
      # points = [第7-i行] + [第7+i列] + [第7+i行] + [第7-i列] # 從左上開(kāi)始,順時(shí)針一圈
      points = [[7-i,c] for c in range(7-i,7+i)] + \
           [[r,7+i] for r in range(7-i,7+i)] + \
           [[7+i,c] for c in range(7+i,7-i,-1)] + \
           [[r,7-i] for r in range(7+i,7-i,-1)]
           
    for point in points:
      if board[point[0]][point[1]] == '.': # 遇到空位,則
        legal_moves.append(point) # 保存點(diǎn)位
      else:
        c2 = False        # 此圈非空
    
    if c2 == True: # 若此圈為空,空圈計(jì)數(shù)器加1
      c1 += 1
      if c1 == 2: break
    else:    # 否則,清零
      c1 = 0
  
  return legal_moves # 越前,棋盤點(diǎn)位分值越高!

def minimax(board, color, maximizingPlayer, depth):
  '''
  極大極小算法
  
  其中:
  maximizingPlayer = True #己方
  
  用例:
  _, move = minimax(board, 'X', True, 4) # 假設(shè)計(jì)算機(jī)執(zhí)黑'X'
  
  #參見(jiàn): https://en.wikipedia.org/wiki/Minimax
  function minimax(node, depth, maximizingPlayer) is
    if depth = 0 or node is a terminal node then
      return the heuristic value of node
    if maximizingPlayer then
      value := ?∞
      for each child of node do
        value := max(value, minimax(child, depth ? 1, FALSE))
      return value
    else (* minimizing player *)
      value := +∞
      for each child of node do
        value := min(value, minimax(child, depth ? 1, TRUE))
      return value

  (* Initial call *)
  minimax(origin, depth, TRUE)
  '''
  pass
  
def get_computer_move(board, color):
  '''
  取計(jì)算機(jī)玩家走法
  
  計(jì)算機(jī)走子策略:
    1.對(duì)所有合法的落子位置逐個(gè)“打分”(如何“打分”,決定了計(jì)算機(jī)下棋的水平)
    2.取所有分值最高的落子位置
  '''
  print('Computer is thinking...', end='')
  legal_moves = _get_legal_moves(board, color)
  
  scores = [_move_score(board, move) for move in legal_moves]
  
  max_score = max(scores) # 最高分值
  best_move = legal_moves[scores.index(max_score)]
  
  print("'{}'".format(translate_move(best_move)))
  return best_move
  
def  _is_legal_move(board, move): #ok
  '''
  判斷落子位置是否合法
  
  說(shuō)明:只要在棋盤內(nèi),且為空,即合法
  
  '''
  if _is_on_board(board, move) and board[move[0]][move[1]] == '.': 
    return True
  
  return False
  
def  _get_legal_moves(board, color): #ok
  '''
  取當(dāng)前顏色棋子所有的合法走法
  
  返回格式:[[x1,y1], [x2,y2], ...]
  '''
  legal_moves = _get_center_enmpty_points(board)
  
  return legal_moves
  
def _is_on_board(board, move): #ok
  '''
  判斷點(diǎn)位是否在棋盤范圍內(nèi)
  '''
  n = len(board)
  
  return move[0] in range(n) and move[1] in range(n)
  
def translate_move(move): #ok
  '''
  轉(zhuǎn)換坐標(biāo)
  
  如'1a'可轉(zhuǎn)換為[0,9];又如[9,10]轉(zhuǎn)換為'ab'
  
  此函數(shù),只是為了方便,不是必要的
  '''
  axises = list('123456789abcdef')

  if type(move) is str: # 如'cd'
    row = axises.index(move[0]) 
    col = axises.index(move[1])
    _move = [row, col] # 得[2,3]
  elif type(move) is list: # 如[2,3]
    row = axises[move[0]]
    col = axises[move[1]]
    _move = '{}{}'.format(row, col) # 得'cd'

  return _move
  
#6.落子
#----------
def do_move(board, move, color): #ok
  '''
  在當(dāng)前位置落子
  '''
  assert board[move[0]][move[1]] == '.'
  
  board[move[0]][move[1]] = color
 
#7.判斷局面、是否終止
#------------------------------
def check_board(board, color): #ok
  '''
  檢查棋盤
  
  返回:是否勝利
  '''
  n = len(board)
  
  directions = ((0,1),(1,0),(1,1),(1,-1)) # 東, 南, 東南, 西南
  # 四個(gè)搜索方向的起點(diǎn)(坐標(biāo)),分四組。
  # 形如:[[第1列的點(diǎn)], [第1行的點(diǎn)], [第1列+第1行的點(diǎn)], [第1行+第n列的點(diǎn)]]
  all_start_points = [[[i, 0] for i in range(n)],
            [[0, j] for j in range(n)],
            [[i, 0] for i in range(n-4)] + [[0, j] for j in range(1,n-4)], # 排除了長(zhǎng)度小于5,及重復(fù)的情況
            [[0, j] for j in range(4,n)] + [[i, n-1] for i in range(1,n-4)]]
  
  for direction, start_points in zip(directions, all_start_points):
    dr, dc = direction   # 步幅
    for start_point in start_points:
      r, c = start_point # 起點(diǎn)
      count = 0
      while _is_on_board(board, [r, c]):
        if board[r][c] == color:
          count += 1
          if count == 5:
            return True
        else:
          count = 0
        
        r, c = r + dr, c + dc # 步進(jìn)
  
  return False

def check_board__(board, color): # 廢棄!
  '''
  檢查棋盤 (不如上面的方式簡(jiǎn)潔)
  
  返回 是否勝利
  '''
  n = len(board)
  uncolor = ['X', 'O'][color == 'X'] # 反色
  
  # 1.行搜索
  for i in range(n):
    count = 0
    for j in range(n):
      if board[i][j] == color:
        count += 1
        if count == 5:
          return True # 'Winner is ' + color
      elif board[i][j] == uncolor:
        count = 0
  
  # 2.列搜索
  for j in range(n):
    count = 0
    for i in range(n):
      if board[i][j] == color:
        count += 1
        if count == 5:
          return True # 'Winner is ' + color
      elif board[i][j] == uncolor:
        count = 0
  
  # 3.斜搜索k=1左上右下
  #3.a.k=1對(duì)角線上方
  for j in range(n-4):   # 終止列n-4
    count = 0
    for i in range(n-j): # 終止行n-j
      if board[i][j+i] == color:
        count += 1
        if count == 5:
          return True
      elif board[i][j+i] == uncolor:
        count = 0
        
  #3.b.k=1對(duì)角線下方
  for i in range(1, n-4):   # 終止行n-4
    count = 0
    for j in range(n-i): # 終止列n-i
      if board[i+j][j] == color:
        count += 1
        if count == 5:
          return True
      elif board[i+j][j] == uncolor:
        count = 0
        
  # 4.斜搜索k=-1左下右上
  #4.a.k=-1對(duì)角線下方
  for j in range(n-4):   # 終止列n-4
    count = 0
    for i in range(n-j): # 終止行n-j
      if board[n-i-1][j+i] == color:
        count += 1
        if count == 5:
          return True
      elif board[n-i-1][j+i] == uncolor:
        count = 0
  
  #4.b.k=-1對(duì)角線上方
  for j in range(4, n):
    count = 0
    for i in range(n-1):
      if board[i][j-i] == color:
        count += 1
        if count == 5:
          return True
      elif board[i][j-i] == uncolor:
        count = 0
  return False
  



#8.游戲結(jié)束,返回信息
#--------------------

  
def logging(func): #ok
  '''
  記錄游戲相關(guān)信息 (裝飾器)
  
  包括:
  開(kāi)始時(shí)間、比賽耗時(shí)、棋盤大小、黑棋玩家、白棋玩家、游戲比分、本局棋譜
  
  保存到reversi.csv文件
  '''
  @wraps(func)
  def wrap(*args, **kwargs):
    try:
      start = time.strftime("%Y%m%d %H:%M:%S", time.localtime()) # 開(kāi)始時(shí)間
      
      t1 = time.time()
      info = func(*args,**kwargs) # 棋盤大小、黑棋玩家、白棋玩家、游戲比分、本局棋譜(主程序)
      t2 = time.time()
      t = int(t2 - t1) # 比賽耗時(shí)
      
      line = [start, t, *info]
      
      with open('gobang.csv', 'a') as f:
        writer = csv.writer(f, lineterminator='\n')
        writer.writerow(line) # 寫入
    except Exception as e:
      pass
  
  return wrap

#==========================================
# 主函數(shù)
#==========================================
#@logging
def main(): #ok
  '''
  主程序

  人機(jī)對(duì)戰(zhàn)

  流程:
  1.初始化棋盤
  2.確定棋手,黑先
  3.進(jìn)入循環(huán)
   4.打印棋盤,提示走子
   5.思考走法,放棄終止
   6.落子
   7.檢查棋盤,是否終止
   8.切換棋手
  9.游戲結(jié)束,返回信息
  '''
  # 1.初始化棋盤
  board = init_board()
  
  # 2.確定玩家,執(zhí)黑先走
  computer_color, human_color = get_player()
  current_color = 'X'
  
  record = '' # 棋譜,如'X:ab O:aa X:ba ...'
  # 3.進(jìn)入循環(huán)
  while True:
    # 4.打印棋盤、提示走子
    print_board(board)
    print("Now turn to '{}'...".format(current_color))
    
    # 5.思考走法,記錄棋譜
    if current_color == computer_color:
      move = get_computer_move(board, current_color)
    elif current_color == human_color:
      move, giveup = get_human_move(board, current_color)
      if giveup == True: break # 放棄則終止
    
    record = record + ' {}:{}'.format(current_color, translate_move(move)) # 錄入棋譜
    
    # 6.落子
    do_move(board, move, current_color)
    
    # 7.判斷局面
    done = check_board(board, current_color) # 返回終止標(biāo)志
    
    # 7_1.終止
    if done == True:
      print_board(board)
      print("Game over! Winner is '{}'".format(current_color))
      break
    
    # 8.切換棋手
    current_color = ['X', 'O'][current_color == 'X']


#測(cè)試
def test_get_center_enmpty_points():
  '''
  #     1 2 3 4 5 6 7 8 9 a b c d e f
  board = [[. . . . . . . . . . . . . . .],#1
       [. . . . . . . . . . . . . . .],#2
       [. . . . . . . . . . . . . . .],#3
       [. . . . . . . . . . . . . . .],#4
       [. . . . . . . . . . . . . . .],#5
       [. . . . . . . . . . . . . . .],#6
       [. . . . . . . . . . . . . . .],#7
       [. . . . . . . . . . . . . . .],#8
       [. . . . . . . . . . . . . . .],#9
       [. . . . . . . . . . . . . . .],#a
       [. . . . . . . . . . . . . . .],#b
       [. . . . . . . . . . . . . . .],#c
       [. . . . . . . . . . . . . . .],#d
       [. . . . . . . . . . . . . . .],#e
       [. . . . . . . . . . . . . . .]]#f
       
  #     1 2 3 4 5 6 7 8 9 a b c d e f
  board = [[. . . . . . . . . . . . . . .],#1
       [. . . . . . . . . . . . . . .],#2
       [. . . . . . . . . . . . . . .],#3
       [. . . . . . . . . . . . . . .],#4
       [. . . . . . . . . X . . . . .],#5
       [. . . . . . X . . . . . . . .],#6
       [. . . . . O . . X O . . . . .],#7
       [. . . . . X X O X . . . . . .],#8
       [. . . . . X O X . . . . . . .],#9
       [. . . . . . . . . . X . . . .],#a
       [. . . X . . . . . . . . . . .],#b
       [. . X . . . . . . . . . . . .],#c
       [. O . . . . . . . . . . . . .],#d
       [. . . . . . . . . . . . . . .],#e
       [. . . . . . . . . . . . . . .]]#f
  '''
  print('Testing _get_center_enmpty_points()...')
  
  #     1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
  board = [['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#1
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#2
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#3
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#4
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#5
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#6
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#7
       ['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#8
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#9
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#a
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#b
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#c
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#d
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#e
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.']]#f
  empty_points = _get_center_enmpty_points(board)
  
  translate_points = [translate_move(move) for move in empty_points]
  #print(translate_points)
  assert translate_points == ['77','78','79','89','99','98','97','87', '66','67','68','69','6a','7a','8a','9a','aa','a9','a8','a7','a6','96','86','76']
  
  #     1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
  board = [['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#1
       ['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#2
       ['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#3
       ['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#4
       ['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#5
       ['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#6
       ['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#7
       ['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#8
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#9
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#a
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#b
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#c
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#d
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#e
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.']]#f
  empty_points = _get_center_enmpty_points(board)
  
  translate_points = [translate_move(move) for move in empty_points]
  print(translate_points)
  assert '11' in translate_points
  
  #     1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
  board = [['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#1
       ['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#2
       ['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#3
       ['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#4
       ['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#5
       ['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#6
       ['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#7
       ['.','.','.','.','.','.','.','X','.','.','.','.','.','.','.'],#8
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#9
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#a
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#b
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#c
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#d
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#e
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.']]#f
  empty_points = _get_center_enmpty_points(board)
  
  translate_points = [translate_move(move) for move in empty_points]
  print(translate_points)
  assert '11' in translate_points
  
  print('ok')
  
def test_move_score():
  '''
  _move_score(board, move, color)
  #     1 2 3 4 5 6 7 8 9 a b c d e f
  board = [[. . . . . . . . . . . . . . .],#1
       [. . . . . . . . . . . . . . .],#2
       [. . . . . . . . . . . . . . .],#3
       [. . . . . . . . . . . . . . .],#4
       [. . . . . . . . . . . . . . .],#5
       [. . . . . . . . . . . . . . .],#6
       [. . . . . . . . . . . . . . .],#7
       [. . . . . . . . . . . . . . .],#8
       [. . . . . . . . . . . . . . .],#9
       [. . . . . . . . . . . . . . .],#a
       [. . . . . . . . . . . . . . .],#b
       [. . . . . . . . . . . . . . .],#c
       [. . . . . . . . . . . . . . .],#d
       [. . . . . . . . . . . . . . .],#e
       [. . . . . . . . . . . . . . .]]#f
       
  #     1 2 3 4 5 6 7 8 9 a b c d e f
  board = [[. . . . . . . . . . . . . . .],#1
       [. . . . . . . . . . . . . . .],#2
       [. . . . . . . . . . . . . . .],#3
       [. . . . . . . . . . . . . . .],#4
       [. . . . . . . . . X . . . . .],#5
       [. . . . . . X . . . . . . . .],#6
       [. . . . . O . . X O . . . . .],#7
       [. . . . . X X O X . . . . . .],#8
       [. . . . . X O X . . . . . . .],#9
       [. . . . . . . . . . X . . . .],#a
       [. . . X . . . . . . . . . . .],#b
       [. . X . . . . . . . . . . . .],#c
       [. O . . . . . . . . . . . . .],#d
       [. . . . . . . . . . . . . . .],#e
       [. . . . . . . . . . . . . . .]]#f
  '''
  print('Testing _move_score()...')
  
  #     1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
  board = [['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#1
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#2
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#3
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#4
       ['.','.','.','.','.','.','.','.','.','X','.','.','.','.','.'],#5
       ['.','.','.','.','.','.','X','.','.','.','.','.','.','.','.'],#6
       ['.','.','.','.','.','O','.','.','X','O','.','.','.','.','.'],#7
       ['.','.','.','.','.','X','X','O','X','.','.','.','.','.','.'],#8
       ['.','.','.','.','.','X','O','X','.','.','.','.','.','.','.'],#9
       ['.','.','.','.','.','.','.','.','.','.','X','.','.','.','.'],#a
       ['.','.','.','X','.','.','.','.','.','.','.','.','.','.','.'],#b
       ['.','.','X','.','.','.','.','.','.','.','.','.','.','.','.'],#c
       ['.','O','.','.','.','.','.','.','.','.','.','.','.','.','.'],#d
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#e
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.']]#f
  #[count, jump_count, block] # 東, 南, 東南, 西南
  lianxin = _get_all_lianxin(board, [6,7], 'X')
  #print(lianxin)
  assert lianxin == [[2,[0,0],[True,False]],
            [1,[0,0],[True,False]],
            [3,[1,0],[False,False]],
            [3,[2,1],[True,False]]]
  #      死一, 活一, 死二, 活二, 死三, 活三, 死四, 活四, 死五, 活五
  scores = [   2,  10,  10,  100,  100, 1000, 1000, 10000,100000,100000]
  assert _move_score(board, [6,7], 'X') == 10 + 2 + (1000 + 10 + 200) + (1000 + 10 + 10 + 200 + 200)
  
  print('ok')
  
def test_get_all_lianxin():
  '''
  get_all_lianxin(board, move, color)
  #     1 2 3 4 5 6 7 8 9 a b c d e f
  board = [[. . . . . . . . . . . . . . .],#1
       [. . . . . . . . . . . . . . .],#2
       [. . . . . . . . . . . . . . .],#3
       [. . . . . . . . . . . . . . .],#4
       [. . . . . . . . . . . . . . .],#5
       [. . . . . . . . . . . . . . .],#6
       [. . . . . . . . . . . . . . .],#7
       [. . . . . . . . . . . . . . .],#8
       [. . . . . . . . . . . . . . .],#9
       [. . . . . . . . . . . . . . .],#a
       [. . . . . . . . . . . . . . .],#b
       [. . . . . . . . . . . . . . .],#c
       [. . . . . . . . . . . . . . .],#d
       [. . . . . . . . . . . . . . .],#e
       [. . . . . . . . . . . . . . .]]#f
       
  #     1 2 3 4 5 6 7 8 9 a b c d e f
  board = [[. . . . . . . . . . . . . . .],#1
       [. . . . . . . . . . . . . . .],#2
       [. . . . . . . . . . . . . . .],#3
       [. . . . . . . . . . . . . . .],#4
       [. . . . . . . . . X . . . . .],#5
       [. . . . . . X . . . . . . . .],#6
       [. . . . . O . . X O . . . . .],#7
       [. . . . . X X O X . . . . . .],#8
       [. . . . . X O X . . . . . . .],#9
       [. . . . . . . . . . X . . . .],#a
       [. . . X . . . . . . . . . . .],#b
       [. . X . . . . . . . . . . . .],#c
       [. O . . . . . . . . . . . . .],#d
       [. . . . . . . . . . . . . . .],#e
       [. . . . . . . . . . . . . . .]]#f
  '''
  print('Testing _get_all_lianxin()...')
  #     1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
  board = [['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#1
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#2
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#3
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#4
       ['.','.','.','.','.','.','.','.','.','X','.','.','.','.','.'],#5
       ['.','.','.','.','.','.','X','.','.','.','.','.','.','.','.'],#6
       ['.','.','.','.','.','O','.','.','X','O','.','.','.','.','.'],#7
       ['.','.','.','.','.','X','X','O','X','.','.','.','.','.','.'],#8
       ['.','.','.','.','.','X','O','X','.','.','.','.','.','.','.'],#9
       ['.','.','.','.','.','.','.','.','.','.','X','.','.','.','.'],#a
       ['.','.','.','X','.','.','.','.','.','.','.','.','.','.','.'],#b
       ['.','.','X','.','.','.','.','.','.','.','.','.','.','.','.'],#c
       ['.','O','.','.','.','.','.','.','.','.','.','.','.','.','.'],#d
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],#e
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.']]#f
  #[count, jump_count, block] # 東, 南, 東南, 西南
  lianxin = _get_all_lianxin(board, [6,7], 'X')
  #print(lianxin)
  assert lianxin == [[2,[0,0],[True,False]],
            [1,[0,0],[True,False]],
            [3,[1,0],[False,False]],
            [3,[2,1],[True,False]]]
  
  #     1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
  board = [['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'], #1
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'], #2
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'], #3
       ['.','.','.','.','.','.','.','.','.','X','.','.','.','.','.'], #4
       ['.','.','.','.','.','.','X','.','.','.','.','.','.','.','.'], #5
       ['.','.','.','.','.','O','.','.','X','O','.','.','.','.','.'], #6
       ['.','.','.','.','.','X','X','O','X','.','.','.','.','.','.'], #7
       ['.','.','.','.','.','X','O','X','.','.','.','.','.','.','.'], #8
       ['.','.','.','.','.','.','.','.','.','.','X','.','.','.','.'], #9
       ['.','.','.','X','.','.','.','.','.','.','.','.','.','.','.'], #a
       ['.','.','X','.','.','.','.','.','.','.','.','.','.','.','.'], #b
       ['.','O','.','.','.','.','.','.','.','.','.','.','.','.','.'], #c
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'], #d
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'], #e
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.']] #f
  #[count, jump_count, block] # 東, 南, 東南, 西南
  lianxin = _get_all_lianxin(board, [5,7], 'X')
  #print(lianxin)
  assert lianxin == [[2,[0,0],[True,False]],
            [1,[0,0],[True,False]],
            [3,[1,0],[False,False]],
            [3,[2,1],[True,False]]]
  
  
  print('ok')
  
def test_check_board():
  '''
  
  '''
  print('Testing check_board()...')
  board = [['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.']]
  assert check_board(board, 'X') == False
  
  board = [['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['X','X','X','X','X','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.']]
  assert check_board(board, 'X') == True
  
  board = [['X','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['X','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['X','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['X','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['X','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.']]
  assert check_board(board, 'X') == True

  board = [['.','.','.','.','.','.','.','.','.','.','X','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','X','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','X','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','X','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','X'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.']]
  assert check_board(board, 'X') == True
  
  board = [['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['X','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','X','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','X','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','X','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','X','.','.','.','.','.','.','.','.','.','.']]
  assert check_board(board, 'X') == True
  
  board = [['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','X'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','X','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','X','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','X','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','X','.','.','.','.']]
  assert check_board(board, 'X') == True
  
  board = [['.','.','.','.','X','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','X','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','X','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','X','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['X','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'],
       ['.','.','.','.','.','.','.','.','.','.','.','.','.','.','.']]
  assert check_board(board, 'X') == True
  
  print('ok')
  
if __name__ == '__main__':
  main()
  #test_check_board()
  #test_get_all_lianxin()
  #test_move_score()
  #test_get_center_enmpty_points()

上述就是小編為大家分享的minimax算法怎么在python中使用了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI