【发布时间】:2016-05-27 23:14:14
【问题描述】:
我目前正在尝试为游戏编写代码,该游戏很像跳棋。我将我的电路板表示为 10 x 10 矩阵。这个游戏有两种移动方式,你可以做一个步骤,检查你附近的所有位置,如果其中任何一个位置是空的,你就可以进入它们。然后,如果您旁边的位置被占用,但该位置旁边的空间是空的,您可以跳到占据所述位置的玩家的顶部。到目前为止,我已经完成了我的游戏的三段重要代码。我的第一段代码是棋盘,第二段代码是两个变量,每个变量包含每个玩家筹码的坐标列表,第三部分是两个函数,一个允许我跳,另一个允许我跳做步骤。
这是我的代码:
import numpy as np
matrix = np.array([[1,1,1,1,1,0,0,0,0,0], #0 this is the board, it's represented by a 10x10 matrix
[1,1,1,1,0,0,0,0,0,0], #1 the 0s represent empty spaces
[1,1,1,0,0,0,0,0,0,0], #2 the 1s represent player 1's chips
[1,1,0,0,0,0,0,0,0,0], #3 the 2s represetn player 2's chips
[1,0,0,0,0,0,0,0,0,0], #4
[0,0,0,0,0,0,0,0,0,2], #5
[0,0,0,0,0,0,0,0,2,2], #6
[0,0,0,0,0,0,0,2,2,2], #7
[0,0,0,0,0,0,2,2,2,2], #8
[0,0,0,0,0,2,2,2,2,2]]) #9
chips1 = list(tuple(map(tuple,np.fliplr(np.argwhere(matrix == 1))))) #Finds all the positions that contain a 1
chips2 = list(tuple(map(tuple,np.fliplr(np.argwhere(matrix == 2))))) #Finds all the positions that contain a 2
print chips2
def step(x,y): #Finds the possible steps for a checker
listMoves = []
if x > 0 and matrix[x-1][y] == 0: #left
listMoves.append([x-1,y])
if x < 9 and matrix[x+1][y] == 0: #right
listMoves.append([x+1,y])
if y < 9: #up
if matrix[x][y+1] == 0:
listMoves.append([x,y+1])
if x>0 and matrix[x-1][y+1] == 0: #up + left
listMoves.append([x-1,y+1])
if x < 9 and matrix[x+1][y+1] == 0: #up + right
listMoves.append([x+1,y+1])
if y > 0: #down
if matrix[x][y-1] == 0:
listMoves.append([x,y-1])
if x > 0 and matrix[x-1][y-1] == 0: #down + left
listMoves.append([x-1,y-1])
if x<9 and matrix[x+1][y-1] == 0:
listMoves.append([x+1,y-1])
return listMoves
def hopper(x,y): #Finds the possible hops for a checker
listHops = []
if x > 1 and matrix[x-1][y] != 0 and matrix[x-2][y] == 0: #left
listHops.append([x-2,y])
if x < 8 and matrix[x+1][y] != 0 and matrix[x+2][y] == 0: #right
listHops.append([x+2,y])
if y > 1:
if matrix[x][y-1] != 0 and matrix[x][y-2] == 0: #down
listHops.append([x,y-2])
if x>1 and matrix[x-1][y-1] != 0 and matrix[x-2][y-2] == 0: #down + left
listHops.append([x-2,y-2])
if x < 8 and matrix[x+1][y+1] != 0 and matrix[x+2][y-2] == 0: #up + right
listHops.append([x+2,y-2])
if y < 8:
if matrix[x][y+1] != 0 and matrix[x][y+2] == 0: #up
listHops.append([x,y+2])
if x > 1 and matrix[x-1][y+1] != 0 and matrix[x-2][y+2] == 0: #up + left
listHops.append([x-2,y+2])
if x < 8 and matrix[x+1][y+1] != 0 and matrix[x+2][y+2] == 0: #up + right
listHops.append([x+2,y+2])
listHops = listHops + step(x,y)
return listHops
'''
def flipBoard():
global matrix
matrix = np.fliplr(np.flipud(matrix))
return matrix.tolist()
flipBoard()
'''
for i in range(0,len(chips2)): #loops through the list of positions to find all the possible steps and hops for
print hopper(chips2[i][0], chips2[i][1])#every single checker on the board
print matrix
for 循环用于遍历我所有筹码的列表并找到所有筹码的所有可能移动。当我用棋盘左上角的 1 个筹码执行此操作时,它会返回所有可能的玩法,如果有任何用处,则为 32。但是,当我使用 2 个芯片运行相同的功能时,出现以下错误:
Traceback (most recent call last):
File "<ipython-input-21-3429014a2777>", line 80, in <module>
print hopper(chips2[i][0], chips2[i][1])#every single checker on the board
File "<ipython-input-21-3429014a2777>", line 59, in hopper
if x < 8 and matrix[x+1][y+1] != 0 and matrix[x+2][y-2] == 0: #up + right
IndexError: index 10 is out of bounds for axis 0 with size 10
到目前为止,我已经设法发现它仅发生在跳跃时,并且仅在您尝试向右跳跃时发生。我尝试将其更改为 x
【问题讨论】:
-
显示完整的回溯。还打印我在指定代码中的内容
-
刚刚编辑了问题以添加完整的回溯。你让我打印 l 是什么意思?
-
我的意思是打印 x 或 y,在失败之前查看它是什么。它可能会给你一个线索,但下面的答案可能适合你