Research portable Memory game | Исследовать портируемую игру Память
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

138 wiersze
2.8KB

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