Python 實現(xiàn)勞拉游戲的實例代碼(四連環(huán)、重力四子棋)
游戲規(guī)則:雙方輪流選擇棋盤的列號放進自己的棋子,若棋盤上有四顆相同型號的棋子在一行、一列或一條斜線上連接起來,則使用該型號棋子的玩家就贏了!
程序?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 = 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(’輸入錯誤的列號,請重新輸入’) print(’輸入錯誤的列號,請重新輸入’, 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(’輸入錯誤的列號,請重新輸入’) print(’輸入錯誤的列號,請重新輸入’, 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ī)則:雙方輪流選擇棋盤的列號放進自己的棋子, 若棋盤上有四顆相同型號的棋子在一行、一列或一條斜線上連接起來, 則使用該型號棋子的玩家就贏了!''') print('''Hi,我是勞拉,我們來玩一局四連環(huán)。我用O型棋子,你用X型棋子。 游戲規(guī)則:雙方輪流選擇棋盤的列號放進自己的棋子, 若棋盤上有四顆相同型號的棋子在一行、一列或一條斜線上連接起來, 則使用該型號棋子的玩家就贏了!''', 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(’******* 難分勝負!@_@’) print(’******* 難分勝負!@_@’, 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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Python2.6版本pip安裝步驟解析2. python公司內(nèi)項目對接釘釘審批流程的實現(xiàn)3. python中Ansible模塊的Playbook的具體使用4. Python自動化之定位方法大殺器xpath5. Python本地及虛擬解釋器配置過程解析6. Python 利用flask搭建一個共享服務(wù)器的步驟7. 基于python實現(xiàn)matlab filter函數(shù)過程詳解8. Python中Anaconda3 安裝gdal庫的方法9. python自動化測試三部曲之request+django實現(xiàn)接口測試10. Python importlib模塊重載使用方法詳解
