Михаил Капелько 5 months ago
parent
commit
d46fb6bb76
7 changed files with 225 additions and 33 deletions
  1. +1
    -1
      v4/Controller.py
  2. +66
    -18
      v4/cli.py
  3. +130
    -0
      v4/cli_test.py
  4. +11
    -4
      v4/main.py
  5. +2
    -2
      v4/memory.py
  6. +7
    -0
      v4/memory_Context.py
  7. +8
    -8
      v4/memory_test.py

+ 1
- 1
v4/Controller.py View File

@@ -44,7 +44,7 @@ class Controller:

def set(self, fieldName, value):
c = copy.deepcopy(self.context)
setattr(c, fieldName, value)
c.setField(fieldName, value)
c.recentField = fieldName
self.queue.append(c)
self.processQueue()

+ 66
- 18
v4/cli.py View File

@@ -5,6 +5,7 @@ from llm import *
#
# Conditions:
# 1. `e`, `exit`, `q`, or `quit` was entered
# 2. Victory has just been reported
@llm_by_value
def cli_exit(
c: memory_Context
@@ -22,6 +23,33 @@ def cli_exit(
c.recentField = "exit"
return c
#}
if (
c.recentField == "outputVictory"
):
c.exit = True
c.recentField = "exit"
return c
#}
c.recentField = "none"
return c
#}

# Ask user to go on
#
# Conditions:
# 1. Items have just matched and there are still items left to select
@llm_by_value
def cli_goOn(
c: memory_Context
) -> memory_Context:
if (
c.recentField == "outputMatchedItems" and
len(c.hiddenItems) != len(c.playfieldItems)
):
c.outputGoOn = "Go on:"
c.recentField = "outputGoOn"
return c
#}
c.recentField = "none"
return c
#}
@@ -47,6 +75,9 @@ def cli_greetUser(
#}

# Ask user to select second item to have a pair of selected items
#
# Conditions:
# 1. Single item has just been selected
@llm_by_value
def cli_promptSecondItemSelection(
c: memory_Context
@@ -64,6 +95,9 @@ def cli_promptSecondItemSelection(
#}

# Report matched items
#
# Conditions:
# 1. Items were hidden (i.e., they matched)
@llm_by_value
def cli_reportMatchedItems(
c: memory_Context
@@ -71,7 +105,7 @@ def cli_reportMatchedItems(
if (
c.recentField == "hiddenItems"
):
c.outputMatchedItems = "Items matched! Go on:"
c.outputMatchedItems = "Items matched!"
c.recentField = "outputMatchedItems"
return c
#}
@@ -79,6 +113,21 @@ def cli_reportMatchedItems(
return c
#}

# Report victory
@llm_by_value
def cli_reportVictory(
c: memory_Context
) -> memory_Context:
if (
c.recentField == "victory"
):
c.outputVictory = "VICTORY! The game is over now"
c.recentField = "outputVictory"
return c
#}
c.recentField = "none"
return c
#}

# Select item
#
@@ -133,23 +182,22 @@ def cli_showHelp(
return c
#}

## Report mismatched items
#@llm_by_value
#def cli_shouldReportMismatchedItems(
# c: cli_Context
#) -> cli_Context:
# if (
# c.recentField == "cMemory" and
# c.cMemory.recentField == "mismatchedItems"
# ):
# c.outputMatchedItems = "Wrong! Try again:"
# c.recentField = "outputMismatchedItems"
# return c
# #}
# c.recentField = "none"
# return c
##}
#
# Report mismatched items
@llm_by_value
def cli_reportMismatchedItems(
c: memory_Context
) -> memory_Context:
if (
c.recentField == "mismatchedItems"
):
c.outputMismatchedItems = "Wrong! Try again:"
c.recentField = "outputMismatchedItems"
return c
#}
c.recentField = "none"
return c
#}

## Report selection of invalid item ids
##
## Conditions:


+ 130
- 0
v4/cli_test.py View File

@@ -30,6 +30,46 @@ def cli_test_exit_exit(
return "ERR: cli_exit_e"
#}

def cli_test_exit_victory(
) -> str:
c = memory_createContext()
c.playfieldSize = 2
c.recentField = "playfieldSize"
c = memory_generateConstPlayfield(c)

# Match the first pair of tiles.
c.input = "1"
c.recentField = "input"
c = cli_selectItem(c)
c = memory_selectItem(c)
c.input = "2"
c.recentField = "input"
c = cli_selectItem(c)
c = memory_selectItem(c)
c = memory_hideMatchingItems(c)

# Match the second pair of tiles.
c.input = "3"
c.recentField = "input"
c = cli_selectItem(c)
c = memory_selectItem(c)
c.input = "4"
c.recentField = "input"
c = cli_selectItem(c)
c = memory_selectItem(c)
c = memory_hideMatchingItems(c)
c = memory_detectVictory(c)
c = cli_reportVictory(c)
c = cli_exit(c)

if (
c.recentField == "exit"
):
return "OK: cli_exit_victory"
#}
return "ERR: cli_exit_victory"
#}

def cli_test_exit_q(
) -> str:
c = memory_createContext()
@@ -58,6 +98,34 @@ def cli_test_exit_quit(
return "ERR: cli_exit_quit"
#}

def cli_test_goOn(
) -> str:
c = memory_createContext()
c.playfieldSize = 2
c.recentField = "playfieldSize"
c = memory_generateConstPlayfield(c)

# Match the first pair of items.
c.input = "1"
c.recentField = "input"
c = cli_selectItem(c)
c = memory_selectItem(c)
c.input = "2"
c.recentField = "input"
c = cli_selectItem(c)
c = memory_selectItem(c)
c = memory_hideMatchingItems(c)
c = cli_reportMatchedItems(c)

c = cli_goOn(c)
if (
c.recentField == "outputGoOn"
):
return "OK: cli_goOn"
#}
return "ERR: cli_goOn"
#}

def cli_test_greetUser(
) -> str:
c = memory_createContext()
@@ -112,6 +180,30 @@ def cli_test_reportMatchedItems(
return "ERR: cli_reportMatchedItems"
#}

def cli_test_reportMismatchedItems(
) -> str:
c = memory_createContext()
c.playfieldSize = 2
c.recentField = "playfieldSize"
c = memory_generateConstPlayfield(c)
c.input = "1"
c.recentField = "input"
c = cli_selectItem(c)
c = memory_selectItem(c)
c.input = "3"
c.recentField = "input"
c = cli_selectItem(c)
c = memory_selectItem(c)
c = memory_detectMismatchedItems(c)
c = cli_reportMismatchedItems(c)
if (
c.recentField == "outputMismatchedItems"
):
return "OK: cli_reportMismatchedItems"
#}
return "ERR: cli_reportMismatchedItems"
#}

def cli_test_selectItem(
) -> str:
c = memory_createContext()
@@ -155,6 +247,44 @@ def cli_test_showHelp_help(
return "ERR: cli_showHelp_help"
#}

def cli_test_reportVictory(
) -> str:
c = memory_createContext()
c.playfieldSize = 2
c.recentField = "playfieldSize"
c = memory_generateConstPlayfield(c)

# Match the first pair of tiles.
c.input = "1"
c.recentField = "input"
c = cli_selectItem(c)
c = memory_selectItem(c)
c.input = "2"
c.recentField = "input"
c = cli_selectItem(c)
c = memory_selectItem(c)
c = memory_hideMatchingItems(c)

# Match the second pair of tiles.
c.input = "3"
c.recentField = "input"
c = cli_selectItem(c)
c = memory_selectItem(c)
c.input = "4"
c.recentField = "input"
c = cli_selectItem(c)
c = memory_selectItem(c)
c = memory_hideMatchingItems(c)
c = memory_detectVictory(c)
c = cli_reportVictory(c)

if (
c.recentField == "outputVictory"
):
return "OK: cli_reportVictory"
#}
return "ERR: cli_reportVictory"
#}

#def cli_test_shouldReportIvalidItemSelection_outOfBoundsMin(
#) -> str:


+ 11
- 4
v4/main.py View File

@@ -4,8 +4,8 @@ from memory_test import *
from Controller import *
import sys

print(memory_test_deselectMismatchedItems())
print(memory_test_deselectMismatchedItems_itemTwice())
print(memory_test_detectMismatchedItems())
print(memory_test_detectMismatchedItems_itemTwice())
print(memory_test_detectVictory())
print(memory_test_generateConstPlayfield())
print(memory_test_hideMatchingItems())
@@ -15,24 +15,31 @@ print(memory_test_selectItem_3x())

print(cli_test_exit_e())
print(cli_test_exit_exit())
print(cli_test_exit_victory())
print(cli_test_exit_q())
print(cli_test_exit_quit())
print(cli_test_goOn())
print(cli_test_greetUser())
print(cli_test_showHelp_h())
print(cli_test_showHelp_help())
print(cli_test_selectItem())
print(cli_test_promptSecondItemSelection())
print(cli_test_reportMatchedItems())
print(cli_test_reportMismatchedItems())
print(cli_test_reportVictory())


ctrl = Controller(memory_createContext())
ctrl.registerFunction(cli_exit)
ctrl.registerFunction(cli_goOn)
ctrl.registerFunction(cli_greetUser)
ctrl.registerFunction(cli_promptSecondItemSelection)
ctrl.registerFunction(cli_reportMatchedItems)
ctrl.registerFunction(cli_reportMismatchedItems)
ctrl.registerFunction(cli_reportVictory)
ctrl.registerFunction(cli_selectItem)
ctrl.registerFunction(cli_showHelp)
ctrl.registerFunction(memory_deselectMismatchedItems)
ctrl.registerFunction(memory_detectMismatchedItems)
ctrl.registerFunction(memory_detectVictory)
ctrl.registerFunction(memory_generateConstPlayfield)
ctrl.registerFunction(memory_hideMatchingItems)
@@ -40,7 +47,7 @@ ctrl.registerFunction(memory_selectItem)

def printOutput(c):
if c.recentField.startswith("output"):
print(getattr(c, c.recentField))
print(c.field(c.recentField))
ctrl.registerCallback(printOutput)
#ctrl.registerCallback(lambda c: print(f"ИГР App.dbg ctx: '{c}'"))
ctrl.registerFieldCallback("exit", lambda c: sys.exit(0))


+ 2
- 2
v4/memory.py View File

@@ -1,14 +1,14 @@
from memory_Context import *
from llm import *

# Deselect mismatched items
# Detect mismatched items
#
# Conditions:
# 0. Two items has just been selected
# 1. The same item has been selected twice
# 2. Selected items are of different groups
@llm_by_value
def memory_deselectMismatchedItems(
def memory_detectMismatchedItems(
c: memory_Context
) -> memory_Context:
if not (


+ 7
- 0
v4/memory_Context.py View File

@@ -4,11 +4,13 @@ class memory_Context:
self.hiddenItems = []
self.input = ""
self.mismatchedItems = []
self.outputGoOn = ""
self.outputGreeting = ""
self.outputHelp = ""
self.outputMatchedItems = ""
self.outputMismatchedItems = ""
self.outputPromptSelection = ""
self.outputVictory = ""
self.playfieldItems = {}
self.playfieldSize = 0
self.recentField = "none"
@@ -16,6 +18,11 @@ class memory_Context:
self.selectedItems = []
self.victory = False

def field(self, fieldName):
return getattr(self, fieldName)

def setField(self, fieldName, value):
setattr(self, fieldName, value)

def __repr__(self):
return self.__str__()


+ 8
- 8
v4/memory_test.py View File

@@ -1,7 +1,7 @@
from memory import *
from memory_Context import *

def memory_test_deselectMismatchedItems(
def memory_test_detectMismatchedItems(
) -> str:
c = memory_createContext()
c.playfieldSize = 2
@@ -17,7 +17,7 @@ def memory_test_deselectMismatchedItems(
c = memory_selectItem(c)

# Detect mismatching.
c = memory_deselectMismatchedItems(c)
c = memory_detectMismatchedItems(c)

# See if the two selected items do not match.
if (
@@ -26,12 +26,12 @@ def memory_test_deselectMismatchedItems(
c.mismatchedItems[0] == 0 and
c.mismatchedItems[1] == 2
):
return "OK: memory_deselectMismatchedItems"
return "OK: memory_detectMismatchedItems"
#}
return "ERR: memory_deselectMismatchedItems"
return "ERR: memory_detectMismatchedItems"
#}

def memory_test_deselectMismatchedItems_itemTwice(
def memory_test_detectMismatchedItems_itemTwice(
) -> str:
c = memory_createContext()
c.playfieldSize = 2
@@ -47,7 +47,7 @@ def memory_test_deselectMismatchedItems_itemTwice(
c = memory_selectItem(c)

# Detect mismatching.
c = memory_deselectMismatchedItems(c)
c = memory_detectMismatchedItems(c)

# See if the two selected items do not match.
if (
@@ -55,9 +55,9 @@ def memory_test_deselectMismatchedItems_itemTwice(
len(c.mismatchedItems) == 1 and
c.mismatchedItems[0] == 0
):
return "OK: memory_deselectMismatchedItems_itemTwice"
return "OK: memory_detectMismatchedItems_itemTwice"
#}
return "ERR: memory_deselectMismatchedItems_itemTwice"
return "ERR: memory_detectMismatchedItems_itemTwice"
#}

def memory_test_detectVictory(


Loading…
Cancel
Save