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.

141 lines
2.9KB

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