Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何输入connect 4的自定义板尺寸?_Python_Python 3.x - Fatal编程技术网

Python 如何输入connect 4的自定义板尺寸?

Python 如何输入connect 4的自定义板尺寸?,python,python-3.x,Python,Python 3.x,我在一个小组中工作,并已获得此代码。我们正在调整我们的python代码以支持tkinter。是否有任何方法可以修改电路板的尺寸(在本任务的第1部分中作为预设值提供),以便用户可以输入自己的尺寸 输入(N)应该是N+4行和N+3列(比如说,如果用户不输入任何内容,板的默认尺寸将是3x4。我对python有点陌生,所以任何东西都会有帮助 干杯 代码: 答案将取决于您是希望用户在实例化tkinter显示之前输入电路板尺寸,还是将其作为tkinter显示的一部分。是的,这就是我正在尝试做的,我要求用户输

我在一个小组中工作,并已获得此代码。我们正在调整我们的python代码以支持tkinter。是否有任何方法可以修改电路板的尺寸(在本任务的第1部分中作为预设值提供),以便用户可以输入自己的尺寸

输入(N)应该是N+4行和N+3列(比如说,如果用户不输入任何内容,板的默认尺寸将是3x4。我对python有点陌生,所以任何东西都会有帮助

干杯

代码:


答案将取决于您是希望用户在实例化tkinter显示之前输入电路板尺寸,还是将其作为tkinter显示的一部分。是的,这就是我正在尝试做的,我要求用户输入,单击按钮后,它应以给定尺寸打印电路板
#importing random so that the basic ai can allocate a random section to drop disk
import random
import tkinter as tk
#make move function prints move location (including column and height)
def make_move(player, choose_stack):
    print('[DEBUG] make_move:', player, choose_stack)

    if bottom_circles[choose_stack] >= 6:
        return False # can't make move

    col = choose_stack
    row = bottom_circles[col]
    circle = circles[col][row]
    print('[DEBUG] col/row/circles:', col, row, circle)

    canvases[col].itemconfig(circle, fill=player)
    bottom_circles[col] += 1

    return True # move was OK

def on_click(event, number):
    global player

    # --- human ---

    player = 'red'

    choose_stack = number

    if not make_move(player, choose_stack):
        print("Wrong move - full stack")
        return # skip computer move

    # TODO: check if human wins

    #if sum(bottom_circles) == 7*6:
    if all(x == 6 for x in bottom_circles):
        print("Board is full")
        return # skip computer move

    # --- computer ---

    player = 'blue'

    while True:
        choose_stack = random.randint(0, 6)

        if not make_move(player, choose_stack):
            print("Wrong move - full stack")
        else:
            break 

    # TODO: check if computer wins

    #if sum(bottom_circles) == 7*6:
    if all(x == 6 for x in bottom_circles):
        print("Board is full")

# --- main ---

window = tk.Tk()
window.title("Connect 4")

player = 'blue'
bottom_circles = [0]*7

canvases = []
circles = []

for col in range(7):
    canvas = tk.Canvas(window, width=100, height=600, background='gray')
    canvas.grid(row=0, column=col)
    canvas.bind("<Button-1>", lambda event, number=col:on_click(event, number))

    canvases.append(canvas)

    circles_column = []
    for row in range(5, -1, -1):
        circle = canvas.create_oval(0, row*100, 100, (row+1)*100, fill="white")
        circles_column.append(circle)

    circles.append(circles_column)

window.mainloop()

game = { 
    'N' : 4,    
    'board':[], # the connect-n board
    'moves':[] # the moves by players 
}

def create_board(n):
    game['N'] = n
    global boardSize
    boardSize = n
    
    # your code should instead create an empty board with N+3 rows and N+2 Columns
    # example code below, creating a board with 4 rows and 3 columns, with some pre-filled disks
    # 0 - empty, 1 - white disk, 2 - black disk
    #board = [[0,1,2],[0,0,0], [0,0,0], [0,0,0]]
    
    board = []
    for i in range(n+3):
        row = [0]*(n+2)
        board.append(row)
    game['board'] = board