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.

functions.py 2.9KB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. from entities import *
  2. def memory_generateConstPlayfield(
  3. c: MemoryContext
  4. ) -> MemoryContext:
  5. idGroups: dict[int, int] = { }
  6. id = 0
  7. for gid in range(0, c.playfieldSize):
  8. idGroups[id] = gid
  9. id += 1
  10. idGroups[id] = gid
  11. id += 1
  12. #}
  13. c.playfieldItems = idGroups
  14. c.recentField = "playfieldItems"
  15. return c
  16. #}
  17. # Select item
  18. #
  19. # Conditions:
  20. # 0. Remove obsolete selected items
  21. # 1. If selectedId is recent
  22. # 2. If it's not recent
  23. def memory_selectItem(
  24. c: MemoryContext
  25. ) -> MemoryContext:
  26. if (
  27. len(c.selectedItems) == 2
  28. ):
  29. c.selectedItems = []
  30. if (
  31. c.recentField == "selectedId"
  32. ):
  33. c.selectedItems.append(c.selectedId)
  34. c.recentField = "selectedItems"
  35. return c
  36. c.recentField = None
  37. return c
  38. #}
  39. # Test.
  40. def test_memory_generateConstPlayfield(
  41. ) -> str:
  42. c = memory_createEmptyContext()
  43. c.playfieldSize = 2
  44. c = memory_generateConstPlayfield(c)
  45. if (
  46. c.recentField == "playfieldItems" and
  47. len(c.playfieldItems) == 4 and
  48. c.playfieldItems[0] == 0 and
  49. c.playfieldItems[1] == 0 and
  50. c.playfieldItems[2] == 1 and
  51. c.playfieldItems[3] == 1
  52. ):
  53. return "OK: memory_generateConstPlayfield"
  54. #}
  55. return "ERR: memory_generateConstPlayfield"
  56. #}
  57. def test_memory_selectOneItem(
  58. ) -> str:
  59. c = memory_createEmptyContext()
  60. c.playfieldSize = 2
  61. c = memory_generateConstPlayfield(c)
  62. # Select the first item.
  63. c.selectedId = 0
  64. c.recentField = "selectedId"
  65. c = memory_selectItem(c)
  66. # See if it's in selectedItems now.
  67. if (
  68. c.recentField == "selectedItems" and
  69. len(c.selectedItems) == 1 and
  70. c.selectedItems[0] == 0
  71. ):
  72. return "OK: memory_selectOneItem"
  73. #}
  74. return "ERR: memory_selectOneItem"
  75. #}
  76. def test_memory_selectTwoItems(
  77. ) -> str:
  78. c = memory_createEmptyContext()
  79. c.playfieldSize = 2
  80. c = memory_generateConstPlayfield(c)
  81. # Select the first two items.
  82. c.selectedId = 0
  83. c.recentField = "selectedId"
  84. c = memory_selectItem(c)
  85. c.selectedId = 1
  86. c.recentField = "selectedId"
  87. c = memory_selectItem(c)
  88. # See if it's both items are selected now.
  89. if (
  90. c.recentField == "selectedItems" and
  91. len(c.selectedItems) == 2 and
  92. c.selectedItems[0] == 0 and
  93. c.selectedItems[1] == 1
  94. ):
  95. return "OK: memory_selectTwoItems"
  96. #}
  97. return "ERR: memory_selectTwoItems"
  98. #}
  99. def test_memory_selectThreeItems(
  100. ) -> str:
  101. c = memory_createEmptyContext()
  102. c.playfieldSize = 2
  103. c = memory_generateConstPlayfield(c)
  104. # Select three items.
  105. c.selectedId = 0
  106. c.recentField = "selectedId"
  107. c = memory_selectItem(c)
  108. c.selectedId = 1
  109. c.recentField = "selectedId"
  110. c = memory_selectItem(c)
  111. c.selectedId = 2
  112. c.recentField = "selectedId"
  113. c = memory_selectItem(c)
  114. # See if only one (last) item is selected now.
  115. if (
  116. c.recentField == "selectedItems" and
  117. len(c.selectedItems) == 1 and
  118. c.selectedItems[0] == 2
  119. ):
  120. return "OK: memory_selectThreeItems"
  121. #}
  122. return "ERR: memory_selectThreeItems"
  123. #}