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.

124 lines
2.7KB

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