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.

memory.cpp 2.9KB

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