Research portable Memory game | Исследовать портируемую игру Память
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

330 rindas
7.1KB

  1. from entities import *
  2. from llm import *
  3. # Detect victory
  4. #
  5. # Conditions:
  6. # 1. Matching items have just been hidden and all items are hidden now
  7. @llm_by_value
  8. def memory_detectVictory(
  9. c: MemoryContext
  10. ) -> MemoryContext:
  11. if (
  12. c.recentField == "hiddenItems" and
  13. len(c.hiddenItems) == len(c.playfieldItems)
  14. ):
  15. c.victory = True
  16. c.recentField = "victory"
  17. return c
  18. c.recentField = None
  19. return c
  20. #}
  21. # Deselect mismatched items
  22. #
  23. # Conditions:
  24. # 1. Two items are selected and they are of different groups
  25. @llm_by_value
  26. def memory_deselectMismatchedItems(
  27. c: MemoryContext
  28. ) -> MemoryContext:
  29. if (
  30. c.recentField == "selectedItems" and
  31. len(c.selectedItems) == 2 and
  32. c.playfieldItems[c.selectedItems[0]] != c.playfieldItems[c.selectedItems[1]]
  33. ):
  34. c.mismatchedItems.clear()
  35. c.mismatchedItems.append(c.selectedItems[0])
  36. c.mismatchedItems.append(c.selectedItems[1])
  37. c.recentField = "mismatchedItems"
  38. return c
  39. c.recentField = None
  40. return c
  41. #}
  42. # Generate constant playfield suitable for testing and debugging
  43. @llm_by_value
  44. def memory_generateConstPlayfield(
  45. c: MemoryContext
  46. ) -> MemoryContext:
  47. idGroups: dict[int, int] = { }
  48. id = 0
  49. for gid in range(0, c.playfieldSize):
  50. idGroups[id] = gid
  51. id += 1
  52. idGroups[id] = gid
  53. id += 1
  54. #}
  55. c.playfieldItems = idGroups
  56. c.recentField = "playfieldItems"
  57. return c
  58. #}
  59. # Hide matching selected items
  60. #
  61. # Conditions:
  62. # 1. Two items are selected and they are of the same group
  63. @llm_by_value
  64. def memory_hideMatchingItems(
  65. c: MemoryContext
  66. ) -> MemoryContext:
  67. if (
  68. c.recentField == "selectedItems" and
  69. len(c.selectedItems) == 2 and
  70. c.playfieldItems[c.selectedItems[0]] == c.playfieldItems[c.selectedItems[1]]
  71. ):
  72. c.hiddenItems.append(c.selectedItems[0])
  73. c.hiddenItems.append(c.selectedItems[1])
  74. c.recentField = "hiddenItems"
  75. return c
  76. c.recentField = None
  77. return c
  78. #}
  79. # Select item
  80. #
  81. # Conditions:
  82. # 1. There are already two selected items
  83. # 2. Item has just been selected
  84. @llm_by_value
  85. def memory_selectItem(
  86. c: MemoryContext
  87. ) -> MemoryContext:
  88. if (
  89. len(c.selectedItems) == 2
  90. ):
  91. c.selectedItems = []
  92. if (
  93. c.recentField == "selectedId"
  94. ):
  95. c.selectedItems.append(c.selectedId)
  96. c.recentField = "selectedItems"
  97. return c
  98. c.recentField = None
  99. return c
  100. #}
  101. # Test.
  102. def test_memory_generateConstPlayfield(
  103. ) -> str:
  104. c = memory_createEmptyContext()
  105. c.playfieldSize = 2
  106. c = memory_generateConstPlayfield(c)
  107. if (
  108. c.recentField == "playfieldItems" and
  109. len(c.playfieldItems) == 4 and
  110. c.playfieldItems[0] == 0 and
  111. c.playfieldItems[1] == 0 and
  112. c.playfieldItems[2] == 1 and
  113. c.playfieldItems[3] == 1
  114. ):
  115. return "OK: memory_generateConstPlayfield"
  116. #}
  117. return "ERR: memory_generateConstPlayfield"
  118. #}
  119. def test_memory_selectItem(
  120. ) -> str:
  121. c = memory_createEmptyContext()
  122. c.playfieldSize = 2
  123. c = memory_generateConstPlayfield(c)
  124. # Select the first item.
  125. c.selectedId = 0
  126. c.recentField = "selectedId"
  127. c = memory_selectItem(c)
  128. # See if it's in selectedItems now.
  129. if (
  130. c.recentField == "selectedItems" and
  131. len(c.selectedItems) == 1 and
  132. c.selectedItems[0] == 0
  133. ):
  134. return "OK: selectItem"
  135. #}
  136. return "ERR: selectItem"
  137. #}
  138. def test_selectTwoItems(
  139. ) -> str:
  140. c = memory_createEmptyContext()
  141. c.playfieldSize = 2
  142. c = memory_generateConstPlayfield(c)
  143. # Select the first two items.
  144. c.selectedId = 0
  145. c.recentField = "selectedId"
  146. c = memory_selectItem(c)
  147. c.selectedId = 1
  148. c.recentField = "selectedId"
  149. c = memory_selectItem(c)
  150. # See if it's both items are selected now.
  151. if (
  152. c.recentField == "selectedItems" and
  153. len(c.selectedItems) == 2 and
  154. c.selectedItems[0] == 0 and
  155. c.selectedItems[1] == 1
  156. ):
  157. return "OK: selectTwoItems"
  158. #}
  159. return "ERR: selectTwoItems"
  160. #}
  161. def test_selectThreeItems(
  162. ) -> str:
  163. c = memory_createEmptyContext()
  164. c.playfieldSize = 2
  165. c = memory_generateConstPlayfield(c)
  166. # Select three items.
  167. c.selectedId = 0
  168. c.recentField = "selectedId"
  169. c = memory_selectItem(c)
  170. c.selectedId = 1
  171. c.recentField = "selectedId"
  172. c = memory_selectItem(c)
  173. c.selectedId = 2
  174. c.recentField = "selectedId"
  175. c = memory_selectItem(c)
  176. # See if only one (last) item is selected now.
  177. if (
  178. c.recentField == "selectedItems" and
  179. len(c.selectedItems) == 1 and
  180. c.selectedItems[0] == 2
  181. ):
  182. return "OK: selectThreeItems"
  183. #}
  184. return "ERR: selectThreeItems"
  185. #}
  186. def test_memory_hideMatchingItems(
  187. ) -> str:
  188. c = memory_createEmptyContext()
  189. c.playfieldSize = 2
  190. c = memory_generateConstPlayfield(c)
  191. # Select two items of the same group.
  192. c.selectedId = 0
  193. c.recentField = "selectedId"
  194. c = memory_selectItem(c)
  195. c.selectedId = 1
  196. c.recentField = "selectedId"
  197. c = memory_selectItem(c)
  198. # Hide matching items.
  199. c = memory_hideMatchingItems(c)
  200. # See if the two selected items match.
  201. if (
  202. c.recentField == "hiddenItems" and
  203. len(c.hiddenItems) == 2 and
  204. c.hiddenItems[0] == 0 and
  205. c.hiddenItems[1] == 1
  206. ):
  207. return "OK: memory_hideMatchingItems"
  208. #}
  209. return "ERR: memory_hideMatchingItems"
  210. #}
  211. def test_memory_deselectMismatchedItems(
  212. ) -> str:
  213. c = memory_createEmptyContext()
  214. c.playfieldSize = 2
  215. c = memory_generateConstPlayfield(c)
  216. # Select two items of different groups.
  217. c.selectedId = 0
  218. c.recentField = "selectedId"
  219. c = memory_selectItem(c)
  220. c.selectedId = 2
  221. c.recentField = "selectedId"
  222. c = memory_selectItem(c)
  223. # Detect mismatching.
  224. c = memory_deselectMismatchedItems(c)
  225. # See if the two selected items do not match.
  226. if (
  227. c.recentField == "mismatchedItems" and
  228. len(c.mismatchedItems) == 2 and
  229. c.mismatchedItems[0] == 0 and
  230. c.mismatchedItems[1] == 2
  231. ):
  232. return "OK: memory_deselectMismatchedItems"
  233. #}
  234. return "ERR: memory_deselectMismatchedItems"
  235. #}
  236. def test_memory_detectVictory(
  237. ) -> str:
  238. c = memory_createEmptyContext()
  239. c.playfieldSize = 2
  240. c = memory_generateConstPlayfield(c)
  241. # Select the first two items of the same group.
  242. c.selectedId = 0
  243. c.recentField = "selectedId"
  244. c = memory_selectItem(c)
  245. c.selectedId = 1
  246. c.recentField = "selectedId"
  247. c = memory_selectItem(c)
  248. # Hide the first pair.
  249. c = memory_hideMatchingItems(c)
  250. # Select the last two items of the same group.
  251. c.selectedId = 2
  252. c.recentField = "selectedId"
  253. c = memory_selectItem(c)
  254. c.selectedId = 3
  255. c.recentField = "selectedId"
  256. c = memory_selectItem(c)
  257. # Hide the second pair.
  258. c = memory_hideMatchingItems(c)
  259. # Detect victory.
  260. c = memory_detectVictory(c)
  261. # See if victory has been detected.
  262. if (
  263. c.recentField == "victory" and
  264. c.victory == True
  265. ):
  266. return "OK: memory_detectVictory"
  267. #}
  268. return "ERR: memory_detectVictory"
  269. #}
  270. def test_passContextByValue(
  271. ) -> str:
  272. c = memory_createEmptyContext()
  273. c.playfieldSize = 2
  274. c = memory_generateConstPlayfield(c)
  275. c.selectedId = 0
  276. c.recentField = "selectedId"
  277. c1 = memory_selectItem(c)
  278. c.selectedId = 1
  279. c.recentField = "selectedId"
  280. c2 = memory_selectItem(c)
  281. # See if c1 and c2 have different recentField.
  282. if (
  283. c1.selectedId != c2.selectedId
  284. ):
  285. return "OK: passContextByValue"
  286. return "ERR: passContextByValue"
  287. #}