Research portable Memory game | Исследовать портируемую игру Память
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Controller.py 1017B

6 个月前
1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import copy
  2. class Controller:
  3. def __init__(self, c):
  4. self.callbacks = []
  5. self.context = c
  6. self.functions = []
  7. self.isProcessingQueue = False
  8. self.queue = []
  9. def executeFunctions(self):
  10. c = self.queue.pop(0)
  11. for f in self.functions:
  12. ctx = f(c)
  13. if ctx.recentField != "none":
  14. self.queue.append(ctx)
  15. self.context = c
  16. self.reportContext()
  17. def processQueue(self):
  18. # Decline recursion.
  19. if self.isProcessingQueue:
  20. return
  21. self.isProcessingQueue = True
  22. while len(self.queue) > 0:
  23. self.executeFunctions()
  24. self.isProcessingQueue = False
  25. def registerCallback(self, cb):
  26. self.callbacks.append(cb)
  27. def registerFunction(self, f):
  28. self.functions.append(f)
  29. def reportContext(self):
  30. for cb in self.callbacks:
  31. cb(self.context)
  32. def set(self, fieldName, value):
  33. c = copy.deepcopy(self.context)
  34. setattr(c, fieldName, value)
  35. c.recentField = fieldName
  36. self.queue.append(c)
  37. self.processQueue()