This commit is contained in:
Михаил Капелько
2024-04-22 22:50:55 +03:00
parent b273079e01
commit c6a08d00a1
5 changed files with 254 additions and 216 deletions

View File

@@ -1,10 +1,11 @@
from memory_api_test import *
from memory_seq_test import *
from memory_test import *
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())
print(memory_test_generateConstPlayfield())
print(memory_test_selectItem_1x())
print(memory_test_selectItem_2x())
print(memory_test_selectItem_3x())
print(memory_test_shouldDeselectMismatchedItems())
print(memory_test_shouldDetectVictory())
print(memory_test_shouldHideMatchingItems())
print(memory_seq_test_selectTwoItems())
#print(memory_seq_test_selectTwoItems())

View File

@@ -1,34 +1,55 @@
from memory_api_Context import *
from memory_Context import *
from llm import *
# Detect victory
#
# Conditions:
# 1. Matching items have just been hidden and all items are hidden now
@llm_by_value
def memory_api_detectVictory(
c: memory_api_Context
) -> memory_api_Context:
if (
c.recentField == "hiddenItems" and
len(c.hiddenItems) == len(c.playfieldItems)
):
c.victory = True
c.recentField = "victory"
return c
########
# Client initiated input
########
c.recentField = None
# Generate constant playfield
@llm_by_value
def memory_generateConstPlayfield(
c: memory_Context
) -> memory_Context:
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
@llm_by_value
def memory_selectItem(
c: memory_Context
) -> memory_Context:
if (
len(c.selectedItems) == 2
):
c.selectedItems = []
c.selectedItems.append(c.selectedId)
c.recentField = "selectedItems"
return c
#}
########
# System initiated reaction
########
# Deselect mismatched items
#
# Conditions:
# 1. Two items are selected and they are of different groups
@llm_by_value
def memory_api_deselectMismatchedItems(
c: memory_api_Context
) -> memory_api_Context:
def memory_shouldDeselectMismatchedItems(
c: memory_Context
) -> memory_Context:
if (
c.recentField == "selectedItems" and
len(c.selectedItems) == 2 and
@@ -44,21 +65,23 @@ def memory_api_deselectMismatchedItems(
return c
#}
# Generate constant playfield suitable for testing and debugging
# Detect victory
#
# Conditions:
# 1. Matching items have just been hidden and all items are hidden now
@llm_by_value
def memory_api_generateConstPlayfield(
c: memory_api_Context
) -> memory_api_Context:
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"
def memory_shouldDetectVictory(
c: memory_Context
) -> memory_Context:
if (
c.recentField == "hiddenItems" and
len(c.hiddenItems) == len(c.playfieldItems)
):
c.victory = True
c.recentField = "victory"
return c
c.recentField = None
return c
#}
@@ -67,9 +90,9 @@ def memory_api_generateConstPlayfield(
# Conditions:
# 1. Two items are selected and they are of the same group
@llm_by_value
def memory_api_hideMatchingItems(
c: memory_api_Context
) -> memory_api_Context:
def memory_shouldHideMatchingItems(
c: memory_Context
) -> memory_Context:
if (
c.recentField == "selectedItems" and
len(c.selectedItems) == 2 and
@@ -83,28 +106,3 @@ def memory_api_hideMatchingItems(
c.recentField = None
return c
#}
# Select item
#
# Conditions:
# 1. There are already two selected items
# 2. Item has just been selected
@llm_by_value
def memory_api_selectItem(
c: memory_api_Context
) -> memory_api_Context:
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
#}

View File

@@ -1,4 +1,4 @@
class memory_api_Context:
class memory_Context:
def __init__(self):
self.hiddenItems = []
self.mismatchedItems = []
@@ -13,7 +13,7 @@ class memory_api_Context:
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}')"
return f"memory_Context(playfieldI/playfieldS/recentF/selectedId/selectedI: '{self.playfieldItems}'/'{self.playfieldSize}'/'{self.recentField}'/'{self.selectedId}'/'{self.selectedItems}')"
def memory_api_createContext():
return memory_api_Context()
def memory_createContext():
return memory_Context()

View File

@@ -1,142 +0,0 @@
from memory_api import *
from memory_api_Context 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"
#}

181
v3/memory_test.py Normal file
View File

@@ -0,0 +1,181 @@
from memory import *
from memory_Context import *
def memory_test_generateConstPlayfield(
) -> str:
c = memory_createContext()
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 memory_test_selectItem_1x(
) -> str:
c = memory_createContext()
c.playfieldSize = 2
c = memory_generateConstPlayfield(c)
# Select the first item.
c.selectedId = 0
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_selectItem_1x"
#}
return "ERR: memory_selectItem_1x"
#}
def memory_test_selectItem_2x(
) -> str:
c = memory_createContext()
c.playfieldSize = 2
c = memory_generateConstPlayfield(c)
# Select the first two items.
c.selectedId = 0
c = memory_selectItem(c)
c.selectedId = 1
c = memory_selectItem(c)
# See if 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_selectItem_2x"
#}
return "ERR: memory_selectItem_2x"
#}
def memory_test_selectItem_3x(
) -> str:
c = memory_createContext()
c.playfieldSize = 2
c = memory_generateConstPlayfield(c)
# Select three items.
c.selectedId = 0
c = memory_selectItem(c)
c.selectedId = 1
c = memory_selectItem(c)
c.selectedId = 2
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_selectItem_3x"
#}
return "ERR: memory_selectItem_3x"
#}
def memory_test_shouldDeselectMismatchedItems(
) -> str:
c = memory_createContext()
c.playfieldSize = 2
c = memory_generateConstPlayfield(c)
# Select two items of different groups.
c.selectedId = 0
c = memory_selectItem(c)
c.selectedId = 2
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) == 2 and
c.mismatchedItems[0] == 0 and
c.mismatchedItems[1] == 2
):
return "OK: memory_shouldDeselectMismatchedItems"
#}
return "ERR: memory_shouldDeselectMismatchedItems"
#}
def memory_test_shouldDetectVictory(
) -> str:
c = memory_createContext()
c.playfieldSize = 2
c = memory_generateConstPlayfield(c)
# Select the first two items of the same group.
c.selectedId = 0
c = memory_selectItem(c)
c.selectedId = 1
c = memory_selectItem(c)
# Hide the first pair.
c = memory_shouldHideMatchingItems(c)
# Select the last two items of the same group.
c.selectedId = 2
c = memory_selectItem(c)
c.selectedId = 3
c = memory_selectItem(c)
# Hide the second pair.
c = memory_shouldHideMatchingItems(c)
# Detect victory.
c = memory_shouldDetectVictory(c)
# See if victory has been detected.
if (
c.recentField == "victory" and
c.victory == True
):
return "OK: memory_shouldDetectVictory"
#}
return "ERR: memory_shouldDetectVictory"
#}
def memory_test_shouldHideMatchingItems(
) -> str:
c = memory_createContext()
c.playfieldSize = 2
c = memory_generateConstPlayfield(c)
# Select two items of the same group.
c.selectedId = 0
c = memory_selectItem(c)
c.selectedId = 1
c = memory_selectItem(c)
# Hide matching items.
c = memory_shouldHideMatchingItems(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_shouldHideMatchingItems"
#}
return "ERR: memory_shouldHideMatchingItems"
#}