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之开始真正编程
Sep 12 Python
python中getaddrinfo()基本用法实例分析
Jun 28 Python
新手常见6种的python报错及解决方法
Mar 09 Python
Python面向对象类的继承实例详解
Jun 27 Python
python实现可逆简单的加密算法
Mar 22 Python
python 将日期戳(五位数时间)转换为标准时间
Jul 11 Python
python如何将多个PDF进行合并
Aug 13 Python
Python3 实现减少可调用对象的参数个数
Dec 20 Python
Python importlib动态导入模块实现代码
Apr 16 Python
Python中的With语句的使用及原理
Jul 29 Python
python pyg2plot的原理知识点总结
Feb 28 Python
Python捕获、播放和保存摄像头视频并提高视频清晰度和对比度
Apr 14 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
php+mysqli实现批量执行插入、更新及删除数据的方法
2015/01/29 PHP
Symfony2实现从数据库获取数据的方法小结
2016/03/18 PHP
php验证身份证号码正确性的函数
2016/07/20 PHP
(function(){})()的用法与优点
2007/03/11 Javascript
chrome浏览器不支持onmouseleave事件的解决技巧
2013/05/31 Javascript
js图片自动轮播代码分享(js图片轮播)
2014/05/06 Javascript
jquery delay()介绍及使用指南
2014/09/02 Javascript
Mac系统下Webstorm快捷键整理大全
2017/05/28 Javascript
原生JS实现Ajax跨域请求flask响应内容
2017/10/24 Javascript
基于vue中解决v-for使用报红并出现警告的问题
2018/03/03 Javascript
Vue头像处理方案小结
2018/07/26 Javascript
从零开始用electron手撸一个截屏工具的示例代码
2018/10/10 Javascript
Vue中使用create-keyframe-animation与动画钩子完成复杂动画
2019/04/09 Javascript
Vue+Element实现表格编辑、删除、以及新增行的最优方法
2019/05/28 Javascript
javascript实现的时间格式加8小时功能示例
2019/06/13 Javascript
Vue.js中的extend绑定节点并显示的方法
2019/06/20 Javascript
vue动态路由:路由参数改变,视图不更新问题的解决
2019/11/05 Javascript
Ant Design的Table组件去除
2020/10/24 Javascript
[01:03:37]Secret vs VGJ.S Supermajor小组赛C组 BO3 第二场 6.3
2018/06/04 DOTA
python中 chr unichr ord函数的实例详解
2017/08/06 Python
django使用LDAP验证的方法示例
2018/12/10 Python
python装饰器练习题及答案
2019/11/01 Python
python+opencv3生成一个自定义纯色图教程
2020/02/19 Python
python 字典item与iteritems的区别详解
2020/04/25 Python
Python txt文件如何转换成字典
2020/11/03 Python
5 个强大的HTML5 API 函数推荐
2014/11/19 HTML / CSS
英国最出名高街品牌:Forever Unique
2018/02/24 全球购物
巴西Mr. Cat在线商店:购买包包和鞋子
2019/09/08 全球购物
优秀大学生推荐信范文
2013/11/28 职场文书
路政管理专业个人自荐信范文
2013/11/30 职场文书
房地产销售经理岗位职责
2014/01/01 职场文书
信访工作者先进事迹
2014/01/17 职场文书
校园安全广播稿
2014/02/08 职场文书
退休教师欢送会主持词
2014/03/31 职场文书
个人租房协议书样本
2014/10/01 职场文书
jquery插件实现图片悬浮
2021/04/16 jQuery