用python重复某些东西

用python重复某些东西,python,Python,我目前面临着一个问题,那就是试图让某些东西重复它自己。下面的代码是一个游戏,用户必须在网格中定位宝箱(如战舰)。代码将1随机宝箱放置在网格中,用户必须找到它。(这些都在变量名treasure\u row=random\u row(board)…treasure\u col=random\u col(board))下,但是我希望在网格中放置的不仅仅是1个宝箱(例如10或20个) 我该怎么做 from random import randint board = [] for x in range

我目前面临着一个问题,那就是试图让某些东西重复它自己。下面的代码是一个游戏,用户必须在网格中定位宝箱(如战舰)。代码将1随机宝箱放置在网格中,用户必须找到它。(这些都在变量名
treasure\u row=random\u row(board)…treasure\u col=random\u col(board)
)下,但是我希望在网格中放置的不仅仅是1个宝箱(例如10或20个)

我该怎么做

from random import randint

board = []

for x in range(8):
    board.append(["•"] * 8)

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

print ("You have chosen the option to play the game")
print_board(board)

def random_row(board):
    return randint(1, len(board) - 1)

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

treasure_row = random_row(board)
treasure_col = random_col(board)

turn = 0
for turn in range(64):
    turn = turn + 1
    guess_row = int(input("Guess Row:"))
    guess_col = int(input("Guess Col:"))

    if guess_row == treasure_row and guess_col == treasure_col:
        print ("Congratulations! you have found a treasure chest!")
        break
    else:
        if (guess_row>8) or (guess_col>8):
                print ("Sorry, that isn't on the grid.")
        elif(board[guess_row][guess_col] == "X"):
                print ("You guessed that one already.")
        else:
            if turn == 64:
                print ("Game Over")
            else:
                print ("Sorry there is no treasure here!")
                board[guess_row][guess_col] = "X"
                print  ("Turn", turn + 1)
    # Print (turn + 1) here!
        print_board(board)

最简单的方法是通过元组列表存储宝箱:

treasure_chests = [(1, 2), (4, 5), (1, 4)]
然后使用y中的x比较猜测:

if (guess_row, guess_col) in treasure_chests:
如果您想生成宝藏,我会使用如下函数:

def generate_treasure(num, max_x, max_y):
    treasure_chests = []
    while len(treasure_chests) < num:
        new_chest = (random.randint(1, max_x), random.randint(1, max_y))
        if new_chest not in treasure_chests:
            treasure_chests.append(new_chest)
    return treasure_chests

最简单的方法是通过元组列表存储宝箱:

treasure_chests = [(1, 2), (4, 5), (1, 4)]
然后使用y中的x比较猜测:

if (guess_row, guess_col) in treasure_chests:
如果您想生成宝藏,我会使用如下函数:

def generate_treasure(num, max_x, max_y):
    treasure_chests = []
    while len(treasure_chests) < num:
        new_chest = (random.randint(1, max_x), random.randint(1, max_y))
        if new_chest not in treasure_chests:
            treasure_chests.append(new_chest)
    return treasure_chests

在您的情况下,只需创建两个不同的
treasure\u行
s和
treasure\u列
s。像这样:

treasure_row_1 = random_row(board)
treasure_col_1 = random_col(board)

treasure_row_2 = random_row(board)
treasure_col_2 = random_col(board)

turn = 0
for turn in range(64):
    turn = turn + 1
    guess_row = int(input("Guess Row:"))
    guess_col = int(input("Guess Col:"))

    if guess_row == treasure_row_1 and guess_col == treasure_col_1 or guess_row == treasure_row_2 and guess_col == treasure_col_2:

或者通过元组数组执行此操作。如您所见,只需创建两个不同的
珍宝行
s和
珍宝列
s即可。像这样:

treasure_row_1 = random_row(board)
treasure_col_1 = random_col(board)

treasure_row_2 = random_row(board)
treasure_col_2 = random_col(board)

turn = 0
for turn in range(64):
    turn = turn + 1
    guess_row = int(input("Guess Row:"))
    guess_col = int(input("Guess Col:"))

    if guess_row == treasure_row_1 and guess_col == treasure_col_1 or guess_row == treasure_row_2 and guess_col == treasure_col_2:

或者通过元组数组执行此操作。如您所见

您可以随时拥有宝箱位置的
列表
宝箱位置
检查:

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

treasure_locations = []
   while len(treasure_locations < 10): #Creates 10 locations
      treasure = random_row(board), random_col(board)
      if treasure in treasure_locations: #duplicate, try again
         continue
      treasure_locations.append(treasure)

turn = 0
for turn in range(64):
    if( not treasure_locations ):
       print "You found all the treasure!"
       break
    turn = turn + 1
    guess_row = int(input("Guess Row:"))
    guess_col = int(input("Guess Col:"))
    guess_loc = guess_row, guess_col
    if guess_loc in treasure_locations:
        print ("Congratulations! you have found a treasure chest!")
        treasure_locations.remove(guess_loc ) #remove once found
        break
    else:
        #...
#。。。
def随机_柱(板):
返回randint(1,len(板[0])-1)
宝藏位置=[]
而len(宝藏位置<10):创建10个位置
宝藏=随机行(板),随机列(板)
如果宝藏位置中的宝藏:#重复,请重试
持续
宝藏位置。附加(宝藏)
转动=0
对于交车范围(64):
如果(不包括地点):
打印“你找到了所有的宝藏!”
打破
转弯=转弯+1
猜测行=int(输入(“猜测行:”)
guess_col=int(输入(“guess col:”)
猜一猜,猜一猜行,猜一猜列
如果猜到宝藏位置的位置:
打印(“恭喜!你找到了一个宝箱!”)
宝藏位置。移除(猜测位置)#发现后移除
打破
其他:
#...

您可以始终拥有一个
宝箱位置列表
宝箱位置
您可以根据以下内容进行检查:

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

treasure_locations = []
   while len(treasure_locations < 10): #Creates 10 locations
      treasure = random_row(board), random_col(board)
      if treasure in treasure_locations: #duplicate, try again
         continue
      treasure_locations.append(treasure)

turn = 0
for turn in range(64):
    if( not treasure_locations ):
       print "You found all the treasure!"
       break
    turn = turn + 1
    guess_row = int(input("Guess Row:"))
    guess_col = int(input("Guess Col:"))
    guess_loc = guess_row, guess_col
    if guess_loc in treasure_locations:
        print ("Congratulations! you have found a treasure chest!")
        treasure_locations.remove(guess_loc ) #remove once found
        break
    else:
        #...
#。。。
def随机_柱(板):
返回randint(1,len(板[0])-1)
宝藏位置=[]
而len(宝藏位置<10):创建10个位置
宝藏=随机行(板),随机列(板)
如果宝藏位置中的宝藏:#重复,请重试
持续
宝藏位置。附加(宝藏)
转动=0
对于交车范围(64):
如果(不包括地点):
打印“你找到了所有的宝藏!”
打破
转弯=转弯+1
猜测行=int(输入(“猜测行:”)
guess_col=int(输入(“guess col:”)
猜一猜,猜一猜行,猜一猜列
如果猜到宝藏位置的位置:
打印(“恭喜!你找到了一个宝箱!”)
宝藏位置。移除(猜测位置)#发现后移除
打破
其他:
#...

对快速和单行表达式使用列表理解

  • 创建董事会
  • 使用一行listcomp创建可能的坐标列表,使用
    元组列表而不是两个单独的列表
  • 使用
    random.sample
    确保选择正确数量的物品(通过重复
    random.randint
    调用,您可以点击相同的位置:宝藏数量将低于预期)
  • 打印宝藏清单(以便您可以测试)
  • 使用元组列表中的
中的
立即检查x&y 简单的概念验证,最多创造5件珍品:

import random

board = [["*"]*8 for _ in range(8)]

possible_coords = [(x+1,y+1) for x in range(len(board[0])) for y in range(len(board))]

treasure_coords = random.sample(possible_coords,5)

print(treasure_coords)

x=int(input("x coord"))
y=int(input("y coord"))

if (x,y) in treasure_coords:
    print("you have found the treasure")
宝藏位置示例:

[(2, 3), (8, 8), (4, 1), (8, 7), (6, 4)]
[(1, 1), (5, 8), (3, 7), (4, 4), (8, 7)]
对代码的评论:

  • randint(1,len(board[0])-1)
    缺少最后一个元素
  • 您也可以在
    循环中增加
    圈数
    圈数

对快速和单行表达式使用列表理解

  • 创建董事会
  • 使用一行listcomp创建可能的坐标列表,使用
    元组列表而不是两个单独的列表
  • 使用
    random.sample
    确保选择正确数量的物品(通过重复
    random.randint
    调用,您可以点击相同的位置:宝藏数量将低于预期)
  • 打印宝藏清单(以便您可以测试)
  • 使用元组列表中的
中的立即检查x&y 简单的概念验证,最多创造5件珍品:

import random

board = [["*"]*8 for _ in range(8)]

possible_coords = [(x+1,y+1) for x in range(len(board[0])) for y in range(len(board))]

treasure_coords = random.sample(possible_coords,5)

print(treasure_coords)

x=int(input("x coord"))
y=int(input("y coord"))

if (x,y) in treasure_coords:
    print("you have found the treasure")
宝藏位置示例:

[(2, 3), (8, 8), (4, 1), (8, 7), (6, 4)]
[(1, 1), (5, 8), (3, 7), (4, 4), (8, 7)]
对代码的评论:

  • randint(1,len(board[0])-1)
    缺少最后一个元素
  • 您也可以在
    循环中增加
    圈数
    圈数

这里有轻微的缩进问题,您是否可以更正它们,这样我们就不会破坏您的代码?例如,
def print\u board(board):
是一个明显的问题。您可以建立一个
(行,列)
元组列表,然后使用
随机。示例
从这些元组中随机选择轻微的缩进问题,您是否可以更正它们,这样我们就不会破坏您的代码?例如,
def print\u board(board):
是一个明显的例子。你可以建立一个
(行,列)
元组列表,然后使用
随机。sample
从中随机选择OK我将尝试这个,以及元组。然而,这段代码不会只返回2个箱子吗?我可能错了,我会试试看。@j.don当然。但你到底想要什么?随机数目的箱子?动态设置的数字?这要看情况了。好的,我会试试这个,还有元组。然而,这段代码不会返回吗