我是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
答案 0 :(得分:0)
好的,您的代码中有一些错误。
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