diff --git a/v2/entities.py b/v2/entities.py index b12e9b9..2be6efd 100644 --- a/v2/entities.py +++ b/v2/entities.py @@ -1,7 +1,16 @@ class MemoryContext: - playfieldItems = {} - playfieldSize = 0 - selectedItems = [] + def __init__(self): + self.playfieldItems = {} + self.playfieldSize = 0 + self.recentField = None + self.selectedId = None + self.selectedItems = [] + + def __repr__(self): + return self.__str__() + + def __str__(self): + return f"MemoryContext(plyfieldI/playfieldS/recentF/selectedId/selectedI: '{self.playfieldItems}'/'{self.playfieldSize}'/'{self.recentField}'/'{self.selectedId}'/'{self.selectedItems}')" def memory_createEmptyContext(): return MemoryContext() diff --git a/v2/functions.py b/v2/functions.py index 18b398d..2c4c47c 100644 --- a/v2/functions.py +++ b/v2/functions.py @@ -2,7 +2,7 @@ from entities import * def memory_generateConstPlayfield( c: MemoryContext -) -> str: +) -> MemoryContext: idGroups: dict[int, int] = { } id = 0 for gid in range(0, c.playfieldSize): @@ -12,15 +12,33 @@ def memory_generateConstPlayfield( id += 1 #} c.playfieldItems = idGroups - return "playfieldItems" + 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, - id: int -) -> str: - c.selectedItems.append(id) - return "selectedItems" + 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. @@ -29,9 +47,9 @@ def test_memory_generateConstPlayfield( ) -> str: c = memory_createEmptyContext() c.playfieldSize = 2 - fieldName = memory_generateConstPlayfield(c) + c = memory_generateConstPlayfield(c) if ( - fieldName == "playfieldItems" and + c.recentField == "playfieldItems" and len(c.playfieldItems) == 4 and c.playfieldItems[0] == 0 and c.playfieldItems[1] == 0 and @@ -43,21 +61,78 @@ def test_memory_generateConstPlayfield( return "ERR: memory_generateConstPlayfield" #} -def test_memory_selectItem( +def test_memory_selectOneItem( ) -> str: c = memory_createEmptyContext() c.playfieldSize = 2 - memory_generateConstPlayfield(c) - # Select the item with group 0. - fieldName = memory_selectItem(c, 0) + 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 ( - fieldName == "selectedItems" and + c.recentField == "selectedItems" and len(c.selectedItems) == 1 and c.selectedItems[0] == 0 ): - return "OK: memory_selectItem" + 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_selectItem" + return "ERR: memory_selectThreeItems" #} diff --git a/v2/main.py b/v2/main.py index bd9a29c..6da1910 100644 --- a/v2/main.py +++ b/v2/main.py @@ -2,4 +2,6 @@ from entities import * from functions import * print(test_memory_generateConstPlayfield()) -print(test_memory_selectItem()) +print(test_memory_selectOneItem()) +print(test_memory_selectTwoItems()) +print(test_memory_selectThreeItems())