25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
6.5KB

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