from entities import * def memory_generateConstPlayfield( c: MemoryContext ) -> MemoryContext: idGroups: dict[int, int] = { } id = 0 for gid in range(0, c.playfieldSize): idGroups[id] = gid id += 1 idGroups[id] = gid id += 1 #} c.playfieldItems = idGroups c.recentField = "playfieldItems" return c #} # Select item # # Conditions: # 0. Remove obsolete selected items # 1. If selectedId is recent # 2. If it's not recent def memory_selectItem( c: MemoryContext ) -> MemoryContext: if ( len(c.selectedItems) == 2 ): c.selectedItems = [] if ( c.recentField == "selectedId" ): c.selectedItems.append(c.selectedId) c.recentField = "selectedItems" return c c.recentField = None return c #} # Test. def test_memory_generateConstPlayfield( ) -> str: c = memory_createEmptyContext() c.playfieldSize = 2 c = memory_generateConstPlayfield(c) if ( c.recentField == "playfieldItems" and len(c.playfieldItems) == 4 and c.playfieldItems[0] == 0 and c.playfieldItems[1] == 0 and c.playfieldItems[2] == 1 and c.playfieldItems[3] == 1 ): return "OK: memory_generateConstPlayfield" #} return "ERR: memory_generateConstPlayfield" #} def test_memory_selectOneItem( ) -> str: c = memory_createEmptyContext() c.playfieldSize = 2 c = memory_generateConstPlayfield(c) # Select the first item. c.selectedId = 0 c.recentField = "selectedId" c = memory_selectItem(c) # See if it's in selectedItems now. if ( c.recentField == "selectedItems" and len(c.selectedItems) == 1 and c.selectedItems[0] == 0 ): return "OK: memory_selectOneItem" #} return "ERR: memory_selectOneItem" #} def test_memory_selectTwoItems( ) -> str: c = memory_createEmptyContext() c.playfieldSize = 2 c = memory_generateConstPlayfield(c) # Select the first two items. c.selectedId = 0 c.recentField = "selectedId" c = memory_selectItem(c) c.selectedId = 1 c.recentField = "selectedId" c = memory_selectItem(c) # See if it's both items are selected now. if ( c.recentField == "selectedItems" and len(c.selectedItems) == 2 and c.selectedItems[0] == 0 and c.selectedItems[1] == 1 ): return "OK: memory_selectTwoItems" #} return "ERR: memory_selectTwoItems" #} def test_memory_selectThreeItems( ) -> str: c = memory_createEmptyContext() c.playfieldSize = 2 c = memory_generateConstPlayfield(c) # Select three items. c.selectedId = 0 c.recentField = "selectedId" c = memory_selectItem(c) c.selectedId = 1 c.recentField = "selectedId" c = memory_selectItem(c) c.selectedId = 2 c.recentField = "selectedId" c = memory_selectItem(c) # See if only one (last) item is selected now. if ( c.recentField == "selectedItems" and len(c.selectedItems) == 1 and c.selectedItems[0] == 2 ): return "OK: memory_selectThreeItems" #} return "ERR: memory_selectThreeItems" #}