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.

cli.cpp 4.3KB

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