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.

221 lines
4.4KB

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