Python 實現(xiàn)勞拉游戲的實例代碼(四連環(huán)、重力四子棋)
游戲規(guī)則:雙方輪流選擇棋盤的列號放進(jìn)自己的棋子,
若棋盤上有四顆相同型號的棋子在一行、一列或一條斜線上連接起來,
則使用該型號棋子的玩家就贏了!
程序?qū)崿F(xiàn)游戲,并將每局的數(shù)據(jù)保存到本地的文件中
首先我們要創(chuàng)建一個空白的棋盤
def into():#初始空白棋盤 for i in range(6): list_width=[] for j in range(8): list_width.append(' '+'|') screen.append(list_width)
然后呢 我們再寫一個輸贏判斷
def eeferee():#判斷輸贏 #判斷行 for i in range(6): for j in range(8-3): if screen[i][j][0]==screen[i][j+1][0]==screen[i][j+2][0]==screen[i][j+3][0] and screen[i][j][0]!=' ': return False #判斷列 for i in range(6-3): for j in range(8): if screen[i][j][0]==screen[i+1][j][0]==screen[i+2][j][0]==screen[i+3][j][0] and screen[i][j][0]!=' ': return False #判斷斜線 for i in range(6-3): for j in range(8-3): if screen[i][j][0]==screen[i+1][j+1][0]==screen[i+2][j+2][0]==screen[i+3][j+3][0] and screen[i][j][0]!=' ': return False if j>=3: if screen[i][j][0] == screen[i+1][j-1][0] == screen[i+2][j-2][0] == screen[i+3][j-3][0] and screen[i][j][0] != ' ': return False return True
下完每步棋,我們要顯示一下棋盤,下面寫一下棋盤的顯示
def screen_print():#打印棋盤 print('',1,2,3,4,5,6,7,8,sep=' ') print('', 1, 2, 3, 4, 5, 6, 7, 8, sep=' ', file=file, flush=True) for i in range(6): print('|',end='') print('|', end='', file=file, flush=True) for j in range(8): print(screen[i][j],end='') print(screen[i][j], end='', file=file, flush=True) print('') print('', file=file, flush=True) print('——'*(9)) print('——' * (9), file=file, flush=True)
下面是勞拉的自動下棋
def lara(): # 勞拉 global screen while True: coordinate=random.randint(0,7) flag = True high = 0 for i in range(5,-1,-1): if screen[i][coordinate][0] == ' ': high = i break if i == 0 and screen[i][coordinate][0] != ' ': flag = False if flag: print('>>>輪到我了,我把O棋子放在第%d列...'%(coordinate+1)) print('>>>輪到我了,我把O棋子放在第%d列...' % (coordinate + 1), file=file, flush=True) screen[high][coordinate] = 'O' + '|' break screen_print()
下棋中 我們還要判斷棋盤是否被下滿了
def full(): for i in screen: for j in i: if j[0] == ' ': return True return False
最后 我們完成一下玩家的下棋
def user(): global screen while True: print(">>>輪到你了,你放X棋子,請選擇列號(1-8): ",end='') print(">>>輪到你了,你放X棋子,請選擇列號(1-8): ", end='', file=file, flush=True) coordinate = int(input())-1 if coordinate not in range(7): print('輸入錯誤的列號,請重新輸入') print('輸入錯誤的列號,請重新輸入', file=file, flush=True) continue flag=True high=0 for i in range(5,-1,-1): if screen[i][coordinate][0] == ' ': high=i break if i==0 and screen[i][coordinate][0] != ' ': flag = False print('你輸入的地方已經(jīng)有棋子了,請重新輸入') print('你輸入的地方已經(jīng)有棋子了,請重新輸入', file=file, flush=True) if flag: screen[high][coordinate] = 'X' + '|' break screen_print()
完整代碼如下:
import random screen = [] #棋盤列表 def into():#初始空白棋盤 for i in range(6): list_width=[] for j in range(8): list_width.append(' '+'|') screen.append(list_width) def screen_print():#打印棋盤 print('',1,2,3,4,5,6,7,8,sep=' ') print('', 1, 2, 3, 4, 5, 6, 7, 8, sep=' ', file=file, flush=True) for i in range(6): print('|',end='') print('|', end='', file=file, flush=True) for j in range(8): print(screen[i][j],end='') print(screen[i][j], end='', file=file, flush=True) print('') print('', file=file, flush=True) print('——'*(9)) print('——' * (9), file=file, flush=True) def eeferee():#判斷輸贏 #判斷行 for i in range(6): for j in range(8-3): if screen[i][j][0]==screen[i][j+1][0]==screen[i][j+2][0]==screen[i][j+3][0] and screen[i][j][0]!=' ': return False #判斷列 for i in range(6-3): for j in range(8): if screen[i][j][0]==screen[i+1][j][0]==screen[i+2][j][0]==screen[i+3][j][0] and screen[i][j][0]!=' ': return False #判斷斜線 for i in range(6-3): for j in range(8-3): if screen[i][j][0]==screen[i+1][j+1][0]==screen[i+2][j+2][0]==screen[i+3][j+3][0] and screen[i][j][0]!=' ': return False if j>=3: if screen[i][j][0] == screen[i+1][j-1][0] == screen[i+2][j-2][0] == screen[i+3][j-3][0] and screen[i][j][0] != ' ': return False return True def full(): for i in screen: for j in i: if j[0] == ' ': return True return False def lara(): # 勞拉 global screen while True: coordinate=random.randint(0,7) flag = True high = 0 for i in range(5,-1,-1): if screen[i][coordinate][0] == ' ': high = i break if i == 0 and screen[i][coordinate][0] != ' ': flag = False if flag: print('>>>輪到我了,我把O棋子放在第%d列...'%(coordinate+1)) print('>>>輪到我了,我把O棋子放在第%d列...' % (coordinate + 1), file=file, flush=True) screen[high][coordinate] = 'O' + '|' break screen_print() def user(): global screen while True: print(">>>輪到你了,你放X棋子,請選擇列號(1-8): ",end='') print(">>>輪到你了,你放X棋子,請選擇列號(1-8): ", end='', file=file, flush=True) coordinate = int(input())-1 if coordinate not in range(7): print('輸入錯誤的列號,請重新輸入') print('輸入錯誤的列號,請重新輸入', file=file, flush=True) continue flag=True high=0 for i in range(5,-1,-1): if screen[i][coordinate][0] == ' ': high=i break if i==0 and screen[i][coordinate][0] != ' ': flag = False print('你輸入的地方已經(jīng)有棋子了,請重新輸入') print('你輸入的地方已經(jīng)有棋子了,請重新輸入', file=file, flush=True) if flag: screen[high][coordinate] = 'X' + '|' break screen_print() if __name__ == '__main__': file=open('四連環(huán)Log-%d.txt'%random.randint(10000,99999),'w',encoding='utf-8') print("""Hi,我是勞拉,我們來玩一局四連環(huán)。我用O型棋子,你用X型棋子。 游戲規(guī)則:雙方輪流選擇棋盤的列號放進(jìn)自己的棋子, 若棋盤上有四顆相同型號的棋子在一行、一列或一條斜線上連接起來, 則使用該型號棋子的玩家就贏了!""") print("""Hi,我是勞拉,我們來玩一局四連環(huán)。我用O型棋子,你用X型棋子。 游戲規(guī)則:雙方輪流選擇棋盤的列號放進(jìn)自己的棋子, 若棋盤上有四顆相同型號的棋子在一行、一列或一條斜線上連接起來, 則使用該型號棋子的玩家就贏了!""", file=file, flush=True) into() print('開始了!這是棋盤的初始狀態(tài):') print('開始了!這是棋盤的初始狀態(tài):', file=file, flush=True) screen_print() flag=True while eeferee() and full(): lara() if not eeferee() and full(): flag=False break user() if full(): print('******* 難分勝負(fù)!@_@') print('******* 難分勝負(fù)!@_@', file=file, flush=True) if flag: print('******* 好吧,你贏了!^_^') print('******* 好吧,你贏了!^_^', file=file, flush=True) else: print('******* 耶,我贏了!^_^') print('******* 耶,我贏了!^_^', file=file, flush=True)
效果圖:
到此這篇關(guān)于Python 實現(xiàn)勞拉游戲的實例代碼(四連環(huán)、重力四子棋)的文章就介紹到這了,更多相關(guān)Python 實現(xiàn)勞拉游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何用VScode配置Python開發(fā)環(huán)境
這篇文章主要介紹了如何用VScode配置Python開發(fā)環(huán)境,vscode有很多優(yōu)點,用VScode來編寫Python,也是相當(dāng)?shù)暮糜玫?需要的朋友可以參考下2023-03-03pytorch-神經(jīng)網(wǎng)絡(luò)擬合曲線實例
今天小編就為大家分享一篇pytorch-神經(jīng)網(wǎng)絡(luò)擬合曲線實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python操作excel的包(openpyxl、xlsxwriter)
這篇文章主要為大家詳細(xì)介紹了python操作excel的包,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06Tensorflow中批量讀取數(shù)據(jù)的案列分析及TFRecord文件的打包與讀取
這篇文章主要介紹了Tensorflow中批量讀取數(shù)據(jù)的案列分析及TFRecord文件的打包與讀取,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06