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

yipeiwu_com5年前Python基础

这个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的matplotlib绘图如何修改背景颜色的实现

Python的matplotlib绘图如何修改背景颜色的实现

在主图中背景颜色不知道怎么改,plt.plot()中没有axisbg参数。 但是子图可以对plt.subplot的参数做修改,下面是对子图的背景颜色修改代码 import matpl...

Python基于scipy实现信号滤波功能

Python基于scipy实现信号滤波功能

​ 1.背景介绍 在深度学习中,有时会使用Matlab进行滤波处理,再将处理过的数据送入神经网络中。这样是一般的处理方法,但是处理起来却有些繁琐,并且有时系统难以运行Matl...

python 开发的三种运行模式详细介绍

python 开发的三种运行模式详细介绍

Python 三种运行模式   Python作为一门脚本语言,使用的范围很广。有的同学用来算法开发,有的用来验证逻辑,还有的作为胶水语言,用它来粘合整个系统的流程。不管怎么说,...

python实现word 2007文档转换为pdf文件

在开发过程中,会遇到在命令行下将DOC文档(或者是其他Office文档)转换为PDF的要求。比如在项目中如果手册是DOC格式的,在项目发布时希望将其转换为PDF格式,并且保留DOC中的书...

Python内置模块ConfigParser实现配置读写功能的方法

本文实例讲述了Python内置模块ConfigParser实现配置读写功能的方法。分享给大家供大家参考,具体如下: 用于对特定的配置进行操作,当前模块的名称在 python 3.x 版本...