游戏规则:双方轮流选择棋盘的列号放进自己的棋子,
若棋盘上有四颗相同型号的棋子在一行、一列或一条斜线上连接起来,
则使用该型号棋子的玩家就赢了!
程序实现游戏,并将每局的数据保存到本地的文件中
首先我们要创建一个空白的棋盘
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('四连环Log-%d.txt'%random.randint(10000,99999),'w',encoding='utf-8') print("""Hi,我是劳拉,我们来玩一局四连环。我用O型棋子,你用X型棋子。 游戏规则:双方轮流选择棋盘的列号放进自己的棋子, 若棋盘上有四颗相同型号的棋子在一行、一列或一条斜线上连接起来, 则使用该型号棋子的玩家就赢了!""") print("""Hi,我是劳拉,我们来玩一局四连环。我用O型棋子,你用X型棋子。 游戏规则:双方轮流选择棋盘的列号放进自己的棋子, 若棋盘上有四颗相同型号的棋子在一行、一列或一条斜线上连接起来, 则使用该型号棋子的玩家就赢了!""", file=file, flush=True) into() print('开始了!这是棋盘的初始状态:') print('开始了!这是棋盘的初始状态:', 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)
效果图:
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
内蒙古资源网 Copyright www.nmgbbs.com
暂无“Python 实现劳拉游戏的实例代码(四连环、重力四子棋)”评论...