我对编程比较陌生,所以请原谅这里没有优雅的代码。该程序有点令人费解,但我想要完成它并查看结果。无论如何,在这个函数中,我正在尝试为计算机创建一个有效的移动列表。
第一步是收集电路板上当前空白的所有位置(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])
答案 0 :(得分:0)
我无法从您的问题中弄明白,但是从您的代码中看起来您的valid_list1将填充包含所有相同值(计算机的原始坐标)的列表。我认为这不是你想要的。
如果您使用if move_listi:
替换valid_list1 += move_listi
语句(其中i是move_list号码)
例如:
# if move_list: -- REMOVE THESE LINES
# valid_list1.append([xcoord_orig, ycoord_orig])
valid_list1 += move_list
...
# if move_list1: -- REMOVE THESE LINES
# valid_list1.append([xcoord_orig, ycoord_orig])
valid_list1 += move_list1
...
# Repeat for move_list2 - move_list7
同样,我不完全确定那就是你所要求的。如果不是,请告诉我。 valid_list1将包含所有地方的列表,其中有一台计算机部件与原始计算机部件在视线范围内。
答案 1 :(得分:0)
我喜欢将othello板表示为10x10,边缘周围有一排空方块。然后,而不是使用x和y坐标,您将位置表示为单个值P = Y * 10 + x,其中x和y从0到9.然后您的num_list和numlist2成为单个列表,方向值为dir = [ - 11 ,-10,-9,-1,1,9,10,11]代表8个方向。您可以通过查看电路板[p + dir [d] *距离]从位置p扫描电路板。现在Python没有数组,查看列表的第N项可能不是最有效的方法,但无论你在2D中做什么在1D中变得更简单。顺便说一句,因为你总是扫描相邻或对角线方块,而你只看到下一个方块被对手或你自己的方块占据,这个循环将自然地终止于空方块的边界,你永远不会有检查你是否跑掉了董事会的边缘。这并不能解决您的具体实现问题,但此实现将更容易调试: - )
答案 2 :(得分:-1)
import numpy
def ai_player(board, scores, turn, valid_moves):
"""
Input:
board: numpy array of the disks for each player, e.g.
[[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 2, 0, 0, 0],
[0, 0, 0, 2, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
- 0 empty locations
- 1 player 1's disks
- 2 player 2's disks
scores: list of score values - [0, 0]
turn: integer turn counter (starts from 1)
valid_moves: numpy array of valid disk locations, represented in row
column combinations:
e.g [[3, 5], # row 3 - col 5
[5, 3], # row 5 - col 3
[4, 2], # etc.
[3, 4]]
Return:
disk loction index --> a numpy array or a list
--> a valid row - col combination to place a disk
--> location must be empty
--> location must have flanks
e.g. if valid_moves = [[3, 5], # row 3 - col 5
[5, 3], # row 5 - col 3
[4, 2], # etc.
[3, 4]]
and you want to place a disk at location row 4, col 2 then you
need to return [4, 2]
"""
如何编程,知道你有四个输入,或者如果你想如何在其他程序中编程以获得最佳的valid_moves。