diff --git a/v3/main.py b/v3/main.py index 7761f66..e6e1327 100644 --- a/v3/main.py +++ b/v3/main.py @@ -5,6 +5,7 @@ print(memory_test_selectItem_1x()) print(memory_test_selectItem_2x()) print(memory_test_selectItem_3x()) print(memory_test_shouldDeselectMismatchedItems()) +print(memory_test_shouldDeselectMismatchedItems_itemTwice()) print(memory_test_shouldDetectVictory()) print(memory_test_shouldHideMatchingItems()) diff --git a/v3/memory.py b/v3/memory.py index d5fcf44..1286dc2 100644 --- a/v3/memory.py +++ b/v3/memory.py @@ -45,14 +45,29 @@ def memory_selectItem( # Deselect mismatched items # # Conditions: -# 1. Two items are selected and they are of different groups +# 0. Two items has just been selected +# 1. The same item has been selected twice +# 1. Selected items are of different groups @llm_by_value def memory_shouldDeselectMismatchedItems( c: memory_Context ) -> memory_Context: - if ( + if not ( c.recentField == "selectedItems" and - len(c.selectedItems) == 2 and + len(c.selectedItems) == 2 + ): + c.recentField = None + return c + + if ( + c.selectedItems[0] == c.selectedItems[1] + ): + c.mismatchedItems.clear() + c.mismatchedItems.append(c.selectedItems[0]) + c.recentField = "mismatchedItems" + return c + + if ( c.playfieldItems[c.selectedItems[0]] != c.playfieldItems[c.selectedItems[1]] ): c.mismatchedItems.clear() diff --git a/v3/memory_test.py b/v3/memory_test.py index b556075..8557dfa 100644 --- a/v3/memory_test.py +++ b/v3/memory_test.py @@ -116,6 +116,32 @@ def memory_test_shouldDeselectMismatchedItems( return "ERR: memory_shouldDeselectMismatchedItems" #} +def memory_test_shouldDeselectMismatchedItems_itemTwice( +) -> str: + c = memory_createContext() + c.playfieldSize = 2 + c = memory_generateConstPlayfield(c) + + # Select the same item twice. + c.selectedId = 0 + c = memory_selectItem(c) + c.selectedId = 0 + c = memory_selectItem(c) + + # Detect mismatching. + c = memory_shouldDeselectMismatchedItems(c) + + # See if the two selected items do not match. + if ( + c.recentField == "mismatchedItems" and + len(c.mismatchedItems) == 1 and + c.mismatchedItems[0] == 0 + ): + return "OK: memory_shouldDeselectMismatchedItems_itemTwice" + #} + return "ERR: memory_shouldDeselectMismatchedItems_itemTwice" +#} + def memory_test_shouldDetectVictory( ) -> str: c = memory_createContext()