Research portable Memory game | Исследовать портируемую игру Память
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

218 rindas
4.3KB

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