Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The code above works perfectly. It makes <code>make_computer_move(board, avail_col)</code> to return the column number, when a given columns is filled, to main. In main this column is taken out before calling again make_computer_board:</p> <pre><code>import random def display_board(board): col = " " cols = len(board[0]) for index1 in range(cols): col += "%i " % index1 print col for index2 in range(len(board)): print str(index2) + ": " + " | ".join(board[index2]) + " |" print " " + "---+" * cols def make_computer_move(board, avail_cols): col = random.choice(avail_cols) for row in range(len(board)-1, -1, -1): # counts from bottom of board and up if board[row][col] == " ": # if there is a blank space, put a "O" and break print "The pairing is (%i,%i)" % (row,col) #Print the coordinates board[row][col] = 'O' break if row == 0: #arrives to the last row return col def main(): board = [[" ", " ", " ", " ", " "] for i in range(5)] avail_cols = range(len(board[0])) display_board(board) for counter in range(25): filled = make_computer_move(board, avail_cols) display_board(board) if filled is not None: avail_cols.remove(filled) main() </code></pre> <p>Note:</p> <ol> <li>random import is now at the top.</li> <li>Code has been beautified a bit following PEP-8.</li> <li>I prepare the board with a list comprehension</li> <li>The two calls to <code>display_board(board)</code>. First one draw starting step, the secon one has been moved after <code>make_computer_move</code> to draw the last figure after the program is finish</li> <li>The use of % interpolations to simplify some lines</li> </ol> <p>There are still some inefficiencies. For example you calculate <code>len(board[0])</code> each time you call the function when this is not neccesary as the board stays always with the same size.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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