Compare commits
6 Commits
fa73ca6f0c
...
c6a08d00a1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c6a08d00a1 | ||
|
|
b273079e01 | ||
|
|
3fb2b1d80a | ||
|
|
e8a82019d4 | ||
|
|
6a3334d39f | ||
|
|
2d0c18a403 |
@@ -1,10 +1,13 @@
|
||||
class MemoryContext:
|
||||
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__()
|
||||
|
||||
215
v2/functions.py
215
v2/functions.py
@@ -1,5 +1,51 @@
|
||||
from entities 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_detectVictory(
|
||||
c: MemoryContext
|
||||
) -> MemoryContext:
|
||||
if (
|
||||
c.recentField == "hiddenItems" and
|
||||
len(c.hiddenItems) == len(c.playfieldItems)
|
||||
):
|
||||
c.victory = True
|
||||
c.recentField = "victory"
|
||||
return c
|
||||
|
||||
c.recentField = None
|
||||
return c
|
||||
#}
|
||||
|
||||
# Deselect mismatched items
|
||||
#
|
||||
# Conditions:
|
||||
# 1. Two items are selected and they are of different groups
|
||||
@llm_by_value
|
||||
def memory_deselectMismatchedItems(
|
||||
c: MemoryContext
|
||||
) -> MemoryContext:
|
||||
if (
|
||||
c.recentField == "selectedItems" and
|
||||
len(c.selectedItems) == 2 and
|
||||
c.playfieldItems[c.selectedItems[0]] != c.playfieldItems[c.selectedItems[1]]
|
||||
):
|
||||
c.mismatchedItems.clear()
|
||||
c.mismatchedItems.append(c.selectedItems[0])
|
||||
c.mismatchedItems.append(c.selectedItems[1])
|
||||
c.recentField = "mismatchedItems"
|
||||
return c
|
||||
|
||||
c.recentField = None
|
||||
return c
|
||||
#}
|
||||
|
||||
# Generate constant playfield suitable for testing and debugging
|
||||
@llm_by_value
|
||||
def memory_generateConstPlayfield(
|
||||
c: MemoryContext
|
||||
) -> MemoryContext:
|
||||
@@ -16,12 +62,34 @@ def memory_generateConstPlayfield(
|
||||
return c
|
||||
#}
|
||||
|
||||
# Hide matching selected items
|
||||
#
|
||||
# Conditions:
|
||||
# 1. Two items are selected and they are of the same group
|
||||
@llm_by_value
|
||||
def memory_hideMatchingItems(
|
||||
c: MemoryContext
|
||||
) -> MemoryContext:
|
||||
if (
|
||||
c.recentField == "selectedItems" and
|
||||
len(c.selectedItems) == 2 and
|
||||
c.playfieldItems[c.selectedItems[0]] == c.playfieldItems[c.selectedItems[1]]
|
||||
):
|
||||
c.hiddenItems.append(c.selectedItems[0])
|
||||
c.hiddenItems.append(c.selectedItems[1])
|
||||
c.recentField = "hiddenItems"
|
||||
return c
|
||||
|
||||
c.recentField = None
|
||||
return c
|
||||
#}
|
||||
|
||||
# Select item
|
||||
#
|
||||
# Conditions:
|
||||
# 0. Remove obsolete selected items
|
||||
# 1. If selectedId is recent
|
||||
# 2. If it's not recent
|
||||
# 1. There are already two selected items
|
||||
# 2. Item has just been selected
|
||||
@llm_by_value
|
||||
def memory_selectItem(
|
||||
c: MemoryContext
|
||||
) -> MemoryContext:
|
||||
@@ -61,7 +129,7 @@ def test_memory_generateConstPlayfield(
|
||||
return "ERR: memory_generateConstPlayfield"
|
||||
#}
|
||||
|
||||
def test_memory_selectOneItem(
|
||||
def test_memory_selectItem(
|
||||
) -> str:
|
||||
c = memory_createEmptyContext()
|
||||
c.playfieldSize = 2
|
||||
@@ -78,12 +146,12 @@ def test_memory_selectOneItem(
|
||||
len(c.selectedItems) == 1 and
|
||||
c.selectedItems[0] == 0
|
||||
):
|
||||
return "OK: memory_selectOneItem"
|
||||
return "OK: selectItem"
|
||||
#}
|
||||
return "ERR: memory_selectOneItem"
|
||||
return "ERR: selectItem"
|
||||
#}
|
||||
|
||||
def test_memory_selectTwoItems(
|
||||
def test_selectTwoItems(
|
||||
) -> str:
|
||||
c = memory_createEmptyContext()
|
||||
c.playfieldSize = 2
|
||||
@@ -104,12 +172,12 @@ def test_memory_selectTwoItems(
|
||||
c.selectedItems[0] == 0 and
|
||||
c.selectedItems[1] == 1
|
||||
):
|
||||
return "OK: memory_selectTwoItems"
|
||||
return "OK: selectTwoItems"
|
||||
#}
|
||||
return "ERR: memory_selectTwoItems"
|
||||
return "ERR: selectTwoItems"
|
||||
#}
|
||||
|
||||
def test_memory_selectThreeItems(
|
||||
def test_selectThreeItems(
|
||||
) -> str:
|
||||
c = memory_createEmptyContext()
|
||||
c.playfieldSize = 2
|
||||
@@ -132,7 +200,130 @@ def test_memory_selectThreeItems(
|
||||
len(c.selectedItems) == 1 and
|
||||
c.selectedItems[0] == 2
|
||||
):
|
||||
return "OK: memory_selectThreeItems"
|
||||
return "OK: selectThreeItems"
|
||||
#}
|
||||
return "ERR: memory_selectThreeItems"
|
||||
return "ERR: selectThreeItems"
|
||||
#}
|
||||
|
||||
def test_memory_hideMatchingItems(
|
||||
) -> str:
|
||||
c = memory_createEmptyContext()
|
||||
c.playfieldSize = 2
|
||||
c = memory_generateConstPlayfield(c)
|
||||
|
||||
# Select two items of the same group.
|
||||
c.selectedId = 0
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
c.selectedId = 1
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
|
||||
# Hide matching items.
|
||||
c = memory_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_hideMatchingItems"
|
||||
#}
|
||||
return "ERR: memory_hideMatchingItems"
|
||||
#}
|
||||
|
||||
def test_memory_deselectMismatchedItems(
|
||||
) -> str:
|
||||
c = memory_createEmptyContext()
|
||||
c.playfieldSize = 2
|
||||
c = memory_generateConstPlayfield(c)
|
||||
|
||||
# Select two items of different groups.
|
||||
c.selectedId = 0
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
c.selectedId = 2
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
|
||||
# Detect mismatching.
|
||||
c = memory_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_deselectMismatchedItems"
|
||||
#}
|
||||
return "ERR: memory_deselectMismatchedItems"
|
||||
#}
|
||||
|
||||
def test_memory_detectVictory(
|
||||
) -> str:
|
||||
c = memory_createEmptyContext()
|
||||
c.playfieldSize = 2
|
||||
c = memory_generateConstPlayfield(c)
|
||||
|
||||
# Select the first two items of the same group.
|
||||
c.selectedId = 0
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
c.selectedId = 1
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
|
||||
# Hide the first pair.
|
||||
c = memory_hideMatchingItems(c)
|
||||
|
||||
# Select the last two items of the same group.
|
||||
c.selectedId = 2
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
c.selectedId = 3
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
|
||||
# Hide the second pair.
|
||||
c = memory_hideMatchingItems(c)
|
||||
|
||||
# Detect victory.
|
||||
c = memory_detectVictory(c)
|
||||
|
||||
# See if victory has been detected.
|
||||
if (
|
||||
c.recentField == "victory" and
|
||||
c.victory == True
|
||||
):
|
||||
return "OK: memory_detectVictory"
|
||||
#}
|
||||
return "ERR: memory_detectVictory"
|
||||
#}
|
||||
|
||||
def test_passContextByValue(
|
||||
) -> str:
|
||||
c = memory_createEmptyContext()
|
||||
c.playfieldSize = 2
|
||||
c = memory_generateConstPlayfield(c)
|
||||
|
||||
c.selectedId = 0
|
||||
c.recentField = "selectedId"
|
||||
c1 = memory_selectItem(c)
|
||||
|
||||
c.selectedId = 1
|
||||
c.recentField = "selectedId"
|
||||
c2 = memory_selectItem(c)
|
||||
|
||||
# See if c1 and c2 have different recentField.
|
||||
if (
|
||||
c1.selectedId != c2.selectedId
|
||||
):
|
||||
return "OK: passContextByValue"
|
||||
|
||||
return "ERR: passContextByValue"
|
||||
#}
|
||||
|
||||
|
||||
10
v2/llm.py
Normal file
10
v2/llm.py
Normal file
@@ -0,0 +1,10 @@
|
||||
import copy
|
||||
|
||||
# Make deep copies of arguments to mimic behaviour of strongly typed languages.
|
||||
# https://stackoverflow.com/a/15398021
|
||||
def llm_by_value(f):
|
||||
def _f(*args, **kwargs):
|
||||
argsCopy = copy.deepcopy(args)
|
||||
kwargsCopy = copy.deepcopy(kwargs)
|
||||
return f(*argsCopy, **kwargsCopy)
|
||||
return _f
|
||||
10
v2/main.py
10
v2/main.py
@@ -2,6 +2,10 @@ from entities import *
|
||||
from functions import *
|
||||
|
||||
print(test_memory_generateConstPlayfield())
|
||||
print(test_memory_selectOneItem())
|
||||
print(test_memory_selectTwoItems())
|
||||
print(test_memory_selectThreeItems())
|
||||
print(test_selectOneItem())
|
||||
print(test_selectTwoItems())
|
||||
print(test_selectThreeItems())
|
||||
print(test_memory_hideMatchingItems())
|
||||
print(test_memory_deselectMismatchedItems())
|
||||
print(test_memory_detectVictory())
|
||||
print(test_passContextByValue())
|
||||
|
||||
19
v3/entities.py
Normal file
19
v3/entities.py
Normal file
@@ -0,0 +1,19 @@
|
||||
class MemoryContext:
|
||||
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"MemoryContext(plyfieldI/playfieldS/recentF/selectedId/selectedI: '{self.playfieldItems}'/'{self.playfieldSize}'/'{self.recentField}'/'{self.selectedId}'/'{self.selectedItems}')"
|
||||
|
||||
def memory_createEmptyContext():
|
||||
return MemoryContext()
|
||||
329
v3/functions.py
Normal file
329
v3/functions.py
Normal file
@@ -0,0 +1,329 @@
|
||||
from entities 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_detectVictory(
|
||||
c: MemoryContext
|
||||
) -> MemoryContext:
|
||||
if (
|
||||
c.recentField == "hiddenItems" and
|
||||
len(c.hiddenItems) == len(c.playfieldItems)
|
||||
):
|
||||
c.victory = True
|
||||
c.recentField = "victory"
|
||||
return c
|
||||
|
||||
c.recentField = None
|
||||
return c
|
||||
#}
|
||||
|
||||
# Deselect mismatched items
|
||||
#
|
||||
# Conditions:
|
||||
# 1. Two items are selected and they are of different groups
|
||||
@llm_by_value
|
||||
def memory_deselectMismatchedItems(
|
||||
c: MemoryContext
|
||||
) -> MemoryContext:
|
||||
if (
|
||||
c.recentField == "selectedItems" and
|
||||
len(c.selectedItems) == 2 and
|
||||
c.playfieldItems[c.selectedItems[0]] != c.playfieldItems[c.selectedItems[1]]
|
||||
):
|
||||
c.mismatchedItems.clear()
|
||||
c.mismatchedItems.append(c.selectedItems[0])
|
||||
c.mismatchedItems.append(c.selectedItems[1])
|
||||
c.recentField = "mismatchedItems"
|
||||
return c
|
||||
|
||||
c.recentField = None
|
||||
return c
|
||||
#}
|
||||
|
||||
# Generate constant playfield suitable for testing and debugging
|
||||
@llm_by_value
|
||||
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
|
||||
#}
|
||||
|
||||
# Hide matching selected items
|
||||
#
|
||||
# Conditions:
|
||||
# 1. Two items are selected and they are of the same group
|
||||
@llm_by_value
|
||||
def memory_hideMatchingItems(
|
||||
c: MemoryContext
|
||||
) -> MemoryContext:
|
||||
if (
|
||||
c.recentField == "selectedItems" and
|
||||
len(c.selectedItems) == 2 and
|
||||
c.playfieldItems[c.selectedItems[0]] == c.playfieldItems[c.selectedItems[1]]
|
||||
):
|
||||
c.hiddenItems.append(c.selectedItems[0])
|
||||
c.hiddenItems.append(c.selectedItems[1])
|
||||
c.recentField = "hiddenItems"
|
||||
return c
|
||||
|
||||
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_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_selectItem(
|
||||
) -> 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: selectItem"
|
||||
#}
|
||||
return "ERR: selectItem"
|
||||
#}
|
||||
|
||||
def test_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: selectTwoItems"
|
||||
#}
|
||||
return "ERR: selectTwoItems"
|
||||
#}
|
||||
|
||||
def test_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: selectThreeItems"
|
||||
#}
|
||||
return "ERR: selectThreeItems"
|
||||
#}
|
||||
|
||||
def test_memory_hideMatchingItems(
|
||||
) -> str:
|
||||
c = memory_createEmptyContext()
|
||||
c.playfieldSize = 2
|
||||
c = memory_generateConstPlayfield(c)
|
||||
|
||||
# Select two items of the same group.
|
||||
c.selectedId = 0
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
c.selectedId = 1
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
|
||||
# Hide matching items.
|
||||
c = memory_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_hideMatchingItems"
|
||||
#}
|
||||
return "ERR: memory_hideMatchingItems"
|
||||
#}
|
||||
|
||||
def test_memory_deselectMismatchedItems(
|
||||
) -> str:
|
||||
c = memory_createEmptyContext()
|
||||
c.playfieldSize = 2
|
||||
c = memory_generateConstPlayfield(c)
|
||||
|
||||
# Select two items of different groups.
|
||||
c.selectedId = 0
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
c.selectedId = 2
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
|
||||
# Detect mismatching.
|
||||
c = memory_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_deselectMismatchedItems"
|
||||
#}
|
||||
return "ERR: memory_deselectMismatchedItems"
|
||||
#}
|
||||
|
||||
def test_memory_detectVictory(
|
||||
) -> str:
|
||||
c = memory_createEmptyContext()
|
||||
c.playfieldSize = 2
|
||||
c = memory_generateConstPlayfield(c)
|
||||
|
||||
# Select the first two items of the same group.
|
||||
c.selectedId = 0
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
c.selectedId = 1
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
|
||||
# Hide the first pair.
|
||||
c = memory_hideMatchingItems(c)
|
||||
|
||||
# Select the last two items of the same group.
|
||||
c.selectedId = 2
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
c.selectedId = 3
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
|
||||
# Hide the second pair.
|
||||
c = memory_hideMatchingItems(c)
|
||||
|
||||
# Detect victory.
|
||||
c = memory_detectVictory(c)
|
||||
|
||||
# See if victory has been detected.
|
||||
if (
|
||||
c.recentField == "victory" and
|
||||
c.victory == True
|
||||
):
|
||||
return "OK: memory_detectVictory"
|
||||
#}
|
||||
return "ERR: memory_detectVictory"
|
||||
#}
|
||||
|
||||
def test_passContextByValue(
|
||||
) -> str:
|
||||
c = memory_createEmptyContext()
|
||||
c.playfieldSize = 2
|
||||
c = memory_generateConstPlayfield(c)
|
||||
|
||||
c.selectedId = 0
|
||||
c.recentField = "selectedId"
|
||||
c1 = memory_selectItem(c)
|
||||
|
||||
c.selectedId = 1
|
||||
c.recentField = "selectedId"
|
||||
c2 = memory_selectItem(c)
|
||||
|
||||
# See if c1 and c2 have different recentField.
|
||||
if (
|
||||
c1.selectedId != c2.selectedId
|
||||
):
|
||||
return "OK: passContextByValue"
|
||||
|
||||
return "ERR: passContextByValue"
|
||||
#}
|
||||
|
||||
2
v3/gen-Python
Executable file
2
v3/gen-Python
Executable file
@@ -0,0 +1,2 @@
|
||||
echo "python3 main.py" > test_memory_Python
|
||||
chmod +x test_memory_Python
|
||||
10
v3/llm.py
Normal file
10
v3/llm.py
Normal file
@@ -0,0 +1,10 @@
|
||||
import copy
|
||||
|
||||
# Make deep copies of arguments to mimic behaviour of strongly typed languages.
|
||||
# https://stackoverflow.com/a/15398021
|
||||
def llm_by_value(f):
|
||||
def _f(*args, **kwargs):
|
||||
argsCopy = copy.deepcopy(args)
|
||||
kwargsCopy = copy.deepcopy(kwargs)
|
||||
return f(*argsCopy, **kwargsCopy)
|
||||
return _f
|
||||
11
v3/main.py
Normal file
11
v3/main.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from memory_test import *
|
||||
|
||||
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())
|
||||
108
v3/memory.py
Normal file
108
v3/memory.py
Normal file
@@ -0,0 +1,108 @@
|
||||
from memory_Context import *
|
||||
from llm import *
|
||||
|
||||
########
|
||||
# Client initiated input
|
||||
########
|
||||
|
||||
# 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_shouldDeselectMismatchedItems(
|
||||
c: memory_Context
|
||||
) -> memory_Context:
|
||||
if (
|
||||
c.recentField == "selectedItems" and
|
||||
len(c.selectedItems) == 2 and
|
||||
c.playfieldItems[c.selectedItems[0]] != c.playfieldItems[c.selectedItems[1]]
|
||||
):
|
||||
c.mismatchedItems.clear()
|
||||
c.mismatchedItems.append(c.selectedItems[0])
|
||||
c.mismatchedItems.append(c.selectedItems[1])
|
||||
c.recentField = "mismatchedItems"
|
||||
return c
|
||||
|
||||
c.recentField = None
|
||||
return c
|
||||
#}
|
||||
|
||||
# Detect victory
|
||||
#
|
||||
# Conditions:
|
||||
# 1. Matching items have just been hidden and all items are hidden now
|
||||
@llm_by_value
|
||||
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
|
||||
#}
|
||||
|
||||
# Hide matching selected items
|
||||
#
|
||||
# Conditions:
|
||||
# 1. Two items are selected and they are of the same group
|
||||
@llm_by_value
|
||||
def memory_shouldHideMatchingItems(
|
||||
c: memory_Context
|
||||
) -> memory_Context:
|
||||
if (
|
||||
c.recentField == "selectedItems" and
|
||||
len(c.selectedItems) == 2 and
|
||||
c.playfieldItems[c.selectedItems[0]] == c.playfieldItems[c.selectedItems[1]]
|
||||
):
|
||||
c.hiddenItems.append(c.selectedItems[0])
|
||||
c.hiddenItems.append(c.selectedItems[1])
|
||||
c.recentField = "hiddenItems"
|
||||
return c
|
||||
|
||||
c.recentField = None
|
||||
return c
|
||||
#}
|
||||
19
v3/memory_Context.py
Normal file
19
v3/memory_Context.py
Normal file
@@ -0,0 +1,19 @@
|
||||
class memory_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_Context(playfieldI/playfieldS/recentF/selectedId/selectedI: '{self.playfieldItems}'/'{self.playfieldSize}'/'{self.recentField}'/'{self.selectedId}'/'{self.selectedItems}')"
|
||||
|
||||
def memory_createContext():
|
||||
return memory_Context()
|
||||
50
v3/memory_seq_test.py
Normal file
50
v3/memory_seq_test.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from memory_api import *
|
||||
from memory_api_Context import *
|
||||
from memory_seq import *
|
||||
from memory_seq_Context import *
|
||||
|
||||
def memory_seq_test_selectThreeItems(
|
||||
) -> str:
|
||||
seq = memory_seq_createContext()
|
||||
|
||||
seq.api = memory_api_createContext()
|
||||
seq.api.playfieldSize = 2
|
||||
seq.api = memory_api_generateConstPlayfield(seq.api)
|
||||
|
||||
seq.itemsToSelect = [0, 1, 2]
|
||||
seq = memory_seq_selectItems(seq)
|
||||
|
||||
# See if only one (last) item is selected now.
|
||||
if (
|
||||
seq.api.recentField == "selectedItems" and
|
||||
len(seq.api.selectedItems) == 1 and
|
||||
seq.api.selectedItems[0] == 2
|
||||
):
|
||||
return "OK: memory_seq_selectThreeItems"
|
||||
#}
|
||||
return "ERR: memory_seq_selectThreeItems"
|
||||
#}
|
||||
|
||||
def memory_seq_test_selectTwoItems(
|
||||
) -> str:
|
||||
seq = memory_seq_createContext()
|
||||
|
||||
seq.api = memory_api_createContext()
|
||||
seq.api.playfieldSize = 2
|
||||
seq.api = memory_api_generateConstPlayfield(seq.api)
|
||||
|
||||
seq.itemsToSelect = [0, 1]
|
||||
seq = memory_seq_selectItems(seq)
|
||||
|
||||
# See if both items are selected now.
|
||||
if (
|
||||
seq.api.recentField == "selectedItems" and
|
||||
len(seq.api.selectedItems) == 2 and
|
||||
seq.api.selectedItems[0] == 0 and
|
||||
seq.api.selectedItems[1] == 1
|
||||
):
|
||||
return "OK: memory_seq_selectTwoItems"
|
||||
#}
|
||||
return "ERR: memory_seq_selectTwoItems"
|
||||
#}
|
||||
|
||||
181
v3/memory_test.py
Normal file
181
v3/memory_test.py
Normal 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"
|
||||
#}
|
||||
231
v3/seq.py
Normal file
231
v3/seq.py
Normal file
@@ -0,0 +1,231 @@
|
||||
from entities import *
|
||||
from llm import *
|
||||
|
||||
# Execute selections and verify
|
||||
#
|
||||
# Conditions:
|
||||
# 1. Matching items have just been hidden and all items are hidden now
|
||||
@llm_by_value
|
||||
def memory_api_detectVictory(
|
||||
c: MemoryContext
|
||||
) -> MemoryContext:
|
||||
|
||||
|
||||
# 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_selectItem(
|
||||
) -> 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: selectItem"
|
||||
#}
|
||||
return "ERR: selectItem"
|
||||
#}
|
||||
|
||||
def test_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: selectTwoItems"
|
||||
#}
|
||||
return "ERR: selectTwoItems"
|
||||
#}
|
||||
|
||||
def test_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: selectThreeItems"
|
||||
#}
|
||||
return "ERR: selectThreeItems"
|
||||
#}
|
||||
|
||||
def test_memory_hideMatchingItems(
|
||||
) -> str:
|
||||
c = memory_createEmptyContext()
|
||||
c.playfieldSize = 2
|
||||
c = memory_generateConstPlayfield(c)
|
||||
|
||||
# Select two items of the same group.
|
||||
c.selectedId = 0
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
c.selectedId = 1
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
|
||||
# Hide matching items.
|
||||
c = memory_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_hideMatchingItems"
|
||||
#}
|
||||
return "ERR: memory_hideMatchingItems"
|
||||
#}
|
||||
|
||||
def test_memory_deselectMismatchedItems(
|
||||
) -> str:
|
||||
c = memory_createEmptyContext()
|
||||
c.playfieldSize = 2
|
||||
c = memory_generateConstPlayfield(c)
|
||||
|
||||
# Select two items of different groups.
|
||||
c.selectedId = 0
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
c.selectedId = 2
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
|
||||
# Detect mismatching.
|
||||
c = memory_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_deselectMismatchedItems"
|
||||
#}
|
||||
return "ERR: memory_deselectMismatchedItems"
|
||||
#}
|
||||
|
||||
def test_memory_detectVictory(
|
||||
) -> str:
|
||||
c = memory_createEmptyContext()
|
||||
c.playfieldSize = 2
|
||||
c = memory_generateConstPlayfield(c)
|
||||
|
||||
# Select the first two items of the same group.
|
||||
c.selectedId = 0
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
c.selectedId = 1
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
|
||||
# Hide the first pair.
|
||||
c = memory_hideMatchingItems(c)
|
||||
|
||||
# Select the last two items of the same group.
|
||||
c.selectedId = 2
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
c.selectedId = 3
|
||||
c.recentField = "selectedId"
|
||||
c = memory_selectItem(c)
|
||||
|
||||
# Hide the second pair.
|
||||
c = memory_hideMatchingItems(c)
|
||||
|
||||
# Detect victory.
|
||||
c = memory_detectVictory(c)
|
||||
|
||||
# See if victory has been detected.
|
||||
if (
|
||||
c.recentField == "victory" and
|
||||
c.victory == True
|
||||
):
|
||||
return "OK: memory_detectVictory"
|
||||
#}
|
||||
return "ERR: memory_detectVictory"
|
||||
#}
|
||||
|
||||
def test_passContextByValue(
|
||||
) -> str:
|
||||
c = memory_createEmptyContext()
|
||||
c.playfieldSize = 2
|
||||
c = memory_generateConstPlayfield(c)
|
||||
|
||||
c.selectedId = 0
|
||||
c.recentField = "selectedId"
|
||||
c1 = memory_selectItem(c)
|
||||
|
||||
c.selectedId = 1
|
||||
c.recentField = "selectedId"
|
||||
c2 = memory_selectItem(c)
|
||||
|
||||
# See if c1 and c2 have different recentField.
|
||||
if (
|
||||
c1.selectedId != c2.selectedId
|
||||
):
|
||||
return "OK: passContextByValue"
|
||||
|
||||
return "ERR: passContextByValue"
|
||||
#}
|
||||
|
||||
Reference in New Issue
Block a user