Python 巨蟒战舰小游戏

Python 巨蟒战舰小游戏,python,python-3.x,Python,Python 3.x,我目前正在使用Python3制作一个简单的战舰游戏,但我似乎无法让棋盘显示出来。这是我的密码 # Battleships def main(): pass if __name__ == '__main__': main() from random import randint # this initialises the board board = [] for x in range(5): board.append(["O"] * 5) def print_

我目前正在使用Python3制作一个简单的战舰游戏,但我似乎无法让棋盘显示出来。这是我的密码

# Battleships

def main():
    pass

if __name__ == '__main__':
    main()

from random import randint

# this initialises the board
board = []

for x in range(5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print (" ".join(row))

# this starts the game and prints the board
    print ("Let's play Battleship!")
    print_board(board)

# defines the location of the ship
def random_row(board):
    return randint(0, len(board) - 1)

def random_col(board):
    return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)

# asks the player to make a guess
for turn in range(5):
    guess_row = int(input("Guess Row:"))
    guess_col = int(input("Guess Col:"))

# if the player guesses correctly, then the game ends cleanly
if guess_row == ship_row and guess_col == ship_col:
    print ("Congratulations! You sunk my battleship!")

else:
# if the player guesses outside the board, then the following message appears
    if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
        print ("Oh dear, you've hit an island!")

# a warning if the guess has already been made by the player
    elif(board[guess_row][guess_col] == "X"):
        print ("That guess has already been made.")

# if the guess is wrong, then the relevant board place is marked with an X
    else:
        print ("You've missed my battleship!")
        board[guess_row][guess_col] = "X"

# prints the turn and updates the board accordingly
    print ("Turn " + str(turn+1) + " out of 5.")
    print_board(board)

# if the user has had 5 guesses, it's game over
if turn >= 3:
    print ("You sunk my battleship! We're gonna need a bigger boat.")
战列舰 def main(): 通过 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu': main() 从随机导入randint #这将初始化董事会 董事会=[] 对于范围(5)内的x: 董事会追加([“O”]*5) def打印板(板): 对于板中的行: 打印(“.”连接(行)) #这将启动游戏并打印棋盘 打印(“让我们玩战舰吧!”) 印刷电路板 #定义船舶的位置 def随机_行(板): 返回randint(0,len(板)-1) def随机_柱(板): 返回randint(0,len(板[0])-1) 船行=随机船行(板) 船列=随机列(船列) #让玩家猜一猜 对于交车范围(5): 猜测行=int(输入(“猜测行:”) guess_col=int(输入(“guess col:”) #如果玩家猜对了,游戏就完蛋了 如果guess\u row==ship\u row和guess\u col==ship\u col: 打印(“祝贺你!你击沉了我的战舰!”) 其他: #如果玩家在棋盘外猜测,则出现以下消息 如果(猜测行<0或猜测行>4)或(猜测列<0或猜测列>4): 打印(“哦,天哪,你撞到一个岛了!”) #如果玩家已经猜到了,则发出警告 elif(董事会[猜测行][猜测列]=“X”): 打印(“已经做出了猜测。”) #如果猜测是错误的,那么相关的电路板位置用X标记 其他: 打印(“你错过了我的战舰!”) 棋盘[猜一排][猜一列]=“X” #打印回合并相应更新棋盘 打印(“旋转”+str(旋转+1)+“共5次”) 印刷电路板 #如果用户有5次猜测,游戏就结束了 如果圈数>=3: 打印(“你击沉了我的战舰!我们需要一艘更大的船。”) 游戏接受坐标,但不打印任何与棋盘有关的内容,也不打印玩家重复猜测或超出游戏范围的内容


任何帮助都将不胜感激

在对代码进行任何操作之前,您的代码要求进行5组猜测,因为响应猜测的代码不在要求猜测的循环中。我,嗯,猜在你的测试中,你从来没有输入足够的猜测来通过这个循环。将猜测处理代码移动到循环中,您至少应该看到对这些猜测的反应。

您的代码在对它们进行任何操作之前要求进行5组猜测,因为响应猜测的代码在要求进行猜测的循环之外。我,嗯,猜在你的测试中,你从来没有输入足够的猜测来通过这个循环。将猜测处理代码移动到循环中,您至少应该看到对这些猜测的反应。

您在循环中迭代,然后是所有if语句。将所有if语句放入for循环中。添加一个计数器,说明如果你击中了我的飞船,请加一分(
score+=1
),然后


在循环中迭代所有if语句。将所有if语句放入for循环中。添加一个计数器,说明如果你击中了我的飞船,请加一分(
score+=1
),然后


您可以在
print\u board
内部调用
print\u board
。我猜这是一个缩进问题,不是缩进问题。程序没有突出显示它是一个问题。如果它导致程序的行为与您预期的不同,这就是一个问题。您可以调用
print\u board
inside
print\u board
。我猜这是一个缩进问题,不是缩进问题。程序没有突出显示它是一个问题。如果它导致程序的行为与您预期的不同,那就是一个问题。我已经尝试过这一点,但我得到了“RuntimeError:相比之下超过了最大递归深度”,这是我之前得到的结果。这也是我超过猜测次数时得到的结果,以及以下重复数十次;>>五分之一。哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦!哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦!O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O OO@interjay在问题的评论中已经解释了这一点:现在您正在调用
print\u board
,由于(再次)缩进问题,它被错误地写为递归。我已经尝试过这一点,但我得到了“RuntimeError:在比较中超过了最大递归深度”,这就是我以前得到的。这也是我超过猜测次数时得到的结果,以及以下重复数十次;>>五分之一。奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥奥!哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦!O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O OO@interjay在问题的评论中已经解释了这一点:现在您正在调用
print\u board
,由于(再次)缩进问题,它被错误地写为递归。仍然得到相同的错误。不确定我是否做得正确?仍然会得到相同的错误。不确定我是否做对了?
if score >= 3:
    print ("You sunk my battleship! We're gonna need a bigger boat.")