为什么随机值与我的Python战舰游戏中的用户值不匹配?

2024-04-26 21:56:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我在guess变量的末尾应用了一个-1值,这样,如果用户为行和列输入一个介于1到5之间的值,那么这些值就可以在网格上正确匹配。因为用户不希望坐标从0开始,而是从1开始。你知道吗

一开始我假设我解决了这个问题,因为这些值与网格匹配得很好。但现在即使输入了正确的坐标,程序也不会将其注册为正确的,因为猜测值永远不会与程序之前设置的随机值匹配。你知道吗

我该如何解决这个问题?你知道吗

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:")) -1
  Guess_Battleship_Board_Column = int(input("Guess the Y coordinate:")) -1


  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-1][Guess_Battleship_Board_Column-1] == "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)

Tags: oftheinboardfordefcolumnrandom
2条回答

这似乎对我有用。我得到了4,1作为row/col值的输出,当我输入5,2时,我得到一条消息,告诉我我击沉了战舰。你知道吗

Let's play a game of Battleships!
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
4
1
Guess the X coordinate:4
Guess the Y coordinate:1
You missed my battleship
Number of turns: 1 out of 5
O O O O O
O O O O O
O O O O O
X O O O O
O O O O O
Guess the X coordinate:5
Guess the Y coordinate:2
You sunk the battleship!
My ship was here: [4][1]

如果使用1-5作为电路板坐标,则无需从用户输入中减去1,所有值都将按所需方式同步。你知道吗

def Random_Battleship_Board_Row(Battleship_Board):
    return random.randint(1, len(Battleship_Board))

def Random_Battleship_Board_Column(Battleship_Board):
    return random.randint(1, len(Battleship_Board[0]))

Guess_Battleship_Board_Row = int(input("Guess the X coordinate:"))
Guess_Battleship_Board_Column = int(input("Guess the Y coordinate:"))

哦,您需要在别处更新逻辑以反映1-5个网格值:

        if (Guess_Battleship_Board_Row < 1 or Guess_Battleship_Board_Row > 5) or (Guess_Battleship_Board_Column < 1 or Guess_Battleship_Board_Column > 5):
            print ("The inserted value is not on the grid.")

但是当使用guess值作为列表索引时,仍然需要减去1。注意:此代码与原始代码相同:

        elif(Battleship_Board[Guess_Battleship_Board_Row-1][Guess_Battleship_Board_Column-1] == "X"):
            print ("You already inserted this combination")

但这个代码是:

        else:
            print ("You missed my battleship")
            Battleship_Board[Guess_Battleship_Board_Row-1][Guess_Battleship_Board_Column-1] = "X"

相关问题 更多 >