Research portable Memory game | Исследовать портируемую игру Память
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

memory.cpp 2.6KB

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