Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Connect Four. Making the computer move
    primarykey
    data
    text
    <p>I need some help with my connect four assignment. My make computer move function has problems. In my assignment I am suppose use a list as the board. The number of lists within the list is the row. And the items within the list within the list are the cols.</p> <pre><code>board=[[" "," "," "," "," "], [" "," "," "," "," "], [" "," "," "," "," "], [" "," "," "," "," "], [" "," "," "," "," "]] </code></pre> <p>There are 5 rows and 5 cols. Therefore 25 free cells. The <code>main</code> function loops 25 times and calls the <code>make_computer_move</code> function.The <code>display_board</code> function is not important. The problem is with the <code>make_computer_move</code>. It should fill the entire board since there are 25 free cells and the main loops 25 times. But it doesn't. There are blank spaces left. Also it doesn't print the coordinates of the move it made. I put a print statement so that whenever a valid move occurs, the coordinates are printed. I noticed that sometimes the board stays the same in the loop and nothing happens. I'm stumped :/</p> <pre><code>def display_board(board): col=" " for index1 in range(len(board[0])): col+=str(index1)+" " print col for index2 in range(len(board)): print str(index2)+": "+" | ".join(board[index2])+" |" print " "+"---+"*(len(board[0])) def make_computer_move(board): import random col=random.randint(0,(len(board[0])-1)) for row in range(len(board)-1,-1,-1): # counts from the bottom of the board and up if board[row][col]==" ": #if there is a blank space it will put a "O" and break print "The pairing is("+str(row),str(col)+")" #Print the coordinates board[row][col] = 'O' break else: #if the break does not occur this else statement executes col=random.randint(0,(len(board[0])-1)) def main(): board=[[" "," "," "," "," "],[" "," "," "," "," "],[" "," "," "," "," "],[" "," "," "," "," "],[" "," "," "," "," "]] for counter in range(25): display_board(board) make_computer_move(board) main() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload