亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當它應該顯示更改的列表時,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 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號