国际象棋骑士,可用动作
Chess Knight, Available movement
# Input coordinates for position, convert to integers.
start = [int(input("x: ")),int(input("y: "))]
# Available moves for Knight ( 3 steps like **L**)
available = [
[start[0]-1,start[1]-2],
[start[0]-2,start[1]-1],
[start[0]+1,start[1]-2],
[start[0]+2,start[1]-1],
[start[0]-1,start[1]+2],
[start[0]-2,start[1]+1],
[start[0]+1,start[1]+2],
[start[0]+2,start[1]+1]
]
对于每一行水平打印 (1-8) 并且对于每一列字符 "O"。如果坐标**(x,y)** 在可用列表中打印 M。第二个 if 检查棋盘上哪里可以移动。
used_positions = 0
for y in range(8):
print(y+1,end=" ")0-8 # y+1 will start from 1-8 and not 0- 8
letters = ['A','B','C','D','E','F','G','H']
for x in range(8):
chr = "O"
if [x+1,y+1] == start:
chr = "S"
if [x+1,y+1] in available:
used_positions+=1
chr = "M"
print(chr,end="") # skips newline
print() # after every column newline
print(end=" ") # newline, 2 whitespaces
for l in letters:
print(l,end="") # prints letters
print() # newline
print("Possible positions:",used_positions) # prints how many positions are possible
print(available,end='') # prints available positions in same line
那是因为骑士在L
移动。所以,-1 和 -2 是为了遵循这种方式。例如:
⬜
⬜
下2步,还剩1步。
例如我的输入是 x=2, y=2。四个可能的位置:[4,1]、[3,4]、[4,3]、[1,4],标记为 M. 每个 x 和 y 都是在可用位置计算的。例如。 [开始[0]-1,开始[1]-2], 2-1 = 1 (x = 1) , 2-2 = 0 (y) , [1,0].
1 OOOMOOOO
2 OSOOOOOO
3 OOOMOOOO
4 MOMOOOOO
5 OOOOOOOO
6 OOOOOOOO
7 OOOOOOOO
8 OOOOOOOO
ABCDEFGH
Possible positions: 4
[[1, 0], [0, 1], [3, 0], [4, 1], [1, 4], [0, 3], [3, 4], [4, 3]]
您可以在过滤掉 'out-of-board' 个位置的列表理解中实现此:
def knightMoves(x,y):
return [(x+dx,y+dy)
for h,v in [(1,2),(2,1)] # magnitudes
for dx,dy in [(h,v),(h,-v),(-h,v),(-h,-v)] # directions
if x+dx in range(1,9) and y+dy in range(1,9) ] # in-board
输出:
knightMoves(4,5)
[(5, 7), (5, 3), (3, 7), (3, 3), (6, 6), (6, 4), (2, 6), (2, 4)]
knightMoves(8,8)
[(7, 6), (6, 7)]
# Input coordinates for position, convert to integers.
start = [int(input("x: ")),int(input("y: "))]
# Available moves for Knight ( 3 steps like **L**)
available = [
[start[0]-1,start[1]-2],
[start[0]-2,start[1]-1],
[start[0]+1,start[1]-2],
[start[0]+2,start[1]-1],
[start[0]-1,start[1]+2],
[start[0]-2,start[1]+1],
[start[0]+1,start[1]+2],
[start[0]+2,start[1]+1]
]
对于每一行水平打印 (1-8) 并且对于每一列字符 "O"。如果坐标**(x,y)** 在可用列表中打印 M。第二个 if 检查棋盘上哪里可以移动。
used_positions = 0
for y in range(8):
print(y+1,end=" ")0-8 # y+1 will start from 1-8 and not 0- 8
letters = ['A','B','C','D','E','F','G','H']
for x in range(8):
chr = "O"
if [x+1,y+1] == start:
chr = "S"
if [x+1,y+1] in available:
used_positions+=1
chr = "M"
print(chr,end="") # skips newline
print() # after every column newline
print(end=" ") # newline, 2 whitespaces
for l in letters:
print(l,end="") # prints letters
print() # newline
print("Possible positions:",used_positions) # prints how many positions are possible
print(available,end='') # prints available positions in same line
那是因为骑士在L
移动。所以,-1 和 -2 是为了遵循这种方式。例如:
⬜
⬜
下2步,还剩1步。
例如我的输入是 x=2, y=2。四个可能的位置:[4,1]、[3,4]、[4,3]、[1,4],标记为 M. 每个 x 和 y 都是在可用位置计算的。例如。 [开始[0]-1,开始[1]-2], 2-1 = 1 (x = 1) , 2-2 = 0 (y) , [1,0].
1 OOOMOOOO
2 OSOOOOOO
3 OOOMOOOO
4 MOMOOOOO
5 OOOOOOOO
6 OOOOOOOO
7 OOOOOOOO
8 OOOOOOOO
ABCDEFGH
Possible positions: 4
[[1, 0], [0, 1], [3, 0], [4, 1], [1, 4], [0, 3], [3, 4], [4, 3]]
您可以在过滤掉 'out-of-board' 个位置的列表理解中实现此:
def knightMoves(x,y):
return [(x+dx,y+dy)
for h,v in [(1,2),(2,1)] # magnitudes
for dx,dy in [(h,v),(h,-v),(-h,v),(-h,-v)] # directions
if x+dx in range(1,9) and y+dy in range(1,9) ] # in-board
输出:
knightMoves(4,5)
[(5, 7), (5, 3), (3, 7), (3, 3), (6, 6), (6, 4), (2, 6), (2, 4)]
knightMoves(8,8)
[(7, 6), (6, 7)]