猿问

当它应该显示更改的列表时,python 给出无输出值

我是python的新手。我正在制作一个类似于井字棋的游戏,但规模更大。我能够在用户输入之前看到网格,但是在用户输入他们希望他们的作品去哪里之后,更新的网格不会显示。输出只会说没有。我认为我的问题在于我如何显示板,但我不确定。请帮忙


print("This is Gomoku Deluxe!")


#starting off with tic-tac-toe


import os


#this is where the code for the game starts

board = []


for i in range(19):

 board.append(["O"] * 19)



#function to print board

def print_board():

    board = []

    for i in range(19):

      board.append(["O"] * 19)

    for row in board:

      print (row)


#function to place player piece

def drop_point(board, row, col, piece):

    board[row][col] = piece


#function checks for empty spot

def valid_location(board, row, col):

    return board[row][col] == 0


# this loop handles user's piece 

print_board()

turn = 0

game_over = False


while not game_over:

    if turn == 0:

      row = int(input("Player 1 Select a row: "))

      col = int(input("Player 1 Select a col: "))


      if valid_location(board, row, col):

          drop_point(board, row, col, 1)


    else:

      row1 = int(input("Player 2 Select a row: "))

      col1 = int(input("Player 2 Select a col: "))


      if valid_location(board, row1, col1):

          drop_point(board, row1, col1, 2)


    print_board()


    turn += 1

    turn = turn % 2



Helenr
浏览 120回答 1
1回答

慕无忌1623718

好的,您的代码中有一些错误。1.) 在valid_location函数中,0即使您将其指定为"O"2.) 在print_board您每次制作新板的功能中。3.) 在drop_point函数中,你只是board在函数内部赋值这是一些新的和改进的代码:print("This is Gomoku Deluxe!")#starting off with tic-tac-toeimport os#this is where the code for the game startsboard = []for i in range(19): board.append([0] * 19)#function to print boarddef print_board(brd):    for row in brd:      print (row)#function to place player piecedef drop_point(brd, row, col, piece):    brd[row][col] = piece    return brd#function checks for empty spotdef valid_location(board, row, col):    return board[row][col] == 0# this loop handles user's piece print_board(board)turn = 0game_over = Falsewhile not game_over:    if turn == 0:      row = int(input("Player 1 Select a row: "))      col = int(input("Player 1 Select a col: "))      if valid_location(board, row, col):          board = drop_point(board, row, col, 1)    else:      row1 = int(input("Player 2 Select a row: "))      col1 = int(input("Player 2 Select a col: "))      if valid_location(board, row1, col1):          board = drop_point(board, row1, col1, 2)    print_board(board)    turn += 1    turn = turn % 2
随时随地看视频慕课网APP

相关分类

Python
我要回答