I'm relatively new to programming so please excuse the lack of elegant code here. The program has gotten a little convoluted but I'm at the point where I want to finish it and see the result. Anyway, in this function I'm trying to create a list of valid moves for the computer.
- board is a list of 64 two item lists each of which represents a spot on the reversi board
- player is the player's piece which is either 'X' or 'O' (determined earlier in the program)
- computer is the computer's piece which is the opposite
First step is to collect all of the spots on the board that are currently blank (valid_list). Next I'm trying to loop through each of these spots to see if any adjacent spot is the player's piece. If it is, I want to collect all the spots such that there is another computer piece in the same row (or column). The code seems to make sense to me but it gives me unexpected results. I'm just wondering if anyone can guess what's causing the strange results (the valid_list1 list).
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])