首页 > 解决方案 > 跳棋1V1,不能移动

问题描述

我正在写一个关于跳棋的项目(1V1)。当我试图移动士兵时,它打印相同的板,我做错了什么?谢谢

示例:我正在尝试从 2X3 移动到 3X4

rows = 8
cols = 8
board = [['B', '-', 'B', '-', 'B', '-', 'B', '-'], ['-', 'B', '-', 'B', '-', 'B', '-', 'B'],
             ['B', '-', 'B', '-', 'B', '-', 'B', '-'], ['-', '-', '-', '-', '-', '-', '-', '-'],
             ['-', '-', '-', '-', '-', '-', '-', '-'], ['-', 'w', '-', 'w', '-', 'w', '-', 'w'],
             ['w', '-', 'w', '-', 'w', '-', 'w', '-'], ['-', 'w', '-', 'w', '-', 'w', '-', 'w']]
def create_game_board():  ##### this function create the game board#####
    for i in range(rows):
        list = []
        for j in range(cols):
            list.append(" ")
        board.append(list)
    return board

validTurn = True

def valid_Move_position(board):
    currentRowCube = input("what is the row of the player that you want to move? ")
    currentColCube = input("what is the col of the player that you want to move? ")
    targetRowCube = input("which cube do you want to move to? ")
    targetColCube = input("which cube do you want to move to? ")

#def move(,targetColCube, targetRowCube ):
    #if (targetRowCube, targetColCube =="-"):
        #currentRowCube = "B"
        #currentColCube = "B"

def print_board(board):  #####This function is drawing the board#####
    print("    0 1 2 3 4 5 6 7 ")
    print("    A B C D E F G H ")
    for i in range(cols):
        print(i, " |", end="")
        for j in range(rows):
            current_cell = board[i][j]
            print(current_cell + "|", end="")
        print("")

标签: python

解决方案


您的功能create_game_board出现故障。如果您的目标是创建默认板的副本,那么您可以使用单行。相反,您的函数会将 8x8 空格列表附加到默认板,因此对其进行修改。

我建议你试试这个:

def create_game_board():
    return [[v for v in line] for line in board]

推荐阅读