Python 3.x 我怎样才能在这个代码中得到坐标?
我即将开始使用Tkinter/Python编写一个国际象棋程序,我正在尝试移动坐标,因为我以前从未使用过坐标。如何才能使此代码立即正常工作我得到:name错误:未定义名称“x”。提前感谢您的帮助Python 3.x 我怎样才能在这个代码中得到坐标?,python-3.x,tkinter,coordinates,Python 3.x,Tkinter,Coordinates,我即将开始使用Tkinter/Python编写一个国际象棋程序,我正在尝试移动坐标,因为我以前从未使用过坐标。如何才能使此代码立即正常工作我得到:name错误:未定义名称“x”。提前感谢您的帮助 movelist=[] 骑士名单=[(x+2,y+1),(x+2,y-1),(x-2,y+1),(x+1,y+2),(x+1,y-2),(x-1,y+2),(x-1,y+2),(x-1,y-2)] 索引=0 工件位置=(3,4) newposition=(0,0) 有关入住骑士名单: newpositi
movelist=[]
骑士名单=[(x+2,y+1),(x+2,y-1),(x-2,y+1),(x+1,y+2),(x+1,y-2),(x-1,y+2),(x-1,y+2),(x-1,y-2)]
索引=0
工件位置=(3,4)
newposition=(0,0)
有关入住骑士名单:
newposition=pieceposition+KNIGHTLIST[索引]
movelist.append(newposition)
索引=索引+1
打印(移动列表)
您需要将适当的偏移量添加到当前工件位置,并返回可能的合法位置列表
也许是这样的:
def get_possible_knight_moves(pos):
x, y = pos
possible_moves = []
for dx, dy in knight_offsets:
new_pos = (x + dx, y + dy)
if is_legal(new_pos):
possible_moves.append(new_pos)
return possible_moves
def is_legal(pos):
x, y = pos
return 0 <= x < 8 and 0 <= y < 8
knight_offsets = [(2, 1), (2, -1), (1, 2), (1, -2), (-2, 1), (-2, -1),(-1, 2),(-1, -2)]
pieceposition = (3 , 4)
movelist = get_possible_knight_moves(pieceposition)
print (movelist)
对于处于
(0,0)
位置的骑士,输出是[(2,1)、(1,2)]
那么x
和y
是什么呢?
[(5, 5), (5, 3), (4, 6), (4, 2), (1, 5), (1, 3), (2, 6), (2, 2)]