From 3fb2b1d80aa47cc6cf00d72cf5e911bde9708388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sat, 20 Apr 2024 23:16:36 +0300 Subject: [PATCH] d --- v3/main.py | 16 ++-- v3/{api.py => memory_api.py} | 22 +++--- v3/memory_api_Context.py | 19 +++++ v3/memory_api_test.py | 143 +++++++++++++++++++++++++++++++++++ 4 files changed, 179 insertions(+), 21 deletions(-) rename v3/{api.py => memory_api.py} (88%) create mode 100644 v3/memory_api_Context.py create mode 100644 v3/memory_api_test.py diff --git a/v3/main.py b/v3/main.py index f83e394..2d8b2ed 100644 --- a/v3/main.py +++ b/v3/main.py @@ -1,11 +1,7 @@ -from entities import * -from functions import * +from memory_api_test import * -print(test_memory_generateConstPlayfield()) -print(test_selectOneItem()) -print(test_selectTwoItems()) -print(test_selectThreeItems()) -print(test_memory_hideMatchingItems()) -print(test_memory_deselectMismatchedItems()) -print(test_memory_detectVictory()) -print(test_passContextByValue()) +print(memory_api_test_deselectMismatchedItems()) +print(memory_api_test_detectVictory()) +print(memory_api_test_generateConstPlayfield()) +print(memory_api_test_hideMatchingItems()) +print(memory_api_test_selectItem()) diff --git a/v3/api.py b/v3/memory_api.py similarity index 88% rename from v3/api.py rename to v3/memory_api.py index 5d1400f..08ee992 100644 --- a/v3/api.py +++ b/v3/memory_api.py @@ -1,4 +1,4 @@ -from entities import * +from memory_api_Context import * from llm import * # Detect victory @@ -7,8 +7,8 @@ from llm import * # 1. Matching items have just been hidden and all items are hidden now @llm_by_value def memory_api_detectVictory( - c: MemoryContext -) -> MemoryContext: + c: memory_api_Context +) -> memory_api_Context: if ( c.recentField == "hiddenItems" and len(c.hiddenItems) == len(c.playfieldItems) @@ -27,8 +27,8 @@ def memory_api_detectVictory( # 1. Two items are selected and they are of different groups @llm_by_value def memory_api_deselectMismatchedItems( - c: MemoryContext -) -> MemoryContext: + c: memory_api_Context +) -> memory_api_Context: if ( c.recentField == "selectedItems" and len(c.selectedItems) == 2 and @@ -47,8 +47,8 @@ def memory_api_deselectMismatchedItems( # Generate constant playfield suitable for testing and debugging @llm_by_value def memory_api_generateConstPlayfield( - c: MemoryContext -) -> MemoryContext: + c: memory_api_Context +) -> memory_api_Context: idGroups: dict[int, int] = { } id = 0 for gid in range(0, c.playfieldSize): @@ -68,8 +68,8 @@ def memory_api_generateConstPlayfield( # 1. Two items are selected and they are of the same group @llm_by_value def memory_api_hideMatchingItems( - c: MemoryContext -) -> MemoryContext: + c: memory_api_Context +) -> memory_api_Context: if ( c.recentField == "selectedItems" and len(c.selectedItems) == 2 and @@ -91,8 +91,8 @@ def memory_api_hideMatchingItems( # 2. Item has just been selected @llm_by_value def memory_api_selectItem( - c: MemoryContext -) -> MemoryContext: + c: memory_api_Context +) -> memory_api_Context: if ( len(c.selectedItems) == 2 ): diff --git a/v3/memory_api_Context.py b/v3/memory_api_Context.py new file mode 100644 index 0000000..e8d8eac --- /dev/null +++ b/v3/memory_api_Context.py @@ -0,0 +1,19 @@ +class memory_api_Context: + def __init__(self): + self.hiddenItems = [] + self.mismatchedItems = [] + self.playfieldItems = {} + self.playfieldSize = 0 + self.recentField = None + self.selectedId = None + self.selectedItems = [] + self.victory = False + + def __repr__(self): + return self.__str__() + + def __str__(self): + return f"memory_api_Context(playfieldI/playfieldS/recentF/selectedId/selectedI: '{self.playfieldItems}'/'{self.playfieldSize}'/'{self.recentField}'/'{self.selectedId}'/'{self.selectedItems}')" + +def memory_api_createContext(): + return memory_api_Context() diff --git a/v3/memory_api_test.py b/v3/memory_api_test.py new file mode 100644 index 0000000..b169b5f --- /dev/null +++ b/v3/memory_api_test.py @@ -0,0 +1,143 @@ +from memory_api import * +from memory_api_Context import * +from llm import * + +def memory_api_test_deselectMismatchedItems( +) -> str: + c = memory_api_createContext() + c.playfieldSize = 2 + c = memory_api_generateConstPlayfield(c) + + # Select two items of different groups. + c.selectedId = 0 + c.recentField = "selectedId" + c = memory_api_selectItem(c) + c.selectedId = 2 + c.recentField = "selectedId" + c = memory_api_selectItem(c) + + # Detect mismatching. + c = memory_api_deselectMismatchedItems(c) + + # See if the two selected items do not match. + if ( + c.recentField == "mismatchedItems" and + len(c.mismatchedItems) == 2 and + c.mismatchedItems[0] == 0 and + c.mismatchedItems[1] == 2 + ): + return "OK: memory_api_deselectMismatchedItems" + #} + return "ERR: memory_api_deselectMismatchedItems" +#} + +def memory_api_test_detectVictory( +) -> str: + c = memory_api_createContext() + c.playfieldSize = 2 + c = memory_api_generateConstPlayfield(c) + + # Select the first two items of the same group. + c.selectedId = 0 + c.recentField = "selectedId" + c = memory_api_selectItem(c) + c.selectedId = 1 + c.recentField = "selectedId" + c = memory_api_selectItem(c) + + # Hide the first pair. + c = memory_api_hideMatchingItems(c) + + # Select the last two items of the same group. + c.selectedId = 2 + c.recentField = "selectedId" + c = memory_api_selectItem(c) + c.selectedId = 3 + c.recentField = "selectedId" + c = memory_api_selectItem(c) + + # Hide the second pair. + c = memory_api_hideMatchingItems(c) + + # Detect victory. + c = memory_api_detectVictory(c) + + # See if victory has been detected. + if ( + c.recentField == "victory" and + c.victory == True + ): + return "OK: memory_api_detectVictory" + #} + return "ERR: memory_api_detectVictory" +#} + +def memory_api_test_generateConstPlayfield( +) -> str: + c = memory_api_createContext() + c.playfieldSize = 2 + c = memory_api_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_api_generateConstPlayfield" + #} + return "ERR: memory_api_generateConstPlayfield" +#} + +def memory_api_test_hideMatchingItems( +) -> str: + c = memory_api_createContext() + c.playfieldSize = 2 + c = memory_api_generateConstPlayfield(c) + + # Select two items of the same group. + c.selectedId = 0 + c.recentField = "selectedId" + c = memory_api_selectItem(c) + c.selectedId = 1 + c.recentField = "selectedId" + c = memory_api_selectItem(c) + + # Hide matching items. + c = memory_api_hideMatchingItems(c) + + # See if the two selected items match. + if ( + c.recentField == "hiddenItems" and + len(c.hiddenItems) == 2 and + c.hiddenItems[0] == 0 and + c.hiddenItems[1] == 1 + ): + return "OK: memory_api_hideMatchingItems" + #} + return "ERR: memory_api_hideMatchingItems" +#} + +def memory_api_test_selectItem( +) -> str: + c = memory_api_createContext() + c.playfieldSize = 2 + c = memory_api_generateConstPlayfield(c) + + # Select the first item. + c.selectedId = 0 + c.recentField = "selectedId" + c = memory_api_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_api_selectItem" + #} + return "ERR: memory_api_selectItem" +#} +