Python 實(shí)現(xiàn)勞拉游戲的實(shí)例代碼(四連環(huán)、重力四子棋)
游戲規(guī)則:雙方輪流選擇棋盤的列號放進(jìn)自己的棋子,若棋盤上有四顆相同型號的棋子在一行、一列或一條斜線上連接起來,則使用該型號棋子的玩家就贏了!
程序?qū)崿F(xiàn)游戲,并將每局的數(shù)據(jù)保存到本地的文件中
首先我們要?jiǎng)?chuàng)建一個(gè)空白的棋盤
def into():#初始空白棋盤 for i in range(6): list_width=[] for j in range(8): list_width.append(’ ’+’|’) screen.append(list_width)
然后呢 我們再寫一個(gè)輸贏判斷
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)
下面是勞拉的自動(dòng)下棋
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 = ibreak 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(’輸入錯(cuò)誤的列號,請重新輸入’) print(’輸入錯(cuò)誤的列號,請重新輸入’, file=file, flush=True) continue flag=True high=0 for i in range(5,-1,-1): if screen[i][coordinate][0] == ’ ’:high=ibreak if i==0 and screen[i][coordinate][0] != ’ ’:flag = Falseprint(’你輸入的地方已經(jīng)有棋子了,請重新輸入’)print(’你輸入的地方已經(jīng)有棋子了,請重新輸入’, file=file, flush=True) if flag: screen[high][coordinate] = ’X’ + ’|’ break screen_print()
完整代碼如下:
import randomscreen = [] #棋盤列表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 Truedef full(): for i in screen: for j in i: if j[0] == ’ ’:return True return Falsedef 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 = ibreak 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(’輸入錯(cuò)誤的列號,請重新輸入’) print(’輸入錯(cuò)誤的列號,請重新輸入’, file=file, flush=True) continue flag=True high=0 for i in range(5,-1,-1): if screen[i][coordinate][0] == ’ ’:high=ibreak if i==0 and screen[i][coordinate][0] != ’ ’:flag = Falseprint(’你輸入的地方已經(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 實(shí)現(xiàn)勞拉游戲的實(shí)例代碼(四連環(huán)、重力四子棋)的文章就介紹到這了,更多相關(guān)Python 實(shí)現(xiàn)勞拉游戲內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Java之JSP教程九大內(nèi)置對象詳解(中篇)2. 基于python計(jì)算滾動(dòng)方差(標(biāo)準(zhǔn)差)talib和pd.rolling函數(shù)差異詳解3. 使用css實(shí)現(xiàn)全兼容tooltip提示框4. JS繪圖Flot如何實(shí)現(xiàn)動(dòng)態(tài)可刷新曲線圖5. 詳解CSS不定寬溢出文本適配滾動(dòng)6. 使用ProcessBuilder調(diào)用外部命令,并返回大量結(jié)果7. CSS自定義滾動(dòng)條樣式案例詳解8. PHP與已存在的Java應(yīng)用程序集成9. 詳解Python中openpyxl模塊基本用法10. springboot基于Redis發(fā)布訂閱集群下WebSocket的解決方案
