我在Python中设置了一个战舰游戏,但是我设置的网格介于0到5之间。这意味着战舰的第一行和列将是(0,0),但是我不想这样做,因为任何搁浅的用户都可能会从1开始计数,所以他们会将( 1,1)或(1,2)值放到他们认为不会输入的值中。我如何使我的程序反映这一点,在1,1是开始列,而不是第二行。由于用户只能输入介于0到4之间的值,所以5被表示为无效值,并表示它不在网格上。
因此,唯一可能的组合是:
行: 0,1,2,3,4,列: 0,1,2,3,4
我希望它是:
行: 1、2、3、4、5栏1、2、3、4、5
这是我的代码:
import random
Battleship_Board = []
for x in range(0,5):
Battleship_Board.append(["O"] * 5)
def print_Battleship_Board(Battleship_Board):
for row in Battleship_Board:
print (" ".join(row))
print ("Let's play a game of Battleships!")
print_Battleship_Board(Battleship_Board)
def Random_Battleship_Board_Row(Battleship_Board):
return random.randint(0, len(Battleship_Board)-1)
def Random_Battleship_Board_Column(Battleship_Board):
return random.randint(0, len(Battleship_Board[0])-1)
Battleship_Board_Row = Random_Battleship_Board_Row(Battleship_Board)
Battleship_Board_Column = Random_Battleship_Board_Column(Battleship_Board)
print (Battleship_Board_Row)
print (Battleship_Board_Column)
for turn in range(5):
Guess_Battleship_Board_Row = int(input("Guess the X coordinate:"))
Guess_Battleship_Board_Column = int(input("Guess the Y coordinate:"))
if Guess_Battleship_Board_Row == Battleship_Board_Row and Guess_Battleship_Board_Column == Battleship_Board_Column:
print ("You sunk the battleship!")
print ("My ship was here: [" + str(Battleship_Board_Row) + "][" + str(Battleship_Board_Column) + "]")
break
else:
if turn + 1 == 5:
Battleship_Board[Guess_Battleship_Board_Row][Guess_Battleship_Board_Column] = "X"
print_Battleship_Board(Battleship_Board)
print ("Game Over")
print ("My ship was here: [" + str(Battleship_Board_Row) + "][" + str(Battleship_Board_Column) + "]")
if (Guess_Battleship_Board_Row < 0 or Guess_Battleship_Board_Row > 4) or (Guess_Battleship_Board_Column < 0 or Guess_Battleship_Board_Column > 4):
print ("The inserted value is not on the grid.")
elif(Battleship_Board[Guess_Battleship_Board_Row ][Guess_Battleship_Board_Column] == "X"):
print ("You already inserted this combination")
else:
print ("You missed my battleship")
Battleship_Board[Guess_Battleship_Board_Row][Guess_Battleship_Board_Column] = "X"
print ("Number of turns:", turn + 1,"out of 5")
print_Battleship_Board(Battleship_Board)发布于 2016-04-13 20:47:10
您可以从用户的猜测中减去一个,还可以添加一个注释,说明这些数字不是基于零的。记住要检查有效的输入!
Guess_Battleship_Board_Row = int(input("Guess the X coordinate:")) - 1
Guess_Battleship_Board_Column = int(input("Guess the Y coordinate:")) - 1发布于 2016-04-13 20:48:03
让用户在1到5范围内输入他们的行和列,但在放置他们的战舰之前先减去1。
例如,如果他们插入'5',减去1,然后放置船等。
Guess_Battleship_Board_Row =int(输入(“猜测X坐标:”)-1 Guess_Battleship_Board_Column =int(输入(“猜测Y坐标:”)-1
发布于 2016-04-13 21:09:26
看上去是个伟大的项目,你在这里!
有很多方法你可以解决这个问题,所以我想我可以打几个。
好的,所以所有数组都从“0”开始,但是(0,0)看起来很难看,我同意。简单的方法是创建一个6,6数组,而不使用0,0部分。
另一种方法是在显示数组索引之前将+1添加到数组索引中,或者在访问数组索引之前添加-1。
因此,当用户键入4,5时,您所要做的就是(arrayAuserinput - 1,arrayBuserinput - 1)有效地存储在3,4,并且用户永远不会知道。
同样,作为一个有趣的挑战,用字母数字字母代替行或列。3、C)
使用开关语句或if/esle语句。
例如:
if userSelection == 'a' or userSelection == 'A'
arrayA[0] = true(对不起,我的python语法很生疏,但是这些概念应该仍然适用)
希望这能帮上忙!
https://stackoverflow.com/questions/36609140
复制相似问题