如何使用以下代码在connect 4中实现移动功能?

2024-05-18 12:04:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个连接4游戏,其中棋盘的尺寸可以手动输入为N,其中棋盘遵守规则,必须有N+3行和N+2列,因此,例如,如果我输入5作为N,那么尺寸将是8行和7列。我在使用drop_disk(c)功能时遇到了问题,因为我不知道如何接受用户输入并将输入转换为移动。关于如何实现此功能,有什么建议吗

我的代码:

# %load connect_n.py

WHITE_DISK = '\u26AA'
BLACK_DISK = '\u26AB'
FW_SPACE = '\u3000' # fullwidth space
FW_LINE = '\u2015' # fullwidth horizontal line

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

def create_board(n):
    '''create the board for the Connect-N game'''
    game['N'] = 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
    
def display_board():
    '''print the board content'''
    
    # Unicode example below. You should update the code below by
    # 1. use a for loop
    # 2. allow it to display board of different sizes
    
    w = f'{WHITE_DISK:^3}' # white
    b = f'{BLACK_DISK:^3}' # black
    e = f'{FW_SPACE:^3}' # empty
    separator = FW_LINE*20
    board = game['board']
    for row  in range(game['N']+3):
        s = ''
        for col in range(game['N']+2): # you should use nested for loop here
            cell = board[row][col] 
            if cell == 0: 
                s = s + f'| {e}'
            elif cell == 1:
                s = s + f'| {w}'
            elif cell == 2:
                s = s + f'| {b}'
        print(s)
    
def drop_disk(c):
    #for i in range
    '''drop disk at column c'''
    print('drop disk at...')
    
#def check_winning(board):    
#def save_board(fname):
#    '''save the game'''
#    # you should save all the information in the game
#    print('save_board')
    
#def load_board(fname):
#    '''load the game'''
#    # load the game
#    print('load board')

#def show_moves():
#    '''show all previous moves'''
#    # for example, ['W2', 'B2'] means white drops disk column 2 and black drops disk at column 2
#    print('show all moves')
    
# EXAMPLE SCEANRIO BELOW
#N = int(input('Please input the size of your board:'))

#N justifies size of the board AND winning condition 
#(for example if you input N = 2, the board will have an area of 5 x 4,
#but the player will have to get 4 in a row to win too.)


N = int (input("please enter board size")) # your code should allow user inputs instead
create_board(N)
#game["board"][0][0] = 1
#game["board"][0][1] = 1
#game["board"][1][0] = 2
#print(game["board"])

display_board()


#drop_disk(2) # white drop disk at column 2
#check_winning()
#drop_disk(2) # black drop disk at column 2
#check_winning()
#drop_disk(3) # white drop disk at column 3
#check_winning()
#drop_disk(3) # black drop disk at column 3
#check_winning()
#drop_disk(4) # white drop disk at column 4
#check_winning() # white wins because she conencts 3 disks
#display_board()




#dropping disk func framework 
#must loop through rows back to front (from 5 to 0 for example), 
#for i in range()
#    board[1][C]
#if == 0:
#board[i][c]
#    = 2
#break 

#Remember, connect 4 works with allocated columns,    
#it is not like tik tak toe where you can put things anywhere,    
#if you put it in a specified column it goes to the lowest point    
#of the column.

如果有很多评论,一半是注释,一半是我还没有真正使用过的代码段,请道歉。 为任何帮助干杯

p.s.第1行,其中说明%load CONNCCET_n.py不影响当前代码

输出:

please enter board size: 5
|    |    |    |    |    |    |    
|    |    |    |    |    |    |    
|    |    |    |    |    |    |    
|    |    |    |    |    |    |    
|    |    |    |    |    |    |    
|    |    |    |    |    |    |    
|    |    |    |    |    |    |    
|    |    |    |    |    |    |    

Tags: thetoinboardgamefordefload