Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have made a class in which there are 3 functions.

  1. def maxvalue
  2. def min value
  3. def getAction

In the def maxvalue function, I have made a list of actions. I want that list to be accessed in def getaction function so that I can reverse the list and then take out the first entry from it. How can do i that??

 def getAction(self,gamestate):
      bestaction.reverse()
      return bestaction[0]




 def maxvalue(gameState, depth):

    actions = gameState.getLegalActions(0);
    v = -9999
    bestaction = []
    for action in actions:
      marks = minvalue(gameState.generateSuccessor(0, action), depth, 1)
      if(v < marks):
       v = marks
       bestaction.append(action)
    return v

It is giving me an error....."global name bestaction is not defined"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
198 views
Welcome To Ask or Share your Answers For Others

1 Answer

It's a good idea to post actual code - it makes it easier to see what's happening.

With that said, you probably want something like this:

class MyClass(object):
    def max_value(self):
        # assigning your list like this will make it accessible 
        # from other methods in the class
        self.list_in_max_value = ["A", "B", "C"]

    def get_action(self):
        # here, we're doing something with the list
        self.list_in_max_value.reverse()
        return self.list_in_max_value[0]

>>> my_class = MyClass()
>>> my_class.max_value()
>>> my_class.get_action()
"C"

You might want to read through the python class tutorial.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...