游戲規(guī)則:雙方輪流選擇棋盤的列號放進自己的棋子,
若棋盤上有四顆相同型號的棋子在一行、一列或一條斜線上連接起來,
則使用該型號棋子的玩家就贏了!
程序實現游戲,并將每局的數據保存到本地的文件中
首先我們要創(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('你輸入的地方已經有棋子了,請重新輸入')
print('你輸入的地方已經有棋子了,請重新輸入', 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('你輸入的地方已經有棋子了,請重新輸入')
print('你輸入的地方已經有棋子了,請重新輸入', 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)
效果圖:

到此這篇關于Python 實現勞拉游戲的實例代碼(四連環(huán)、重力四子棋)的文章就介紹到這了,更多相關Python 實現勞拉游戲內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 用Python實現童年貪吃蛇小游戲功能的實例代碼
- 利用python制作拼圖小游戲的全過程
- 利用python如何實現貓捉老鼠小游戲