您好,登錄后才能下訂單哦!
今天小編給大家分享一下python怎么實現三子棋游戲的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
三子棋游戲實現邏輯如下:
1、創建初始化3*3棋盤;
2、玩家執U子,先進行落子;
3、勝負判定【勝、負、和棋】,若勝負未分,則繼續如下
4、電腦執T子,進行落子;
5、勝負判定,若勝負未分,則從步驟2繼續執行
選擇1是開始游戲,選擇2是退出游戲
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
三子棋棋盤是3*3的方陣,在python中用列表來進行存儲。
chess_board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
那么如何將這個存儲列表打印出來,成為棋盤呢?
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
玩家在3*3的棋盤中選擇落子的橫縱坐標。坐標點需要滿足:1、該點在棋盤內;2、該點還未置子。
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
電腦落子算法:
4.1、先檢查一下棋盤,看電腦已占有棋面中是否已經有兩子連成、即將成棋的狀態。若已有,則獲取可以促成勝利的坐標點,進行落子T;
4.2、若4.1不滿足,則再去檢查一下棋盤,看玩家已占有棋面中是否已經有兩子連成、即將成棋的狀態。若已有,則獲取玩家即將勝利的坐標點,落子T進行攔截;
4.3、若4.1、4.2均不滿足,則在棋面中選擇電腦端有利的點進行落子;
A、先判斷中心位置[1][1]處是否被占領,若未被占領,則這是最有利點。當占領[1][1]點時,則阻斷了玩家的橫、縱、正對角線、副對角線四條線路;
B、次有利點則是3*3棋盤的四個角,每占領一個角,則會阻斷玩家的三條線路;
C、最后有利的點則是每條邊的中心位置,會阻斷玩家的兩條線路;
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
最終的結果:輸、贏、和棋D
判定流程:判斷每個橫線、縱線、對角線上是否有玩家U或電腦T連成三子的,若有則是該方勝出;當整個棋面都被占滿,但玩家和電腦都未成棋時,則說明和棋。
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
4.1 在以下截圖中,展示了電腦攔截、占據有利位置、并率先成棋的過程
以上就是“python怎么實現三子棋游戲”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。