简体   繁体   中英

Python Adding Unknown Values to Lists

I have made a treasure hunt game, and I need it so that once a square containing a treasure chest has been visited 3 times it turns into a 'bandit'. In order to covunt the number of times a treasure chest has been visited, I added another lot of the same coordinates to the list of TreasureChestCoordinates. Then I plan to have an if statement so that if any of the coordinates are written 3 times, all 3 will be removed and put in the 'bandit' list. Here's my draft code for the section:

            # Treasure Chests Collecting Coins #
        if (x,y) in TreasureCoords[0:(TCNumber -1)]:
            print("You have landed on a treasure chest! You can visit it 2 more times before it becomes a bandit.")
            CoinCollection = CoinCollection + 10
            TreasureCoords.append(x,y)

        #if TreasureCoords.count(any item) > 3:
            #remove all copies of item from list
            #add item to bandit list

x,y means the coordinates the user is currently in. CoinCollection is a seperate variable that changes when the user lands on a treasure chest or bandit.By the way, the number of coordinates is unlimited because the user decides that earlier on in the game. So, how can I duplicate my list of coordinates, remove them once they are in the list 3 times and put them in a new list? I presume this is the easiest way to 'count' how many times the user has been on a square, and change the treasure chest to a bandit.

First... you don't need to search the list for any coordinate present three times. That's wasteful. If you remove them once that happens, then the only coordinate that can possibly be present three times is the one you just visited.

If you really want to do it as shown in your question, just do something like while (x,y) in TreasureCoords: TreasureCoords.remove((x,y)) . However, it would probably be easier to use a dict . You can probably avoid having separate variables, even. Assuming that ItemCoords is initially filled with coordinates that represent unvisited treasure chests:

ItemCoords[(x,y)] = 0 # add a treasure

...then you can just look at how many times the treasure is visited:

visits = ItemCoords.get((x,y), None)
if visits is None:
    pass # nothing there
else if visits > 3:
    fightBandit() # ...or whatever
else:
    print("You have landed on a treasure chest! You can visit it %i more times before it becomes a bandit." % (2 - visits))
    CoinCollection += 10
    ItemCoords[(x,y)] += 1

This is still simplistic. You probably want a more object-oriented approach as hinted at by Christian:

class Treasure:
    def __init__(x,y):
        self.x = x
        self.y = y
        self.visits = 0

    def visit():
        print("You have landed on a treasure chest! You can visit it %i more times before it becomes a bandit." % (2 - visits))    
        CoinCollection += 10

        if visits == 3:
            world[(self.x, self.y)] = Bandit(self.x, self.y)

class Bandit:
    def __init__(x,y):
        self.x = x
        self.y = y

    def visit():
        # Replace this with whatever happens when the player meets a bandit
        print("You have been eaten by a grue.")
        sys.exit(0)

# Initialize the world
for n in range(NumberOfTreasure):
    # get x,y somehow, e.g. random
    world[(x,y)] = Treasure(x,y)

# ...code for player wandering around...
here = world.get((x,y), None)
if here is not None:
    here.visit()

I think this is a case where an object would be beneficial - instead of tracking separate lists with facts about the same thing, use an list of objects which store all relevant information.

Could look like this:

class Monster(object):
    #define like you like it
    pass

class Treasure(object):
   def init(x, y):
      self.x = x
      self.y = y
      self.visits = 0

    def visit():
        self.visits += 1

        if self.visits >= 3:
            return Monster()
        else:
            return 10


calling code:
    treasure = Treasure(1,1)
    rv = treasure.visit()
    if isinstance(rv, Monster):
        # lets the monster rip him to shreds
    else:
        CoinCollection +=treasure.visit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM