3

我目前正在使用 Python 编写猫和老鼠程序,在设置我的板时,我想知道是否有更有效的方法来替换我的 2d 列表中的“占位符”。目前我正在做:

# setting dimensions and printing all placeholders as '[ . ]'
rows, cols = (6, 10)
board = [['[ . ]' for i in range(cols)] for j in range(rows)]

# manually changing placeholders to add numbers for coordinate system on each side
# changing values of top of board
board[0][0] = '     '
board[0][1] = '  1  '
board[0][2] = '  2  '
board[0][3] = '  3  '
board[0][4] = '  4  '
board[0][5] = '  5  '
board[0][6] = '  6  '
board[0][7] = '  7  '
board[0][8] = '  8  '
board[0][9] = '     '
# changes left side of board
board[1][0] = '  1  '
board[2][0] = '  2  '
board[3][0] = '  3  '
board[4][0] = '  4  '
board[5][0] = '     '

以此类推板的右侧和底部。最终棋盘看起来像: 空棋盘

我想有一种更有效的方法可以做到这一点,但我不确定我会如何做到这一点。任何帮助将不胜感激,谢谢。

4

2 回答 2

2

下面的解决方案使用该库,因为它是用于矩阵操作的优秀(如果不是)numpy。您可能熟悉也可能不熟悉它,所以我尝试使用更明确的代码并进行相应的评论。

示例代码使用两个函数:

  • build()构建初始游戏板。
  • show()在游戏的任何阶段展示棋盘。

未来发展:
我留了一些东西让你自己开发。一些想法是:

  • 代码可以优化为class.
  • 包括一个move()可以根据每个玩家的动作更新棋盘的功能。但我会把这个小项目留给你去研究和实施。

示例代码:

import numpy as np

def build() -> np.ndarray:
    """Build the board.
    
    Returns:
        A new playing board as a ``np.ndarray``.
    
    """
    # Board configuration.
    cols = 8
    rows = 4
    # Create horizontal, vertical and dot lists.
    h = list(range(0, cols+1)) + [0]
    v = list(range(0, rows+1)) + [0]
    d = ['.' for i in range(1, cols+1)]
    # Initialise a new (empty) matrix of size.
    m = np.zeros([rows+2, cols+2], dtype=str)
    # Populate vertical labels.
    m[:, 0] = np.array(v)
    m[:, -1] = np.array(v)
    # Populate horizontal labels.
    m[0, :] = np.array(h)
    m[-1, :] = np.array(h)
    # Populate dots.
    m[1:-1, 1:-1] = np.array(d)
    # Replace remaining zeros with a space.
    m[m == '0'] = ' '
    # Return the new playing board.
    return m

def show(board):
    """Display the ndarray as a board.
    
    Args:
        board (np.ndarray): Board to be displayed.
    
    """
    for row in board:
        print(' '.join(row))

# Create and show a new playing board.
board = build()
show(board=board)

新板:

  1 2 3 4 5 6 7 8  
1 . . . . . . . . 1
2 . . . . . . . . 2
3 . . . . . . . . 3
4 . . . . . . . . 4
  1 2 3 4 5 6 7 8  

玩!:

我不确定游戏是如何玩的,但假设每个玩家在下面转一圈,xPlayer1 是, Player2 是o

棋盘会随着每个玩家的棋子而更新。

# Update `board` for [row, col]
board[1, 3] = 'x'
board[3, 6] = 'o'

# Show the updated board.
show(board=board)

更新板:

  1 2 3 4 5 6 7 8  
1 . . x . . . . . 1
2 . . . . . . . . 2
3 . . . . . o . . 3
4 . . . . . . . . 4
  1 2 3 4 5 6 7 8  
于 2020-10-05T08:23:30.397 回答
1

您可以对顶/底和右/左墙使用两个循环,然后只需手动设置角落:

rows, cols = (6, 10)
board = [['[ . ]' for i in range(cols)] for j in range(rows)]

for i in range(1, rows-1):
    board[i][0] = board[i][cols-1] = f'  {i}  '

for j in range(1, cols-1):
    board[0][j] = board[rows-1][j] = f'  {j}  '

board[0][0] = board[0][cols-1] = board[rows-1][0] = board[rows-1][cols-1] = '     '

要打印它,您可以使用:print(*(' '.join(row) for row in board), sep='\n')它给出:

        1     2     3     4     5     6     7     8        
  1   [ . ] [ . ] [ . ] [ . ] [ . ] [ . ] [ . ] [ . ]   1  
  2   [ . ] [ . ] [ . ] [ . ] [ . ] [ . ] [ . ] [ . ]   2  
  3   [ . ] [ . ] [ . ] [ . ] [ . ] [ . ] [ . ] [ . ]   3  
  4   [ . ] [ . ] [ . ] [ . ] [ . ] [ . ] [ . ] [ . ]   4  
        1     2     3     4     5     6     7     8        

您还可以通过检查索引将其压缩到一个循环:

for i in range(1, max(rows, cols)-1):
    val = f'  {i}  '
    if i < rows-1:
        board[i][0] = board[i][cols-1] = val
    if i < cols-1:
        board[0][i] = board[rows-1][i] = val
于 2020-10-05T08:38:00.117 回答