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.

пре 1 месец
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <!DOCTYPE html>
  2. <html>
  3. <meta charset="utf-8">
  4. <head>
  5. <link rel="stylesheet" href="../../style.css">
  6. </head>
  7. <body>
  8. <script data-goatcounter="https://services.opengamestudio.org:443/count" async src="//services.opengamestudio.org:443/count.js"></script>
  9. <div id="header">
  10. <div class="header2">
  11. <div class="menu">
  12. <a href="../../en/news/index.html">News</a>
  13. <a href="../../en/game/index.html">Games</a>
  14. <a href="../../en/tool/index.html">Tools</a>
  15. <a href="../../en/page/about.html">About</a>
  16. </div>
  17. <div id="lang">
  18. <a href="../../en/news/memory-logic.html">EN</a>
  19. <a href="../../ru/news/memory-logic.html">RU</a>
  20. </div>
  21. <div class="clear"></div>
  22. </div>
  23. </div>
  24. <h3 class="left_item_title">In the news...</h3>
  25. <center>
  26. <div class="news_item">
  27. <h2 class="news_item_title">
  28. <a href="memory-logic.html">"Memory" game logic</a>
  29. </h2>
  30. <p class="news_item_date">
  31. 2024-05-03 00:00
  32. </p>
  33. <div class="news_item_contents">
  34. <h1 id="memorygamelogic">"Memory" game logic</h1>
  35. <p>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.</p>
  36. <p>Limited language model assumes the following architecture of two parts:</p>
  37. <ol>
  38. <li>state context</li>
  39. <li>pure functions without side effects working only with the context</li>
  40. </ol>
  41. <p>Game logic state context in Python currently looks like this (<a href="https://git.opengamestudio.org/kornerr/research-portable-memory/src/commit/6fcd542daa6242c8c23dddb88d04cda74a730328/v3/memory_Context.h">C++</a>):</p>
  42. <pre><code class="python language-python">class memory_Context:
  43. def __init__(self):
  44. self.hiddenItems = []
  45. self.mismatchedItems = []
  46. self.playfieldItems = {}
  47. self.playfieldSize = 0
  48. self.recentField = "none"
  49. self.selectedId = -1
  50. self.selectedItems = []
  51. self.victory = False
  52. </code></pre>
  53. <p>Since the instrument only works with functions at the moment, I had to write C++ context code manually.</p>
  54. <p>Functions look like this (<a href="https://git.opengamestudio.org/kornerr/research-portable-memory/src/commit/6fcd542daa6242c8c23dddb88d04cda74a730328/v3/memory.cpp#L29">C++</a>):</p>
  55. <pre><code class="python language-python"># Select item
  56. @llm_by_value
  57. def memory_selectItem(
  58. c: memory_Context
  59. ) -&gt; memory_Context:
  60. if (
  61. len(c.selectedItems) == 2
  62. ):
  63. c.selectedItems.clear()
  64. #}
  65. c.selectedItems.append(c.selectedId)
  66. c.recentField = "selectedItems"
  67. return c
  68. #}
  69. </code></pre>
  70. <p>Limited language model functions have the following features:</p>
  71. <ul>
  72. <li><code>@llm_by_value</code> Python decorator is used to pass arguments by value instead of by reference (which is default in Python)</li>
  73. <li>passing arguments by value is mandatory to limit function scope to only single context field, it's formalized Single Responsibility Principle</li>
  74. <li>context contains <code>recentField</code> representing a stored "event"; this allows functions to run only when necessary field changes</li>
  75. <li>function must use <code>recentField</code> to specify which field it changed or provide <code>none</code> if no action was performed</li>
  76. <li>function takes only context as an input and only returns (an updated) context as an output</li>
  77. </ul>
  78. <p>"Memory" game logic has the following functions:</p>
  79. <table>
  80. <thead>
  81. <tr>
  82. <th>№</th>
  83. <th>Function</th>
  84. <th>Caller</th>
  85. <th>Description</th>
  86. </tr>
  87. </thead>
  88. <tbody>
  89. <tr>
  90. <td>1</td>
  91. <td><code>memory_generateConstPlayfield</code></td>
  92. <td>User</td>
  93. <td>Generate simplified playfield with items of the same group following each other sequentially</td>
  94. </tr>
  95. <tr>
  96. <td>2</td>
  97. <td><code>memory_selectItem</code></td>
  98. <td>User</td>
  99. <td>Select playfield item</td>
  100. </tr>
  101. <tr>
  102. <td>3</td>
  103. <td><code>memory_shouldDeselectMismatchedItems</code></td>
  104. <td>System</td>
  105. <td>Reaction to deselect a pair of the selected items of different groups</td>
  106. </tr>
  107. <tr>
  108. <td>4</td>
  109. <td><code>memory_shouldDetectVictory</code></td>
  110. <td>System</td>
  111. <td>Reaction to detect victory when all items got hidden</td>
  112. </tr>
  113. <tr>
  114. <td>5</td>
  115. <td><code>memory_shouldHideMatchingItems</code></td>
  116. <td>System</td>
  117. <td>Reaction to hide a pair of the selected items of the same group</td>
  118. </tr>
  119. </tbody>
  120. </table>
  121. <p>To verify functions work identically in both Python and C++, I cover each function with at least one test:</p>
  122. <ul>
  123. <li><code>memory_test_generateConstPlayfield</code></li>
  124. <li><code>memory_test_selectItem_1x</code></li>
  125. <li><code>memory_test_selectItem_2x</code></li>
  126. <li><code>memory_test_selectItem_3x</code></li>
  127. <li><code>memory_test_shouldDeselectMismatchedItems</code></li>
  128. <li><code>memory_test_shouldDeselectMismatchedItems_itemTwice</code></li>
  129. <li><code>memory_test_shouldDetectVictory</code></li>
  130. <li><code>memory_test_shouldHideMatchingItems</code></li>
  131. </ul>
  132. <h1 id="mayplans">May plans</h1>
  133. <p>I'll make a text UI for "Memory" game.</p>
  134. </div>
  135. </div>
  136. <div id="disqus_thread"></div>
  137. <script>
  138. var disqus_config = function () {
  139. this.page.url = "https://opengamestudio.org/en/news/memory-logic.html";
  140. this.page.identifier = "memory-logic.html";
  141. };
  142. (function() { // DON'T EDIT BELOW THIS LINE
  143. var d = document, s = d.createElement('script');
  144. s.src = 'https://opengamestudio.disqus.com/embed.js';
  145. s.setAttribute('data-timestamp', +new Date());
  146. (d.head || d.body).appendChild(s);
  147. })();
  148. </script>
  149. <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
  150. <div id="footer">
  151. The site has been generated by <a href="http://opengamestudio.org/pskov">PSKOV</a>
  152. from <a href="http://github.com/ogstudio/site-opengamestudio">this source code</a>.
  153. </div>
  154. </center>
  155. </body>
  156. </html>