欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)三子棋游戲

 更新時(shí)間:2022年05月04日 12:40:38   作者:sakura_ll  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)三子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

三子棋的python實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下

一、基本流程

三子棋游戲?qū)崿F(xiàn)邏輯如下:

1、創(chuàng)建初始化3*3棋盤(pán);
2、玩家執(zhí)U子,先進(jìn)行落子;
3、勝負(fù)判定【勝、負(fù)、和棋】,若勝負(fù)未分,則繼續(xù)如下
4、電腦執(zhí)T子,進(jìn)行落子;
5、勝負(fù)判定,若勝負(fù)未分,則從步驟2繼續(xù)執(zhí)行

二、基本步驟

1、菜單界面

選擇1是開(kāi)始游戲,選擇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

2、初始化棋盤(pán)、打印棋盤(pán)

三子棋棋盤(pán)是3*3的方陣,在python中用列表來(lái)進(jìn)行存儲(chǔ)。

chess_board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

那么如何將這個(gè)存儲(chǔ)列表打印出來(lái),成為棋盤(pán)呢?

def init_cheaa_board(chess_board): #先對(duì)列表進(jìn)行初始化
? ? for i in range(MAX_ROW):
? ? ? ? for j in range(MAX_COL):
? ? ? ? ? ? chess_board[i][j] = ' '
? ? pass

def print_chess_board(chess_board): #棋盤(pán)打印
? ? 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*3的棋盤(pán)中選擇落子的橫縱坐標(biāo)。坐標(biāo)點(diǎn)需要滿(mǎn)足:1、該點(diǎn)在棋盤(pán)內(nèi);2、該點(diǎn)還未置子。

def player_first(chess_board):
? ? while(1):
? ? ? ? x = int(input('please input x:'))
? ? ? ? y = int(input('please input y:'))
? ? ? ? if(chess_board[x][y] != ' '): #若已被置子,則重新選擇坐標(biāo)
? ? ? ? ? ? print('This position is already occupied!')
? ? ? ? ? ? pass
? ? ? ? elif(x >= MAX_ROW or y >= MAX_COL or x < 0 or y < 0): #所選坐標(biāo)超出棋盤(pán)范圍,重新選擇坐標(biāo)
? ? ? ? ? ? print('This position is beyond the chessboard!')
? ? ? ? ? ? pass
? ? ? ? else: #若坐標(biāo)可以落子,則將該坐標(biāo)置為玩家的棋子U
? ? ? ? ? ? chess_board[x][y] = 'U'
? ? ? ? ? ? print_chess_board(chess_board)
? ? ? ? ? ? #return x,y
? ? ? ? ? ? break
? ? ? ? ? ? pass
? ? pass

4、電腦落子

電腦落子算法:

4.1、先檢查一下棋盤(pán),看電腦已占有棋面中是否已經(jīng)有兩子連成、即將成棋的狀態(tài)。若已有,則獲取可以促成勝利的坐標(biāo)點(diǎn),進(jìn)行落子T;

4.2、若4.1不滿(mǎn)足,則再去檢查一下棋盤(pán),看玩家已占有棋面中是否已經(jīng)有兩子連成、即將成棋的狀態(tài)。若已有,則獲取玩家即將勝利的坐標(biāo)點(diǎn),落子T進(jìn)行攔截;

4.3、若4.1、4.2均不滿(mǎn)足,則在棋面中選擇電腦端有利的點(diǎn)進(jìn)行落子;

A、先判斷中心位置[1][1]處是否被占領(lǐng),若未被占領(lǐng),則這是最有利點(diǎn)。當(dāng)占領(lǐng)[1][1]點(diǎn)時(shí),則阻斷了玩家的橫、縱、正對(duì)角線、副對(duì)角線四條線路;
B、次有利點(diǎn)則是3*3棋盤(pán)的四個(gè)角,每占領(lǐng)一個(gè)角,則會(huì)阻斷玩家的三條線路;
C、最后有利的點(diǎn)則是每條邊的中心位置,會(huì)阻斷玩家的兩條線路;

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): ?# 在主對(duì)角線中的玩家落子情況
? ? ? ? ? ? ? ? count2 += 1
? ? ? ? ? ? ? ? index2.append(j)
? ? ? ? if(count == 2): ? ?#在每一行中 ?獲取具體的可以攔截的位置坐標(biāo) ?需要排除掉已經(jīng)填充的位置
? ? ? ? ? ? result = list(set(allindex).difference(set(index)))
? ? ? ? ? ? result = result[0]
? ? ? ? ? ? if(chess_board[i][result] == ' '): #當(dāng)這個(gè)位置可以進(jìn)行攔截時(shí),進(jìn)行坐標(biāo)返回
? ? ? ? ? ? ? ? #return i,result
? ? ? ? ? ? ? ? intercept_index['x'] = i
? ? ? ? ? ? ? ? intercept_index['y'] = result
? ? ? ? ? ? ? ? return intercept_index
? ? ? ? #print(count1,'------->',index1)
? ? ? ? if (count1 == 2): ?# 在每一列中 獲取具體的可以攔截的位置坐標(biāo) ?需要排除掉已經(jīng)填充的位置
? ? ? ? ? ? result = list(set(allindex).difference(set(index1)))
? ? ? ? ? ? result = result[0]
? ? ? ? ? ? #print('count1==2,result:',result)
? ? ? ? ? ? if (chess_board[result][i] == ' '): ?# 當(dāng)這個(gè)位置可以進(jìn)行攔截時(shí),進(jìn)行坐標(biāo)返回
? ? ? ? ? ? ? ? intercept_index['x'] = result
? ? ? ? ? ? ? ? intercept_index['y'] = i
? ? ? ? ? ? ? ? return intercept_index
? ? ? ? ? ? ? ? #return i, result
? ? ? ? if (count2 == 2): ?# 在主對(duì)角線上 獲取具體的可以攔截的位置坐標(biāo) ?需要排除掉已經(jīng)填充的位置
? ? ? ? ? ? result = list(set(allindex).difference(set(index2)))
? ? ? ? ? ? result = result[0]
? ? ? ? ? ? if (chess_board[i][result] == ' '): ?# 當(dāng)這個(gè)位置可以進(jìn)行攔截時(shí),進(jìn)行坐標(biāo)返回
? ? ? ? ? ? ? ? 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、先檢查一下電腦是否兩子成棋 ?若已有,則獲取空位置坐標(biāo) 自己先成棋
? ? 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、若玩家快成棋 ? 則先進(jìn)行攔截
? ? intercept_index = Intercept_player(chess_board,'U') ? #若玩家已經(jīng)兩子成棋 ?則獲取空位置的坐標(biāo)
? ? #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、如果沒(méi)有,則電腦端排棋 ?以促進(jìn)成棋
? ? #3.1、 占領(lǐng)中心位置 ?如若中心位置[1,1]未被占領(lǐng)
? ? if(chess_board[1][1] == ' '):
? ? ? ? chess_board[1][1] = 'T'
? ? ? ? return
? ? #3.2、 占領(lǐng)四角位置 ?若[0,0] ?[0,2] ?[2,0] ?[2,2]未被占領(lǐng)
? ? 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、 占領(lǐng)每一邊中心位置 ?若[0,1] ?[1,0] ?[1,2] ?[2,1]未被占領(lǐng)
? ? 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、輸贏判定

最終的結(jié)果:輸、贏、和棋D
判定流程:判斷每個(gè)橫線、縱線、對(duì)角線上是否有玩家U或電腦T連成三子的,若有則是該方勝出;當(dāng)整個(gè)棋面都被占滿(mǎn),但玩家和電腦都未成棋時(shí),則說(shuō)明和棋。

def chess_board_isfull(chess_board): ? #判斷棋盤(pán)是否填充滿(mǎn)
? ? 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]): ?# 判斷棋盤(pán)正對(duì)角線
? ? ? ? return chess_board[0][0]

? ? if (chess_board[0][2] == chess_board[1][1] == chess_board[2][0]): ?# 判斷棋盤(pán)反對(duì)角線
? ? ? ? return chess_board[0][2]

? ? if isfull:
? ? ? ? return 'D' ?# 經(jīng)過(guò)以上的判斷,都不滿(mǎn)足(既沒(méi)贏也沒(méi)輸),但是棋盤(pán)也已經(jīng)填充滿(mǎn),則說(shuō)明和棋
? ? 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): ? #判斷棋盤(pán)是否填充滿(mǎn)
? ? 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]): ?# 判斷棋盤(pán)正對(duì)角線
? ? ? ? return chess_board[0][0]

? ? if (chess_board[0][2] == chess_board[1][1] == chess_board[2][0]): ?# 判斷棋盤(pán)反對(duì)角線
? ? ? ? return chess_board[0][2]

? ? if isfull:
? ? ? ? return 'D' ?# 經(jīng)過(guò)以上的判斷,都不滿(mǎn)足(既沒(méi)贏也沒(méi)輸),但是棋盤(pán)也已經(jīng)填充滿(mǎn),則說(shuō)明和棋
? ? else:
? ? ? ? return ' '

def computer_second_random(chess_board): ? ?#電腦隨機(jī)出棋
? ? 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): ?# 在主對(duì)角線中的玩家落子情況
? ? ? ? ? ? ? ? count2 += 1
? ? ? ? ? ? ? ? index2.append(j)
? ? ? ? if(count == 2): ? ?#在每一行中 ?獲取具體的可以攔截的位置坐標(biāo) ?需要排除掉已經(jīng)填充的位置
? ? ? ? ? ? result = list(set(allindex).difference(set(index)))
? ? ? ? ? ? result = result[0]
? ? ? ? ? ? if(chess_board[i][result] == ' '): #當(dāng)這個(gè)位置可以進(jìn)行攔截時(shí),進(jìn)行坐標(biāo)返回
? ? ? ? ? ? ? ? #return i,result
? ? ? ? ? ? ? ? intercept_index['x'] = i
? ? ? ? ? ? ? ? intercept_index['y'] = result
? ? ? ? ? ? ? ? return intercept_index
? ? ? ? #print(count1,'------->',index1)
? ? ? ? if (count1 == 2): ?# 在每一列中 獲取具體的可以攔截的位置坐標(biāo) ?需要排除掉已經(jīng)填充的位置
? ? ? ? ? ? result = list(set(allindex).difference(set(index1)))
? ? ? ? ? ? result = result[0]
? ? ? ? ? ? #print('count1==2,result:',result)
? ? ? ? ? ? if (chess_board[result][i] == ' '): ?# 當(dāng)這個(gè)位置可以進(jìn)行攔截時(shí),進(jìn)行坐標(biāo)返回
? ? ? ? ? ? ? ? intercept_index['x'] = result
? ? ? ? ? ? ? ? intercept_index['y'] = i
? ? ? ? ? ? ? ? return intercept_index
? ? ? ? ? ? ? ? #return i, result
? ? ? ? if (count2 == 2): ?# 在主對(duì)角線上 獲取具體的可以攔截的位置坐標(biāo) ?需要排除掉已經(jīng)填充的位置
? ? ? ? ? ? result = list(set(allindex).difference(set(index2)))
? ? ? ? ? ? result = result[0]
? ? ? ? ? ? if (chess_board[i][result] == ' '): ?# 當(dāng)這個(gè)位置可以進(jìn)行攔截時(shí),進(jìn)行坐標(biāo)返回
? ? ? ? ? ? ? ? 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、先檢查一下電腦是否兩子成棋 ?若已有,則獲取空位置坐標(biāo) 自己先成棋
? ? 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、若玩家快成棋 ? 則先進(jìn)行攔截
? ? intercept_index = Intercept_player(chess_board,'U') ? #若玩家已經(jīng)兩子成棋 ?則獲取空位置的坐標(biāo)
? ? #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、如果沒(méi)有,則電腦端排棋 ?以促進(jìn)成棋
? ? #3.1、 占領(lǐng)中心位置 ?如若中心位置[1,1]未被占領(lǐng)
? ? if(chess_board[1][1] == ' '):
? ? ? ? chess_board[1][1] = 'T'
? ? ? ? return
? ? #3.2、 占領(lǐng)四角位置 ?若[0,0] ?[0,2] ?[2,0] ?[2,2]未被占領(lǐng)
? ? 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、 占領(lǐng)每一邊中心位置 ?若[0,1] ?[1,0] ?[1,2] ?[2,1]未被占領(lǐng)
? ? 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: #棋盤(pán)還沒(méi)滿(mǎn),該電腦出棋
? ? ? ? ? ? #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

四、結(jié)果展示

4.1 在以下截圖中,展示了電腦攔截、占據(jù)有利位置、并率先成棋的過(guò)程

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 利用python為PostgreSQL的表自動(dòng)添加分區(qū)

    利用python為PostgreSQL的表自動(dòng)添加分區(qū)

    這篇文章主要介紹了利用python為PostgreSQL的表自動(dòng)添加分區(qū),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Pandas.DataFrame行和列的轉(zhuǎn)置的實(shí)現(xiàn)

    Pandas.DataFrame行和列的轉(zhuǎn)置的實(shí)現(xiàn)

    本文主要介紹了Pandas.DataFrame行和列的轉(zhuǎn)置的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • 分享Python中四個(gè)不常見(jiàn)的小技巧

    分享Python中四個(gè)不常見(jiàn)的小技巧

    這篇文章主要介紹了分享Python中四個(gè)不常見(jiàn)的小技巧,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • Python退出While循環(huán)的3種方法舉例詳解

    Python退出While循環(huán)的3種方法舉例詳解

    在每次循環(huán)結(jié)束后,我們需要檢查循環(huán)條件是否滿(mǎn)足。如果條件滿(mǎn)足,則繼續(xù)執(zhí)行循環(huán)體內(nèi)的代碼,否則退出循環(huán),這篇文章主要給大家介紹了關(guān)于Python退出While循環(huán)的3種方法,需要的朋友可以參考下
    2023-10-10
  • python簡(jiǎn)單的三元一次方程求解實(shí)例

    python簡(jiǎn)單的三元一次方程求解實(shí)例

    這篇文章主要介紹了python簡(jiǎn)單的三元一次方程求解實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • python 實(shí)現(xiàn)讓字典的value 成為列表

    python 實(shí)現(xiàn)讓字典的value 成為列表

    今天小編就為大家分享一篇python 實(shí)現(xiàn)讓字典的value 成為列表,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • python實(shí)戰(zhàn)之90行代碼寫(xiě)個(gè)猜數(shù)字游戲

    python實(shí)戰(zhàn)之90行代碼寫(xiě)個(gè)猜數(shù)字游戲

    這篇文章主要介紹了python實(shí)戰(zhàn)之90行代碼寫(xiě)個(gè)猜數(shù)字,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很大的幫助,需要的朋友可以參考下
    2021-04-04
  • python密碼學(xué)換位密碼及換位解密轉(zhuǎn)置加密教程

    python密碼學(xué)換位密碼及換位解密轉(zhuǎn)置加密教程

    這篇文章主要為大家介紹了python密碼學(xué)換位密碼及換位解密轉(zhuǎn)置加密教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 詳解python的變量

    詳解python的變量

    這篇文章主要為大家介紹了python中的變量,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12
  • matplotlib.subplot()畫(huà)子圖并共享y坐標(biāo)軸的方法

    matplotlib.subplot()畫(huà)子圖并共享y坐標(biāo)軸的方法

    Matplotlib的可以把很多張圖畫(huà)到一個(gè)顯示界面,本文主要介紹matplotlib.subplot()畫(huà)子圖并共享y坐標(biāo)軸的方法,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05

最新評(píng)論