2017-09-02 43 views
0

我正在嘗試構建一些python腳本,該腳本可以概述如果某人在x位置開始的棋局中可用的棋步。我的代碼當前如下:如果語句在python中的if語句中用於象棋遊戲

import argparse, json 

chessBoard = [[1, 1, 1, 1, 1, 1, 1, 1] for i in range(8)] 

chess_map_from_alpha_to_index = { 
    "b" : 0, 
    "c" : 1, 
    "d" : 2, 
    "e" : 3, 
    "f" : 4, 
    "g" : 5, 
    "h" : 6, 
    "i" : 7 
} 

chess_map_from_index_to_alpha = { 
    0: "b", 
    1: "c", 
    2: "d", 
    3: "e", 
    4: "f", 
    5: "g", 
    6: "h", 
    7: "i" 
} 


def getRookMoves(pos, chessBoard): 
    column, row = list(pos.strip().lower()) 
    row = int(row) - 1 
    column = chess_map_from_alpha_to_index[column] 
    i,j = row, column 
    solutionMoves = [] 

    # Compute the moves in Rank 
    for j in xrange(8): 
     if j != column: 
      solutionMoves.append((row, j)) 

    # Compute the moves in File 
    for i in xrange(8): 
     if i != row: 
      solutionMoves.append((i, column)) 

    solutionMoves = ["".join([chess_map_from_index_to_alpha[i[1]], str(i[0] + 1)]) for i in solutionMoves] 
    solutionMoves.sort() 
    return solutionMoves 

def getKnightMoves(pos, chessBoard): 
    """ A function that returns the all possible moves 
     of a knight stood on a given position 
    """ 
    column, row = list(pos.strip().lower()) 
    row = int(row) - 1 
    column = chess_map_from_alpha_to_index[column] 
    i,j = row, column 
    solutionMoves = [] 

    try: 
     temp = chessBoard[i - 6][j + 1] 
     solutionMoves.append([i - 6, j + 1]) 
    except: 
     pass 
    try: 
     temp = chessBoard[i - 3][j + 2] 
     solutionMoves.append([i - 3, j + 2]) 
    except: 
     pass 
    try: 
     temp = chessBoard[i - 5][j + 3] 
     solutionMoves.append([i - 5, j + 3]) 
    except: 
     pass 
    try: 
     temp = chessBoard[i - 7][j + 4] 
     solutionMoves.append([i - 7, j + 4]) 
    except: 
     pass 
    try: 
     temp = chessBoard[i - 1][j + 5] 
     solutionMoves.append([i - 1, j + 5]) 
    except: 
     pass 
    try: 
     temp = chessBoard[i - 4][j + 6] 
     solutionMoves.append([i - 4, j + 6]) 
    except: 
     pass 
    try: 
     temp = chessBoard[i - 2][j + 7] 
     solutionMoves.append([i - 2, j + 7]) 
    except: 
     pass 
    # Filter all negative values 
    temp = [i for i in solutionMoves if i[0] >=0 and i[1] >=0] 
    allPossibleMoves = ["".join([chess_map_from_index_to_alpha[i[1]], str(i[0] + 1)]) for i in temp] 
    allPossibleMoves.sort() 
    return allPossibleMoves 

parser = argparse.ArgumentParser() 
parser.add_argument("-p", "--piece", help="chess piece name: ex- rook, knight, pawn etc") 
parser.add_argument("-l", "--location", help="chess notation string: ex- E4, D6 etc") 
args = parser.parse_args() 

piece = args.piece.strip().lower() 
location = args.location.strip() 
# According to the type of piece adjust function 
if (piece == "rook"): 
    print (json.dumps({"piece":piece, 
         "current_location": location, 
         "moves": getRookMoves(location, chessBoard)})) 
elif (piece == "knight"): 
    print (json.dumps({"piece":piece, 
         "current_location": location, 
         "moves": getKnightMoves(location, chessBoard)})) 

但是理想情況下,我想保持在末尾如果elif的json.dumps但修改try: temp = chessBoard[i - 5][j + 3] solutionMoves.append([i - 5, j + 3]) except: pass

部分,因此這個if語句也成爲。因此,如果一個人輸入python main.py -p「knight」-l「b8」,那麼代碼讀取`temp = chessBoard [i - 5] [j + 3] solutionMoves.append([i - 5,j + 3]) 除外: pass``

與b8有關,並根據它輸出答案。

希望這是有道理的。感謝在這裏給予的任何幫助。

+0

每下方的評論,我覺得你應該修改你的問題,以反映你真的在問。我的回答簡化了'getKnightMoves'函數,但它聽起來像你希望有一種方法來簡化整個事情。那是對的嗎? – Robb

+0

我更新了我的答案的第二部分以使用正確的相對移動列表 – Robb

回答

0

您正在依靠list index out of range異常觸發您的嘗試代碼。如果你退後一步,想一想爲什麼拋出異常,你可以將它重新加入到所需的if語句中。發生這種異常是因爲你的其中一個索引大於棋盤上的列表。在任一方向的棋盤是8個項目,所以你可以只對證0和8:

if 0 <= i-3 < 8 and 0 <= j+2 < 8: 
    solutionMoves.append([i - 3, j + 2]) 

你必須有額外的負過濾器之前之所以爲負值的列表中選擇有效的索引值。你可以立即擺脫那些。

您可能需要考慮創建一個您正在檢查的所有案例(例如(-6,1),(-3,2)等)的列表,並將for循環中的if檢查嵌入到減少代碼重複。這樣的事情:

# possible_moves are moves relative to current location 
possible_moves = [(2, -1), (2, 1), (-1, 2), (1, 2), (-2, -1), (-2, 1), (-1, -2), (1, -2)] 
for move in possible_moves: 
    new_loc = (i + move[0], j + move[1]) 
    if 0 <= new_loc[0] < 8 and 0 <= new_loc[1] < 8: 
     solutionMoves.append(new_loc) 
0

你剛纔檢查的舉動不會把你從董事會?你能做到這一點的東西,如:

rf = i - 6 
cf = j + 1 
if 0 <= rf < 8 and 0 <= cf < 8: 
    solutionMoves.append((rf, cf)) 

雖然我可能失去了一些東西,你可能想看看這個: How do I generate all of a knight's moves?

+0

感謝您的快速響應。我不確定我是否原來很清楚,所以我想進一步解釋。理想情況下,我想說一個場景,我可以說一塊(假設騎士來放鬆)在B8中,然後做x移動,如果在B7中移動(完全分開的移動)。我的想法是它可以作爲一個if語句來完成,所以如果location = b8返回x個移動,elif位置= b2返回y個移動等等,但是每個return都獨立於最後一個,所以b8會列出可能的移動列表與b2等不同,等等 – sc131