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.

126 lines
2.7KB

  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. ////////////////
  10. // Client initiated input
  11. ////////////////
  12. // Generate constant playfield
  13. memory_Context memory_generateConstPlayfield(
  14. memory_Context c
  15. ) {
  16. std::map<int, int> idGroups = { };
  17. auto id = 0;
  18. for (auto gid = 0; gid < c.playfieldSize; ++gid) {
  19. idGroups[id] = gid;
  20. id += 1;
  21. idGroups[id] = gid;
  22. id += 1;
  23. }
  24. c.playfieldItems = idGroups;
  25. c.recentField = "playfieldItems";
  26. return c;
  27. }
  28. // Select item
  29. memory_Context memory_selectItem(
  30. memory_Context c
  31. ) {
  32. if (
  33. c.selectedItems.size() == 2
  34. ) {
  35. c.selectedItems.clear();
  36. }
  37. c.selectedItems.push_back(c.selectedId);
  38. c.recentField = "selectedItems";
  39. return c;
  40. }
  41. ////////////////
  42. // System initiated reaction
  43. ////////////////
  44. // Deselect mismatched items
  45. //
  46. // Conditions:
  47. // 0. Two items has just been selected
  48. // 1. The same item has been selected twice
  49. // 2. Selected items are of different groups
  50. memory_Context memory_shouldDeselectMismatchedItems(
  51. memory_Context c
  52. ) {
  53. if (!(
  54. c.recentField == "selectedItems" &&
  55. c.selectedItems.size() == 2
  56. )) {
  57. c.recentField = "none";
  58. return c;
  59. }
  60. if (
  61. c.selectedItems[0] == c.selectedItems[1]
  62. ) {
  63. c.mismatchedItems.clear();
  64. c.mismatchedItems.push_back(c.selectedItems[0]);
  65. c.recentField = "mismatchedItems";
  66. return c;
  67. }
  68. if (
  69. c.playfieldItems[c.selectedItems[0]] != c.playfieldItems[c.selectedItems[1]]
  70. ) {
  71. c.mismatchedItems.clear();
  72. c.mismatchedItems.push_back(c.selectedItems[0]);
  73. c.mismatchedItems.push_back(c.selectedItems[1]);
  74. c.recentField = "mismatchedItems";
  75. return c;
  76. }
  77. c.recentField = "none";
  78. return c;
  79. }
  80. // Detect victory
  81. //
  82. // Conditions:
  83. // 1. Matching items have just been hidden and all items are hidden now
  84. memory_Context memory_shouldDetectVictory(
  85. memory_Context c
  86. ) {
  87. if (
  88. c.recentField == "hiddenItems" &&
  89. c.hiddenItems.size() == c.playfieldItems.size()
  90. ) {
  91. c.victory = true;
  92. c.recentField = "victory";
  93. return c;
  94. }
  95. c.recentField = "none";
  96. return c;
  97. }
  98. // Hide matching selected items
  99. //
  100. // Conditions:
  101. // 1. Two items are selected and they are of the same group
  102. memory_Context memory_shouldHideMatchingItems(
  103. memory_Context c
  104. ) {
  105. if (
  106. c.recentField == "selectedItems" &&
  107. c.selectedItems.size() == 2 &&
  108. c.playfieldItems[c.selectedItems[0]] == c.playfieldItems[c.selectedItems[1]]
  109. ) {
  110. c.hiddenItems.push_back(c.selectedItems[0]);
  111. c.hiddenItems.push_back(c.selectedItems[1]);
  112. c.recentField = "hiddenItems";
  113. return c;
  114. }
  115. c.recentField = "none";
  116. return c;
  117. }