python五子棋游戏的设计与实现


Posted in Python onJune 18, 2019

这个python的小案例是五子棋游戏的实现,在这个案例中,我们可以实现五子棋游戏的两个玩家在指定的位置落子,画出落子后的棋盘,并且根据函数判断出输赢的功能。

这个案例的思路如下所示:

首先,根据棋盘的样子画出棋盘
然后,对棋盘进行初始化,将可以落子的位置进行统一化处理
接下来,就是进入游戏的环节,双方轮流落子,落子后,并将棋盘画出
最后,根据落子的位置判断选手的的输赢情况,游戏结束

五子棋游戏的设计和实现

代码如下:

def main():
  print("五子棋游戏".center(50,'='))
  guige=int(input("请输入棋盘的规格:"))
 
  #按照五子棋的棋盘样式,画出棋盘
  print_init(guige)
 
  # 初始化棋盘
  wzq = {}
  for i in range(1, guige + 1):
    for j in range(1, guige + 1):
      wzq[(i, j)] = "+"
 
  #重画棋盘
  reprint(guige,wzq)
 
#按照五子棋的棋盘样式,画出棋盘
def print_init(guige):
  #打印出首行的棋盘列
  for i in range(guige):
    print("%4d"%(i+1),end='')
  print()
  #双重循环,第一重为棋盘的行,隔一行输出棋盘的行数
  for i in range(guige*2-1):
    #打印出类似 “1 +---+---+---+---+---+”,行号,+,—
    if(i%2==0):
      print("%-3d"%((i+2)/2),end='')
      #第二重循环,为棋盘的列,主要在于找出对应位置显示的内容
      for j in range(guige * 4 - 3):
        if (j % 4 == 0):
          print("+", end='')
        else:
          print("-", end='')
    #打印出类似“ |  |  |  |  |  |”
    else:
      print("%3s"%' ',end='')
      for j in range(guige*4-3):
        if(j%4==0):
          print("|",end='')
        else:
          print(" ",end='')
    #每行输出完成之后换行
    print()
 
#重画棋盘,对双方下子进行重画
def reprint(guige,wzq):
  for i in range(guige*guige) :
    if i % 2 == 0:
      xuanshou = "X"
    else:
      xuanshou = "O"
    # 双方轮流下棋
    while 1:
      print("现在轮到%s方落子" % xuanshou)
      position = input("请输入落子位置:")
      x_position = int(position.split()[0])
      y_position = int(position.split()[1])
      #判断输入的位置是否有子,有子一直输入,直到输入的位置无子
      if wzq[(x_position, y_position)] in ["X", "O"]:
        print("您输入的位置有子,请重新输入!")
      else:
        break
 
    if wzq[(x_position, y_position)]=="+":
      #重画棋盘
      wzq[(x_position, y_position)] = xuanshou
      for i in range(guige):
        print("%4d"%(i+1),end='')
      print()
      for i in range(guige*2-1):
        if(i%2==0):
          print("%-3d"%((i+2)/2),end='')
          for j in range(guige * 4 - 3):
            if (j % 4 == 0):
              x=(i+2)/2
              y=j/4+1
              print(wzq[(x,y)],end='')
            else:
              print("-", end='')
        else:
          print("%3s"%' ',end='')
          for j in range(guige*4-3):
            if(j%4==0):
              print("|",end='')
            else:
              print(" ",end='')
        print()
    else:
      isture=True
      print("您输入的位置已经有子,请重新输入!")
      # 判断输赢
    # 第一种情况
    wzq_win1(wzq,guige,xuanshou)
    # 第二种情况
    wzq_win2(wzq,guige,xuanshou)
    # 第三种情况
    wzq_win3(wzq,guige,xuanshou)
    # 第四种情况
    wzq_win4(wzq,guige,xuanshou)
  else:
    print("游戏结束,平局!")
 
# 判断输赢
def wzq_win1(wzq,guige,xuanshou):
  # 第一种输赢情况
  for i in range(1, guige + 1):
    for j in range(1, guige - 3):
      if (wzq[(i, j)] == wzq[(i, j + 1)] == wzq[(i, j + 2)] == wzq[(i, j + 3)] == wzq[(i, j + 4)] and wzq[(i, j)] in ["X", "O"]):
        print("%s获胜,游戏结束!" % xuanshou)
        exit()
def wzq_win2(wzq,guige,xuanshou):
  # 第二种输赢情况
  for i in range(1, guige - 3):
    for j in range(1, guige + 1):
      if (wzq[(i, j)] == wzq[(i + 1, j)] == wzq[(i + 2, j)] == wzq[(i + 3, j)] == wzq[(i + 4, j)] and wzq[(i, j)] in ["X", "O"]):
        print("%s获胜,游戏结束!" % xuanshou)
        exit()
def wzq_win3(wzq,guige,xuanshou):
  # 第三种输赢情况
  for i in range(1, guige - 3):
    for j in range(1, guige - 3):
      if (wzq[(i, j)] == wzq[(i + 1, j + 1)] == wzq[(i + 2, j + 2)] == wzq[(i + 3, j + 3)] == wzq[(i + 4, j + 4)] and wzq[(i, j)] in ["X", "O"]):
        print("%s获胜,游戏结束!" % xuanshou)
        exit()
def wzq_win4(wzq, guige, xuanshou):
  # 第四种输赢情况
  for i in range(1, guige - 3):
    for j in range(5, guige + 1):
      if (wzq[(i, j)] == wzq[(i +1, j - 1)] == wzq[(i + 2, j - 2)] == wzq[(i + 3, j - 3)] == wzq[(i + 4, j - 4)] and wzq[(i, j)] in ["X", "O"]):
        print("%s获胜,游戏结束!" % xuanshou)
        exit()
main()

结果如下:

======================五子棋游戏=======================
请输入棋盘的规格:8
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
6 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
现在轮到X方落子
请输入落子位置:5 5
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---+---X---+---+---+
  |  |  |  |  |  |  |  |
6 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
现在轮到O方落子
请输入落子位置:6 1
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---+---X---+---+---+
  |  |  |  |  |  |  |  |
6 O---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
现在轮到X方落子
请输入落子位置:4 4
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---X---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---+---X---+---+---+
  |  |  |  |  |  |  |  |
6 O---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
现在轮到O方落子
请输入落子位置:5 4
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---X---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---O---X---+---+---+
  |  |  |  |  |  |  |  |
6 O---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
现在轮到X方落子
请输入落子位置:6 6
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---X---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---O---X---+---+---+
  |  |  |  |  |  |  |  |
6 O---+---+---+---+---X---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
现在轮到O方落子
请输入落子位置:6 5
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---X---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---O---X---+---+---+
  |  |  |  |  |  |  |  |
6 O---+---+---+---O---X---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
现在轮到X方落子
请输入落子位置:3 3
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---X---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---X---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---O---X---+---+---+
  |  |  |  |  |  |  |  |
6 O---+---+---+---O---X---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
现在轮到O方落子
请输入落子位置:5 6
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---X---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---X---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---O---X---O---+---+
  |  |  |  |  |  |  |  |
6 O---+---+---+---O---X---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
现在轮到X方落子
请输入落子位置:2 2
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---X---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---X---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---X---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---O---X---O---+---+
  |  |  |  |  |  |  |  |
6 O---+---+---+---O---X---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
X获胜,游戏结束!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python 使用os.remove删除文件夹时报错的解决方法
Jan 13 Python
python批量添加zabbix Screens的两个脚本分享
Jan 16 Python
python模块smtplib实现纯文本邮件发送功能
May 22 Python
python爬取网页内容转换为PDF文件
Jul 28 Python
解决Python中list里的中文输出到html模板里的问题
Dec 17 Python
python实现控制COM口的示例
Jul 03 Python
Django REST Framework序列化外键获取外键的值方法
Jul 26 Python
TensorFlow车牌识别完整版代码(含车牌数据集)
Aug 05 Python
python语言的优势是什么
Jun 17 Python
详解Python中的路径问题
Sep 02 Python
Python字符串的15个基本操作(小结)
Feb 03 Python
基于Python的EasyGUI学习实践
May 07 Python
Python基础学习之时间转换函数用法详解
Jun 18 #Python
python实现简单五子棋游戏
Jun 18 #Python
Python基础学习之函数方法实例详解
Jun 18 #Python
pyqt5 实现在别的窗口弹出进度条
Jun 18 #Python
Python弹出输入框并获取输入值的实例
Jun 18 #Python
python使用tkinter库实现五子棋游戏
Jun 18 #Python
Python基础学习之基本数据结构详解【数字、字符串、列表、元组、集合、字典】
Jun 18 #Python
You might like
CodeIgniter中使用cookie的三种方式详解
2014/07/18 PHP
php提示Warning:mysql_fetch_array() expects的解决方法
2014/12/16 PHP
PHP使用DirectoryIterator显示下拉文件列表的方法
2015/03/13 PHP
在phpstudy集成环境下的nginx服务器下配置url重写
2019/12/02 PHP
共享自己写一个框架DreamScript
2007/01/20 Javascript
asp.net 30分钟掌握无刷新 Repeater
2011/09/16 Javascript
javascript中的缓动效果实现程序
2012/12/29 Javascript
jquery操作cookie插件分享
2014/01/14 Javascript
Shell脚本实现Linux系统和进程资源监控
2015/03/05 Javascript
jQuery中的基本选择器用法学习教程
2016/04/14 Javascript
微信小程序 向左滑动删除功能的实现
2017/03/10 Javascript
React利用插件和不用插件实现双向绑定的方法详解
2017/07/03 Javascript
php register_shutdown_function函数详解
2017/07/23 Javascript
Vue.js devtool插件安装后无法使用的解决办法
2017/11/27 Javascript
Angular网络请求的封装方法
2018/05/22 Javascript
详解微信JS-SDK选择图片遇到的坑
2018/08/15 Javascript
Vue快速实现通用表单验证功能
2019/12/05 Javascript
微信小程序文章详情功能完整实例
2020/06/03 Javascript
vue用ant design中table表格,点击某行时触发的事件操作
2020/10/28 Javascript
JavaScript对象访问器Getter及Setter原理解析
2020/12/08 Javascript
[47:53]DOTA2上海特级锦标赛主赛事日 - 1 败者组第一轮#2COL VS Spirit
2016/03/02 DOTA
Python使用pip安装报错:is not a supported wheel on this platform的解决方法
2018/01/23 Python
Python实现Pig Latin小游戏实例代码
2018/02/02 Python
python3第三方爬虫库BeautifulSoup4安装教程
2018/06/19 Python
Python爬虫之Selenium设置元素等待的方法
2020/12/04 Python
PyChon中关于Jekins的详细安装(推荐)
2020/12/28 Python
python+playwright微软自动化工具的使用
2021/02/02 Python
什么是smarty? Smarty的优点是什么?
2013/08/11 面试题
文秘自荐信
2013/10/20 职场文书
微博营销计划书
2014/01/10 职场文书
开学季活动策划方案
2014/02/28 职场文书
公证书标准格式
2014/04/10 职场文书
企业读书活动总结
2014/06/30 职场文书
2015公务员年度考核评语
2015/03/25 职场文书
2020年元旦晚会策划书模板
2019/12/30 职场文书
JavaScript阻止事件冒泡的方法
2021/12/06 Javascript