两个应该相等的值不相等

问题描述 投票:0回答:1

我正在尝试从终端运行国际象棋游戏,您必须输入一个动作才能移动棋子,并且我试图让系统检查玩家输入的棋子是否与玩家输入的棋子相同使用 if 语句位于上一个位置,并且首先可以工作,但是当您尝试移动已经移动的棋子时,它会说它不是同一棋子

我期待 if 语句说它们是相同的并移动该块(我们删除旧块并添加新块)

这是代码


#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
from time import sleep
import colorama
from colorama import Back, Fore, Style


board = [["R", "N", "B", "Q", "K", "B", "N", "R"],
         ["P", "P", "P", "P", "P", "P", "P", "P"],
         [" ", " ", " ", " ", " ", " ", " ", " "],
         [" ", " ", " ", " ", " ", " ", " ", " "],
         [" ", " ", " ", " ", " ", " ", " ", " "],
         [" ", " ", " ", " ", " ", " ", " ", " "],
         ["p", "p", "p", "p", "p", "p", "p", "p"],
         ["r", "n", "b", "q", "k", "b", "n", "r"],]

piece_m = ""
row_m = ""
colomn_m = ""

xColomn_m = ""
xRow_m = ""
xPiece_m = ""

piece_c = Fore.WHITE

def draw_b():
    print(Fore.YELLOW+" +---+---+---+---+---+---+---+---+")
    print("8| {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} |".format(Fore.BLACK, board[7][0], Fore.YELLOW, Fore.BLACK, board[7][1], Fore.YELLOW, Fore.BLACK, board[7][2], Fore.YELLOW, Fore.BLACK, board[7][3], Fore.YELLOW, Fore.BLACK, board[7][4], Fore.YELLOW, Fore.BLACK, board[7][5], Fore.YELLOW, Fore.BLACK, board[7][6], Fore.YELLOW, Fore.BLACK, board[7][7], Fore.YELLOW))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("7| {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} |".format(Fore.BLACK, board[6][0], Fore.YELLOW, Fore.BLACK, board[6][1], Fore.YELLOW, Fore.BLACK, board[6][2], Fore.YELLOW, Fore.BLACK, board[6][3], Fore.YELLOW, Fore.BLACK, board[6][4], Fore.YELLOW, Fore.BLACK, board[6][5], Fore.YELLOW, Fore.BLACK, board[6][6], Fore.YELLOW, Fore.BLACK, board[6][7], Fore.YELLOW))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("6| {} | {} | {} | {} | {} | {} | {} | {} |".format(board[5][0], board[5][1], board[5][2], board[5][3], board[5][4], board[5][5], board[5][6], board[5][7]))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("5| {} | {} | {} | {} | {} | {} | {} | {} |".format(board[4][0], board[4][1], board[4][2], board[4][3], board[4][4], board[4][5], board[4][6], board[4][7]))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("4| {} | {} | {} | {} | {} | {} | {} | {} |".format(board[3][0], board[3][1], board[3][2], board[3][3], board[3][4], board[3][5], board[3][6], board[3][7]))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("3| {} | {} | {} | {} | {} | {} | {} | {} |".format(board[2][0], board[2][1], board[2][2], board[2][3], board[2][4], board[2][5], board[2][6], board[2][7]))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("2| {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} |".format(Fore.WHITE, board[1][0], Fore.YELLOW, Fore.WHITE, board[1][1], Fore.YELLOW, Fore.WHITE, board[1][2], Fore.YELLOW, Fore.WHITE, board[1][3], Fore.YELLOW, Fore.WHITE, board[1][4], Fore.YELLOW, Fore.WHITE, board[1][5], Fore.YELLOW, Fore.WHITE, board[1][6], Fore.YELLOW, Fore.WHITE, board[1][7], Fore.YELLOW))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("1| {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} | {}{}{} |".format(Fore.WHITE, board[0][0], Fore.YELLOW, Fore.WHITE, board[0][1], Fore.YELLOW, Fore.WHITE, board[0][2], Fore.YELLOW, Fore.WHITE, board[0][3], Fore.YELLOW, Fore.WHITE, board[0][4], Fore.YELLOW, Fore.WHITE, board[0][5], Fore.YELLOW, Fore.WHITE, board[0][6], Fore.YELLOW, Fore.WHITE, board[0][7], Fore.YELLOW))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("   a   b   c   d   e   f   g   h  "+Fore.WHITE)#emptyrom*********************************************************************************************

draw_b()

while True:

    #game loop

    try:
        move = input("Enter your move: ")
    except ValueError:
        move = input("Please enter a correct move 'xxx piece_letter+the_colom+the_row': ")

    if move == "exit":
        print("\n\nexiting program now")
        sleep(1)
        print("on 3...")
        sleep(0.3)
        print("2")
        sleep(0.3)
        print("1")
        sleep(0.2)
        print("have a beautiful day")
        exit()
  
    piece_m = move[0]

    try:
        xColomn_m = 1+"abcdefgh".index(move[1])
    except ValueError:
        try:
            xColomn_m = 1+"ABCDEFGH".index(move[1])
            print("the move has been made with out any errors for the next move please use lower case for less error and less memory usage")
        except ValueError:
            xColomn_m = 1+"abcdefgh".index(input("An error has occurred. Please check for spaces or numbers when entering the previous column: "))
    xRow_m = move[2]
    try:
        colomn_m = 1+"abcdefgh".index(move[1])
    except ValueError:
        try:
            colomn_m = 1+"ABCDEFGH".index(move[1])
            print("the move has been made with out any errors for the next move please use lower case for less errors and less memory usage")
        except ValueError:
            colomn_m = 1+"abcdefgh".index(input("An error has occurred. Please check for spaces or numbers when entering the next column: "))
    row_m = move[4]

    if piece_m.isupper():
        piece_c = Fore.WHITE
    elif piece_m.islower():
        piece_c = Fore.BLACK

    xPiece_m = board[int(xRow_m)-1][int(xColomn_m)-1]

    print(xPiece_m)
    print(piece_m)
    if piece_c+xPiece_m == piece_c+piece_m:
        board[int(xRow_m)-1][int(xColomn_m)-1] = " "
        board[int(row_m)-1][int(colomn_m)-1] = f"{piece_c}{piece_m}{Fore.YELLOW}"
        draw_b()
    else:
        print("\n\n\nyou are trying to move a piece that does not match the starting piece please enter a valid move if you are trying to end the game or exit type 'exit'\n\n")
        sleep(2)

    print(Fore.WHITE)
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


python python-3.x if-statement chess python-chess
1个回答
0
投票

嗨,

重新定位小部件后,文本颜色发生变化,影响最终结果。使用独特的艺术品并将其与小部件组合可能会导致识别,因为该艺术品现在以不同的颜色显示(

Fore.WHITE
Fore.BLACK
)。

您可以尝试这个解决方案:

检查作品时,仅关注基本字符,忽略颜色或布局。为此,不要考虑颜色,仅验证基本字符。

要解决该问题,请按以下方式修改代码:

检查棋子时不要使用piece_c。 检查没有添加颜色或纹理的同一件作品。

您应该只检查该作品,而不考虑添加到该作品的颜色或布局。这可以通过仅对基本字符而不是颜色进行验证来完成。

以下是如何修改代码来解决问题:

检查棋子时省略使用

piece_c
。 查看未添加颜色或纹理的同一件作品。

© www.soinside.com 2019 - 2024. All rights reserved.