【发布时间】:2017-09-02 22:13:20
【问题描述】:
我正在尝试构建一些 python 脚本,如果有人从 x 位置开始,它可以概述国际象棋游戏中可用的动作。我目前的代码如下:
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)}))
但理想情况下,我想将 if 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]) 除了: 通过``
将b8作为一个位置,并基于它输出一个答案。
希望这是有道理的。感谢您在此处提供的任何帮助。
【问题讨论】:
-
根据您在下面的评论,我认为您应该编辑您的问题以反映您的真正要求。我的回答简化了
getKnightMoves函数,但听起来您希望找到一种方法来简化整个事情。对吗? -
我更新了答案的第二部分以使用正确的相对移动列表
标签: python python-3.x