为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Helenr 2021-08-24 14:52:36
我是python的新手。我正在制作一个类似于井字棋的游戏,但规模更大。我能够在用户输入之前看到网格,但是在用户输入他们希望他们的作品去哪里之后,更新的网格不会显示。输出只会说没有。我认为我的问题在于我如何显示板,但我不确定。请帮忙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(["O"] * 19)#function to print boarddef print_board():    board = []    for i in range(19):      board.append(["O"] * 19)    for row in board:      print (row)#function to place player piecedef drop_point(board, row, col, piece):    board[row][col] = piece#function checks for empty spotdef valid_location(board, row, col):    return board[row][col] == 0# this loop handles user's piece print_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):          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
查看完整描述

1 回答

?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

好的,您的代码中有一些错误。


1.) 在valid_location函数中,0即使您将其指定为"O"


2.) 在print_board您每次制作新板的功能中。


3.) 在drop_point函数中,你只是board在函数内部赋值


这是一些新的和改进的代码:


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([0] * 19)



#function to print board

def print_board(brd):

    for row in brd:

      print (row)


#function to place player piece

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

    brd[row][col] = piece

    return brd


#function checks for empty spot

def valid_location(board, row, col):

    return board[row][col] == 0


# this loop handles user's piece 

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

          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



查看完整回答
反对 回复 2021-08-24
  • 1 回答
  • 0 关注
  • 119 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信