Python Connect Four。让电脑移动

时间:2012-01-15 22:50:19

标签: python

我需要一些帮助才能完成连接四项任务。我的电脑移动功能有问题。在我的任务中,我想使用一个列表作为董事会。列表中的列表数是行。列表中列表中的项目是cols。

board=[[" "," "," "," "," "],
       [" "," "," "," "," "],
       [" "," "," "," "," "],
       [" "," "," "," "," "],
       [" "," "," "," "," "]]

共有5行和5列。因此25个免费细胞。 main函数循环25次并调用make_computer_move函数。display_board函数并不重要。问题在于make_computer_move。它应该填满整个板,因为有25个自由单元和主循环25次。但事实并非如此。还剩下空白区域。 它也不会打印它所做的移动的坐标。我放了一个print语句,这样无论何时发生有效移动,都会打印坐标。我注意到有时候电路板在循环中保持不变,没有任何反应。 我很难过:/

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()

4 个答案:

答案 0 :(得分:0)

最简单的说,如果它在该列中找不到空闲插槽,则需要再次调用make_computer_move(),因为此时,您设置了一个新列,然后对其执行任何操作。< / p>

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
       make_computer_move(board)

请注意,您所持有的方法并不是特别有效,因为没有保证它会找到一个免费插槽。您可能希望至少执行类似于具有columns_not_full = [0,1,2,... n]的操作,然后在填充最后一个插槽时删除列,并使用random.choice(columns_not_full)来获取要播放的列

答案 1 :(得分:0)

这将为您提供随机打开列的索引(或行,具体取决于您的查看方式)。

openColumns = [i for i in range(len(board)) if board[i].count(' ') > 0]
if openColumns: ComputerChoice = openColumns[random.randint(0, len(openColumns)]
else: #do something if there are no open columns (i.e. the board is full)

答案 2 :(得分:0)

首先,您应该测试至少一个空白区域,通常导入位于程序的顶部。

import random

def make_computer_move(board): for row in board: ## look for first empty space if " " in row: while True: ## infinite loop col=random.randint(0,(len(board[0])-1)) for row in range(len(board)-1,-1,-1): if board[row][col]==" ": print "The pairing is("+str(row),str(col)+")" #Print the coordinates board[row][col] = 'O' return board print "All squares filled -- Game Over"

Another alternative is to create a list of available spaces and choose

available = [] for row in range(5): for col in range(5): if board[row][col] == " ": available.append([row, col]) move = random.choice(available)

答案 3 :(得分:0)

上面的代码完美无缺。它使make_computer_move(board, avail_col)在填充给定列时将列号返回到main。在main中,在再次调用make_computer_board:

之前取出该列
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()

注意:

  1. 随机导入现在位于顶部。
  2. 在PEP-8之后,代码已经被美化了一点。
  3. 我使用列表理解准备董事会
  4. display_board(board)的两次调用。第一个画画开始 步骤,secon one已在make_computer_move之后移动 程序完成后绘制最后一个数字
  5. 使用%插值来简化某些行
  6. 仍然存在一些效率低下的问题。例如,每次调用函数时都会计算len(board[0]),因为电路板始终保持相同的大小。