Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

2024-05_memory-logic.md 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. Title: "Memory" game logic
  2. Date: 2024-05-03 00:00
  3. Category: News
  4. Slug: memory-logic
  5. Lang: en
  6. # "Memory" game logic
  7. In April I implemented "Memory" game logic in Python as limited language model and successfully converted the code to C++ by the instrument under development.
  8. Limited language model assumes the following architecture of two parts:
  9. 1. state context
  10. 1. pure functions without side effects working only with the context
  11. Game logic state context in Python currently looks like this ([C++][ctx_cxx]):
  12. ```python
  13. class memory_Context:
  14. def __init__(self):
  15. self.hiddenItems = []
  16. self.mismatchedItems = []
  17. self.playfieldItems = {}
  18. self.playfieldSize = 0
  19. self.recentField = "none"
  20. self.selectedId = -1
  21. self.selectedItems = []
  22. self.victory = False
  23. ```
  24. Since the instrument only works with functions at the moment, I had to write C++ context code manually.
  25. Functions look like this ([C++][func_cxx]):
  26. ```python
  27. # Select item
  28. @llm_by_value
  29. def memory_selectItem(
  30. c: memory_Context
  31. ) -> memory_Context:
  32. if (
  33. len(c.selectedItems) == 2
  34. ):
  35. c.selectedItems.clear()
  36. #}
  37. c.selectedItems.append(c.selectedId)
  38. c.recentField = "selectedItems"
  39. return c
  40. #}
  41. ```
  42. Limited language model functions have the following features:
  43. * `@llm_by_value` Python decorator is used to pass arguments by value instead of by reference (which is default in Python)
  44. * passing arguments by value is mandatory to limit function scope to only single context field, it's formalized Single Responsibility Principle
  45. * context contains `recentField` representing a stored "event"; this allows functions to run only when necessary field changes
  46. * function must use `recentField` to specify which field it changed or provide `none` if no action was performed
  47. * function takes only context as an input and only returns (an updated) context as an output
  48. "Memory" game logic has the following functions:
  49. | № | Function | Caller| Description |
  50. | --- | --- | --- | --- |
  51. | 1 | `memory_generateConstPlayfield` | User | Generate simplified playfield with items of the same group following each other sequentially |
  52. | 2 | `memory_selectItem` | User | Select playfield item |
  53. | 3 | `memory_shouldDeselectMismatchedItems` | System | Reaction to deselect a pair of the selected items of different groups |
  54. | 4 | `memory_shouldDetectVictory` | System | Reaction to detect victory when all items got hidden |
  55. | 5 | `memory_shouldHideMatchingItems` | System | Reaction to hide a pair of the selected items of the same group |
  56. To verify functions work identically in both Python and C++, I cover each function with at least one test:
  57. * `memory_test_generateConstPlayfield`
  58. * `memory_test_selectItem_1x`
  59. * `memory_test_selectItem_2x`
  60. * `memory_test_selectItem_3x`
  61. * `memory_test_shouldDeselectMismatchedItems`
  62. * `memory_test_shouldDeselectMismatchedItems_itemTwice`
  63. * `memory_test_shouldDetectVictory`
  64. * `memory_test_shouldHideMatchingItems`
  65. # May plans
  66. I'll make a text UI for "Memory" game.
  67. [ctx_cxx]: https://git.opengamestudio.org/kornerr/research-portable-memory/src/commit/6fcd542daa6242c8c23dddb88d04cda74a730328/v3/memory_Context.h
  68. [func_cxx]: https://git.opengamestudio.org/kornerr/research-portable-memory/src/commit/6fcd542daa6242c8c23dddb88d04cda74a730328/v3/memory.cpp#L29