1

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])
AJ.
  • 27,586
  • 18
  • 84
  • 94
  • 2
    "gives me unexpected results"? Really? You'll have to explain that in some detail. Also, it's very, very helpful to put print statements (or functions) into your code to trace execution and show your intermediate results. Please (1) define what's unexpected and (2) put in enough print statements to narrow down the problem and (3) based on that, post the least piece of code that includes the actual problem. – S.Lott May 17 '11 at 19:04
  • What results are you getting, and what do you expect? Providing some sample input & output will help narrow down where the problem may be. Also, if this is homework you should tag it as such. – SteveMc May 17 '11 at 19:06
  • Have you tried using a debugger on it? The code is too long for the bug to be obvious on a cursory inspection. The trick now is to learn how figure out what went wrong. – Winston Ewert May 17 '11 at 19:09
  • 1
    This function is way too long. If you break it up in smaller steps, you can test and verify every little piece. So, from your description "if any adjacent spot is the player's piece", that should naturally translate to a if statement and one function "adjacent_spot_is_the_players_piece". Hope that helps. – Ishtar May 17 '11 at 19:16
  • Why is the board 64 two-item lists? What are the 2 items? – phkahler May 17 '11 at 19:30

3 Answers3

0

I couldn't quite figure it out from your question, but from your code it looks like your valid_list1 will be filled with lists containing all the same values (the computer's original coordinates). I don't think that's what you want.

If you replace your if move_listi: statements (where i is the move_list number) with valid_list1 += move_listi

For example:

# 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

Again, I'm not entirely sure that that was what you were asking. If it's not, let me know. valid_list1 will contain a list of all places where there is a computer piece in line-of-sight from the original computer piece.

Bryce Siedschlaw
  • 4,136
  • 1
  • 24
  • 36
0

I like to represent the othello board as a 10x10 with a row of empty squares around the edge. Then rather than using x and y coordinates, you represent position as a single value P=Y*10+x where x and y go from 0 to 9. Then your num_list and numlist2 become a single list with direction values dir=[-11,-10,-9,-1,1,9,10,11] representing the 8 directions. You scan the board from position p by looking at board[p+dir[d]*distance]. Now Python doesn't have arrays and looking at the Nth item of a list may not be the most efficient way, but whatever you're doing in 2D becomes simpler in 1D. BTW since you're always scanning adjacent or diagonal squares, and you're looking only so far as the next square is occupied by the opponent piece or your own, the loop will naturally terminate at the border of empty squares and you will never have to check if you're running off the edge of the board. This doesn't solve your specific implementation issue, but this implementation will be much easier to debug :-)

phkahler
  • 5,687
  • 1
  • 23
  • 31
-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]
"""

how do you program this knowing that you have four input , or if you want how do you program in other to get the best valid_moves .

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
josias
  • 1
  • 1