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

139 行
2.9KB

  1. from entities import *
  2. def memory_generateConstPlayfield(
  3. c: MemoryContext
  4. ) -> MemoryContext:
  5. idGroups: dict[int, int] = { }
  6. id = 0
  7. for gid in range(0, c.playfieldSize):
  8. idGroups[id] = gid
  9. id += 1
  10. idGroups[id] = gid
  11. id += 1
  12. #}
  13. c.playfieldItems = idGroups
  14. c.recentField = "playfieldItems"
  15. return c
  16. #}
  17. # Select item
  18. #
  19. # Conditions:
  20. # 0. Remove obsolete selected items
  21. # 1. If selectedId is recent
  22. # 2. If it's not recent
  23. def memory_selectItem(
  24. c: MemoryContext
  25. ) -> MemoryContext:
  26. if (
  27. len(c.selectedItems) == 2
  28. ):
  29. c.selectedItems = []
  30. if (
  31. c.recentField == "selectedId"
  32. ):
  33. c.selectedItems.append(c.selectedId)
  34. c.recentField = "selectedItems"
  35. return c
  36. c.recentField = None
  37. return c
  38. #}
  39. # Test.
  40. def test_memory_generateConstPlayfield(
  41. ) -> str:
  42. c = memory_createEmptyContext()
  43. c.playfieldSize = 2
  44. c = memory_generateConstPlayfield(c)
  45. if (
  46. c.recentField == "playfieldItems" and
  47. len(c.playfieldItems) == 4 and
  48. c.playfieldItems[0] == 0 and
  49. c.playfieldItems[1] == 0 and
  50. c.playfieldItems[2] == 1 and
  51. c.playfieldItems[3] == 1
  52. ):
  53. return "OK: memory_generateConstPlayfield"
  54. #}
  55. return "ERR: memory_generateConstPlayfield"
  56. #}
  57. def test_memory_selectOneItem(
  58. ) -> str:
  59. c = memory_createEmptyContext()
  60. c.playfieldSize = 2
  61. c = memory_generateConstPlayfield(c)
  62. # Select the first item.
  63. c.selectedId = 0
  64. c.recentField = "selectedId"
  65. c = memory_selectItem(c)
  66. # See if it's in selectedItems now.
  67. if (
  68. c.recentField == "selectedItems" and
  69. len(c.selectedItems) == 1 and
  70. c.selectedItems[0] == 0
  71. ):
  72. return "OK: memory_selectOneItem"
  73. #}
  74. return "ERR: memory_selectOneItem"
  75. #}
  76. def test_memory_selectTwoItems(
  77. ) -> str:
  78. c = memory_createEmptyContext()
  79. c.playfieldSize = 2
  80. c = memory_generateConstPlayfield(c)
  81. # Select the first two items.
  82. c.selectedId = 0
  83. c.recentField = "selectedId"
  84. c = memory_selectItem(c)
  85. c.selectedId = 1
  86. c.recentField = "selectedId"
  87. c = memory_selectItem(c)
  88. # See if it's both items are selected now.
  89. if (
  90. c.recentField == "selectedItems" and
  91. len(c.selectedItems) == 2 and
  92. c.selectedItems[0] == 0 and
  93. c.selectedItems[1] == 1
  94. ):
  95. return "OK: memory_selectTwoItems"
  96. #}
  97. return "ERR: memory_selectTwoItems"
  98. #}
  99. def test_memory_selectThreeItems(
  100. ) -> str:
  101. c = memory_createEmptyContext()
  102. c.playfieldSize = 2
  103. c = memory_generateConstPlayfield(c)
  104. # Select three items.
  105. c.selectedId = 0
  106. c.recentField = "selectedId"
  107. c = memory_selectItem(c)
  108. c.selectedId = 1
  109. c.recentField = "selectedId"
  110. c = memory_selectItem(c)
  111. c.selectedId = 2
  112. c.recentField = "selectedId"
  113. c = memory_selectItem(c)
  114. # See if only one (last) item is selected now.
  115. if (
  116. c.recentField == "selectedItems" and
  117. len(c.selectedItems) == 1 and
  118. c.selectedItems[0] == 2
  119. ):
  120. return "OK: memory_selectThreeItems"
  121. #}
  122. return "ERR: memory_selectThreeItems"
  123. #}