La logique de mise en œuvre du jeu d'échecs en trois pièces est la suivante :
1. 🎜🎜#2. Le joueur exécute la pièce en T, déplacez la pièce en premier
3. Déterminez le résultat [gagner, perdre, faire match nul], si le résultat n'est pas décidé, continuez comme suit ; . L'ordinateur tient la pièce en T, et déplace la pièce ; #🎜🎜 #5. Si le résultat n'est pas décidé, continuez à partir de l'étape 2.
2. Étapes de base #🎜. 🎜#
1. Interface du menu
def menu(): print('-'*20) print('1---------------begin') print('2---------------exit') print('please select begin or exit') print('-' * 20) while(1): select = input('please input:') if select == '1': begin_games() pass elif select == '2': print('exit the game') break #pass pass
chess_board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Alors comment imprimer cette liste de rangement et la transformer en échiquier ?
def init_cheaa_board(chess_board): #先对列表进行初始化 for i in range(MAX_ROW): for j in range(MAX_COL): chess_board[i][j] = ' ' pass def print_chess_board(chess_board): #棋盘打印 print('*'+'-'*7+'*'+'-'*7+'*'+'-'*7+'*') for i in range(MAX_ROW): print('|'+' '*3+chess_board[i][0]+' '*3+'|'+' '*3+chess_board[i][1]+' '*3+'|'+' '*3+chess_board[i][2]+' '*3+'|') print('*' + '-' * 7 + '*' + '-' * 7 + '*' + '-' * 7 + '*') pass pass
def player_first(chess_board): while(1): x = int(input('please input x:')) y = int(input('please input y:')) if(chess_board[x][y] != ' '): #若已被置子,则重新选择坐标 print('This position is already occupied!') pass elif(x >= MAX_ROW or y >= MAX_COL or x < 0 or y < 0): #所选坐标超出棋盘范围,重新选择坐标 print('This position is beyond the chessboard!') pass else: #若坐标可以落子,则将该坐标置为玩家的棋子U chess_board[x][y] = 'U' print_chess_board(chess_board) #return x,y break pass pass
Algorithme de déplacement de l'ordinateur :
4.2 Si 4.1 n'est pas satisfait, vérifiez à nouveau l'échiquier pour voir s'il y a déjà deux joueurs sur l'échiquier ; . L'état dans lequel les pièces sont connectées et sur le point de devenir une pièce d'échec. S'il existe déjà, récupérez le point de coordonnées où le joueur est sur le point de gagner, et déplacez le T pour intercepter
A. Déterminez d'abord si la position centrale [1][1] est occupée. Sinon, c'est le point le plus avantageux. Lorsque le point [1][1] est occupé, les quatre lignes horizontales, verticales, diagonales et sous-diagonales du joueur sont bloquées
B, le point avantageux secondaire est les quatre lignes de l'échiquier 3*3 Chacune ; le coin occupé bloquera les trois lignes du joueur ;C Le dernier point avantageux est le centre de chaque côté, qui bloquera les deux lignes du joueur ;
def Intercept_player(chess_board,key): count2 = 0 index2 = [] intercept_index = {'x':-1,'y':-1} for i in range(MAX_ROW): index = [] count = 0 count1 = 0 index1 = [] allindex = [0,1,2] for j in range(MAX_ROW): if(chess_board[i][j] == key): #每一行的玩家落子情况 count += 1 index.append(j) if(chess_board[j][i] == key): #每一列的玩家落子情况 #print('j'+str(j)+',i'+str(i)+'='+chess_board[j][i]) count1 += 1 index1.append(j) if (i == j and chess_board[j][i] == key): # 在主对角线中的玩家落子情况 count2 += 1 index2.append(j) if(count == 2): #在每一行中 获取具体的可以拦截的位置坐标 需要排除掉已经填充的位置 result = list(set(allindex).difference(set(index))) result = result[0] if(chess_board[i][result] == ' '): #当这个位置可以进行拦截时,进行坐标返回 #return i,result intercept_index['x'] = i intercept_index['y'] = result return intercept_index #print(count1,'------->',index1) if (count1 == 2): # 在每一列中 获取具体的可以拦截的位置坐标 需要排除掉已经填充的位置 result = list(set(allindex).difference(set(index1))) result = result[0] #print('count1==2,result:',result) if (chess_board[result][i] == ' '): # 当这个位置可以进行拦截时,进行坐标返回 intercept_index['x'] = result intercept_index['y'] = i return intercept_index #return i, result if (count2 == 2): # 在主对角线上 获取具体的可以拦截的位置坐标 需要排除掉已经填充的位置 result = list(set(allindex).difference(set(index2))) result = result[0] if (chess_board[i][result] == ' '): # 当这个位置可以进行拦截时,进行坐标返回 intercept_index['x'] = i intercept_index['y'] = result return intercept_index #return i, result count3 = 0 if(chess_board[0][2] == key): count3 += 1 if (chess_board[1][1] == key): count3 += 1 if (chess_board[2][0] == key): count3 += 1 if(count3 == 2): if(chess_board[0][2] == ' '): intercept_index['x'] = 0 intercept_index['y'] = 2 elif (chess_board[1][1] == ' '): intercept_index['x'] = 1 intercept_index['y'] = 1 elif (chess_board[2][0] == ' '): intercept_index['x'] = 2 intercept_index['y'] = 0 return intercept_index def computer_second(chess_board): #电脑智能出棋 #1、先检查一下电脑是否两子成棋 若已有,则获取空位置坐标 自己先成棋 intercept_index = Intercept_player(chess_board, 'T') if (intercept_index['x'] == -1 and intercept_index['y'] == -1): pass else: # 电脑可落子 x = intercept_index['x'] y = intercept_index['y'] chess_board[x][y] = 'T' return #2、若玩家快成棋 则先进行拦截 intercept_index = Intercept_player(chess_board,'U') #若玩家已经两子成棋 则获取空位置的坐标 #print('intercept_index---:') #print(intercept_index) if(intercept_index['x'] == -1 and intercept_index['y'] == -1): pass else: #电脑可落子 x = intercept_index['x'] y = intercept_index['y'] chess_board[x][y] = 'T' return #3、如果没有,则电脑端排棋 以促进成棋 #3.1、 占领中心位置 如若中心位置[1,1]未被占领 if(chess_board[1][1] == ' '): chess_board[1][1] = 'T' return #3.2、 占领四角位置 若[0,0] [0,2] [2,0] [2,2]未被占领 if (chess_board[0][0] == ' '): chess_board[0][0] = 'T' return if (chess_board[0][2] == ' '): chess_board[0][2] = 'T' return if (chess_board[2][0] == ' '): chess_board[2][0] = 'T' return if (chess_board[2][2] == ' '): chess_board[2][2] = 'T' return # 3.3、 占领每一边中心位置 若[0,1] [1,0] [1,2] [2,1]未被占领 if (chess_board[0][1] == ' '): chess_board[0][1] = 'T' return if (chess_board[1][0] == ' '): chess_board[1][0] = 'T' return if (chess_board[1][2] == ' '): chess_board[1][2] = 'T' return if (chess_board[2][1] == ' '): chess_board[2][1] = 'T' return
5.
Résultat final : Perdre, gagner, faire match nul D
Processus de détermination : Déterminer s'il y a le joueur U sur chaque ligne horizontale, ligne verticale et ligne diagonale Ou si l'ordinateur T forme trois pièces en une si c'est le cas, ce camp gagne ; lorsque toute la surface d'échecs est occupée mais que ni le joueur ni l'ordinateur n'ont formé une pièce d'échecs, cela signifie un match nul.def chess_board_isfull(chess_board): #判断棋盘是否填充满 for i in range(MAX_ROW): if (' ' in chess_board[i]): return 0 return 1 pass def Win_or_lose(chess_board): isfull = chess_board_isfull(chess_board) for i in range(MAX_ROW): #每一列的判断 if( chess_board[0][i] == chess_board[1][i] == chess_board[2][i]): return chess_board[0][i] pass pass for i in range(MAX_ROW): # 每一行的判断 if( chess_board[i][0] == chess_board[i][1] == chess_board[i][2]): return chess_board[i][0] pass pass if (chess_board[0][0] == chess_board[1][1] == chess_board[2][2]): # 判断棋盘正对角线 return chess_board[0][0] if (chess_board[0][2] == chess_board[1][1] == chess_board[2][0]): # 判断棋盘反对角线 return chess_board[0][2] if isfull: return 'D' # 经过以上的判断,都不满足(既没赢也没输),但是棋盘也已经填充满,则说明和棋 else: return ' '
# coding=utf-8import random MAX_ROW = 3 MAX_COL = 3 #array = ['0','0','0'] chess_board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] #[array] * 3 def init_cheaa_board(chess_board): for i in range(MAX_ROW): for j in range(MAX_COL): chess_board[i][j] = ' ' pass def print_chess_board(chess_board): print('*'+'-'*7+'*'+'-'*7+'*'+'-'*7+'*') for i in range(MAX_ROW): print('|'+' '*3+chess_board[i][0]+' '*3+'|'+' '*3+chess_board[i][1]+' '*3+'|'+' '*3+chess_board[i][2]+' '*3+'|') print('*' + '-' * 7 + '*' + '-' * 7 + '*' + '-' * 7 + '*') pass pass def player_first(chess_board): while(1): x = int(input('please input x:')) y = int(input('please input y:')) if(chess_board[x][y] != ' '): print('This position is already occupied!') pass elif(x >= MAX_ROW or y >= MAX_COL or x < 0 or y < 0): print('This position is beyond the chessboard!') pass else: chess_board[x][y] = 'U' print_chess_board(chess_board) #return x,y break pass pass def chess_board_isfull(chess_board): #判断棋盘是否填充满 for i in range(MAX_ROW): if (' ' in chess_board[i]): return 0 return 1 pass def Win_or_lose(chess_board): isfull = chess_board_isfull(chess_board) for i in range(MAX_ROW): #每一列的判断 if( chess_board[0][i] == chess_board[1][i] == chess_board[2][i]): return chess_board[0][i] pass pass for i in range(MAX_ROW): # 每一行的判断 if( chess_board[i][0] == chess_board[i][1] == chess_board[i][2]): return chess_board[i][0] pass pass if (chess_board[0][0] == chess_board[1][1] == chess_board[2][2]): # 判断棋盘正对角线 return chess_board[0][0] if (chess_board[0][2] == chess_board[1][1] == chess_board[2][0]): # 判断棋盘反对角线 return chess_board[0][2] if isfull: return 'D' # 经过以上的判断,都不满足(既没赢也没输),但是棋盘也已经填充满,则说明和棋 else: return ' ' def computer_second_random(chess_board): #电脑随机出棋 while(1): x = random.randint(0,2) y = random.randint(0,2) if(chess_board[x][y] != ' '): continue else: chess_board[x][y] = 'T' break def Intercept_player(chess_board,key): count2 = 0 index2 = [] intercept_index = {'x':-1,'y':-1} for i in range(MAX_ROW): index = [] count = 0 count1 = 0 index1 = [] allindex = [0,1,2] for j in range(MAX_ROW): if(chess_board[i][j] == key): #每一行的玩家落子情况 count += 1 index.append(j) if(chess_board[j][i] == key): #每一列的玩家落子情况 #print('j'+str(j)+',i'+str(i)+'='+chess_board[j][i]) count1 += 1 index1.append(j) if (i == j and chess_board[j][i] == key): # 在主对角线中的玩家落子情况 count2 += 1 index2.append(j) if(count == 2): #在每一行中 获取具体的可以拦截的位置坐标 需要排除掉已经填充的位置 result = list(set(allindex).difference(set(index))) result = result[0] if(chess_board[i][result] == ' '): #当这个位置可以进行拦截时,进行坐标返回 #return i,result intercept_index['x'] = i intercept_index['y'] = result return intercept_index #print(count1,'------->',index1) if (count1 == 2): # 在每一列中 获取具体的可以拦截的位置坐标 需要排除掉已经填充的位置 result = list(set(allindex).difference(set(index1))) result = result[0] #print('count1==2,result:',result) if (chess_board[result][i] == ' '): # 当这个位置可以进行拦截时,进行坐标返回 intercept_index['x'] = result intercept_index['y'] = i return intercept_index #return i, result if (count2 == 2): # 在主对角线上 获取具体的可以拦截的位置坐标 需要排除掉已经填充的位置 result = list(set(allindex).difference(set(index2))) result = result[0] if (chess_board[i][result] == ' '): # 当这个位置可以进行拦截时,进行坐标返回 intercept_index['x'] = i intercept_index['y'] = result return intercept_index #return i, result count3 = 0 if(chess_board[0][2] == key): count3 += 1 if (chess_board[1][1] == key): count3 += 1 if (chess_board[2][0] == key): count3 += 1 if(count3 == 2): if(chess_board[0][2] == ' '): intercept_index['x'] = 0 intercept_index['y'] = 2 elif (chess_board[1][1] == ' '): intercept_index['x'] = 1 intercept_index['y'] = 1 elif (chess_board[2][0] == ' '): intercept_index['x'] = 2 intercept_index['y'] = 0 return intercept_index def computer_second(chess_board): #电脑智能出棋 #1、先检查一下电脑是否两子成棋 若已有,则获取空位置坐标 自己先成棋 intercept_index = Intercept_player(chess_board, 'T') if (intercept_index['x'] == -1 and intercept_index['y'] == -1): pass else: # 电脑可落子 x = intercept_index['x'] y = intercept_index['y'] chess_board[x][y] = 'T' return #2、若玩家快成棋 则先进行拦截 intercept_index = Intercept_player(chess_board,'U') #若玩家已经两子成棋 则获取空位置的坐标 #print('intercept_index---:') #print(intercept_index) if(intercept_index['x'] == -1 and intercept_index['y'] == -1): pass else: #电脑可落子 x = intercept_index['x'] y = intercept_index['y'] chess_board[x][y] = 'T' return #3、如果没有,则电脑端排棋 以促进成棋 #3.1、 占领中心位置 如若中心位置[1,1]未被占领 if(chess_board[1][1] == ' '): chess_board[1][1] = 'T' return #3.2、 占领四角位置 若[0,0] [0,2] [2,0] [2,2]未被占领 if (chess_board[0][0] == ' '): chess_board[0][0] = 'T' return if (chess_board[0][2] == ' '): chess_board[0][2] = 'T' return if (chess_board[2][0] == ' '): chess_board[2][0] = 'T' return if (chess_board[2][2] == ' '): chess_board[2][2] = 'T' return # 3.3、 占领每一边中心位置 若[0,1] [1,0] [1,2] [2,1]未被占领 if (chess_board[0][1] == ' '): chess_board[0][1] = 'T' return if (chess_board[1][0] == ' '): chess_board[1][0] = 'T' return if (chess_board[1][2] == ' '): chess_board[1][2] = 'T' return if (chess_board[2][1] == ' '): chess_board[2][1] = 'T' return def begin_games(): global chess_board init_cheaa_board(chess_board) result = ' ' while(1): print_chess_board(chess_board) player_first(chess_board) result = Win_or_lose(chess_board) if(result != ' '): break else: #棋盘还没满,该电脑出棋 #computer_second_random(chess_board) computer_second(chess_board) result = Win_or_lose(chess_board) if (result != ' '): break print_chess_board(chess_board) if (result == 'U'): print('Congratulations on your victory!') elif (result == 'T'): print('Unfortunately, you failed to beat the computer.') elif (result == 'D'): print('The two sides broke even.') def menu(): print('-'*20) print('1---------------begin') print('2---------------exit') print('please select begin or exit') print('-' * 20) while(1): select = input('please input:') if select == '1': begin_games() pass elif select == '2': print('exit the game') break #pass pass if __name__ == "__main__": menu() pass
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!