Python 实现劳拉游戏的实例代码(四连环、重力四子棋)


Posted in Python onMarch 03, 2021

游戏规则:双方轮流选择棋盘的列号放进自己的棋子,
若棋盘上有四颗相同型号的棋子在一行、一列或一条斜线上连接起来,
则使用该型号棋子的玩家就赢了!

程序实现游戏,并将每局的数据保存到本地的文件中

首先我们要创建一个空白的棋盘

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)

效果图:

Python 实现劳拉游戏的实例代码(四连环、重力四子棋)

到此这篇关于Python 实现劳拉游戏的实例代码(四连环、重力四子棋)的文章就介绍到这了,更多相关Python 实现劳拉游戏内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python实现更改图片尺寸大小的方法(基于Pillow包)
Sep 19 Python
Python中顺序表的实现简单代码分享
Jan 09 Python
原生python实现knn分类算法
Oct 24 Python
opencv3/Python 稠密光流calcOpticalFlowFarneback详解
Dec 11 Python
tensorflow estimator 使用hook实现finetune方式
Jan 21 Python
python实现飞机大战游戏(pygame版)
Oct 26 Python
通过Python实现一个简单的html页面
May 16 Python
Python使用socketServer包搭建简易服务器过程详解
Jun 12 Python
pycharm配置python 设置pip安装源为豆瓣源
Feb 05 Python
基于注解实现 SpringBoot 接口防刷的方法
Mar 02 Python
如何使用Python对NetCDF数据做空间相关分析
Apr 21 Python
pytorch 如何使用float64训练
May 24 Python
对pytorch中x = x.view(x.size(0), -1) 的理解说明
Mar 03 #Python
Jupyter安装拓展nbextensions及解决官网下载慢的问题
Mar 03 #Python
Pytorch 中的optimizer使用说明
Mar 03 #Python
解决pytorch 的state_dict()拷贝问题
Mar 03 #Python
解决pytorch 保存模型遇到的问题
Mar 03 #Python
解决pytorch 模型复制的一些问题
Mar 03 #Python
Pytorch模型迁移和迁移学习,导入部分模型参数的操作
Mar 03 #Python
You might like
php adodb连接不同数据库
2009/03/19 PHP
php 文章采集正则代码
2009/12/28 PHP
php include类文件超时问题处理
2015/02/06 PHP
crontab无法执行php的解决方法
2016/01/25 PHP
Node.js:Windows7下搭建的Node.js服务(来玩玩服务器端的javascript吧,这可不是前端js插件)
2011/06/27 Javascript
jquery多行滚动/向左或向上滚动/响应鼠标实现思路及代码
2013/01/23 Javascript
jsp js鼠标移动到指定区域显示选项卡离开时隐藏示例
2013/06/14 Javascript
js弹出窗口之弹出层的小例子
2013/06/17 Javascript
transport.js和jquery冲突问题的解决方法
2015/02/10 Javascript
Three.js快速入门教程
2016/09/09 Javascript
Vue.js 60分钟快速入门教程
2017/03/28 Javascript
Easyui ueditor 整合解决不能编辑的问题(推荐)
2017/06/25 Javascript
使用Javascript简单计算器
2018/11/17 Javascript
vue路由传参三种基本方式详解
2019/12/09 Javascript
js实现表格数据搜索
2020/08/09 Javascript
基于Ionic3实现选项卡切换并重新加载echarts
2020/09/24 Javascript
vue-cli4使用全局less文件中的变量配置操作
2020/10/21 Javascript
python和shell变量互相传递的几种方法
2013/11/20 Python
Python 元组(Tuple)操作详解
2014/03/11 Python
python中精确输出JSON浮点数的方法
2014/04/18 Python
Python基于checksum计算文件是否相同的方法
2015/07/09 Python
python中requests小技巧
2017/05/10 Python
将python2.7添加进64位系统的注册表方式
2019/11/20 Python
Python使用指定字符长度切分数据示例
2019/12/05 Python
TensorFlow内存管理bfc算法实例
2020/02/03 Python
python 常见的反爬虫策略
2020/09/27 Python
python从ftp获取文件并下载到本地
2020/12/05 Python
美国休闲服装品牌:Express
2016/09/24 全球购物
新西兰便宜隐形眼镜购买网站:QUICKLENS New Zealand
2019/03/02 全球购物
MySQL面试题目集锦
2016/04/14 面试题
财务会计专业应届毕业生求职信
2013/10/18 职场文书
优秀大学生的自我评价
2014/01/16 职场文书
消防安全宣传标语
2014/06/07 职场文书
火锅店的开业营销方案范本!
2019/07/05 职场文书
Python 如何解决稀疏矩阵运算
2021/05/26 Python
nginx sticky实现基于cookie负载均衡示例详解
2022/12/24 Servers