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.

219 lines
4.3KB

  1. #include <map>
  2. #include <string>
  3. #include <vector>
  4. #include "llm.h"
  5. #include "memory_Context.h"
  6. #include "main.h"
  7. // Exit
  8. //
  9. // Conditions:
  10. // 1. `e`, `exit`, `q`, or `quit` was entered
  11. // 2. Victory has just been reported
  12. memory_Context cli_exit(
  13. memory_Context c
  14. ) {
  15. if (
  16. c.recentField == "input" &&
  17. (
  18. c.input == "e" ||
  19. c.input == "exit" ||
  20. c.input == "q" ||
  21. c.input == "quit"
  22. )
  23. ) {
  24. c.exit = true;
  25. c.recentField = "exit";
  26. return c;
  27. }
  28. if (
  29. c.recentField == "outputVictory"
  30. ) {
  31. c.exit = true;
  32. c.recentField = "exit";
  33. return c;
  34. }
  35. c.recentField = "none";
  36. return c;
  37. }
  38. // Ask user to go on
  39. //
  40. // Conditions:
  41. // 1. Items have just matched and there are still items left to select
  42. memory_Context cli_goOn(
  43. memory_Context c
  44. ) {
  45. if (
  46. c.recentField == "outputMatchedItems" &&
  47. c.hiddenItems.size() != c.playfieldItems.size()
  48. ) {
  49. c.outputGoOn = "Go on:";
  50. c.recentField = "outputGoOn";
  51. return c;
  52. }
  53. c.recentField = "none";
  54. return c;
  55. }
  56. // Greet the user
  57. //
  58. // Conditions:
  59. // 1. Just launched
  60. memory_Context cli_greetUser(
  61. memory_Context c
  62. ) {
  63. if (
  64. c.recentField == "didLaunch" &&
  65. c.didLaunch == true
  66. ) {
  67. c.outputGreeting = "OGS Memory Command Line Interface";
  68. c.recentField = "outputGreeting";
  69. return c;
  70. }
  71. c.recentField = "none";
  72. return c;
  73. }
  74. // Ask user to select second item to have a pair of selected items
  75. //
  76. // Conditions:
  77. // 1. Single item has just been selected
  78. memory_Context cli_promptSecondItemSelection(
  79. memory_Context c
  80. ) {
  81. if (
  82. c.recentField == "selectedItems" &&
  83. c.selectedItems.size() == 1
  84. ) {
  85. c.outputPromptSelection = "Select the second item now:";
  86. c.recentField = "outputPromptSelection";
  87. return c;
  88. }
  89. c.recentField = "none";
  90. return c;
  91. }
  92. // Report matched items
  93. //
  94. // Conditions:
  95. // 1. Items were hidden (i.e., they matched)
  96. memory_Context cli_reportMatchedItems(
  97. memory_Context c
  98. ) {
  99. if (
  100. c.recentField == "hiddenItems"
  101. ) {
  102. c.outputMatchedItems = "Items matched!";
  103. c.recentField = "outputMatchedItems";
  104. return c;
  105. }
  106. c.recentField = "none";
  107. return c;
  108. }
  109. // Report victory
  110. memory_Context cli_reportVictory(
  111. memory_Context c
  112. ) {
  113. if (
  114. c.recentField == "victory"
  115. ) {
  116. c.outputVictory = "VICTORY! The game is over now";
  117. c.recentField = "outputVictory";
  118. return c;
  119. }
  120. c.recentField = "none";
  121. return c;
  122. }
  123. // Select item
  124. //
  125. // Conditions:
  126. // 1. Id is digit
  127. memory_Context cli_selectItem(
  128. memory_Context c
  129. ) {
  130. if (
  131. c.recentField == "input" &&
  132. llm_isDigit(c.input)
  133. ) {
  134. // CLI ids start with 1 while memory module has ids starting with 0
  135. // Convert CLI id to memory id
  136. c.selectedId = llm_strToInt(c.input) - 1;
  137. c.recentField = "selectedId";
  138. return c;
  139. }
  140. c.recentField = "none";
  141. return c;
  142. }
  143. // Show help (aka commands)
  144. //
  145. // Conditions:
  146. // 1. Just launched
  147. // 1. `h` or `help` was entered
  148. memory_Context cli_showHelp(
  149. memory_Context c
  150. ) {
  151. if (
  152. (
  153. c.recentField == "didLaunch" &&
  154. c.didLaunch == true
  155. ) ||
  156. (
  157. c.recentField == "input" &&
  158. c.input == "h"
  159. ) ||
  160. (
  161. c.recentField == "input" &&
  162. c.input == "help"
  163. )
  164. ) {
  165. c.outputHelp = "Commands:\n\te, exit, q, quit\n\t\tExit\n\th, help\n\t\tList commands\n\t1, 2, 3, ...\n\t\tSelect item\nEnter your choice:";
  166. c.recentField = "outputHelp";
  167. return c;
  168. }
  169. c.recentField = "none";
  170. return c;
  171. }
  172. // Report mismatched items
  173. memory_Context cli_reportMismatchedItems(
  174. memory_Context c
  175. ) {
  176. if (
  177. c.recentField == "mismatchedItems"
  178. ) {
  179. c.outputMismatchedItems = "Wrong! Try again:";
  180. c.recentField = "outputMismatchedItems";
  181. return c;
  182. }
  183. c.recentField = "none";
  184. return c;
  185. }
  186. //// Report selection of invalid item ids
  187. ////
  188. //// Conditions:
  189. //// 1. Index out of bounds: less than minimum
  190. //// 2. Index out of bounds: greater than maximum
  191. //// 3. Item is already hidden
  192. ////@llm_by_value
  193. ////def cli_shouldReportInvalidItemSelection(
  194. //// c: cli_Context
  195. ////) -> cli_Context:
  196. //// if (
  197. //// c.recentField == "cMemory" and
  198. //// c.cMemory.recentField == "selectedItems" and
  199. //// len(c.cMemory.selectedItems) == 1
  200. //// ):
  201. //// c.outputPromptSelection = "Select the second item now:"
  202. //// c.recentField = "outputPromptSelection"
  203. //// return c
  204. //// //}
  205. //// c.recentField = "none"
  206. //// return c
  207. //////}