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.py 4.3KB

6 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. from memory_Context import *
  2. from llm import *
  3. # Exit
  4. #
  5. # Conditions:
  6. # 1. `e`, `exit`, `q`, or `quit` was entered
  7. # 2. Victory has just been reported
  8. @llm_by_value
  9. def cli_exit(
  10. c: memory_Context
  11. ) -> memory_Context:
  12. if (
  13. c.recentField == "input" and
  14. (
  15. c.input == "e" or
  16. c.input == "exit" or
  17. c.input == "q" or
  18. c.input == "quit"
  19. )
  20. ):
  21. c.exit = True
  22. c.recentField = "exit"
  23. return c
  24. #}
  25. if (
  26. c.recentField == "outputVictory"
  27. ):
  28. c.exit = True
  29. c.recentField = "exit"
  30. return c
  31. #}
  32. c.recentField = "none"
  33. return c
  34. #}
  35. # Ask user to go on
  36. #
  37. # Conditions:
  38. # 1. Items have just matched and there are still items left to select
  39. @llm_by_value
  40. def cli_goOn(
  41. c: memory_Context
  42. ) -> memory_Context:
  43. if (
  44. c.recentField == "outputMatchedItems" and
  45. len(c.hiddenItems) != len(c.playfieldItems)
  46. ):
  47. c.outputGoOn = "Go on:"
  48. c.recentField = "outputGoOn"
  49. return c
  50. #}
  51. c.recentField = "none"
  52. return c
  53. #}
  54. # Greet the user
  55. #
  56. # Conditions:
  57. # 1. Just launched
  58. @llm_by_value
  59. def cli_greetUser(
  60. c: memory_Context
  61. ) -> memory_Context:
  62. if (
  63. c.recentField == "didLaunch" and
  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. @llm_by_value
  78. def cli_promptSecondItemSelection(
  79. c: memory_Context
  80. ) -> memory_Context:
  81. if (
  82. c.recentField == "selectedItems" and
  83. len(c.selectedItems) == 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. @llm_by_value
  97. def cli_reportMatchedItems(
  98. c: memory_Context
  99. ) -> memory_Context:
  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. @llm_by_value
  112. def cli_reportVictory(
  113. c: memory_Context
  114. ) -> memory_Context:
  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. @llm_by_value
  130. def cli_selectItem(
  131. c: memory_Context
  132. ) -> memory_Context:
  133. if (
  134. c.recentField == "input" and
  135. llm_isDigit(c.input)
  136. ):
  137. # CLI ids start with 1 while memory module has ids starting with 0
  138. # Convert CLI id to memory id
  139. c.selectedId = llm_strToInt(c.input) - 1
  140. c.recentField = "selectedId"
  141. return c
  142. #}
  143. c.recentField = "none"
  144. return c
  145. #}
  146. # Show help (aka commands)
  147. #
  148. # Conditions:
  149. # 1. Just launched
  150. # 1. `h` or `help` was entered
  151. @llm_by_value
  152. def cli_showHelp(
  153. c: memory_Context
  154. ) -> memory_Context:
  155. if (
  156. (
  157. c.recentField == "didLaunch" and
  158. c.didLaunch == True
  159. ) or
  160. (
  161. c.recentField == "input" and
  162. c.input == "h"
  163. ) or
  164. (
  165. c.recentField == "input" and
  166. c.input == "help"
  167. )
  168. ):
  169. 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:"
  170. c.recentField = "outputHelp"
  171. return c
  172. #}
  173. c.recentField = "none"
  174. return c
  175. #}
  176. # Report mismatched items
  177. @llm_by_value
  178. def cli_reportMismatchedItems(
  179. c: memory_Context
  180. ) -> memory_Context:
  181. if (
  182. c.recentField == "mismatchedItems"
  183. ):
  184. c.outputMismatchedItems = "Wrong! Try again:"
  185. c.recentField = "outputMismatchedItems"
  186. return c
  187. #}
  188. c.recentField = "none"
  189. return c
  190. #}
  191. ## Report selection of invalid item ids
  192. ##
  193. ## Conditions:
  194. ## 1. Index out of bounds: less than minimum
  195. ## 2. Index out of bounds: greater than maximum
  196. ## 3. Item is already hidden
  197. ##@llm_by_value
  198. ##def cli_shouldReportInvalidItemSelection(
  199. ## c: cli_Context
  200. ##) -> cli_Context:
  201. ## if (
  202. ## c.recentField == "cMemory" and
  203. ## c.cMemory.recentField == "selectedItems" and
  204. ## len(c.cMemory.selectedItems) == 1
  205. ## ):
  206. ## c.outputPromptSelection = "Select the second item now:"
  207. ## c.recentField = "outputPromptSelection"
  208. ## return c
  209. ## #}
  210. ## c.recentField = "none"
  211. ## return c
  212. ###}