Design and Implementation of python Gobang Game

  • 2021-06-28 13:16:22
  • OfStack

This small case of python is the implementation of a 5-chess game, in which we can achieve two players of the 5-chess game to drop stones at a specified position, draw the board after the stones, and judge the function of winning or losing based on the function.

The idea for this case is as follows:

First, draw the board according to its shape
Then, the board is initialized to unify the positions that can fall.
Next, it's the part of the game where the two sides take turns to drop the pieces, then draw the board.
Finally, according to the position of the loser to determine the winner or loser, the game ends

Design and Implementation of 5 Chess Game

The code is as follows:


def main():
  print("5子棋游戏".center(50,'='))
  guige=int(input("请输入棋盘的规格:"))
 
  #按照5子棋的棋盘样式,画出棋盘
  print_init(guige)
 
  # 初始化棋盘
  wzq = {}
  for i in range(1, guige + 1):
    for j in range(1, guige + 1):
      wzq[(i, j)] = "+"
 
  #重画棋盘
  reprint(guige,wzq)
 
#按照5子棋的棋盘样式,画出棋盘
def print_init(guige):
  #打印出首行的棋盘列
  for i in range(guige):
    print("%4d"%(i+1),end='')
  print()
  #双重循环,第1重为棋盘的行,隔1行输出棋盘的行数
  for i in range(guige*2-1):
    #打印出类似 “1 +---+---+---+---+---+”,行号,+,―
    if(i%2==0):
      print("%-3d"%((i+2)/2),end='')
      #第2重循环,为棋盘的列,主要在于找出对应位置显示的内容
      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])
      #判断输入的位置是否有子,有子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("您输入的位置已经有子,请重新输入!")
      # 判断输赢
    # 第1种情况
    wzq_win1(wzq,guige,xuanshou)
    # 第2种情况
    wzq_win2(wzq,guige,xuanshou)
    # 第3种情况
    wzq_win3(wzq,guige,xuanshou)
    # 第4种情况
    wzq_win4(wzq,guige,xuanshou)
  else:
    print("游戏结束,平局!")
 
# 判断输赢
def wzq_win1(wzq,guige,xuanshou):
  # 第1种输赢情况
  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):
  # 第2种输赢情况
  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):
  # 第3种输赢情况
  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):
  # 第4种输赢情况
  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()

The results are as follows:


======================5 Chess Game =======================
 Please enter the specifications of the board: 8
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
6 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
 Now it's your turn X Square Fructus Setariae 
 Please enter the drop position: 5 5
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---+---X---+---+---+
  |  |  |  |  |  |  |  |
6 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
 Now it's your turn O Square Fructus Setariae 
 Please enter the drop position: 6 1
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---+---X---+---+---+
  |  |  |  |  |  |  |  |
6 O---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
 Now it's your turn X Square Fructus Setariae 
 Please enter the drop position: 4 4
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---X---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---+---X---+---+---+
  |  |  |  |  |  |  |  |
6 O---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
 Now it's your turn O Square Fructus Setariae 
 Please enter the drop position: 5 4
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---X---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---O---X---+---+---+
  |  |  |  |  |  |  |  |
6 O---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
 Now it's your turn X Square Fructus Setariae 
 Please enter the drop position: 6 6
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---X---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---O---X---+---+---+
  |  |  |  |  |  |  |  |
6 O---+---+---+---+---X---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
 Now it's your turn O Square Fructus Setariae 
 Please enter the drop position: 6 5
  1  2  3  4  5  6  7  8
1 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
2 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
3 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
4 +---+---+---X---+---+---+---+
  |  |  |  |  |  |  |  |
5 +---+---+---O---X---+---+---+
  |  |  |  |  |  |  |  |
6 O---+---+---+---O---X---+---+
  |  |  |  |  |  |  |  |
7 +---+---+---+---+---+---+---+
  |  |  |  |  |  |  |  |
8 +---+---+---+---+---+---+---+
 Now it's your turn X Square Fructus Setariae 
 Please enter the drop position: 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 +---+---+---+---+---+---+---+
 Now it's your turn O Square Fructus Setariae 
 Please enter the drop position: 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 +---+---+---+---+---+---+---+
 Now it's your turn X Square Fructus Setariae 
 Please enter the drop position: 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 Win victory , Game over! 

Related articles: