我对编程比较陌生,所以请原谅这里缺少优雅的代码。该程序有点复杂,但我正处于想要完成它并查看结果的地步。无论如何,在这个函数中,我试图为计算机创建一个有效动作列表。
- board 是 64 个两个项目列表的列表,每个列表都代表黑白棋板上的一个点
- player 是玩家的棋子,可以是“X”或“O”(在程序前面确定)
- 计算机是计算机的一部分,相反
第一步是收集板上当前空白的所有点(valid_list)。接下来,我尝试遍历这些点中的每一个,以查看是否有任何相邻点是玩家的棋子。如果是,我想收集所有点,以便在同一行(或列)中有另一台计算机。该代码对我来说似乎很有意义,但它给了我意想不到的结果。我只是想知道是否有人能猜出导致奇怪结果的原因(valid_list1 列表)。
def comp_move(board, player, computer):
valid_list = []
for xcoord in range(8):
for ycoord in range(8):
if board[xcoord][ycoord] == ' ':
valid_list.append([xcoord, ycoord])
copy = getCopy(board)
num_list = [-1,0,1,-1,1,-1,0,1]
num_list2 = [-1,-1,-1,0,0,1,1,1]
for num in range(8):
for i in range(len(valid_list)):
xcoord_orig = valid_list[i][0]
ycoord_orig = valid_list[i][1]
xcoord1 = valid_list[i][0] + num_list[num]
ycoord1 = valid_list[i][1] + num_list2[num]
#test to see whether any of the surrounding spots are occupied by the player's piece
if 0 <= xcoord1 <= 7 and 0 <= ycoord1 <= 7:
piece = board[xcoord1][ycoord1]
if piece == player:
move_list = []
move_list1 = []
move_list2 = []
move_list3 = []
move_list4 = []
move_list5 = []
move_list6 = []
move_list7 = []
valid_list1 = []
#I changed the beginning of the range to 2 because we already know that the adjacent piece is the player's piece
#test spots above
for i in range(2,8):
#iterate through spots above the computer's spot.
#create a list of all the spots above the computer's spot
xcoordT = xcoord_orig
ycoordT = ycoord_orig - i
if 0 <= ycoordT <= 7:
if board[xcoordT][ycoordT] == computer:
move_list.append([xcoordT, ycoordT])
if move_list:
valid_list1.append([xcoord_orig, ycoord_orig])
#Test spots below
for i in range(2,8):
xcoordB = xcoord_orig
ycoordB = ycoord_orig + i
if 0 <= ycoordB <= 7:
if board[xcoordB][ycoordB] == computer:
move_list1.append([xcoordB, ycoordB])
if move_list1:
valid_list1.append([xcoord_orig, ycoord_orig])
#Test spots to the right
for i in range(2,8):
xcoordR = xcoord_orig + i
ycoordR = ycoord_orig
if 0 <= xcoordR <= 7:
if board[xcoordR][ycoordR] == computer:
move_list2.append([xcoordR, ycoordR])
if move_list2:
valid_list1.append([xcoord_orig, ycoord_orig])
#Test spots to the left
for i in range(2,8):
xcoordL = xcoord_orig - i
ycoordL = ycoord_orig
if 0 <= xcoordL <= 7:
if board[xcoordL][ycoordL] == computer:
move_list3.append([xcoordL, ycoordL])
if move_list3:
valid_list1.append([xcoord_orig, ycoord_orig])
#Test upper-right diagonal spots
for i in range(2,8):
xcoordTD = xcoord_orig + i
ycoordTD = ycoord_orig - i
if 0 <= xcoordTD <= 7 and 0 <= ycoordTD <= 7:
if board[xcoordTD][ycoordTD] == computer:
move_list4.append([xcoordTD, ycoordTD])
if move_list4:
valid_list1.append([xcoord_orig, ycoord_orig])
#Test lower-right diagonal spots
for i in range(2,8):
xcoordBD = xcoord_orig + i
ycoordBD = ycoord_orig + i
if 0 <= xcoordBD <= 7 and 0 <= ycoordBD <= 7:
if board[xcoordBD][ycoordBD] == computer:
move_list5.append([xcoordBD, ycoordBD])
if move_list5:
valid_list1.append([xcoord_orig, ycoord_orig])
#Test upper-left diagonal spots
for i in range(2,8):
xcoordTD1 = xcoord_orig - i
ycoordTD1 = ycoord_orig - i
if 0 <= xcoordTD1 <= 7 and 0 <= ycoordTD1 <= 7:
if board[xcoordTD1][ycoordTD1] == computer:
move_list6.append([xcoordTD1, ycoordTD1])
if move_list6:
valid_list1.append([xcoord_orig, ycoord_orig])
#Test lower-left diagal spots
for i in range(2,8):
xcoordBD1 = xcoord_orig - i
ycoordBD1 = ycoord_orig + i
if 0 <= xcoordBD1 <= 7 and 0 <= ycoordBD1 <= 7:
if board[xcoordBD1][ycoordBD1] == computer:
move_list7.append([xcoordBD1, ycoordBD1])
if move_list7:
valid_list1.append([xcoord_orig, ycoord_orig])