如何将棋子放入Python棋盘网格中?

发布于 2025-01-09 22:29:26 字数 787 浏览 0 评论 0原文

我是一名正在从事大学项目的初学者,我需要制作一个在 python 控制台中运行的国际象棋游戏(不能使用国际象棋导入),我刚刚完成了棋盘,但现在我不知道如何将棋子放入,有人可以帮忙吗?(它们可以用 Pawn=P 等字母表示)

这就是我到目前为止所做的


def create_grid(): 
""" Creates the grid return=an array that represents the grid """
 
    tab = []
    for i in range(GRID_SIZE - 1):
        tab.append([]) 
        for j in range(GRID_SIZE):
            tab[i].append('')
    return tab
 
def print_grid(tab):
 """ prints the grid """ 

  print( ' 1','2','3','4','5','6','7','8') 
  for i in range(GRID_SIZE - 1): 
      print("{} ".format(i+1), end="")
      for j in range(GRID_SIZE):
          print(" {}".format(tab[i][j]), end="")
          if j < GRID_SIZE: 
             print("|", end="")

      if i < GRID_SIZE:
         print()

print_grid(create_grid)

I am a begginer working on a college project and i need to make a chess game that works in the python console(can´t use the chess import), i just completed the board but now i have no idea how to put the pieces in,can someone help?(they can be represented by letters like Pawn=P )

this is what i did so far


def create_grid(): 
""" Creates the grid return=an array that represents the grid """
 
    tab = []
    for i in range(GRID_SIZE - 1):
        tab.append([]) 
        for j in range(GRID_SIZE):
            tab[i].append('')
    return tab
 
def print_grid(tab):
 """ prints the grid """ 

  print( ' 1','2','3','4','5','6','7','8') 
  for i in range(GRID_SIZE - 1): 
      print("{} ".format(i+1), end="")
      for j in range(GRID_SIZE):
          print(" {}".format(tab[i][j]), end="")
          if j < GRID_SIZE: 
             print("|", end="")

      if i < GRID_SIZE:
         print()

print_grid(create_grid)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零崎曲识 2025-01-16 22:29:26

尝试一下并根据需要进行改进。我考虑了 @GC 的评论来打印 unicodes。

GRID_SIZE = 8


def create_grid():
    """Creates the grid return=an array that represents the grid"""

    tab = []
    for i in range(GRID_SIZE):
        tab.append([])
        for j in range(GRID_SIZE):
            tab[i].append("")
    return tab


def print_grid(tab):
    """prints the grid"""

    print(" 1", "2", "3", "4", "5", "6", "7", "8")
    for i in range(GRID_SIZE):
        print("{} ".format(i + 1), end="")
        for j in range(GRID_SIZE):
            print("\u2654{}".format(tab[i][j]), end="")
            if j < GRID_SIZE:
                print("|", end="")

        if i < GRID_SIZE:
            print()


print_grid(create_grid())

它会打印出这样的东西

 1 2 3 4 5 6 7 8
1 ♔|♔|♔|♔|♔|♔|♔|♔|
2 ♔|♔|♔|♔|♔|♔|♔|♔|
3 ♔|♔|♔|♔|♔|♔|♔|♔|
4 ♔|♔|♔|♔|♔|♔|♔|♔|
5 ♔|♔|♔|♔|♔|♔|♔|♔|
6 ♔|♔|♔|♔|♔|♔|♔|♔|
7 ♔|♔|♔|♔|♔|♔|♔|♔|
8 ♔|♔|♔|♔|♔|♔|♔|♔|

Try this and improve as needed. I considered @G.C's comment to print unicodes.

GRID_SIZE = 8


def create_grid():
    """Creates the grid return=an array that represents the grid"""

    tab = []
    for i in range(GRID_SIZE):
        tab.append([])
        for j in range(GRID_SIZE):
            tab[i].append("")
    return tab


def print_grid(tab):
    """prints the grid"""

    print(" 1", "2", "3", "4", "5", "6", "7", "8")
    for i in range(GRID_SIZE):
        print("{} ".format(i + 1), end="")
        for j in range(GRID_SIZE):
            print("\u2654{}".format(tab[i][j]), end="")
            if j < GRID_SIZE:
                print("|", end="")

        if i < GRID_SIZE:
            print()


print_grid(create_grid())

It will print something like this

 1 2 3 4 5 6 7 8
1 ♔|♔|♔|♔|♔|♔|♔|♔|
2 ♔|♔|♔|♔|♔|♔|♔|♔|
3 ♔|♔|♔|♔|♔|♔|♔|♔|
4 ♔|♔|♔|♔|♔|♔|♔|♔|
5 ♔|♔|♔|♔|♔|♔|♔|♔|
6 ♔|♔|♔|♔|♔|♔|♔|♔|
7 ♔|♔|♔|♔|♔|♔|♔|♔|
8 ♔|♔|♔|♔|♔|♔|♔|♔|
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文