Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
2.6KB

  1. from memory_Context import *
  2. from llm import *
  3. ########
  4. # Client initiated input
  5. ########
  6. # Generate constant playfield
  7. #
  8. # Both ids and group ids start with 0
  9. @llm_by_value
  10. def memory_generateConstPlayfield(
  11. c: memory_Context
  12. ) -> memory_Context:
  13. idGroups: dict[int, int] = { }
  14. id = 0
  15. for gid in range(0, c.playfieldSize):
  16. idGroups[id] = gid
  17. id += 1
  18. idGroups[id] = gid
  19. id += 1
  20. #}
  21. c.playfieldItems = idGroups
  22. c.recentField = "playfieldItems"
  23. return c
  24. #}
  25. # Select item
  26. @llm_by_value
  27. def memory_selectItem(
  28. c: memory_Context
  29. ) -> memory_Context:
  30. if (
  31. len(c.selectedItems) == 2
  32. ):
  33. c.selectedItems.clear()
  34. #}
  35. c.selectedItems.append(c.selectedId)
  36. c.recentField = "selectedItems"
  37. return c
  38. #}
  39. ########
  40. # System initiated reaction
  41. ########
  42. # Deselect mismatched items
  43. #
  44. # Conditions:
  45. # 0. Two items has just been selected
  46. # 1. The same item has been selected twice
  47. # 2. Selected items are of different groups
  48. @llm_by_value
  49. def memory_shouldDeselectMismatchedItems(
  50. c: memory_Context
  51. ) -> memory_Context:
  52. if not (
  53. c.recentField == "selectedItems" and
  54. len(c.selectedItems) == 2
  55. ):
  56. c.recentField = "none"
  57. return c
  58. #}
  59. if (
  60. c.selectedItems[0] == c.selectedItems[1]
  61. ):
  62. c.mismatchedItems.clear()
  63. c.mismatchedItems.append(c.selectedItems[0])
  64. c.recentField = "mismatchedItems"
  65. return c
  66. #}
  67. if (
  68. c.playfieldItems[c.selectedItems[0]] != c.playfieldItems[c.selectedItems[1]]
  69. ):
  70. c.mismatchedItems.clear()
  71. c.mismatchedItems.append(c.selectedItems[0])
  72. c.mismatchedItems.append(c.selectedItems[1])
  73. c.recentField = "mismatchedItems"
  74. return c
  75. #}
  76. c.recentField = "none"
  77. return c
  78. #}
  79. # Detect victory
  80. #
  81. # Conditions:
  82. # 1. Matching items have just been hidden and all items are hidden now
  83. @llm_by_value
  84. def memory_shouldDetectVictory(
  85. c: memory_Context
  86. ) -> memory_Context:
  87. if (
  88. c.recentField == "hiddenItems" and
  89. len(c.hiddenItems) == len(c.playfieldItems)
  90. ):
  91. c.victory = True
  92. c.recentField = "victory"
  93. return c
  94. #}
  95. c.recentField = "none"
  96. return c
  97. #}
  98. # Hide matching selected items
  99. #
  100. # Conditions:
  101. # 1. Two items are selected and they are of the same group
  102. @llm_by_value
  103. def memory_shouldHideMatchingItems(
  104. c: memory_Context
  105. ) -> memory_Context:
  106. if (
  107. c.recentField == "selectedItems" and
  108. len(c.selectedItems) == 2 and
  109. c.playfieldItems[c.selectedItems[0]] == c.playfieldItems[c.selectedItems[1]]
  110. ):
  111. c.hiddenItems.append(c.selectedItems[0])
  112. c.hiddenItems.append(c.selectedItems[1])
  113. c.recentField = "hiddenItems"
  114. return c
  115. #}
  116. c.recentField = "none"
  117. return c
  118. #}