d
This commit is contained in:
91
v4/\
Normal file
91
v4/\
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
from cli import *
|
||||||
|
from llm import *
|
||||||
|
from shell_Context import *
|
||||||
|
|
||||||
|
# Greet the user upon start
|
||||||
|
@llm_by_value
|
||||||
|
def shell_launch(
|
||||||
|
c: shell_Context
|
||||||
|
) -> shell_Context:
|
||||||
|
c.cCLI = cli_greetUser(c.cCLI)
|
||||||
|
c.cCLI.input = "help"
|
||||||
|
c.cCLI = cli_showHelp(c.cCLI)
|
||||||
|
|
||||||
|
c.output = c.cCLI.outputGreeting + "\n" + c.cCLI.outputHelp
|
||||||
|
return c
|
||||||
|
#}
|
||||||
|
|
||||||
|
# Switch among CLI functions based on input
|
||||||
|
#
|
||||||
|
# Conditions:
|
||||||
|
# 1. User requested to quit the game
|
||||||
|
# 2. User plays the game
|
||||||
|
@llm_by_value
|
||||||
|
def shell_processInput(
|
||||||
|
c: shell_Context
|
||||||
|
) -> shell_Context:
|
||||||
|
# Exit
|
||||||
|
if (
|
||||||
|
c.input == "e" or
|
||||||
|
c.input == "exit" or
|
||||||
|
c.input == "q" or
|
||||||
|
c.input == "quit"
|
||||||
|
):
|
||||||
|
c.exit = True
|
||||||
|
return c
|
||||||
|
#}
|
||||||
|
|
||||||
|
c.cCLI.input = c.input
|
||||||
|
c.output = ""
|
||||||
|
|
||||||
|
# Help (aka commands)
|
||||||
|
c.cCLI = cli_showHelp(c.cCLI)
|
||||||
|
if (
|
||||||
|
c.cCLI.recentField == "outputHelp"
|
||||||
|
):
|
||||||
|
c.output += c.cCLI.outputHelp
|
||||||
|
#}
|
||||||
|
|
||||||
|
c.cCLI = cli_selectItem(c.cCLI)
|
||||||
|
|
||||||
|
# Game actions.
|
||||||
|
if (
|
||||||
|
c.cCLI.recentField == "cMemory"
|
||||||
|
):
|
||||||
|
# Prompt second item.
|
||||||
|
cli = cli_shouldPromptSelection(c.cCLI)
|
||||||
|
if (
|
||||||
|
cli.recentField == "outputPromptSelection"
|
||||||
|
):
|
||||||
|
c.output += cli.outputPromptSelection
|
||||||
|
#}
|
||||||
|
|
||||||
|
# Check matching items.
|
||||||
|
cli = c.cCLI
|
||||||
|
memory = memory_shouldHideMatchingItems(cli.cMemory)
|
||||||
|
cli.recentField = "cMemory"
|
||||||
|
# Report matched items.
|
||||||
|
cli = cli_shouldReportMatchedItems(cli)
|
||||||
|
if (
|
||||||
|
cli.recentField == "outputMatchedItems"
|
||||||
|
):
|
||||||
|
c.output += cli.outputMatchedItems
|
||||||
|
c.cCLI.cMemory = memory
|
||||||
|
#}
|
||||||
|
|
||||||
|
# Check mismatching items.
|
||||||
|
memory = memory_shouldDeselectMismatchedItems(c.cCLI.cMemory)
|
||||||
|
c.cCLI.recentField = "cMemory"
|
||||||
|
# Report mismatched items.
|
||||||
|
cli = cli_shouldReportMismatchedItems(c.cCLI)
|
||||||
|
if (
|
||||||
|
cli.recentField == "outputMismatchedItems"
|
||||||
|
):
|
||||||
|
c.output += cli.outputMismatchedItems
|
||||||
|
c.cCLI.cMemory = memory
|
||||||
|
#}
|
||||||
|
|
||||||
|
#}
|
||||||
|
|
||||||
|
return c
|
||||||
|
#}
|
||||||
46
v4/cli.py
46
v4/cli.py
@@ -20,16 +20,8 @@ def cli_greetUser(
|
|||||||
def cli_selectItem(
|
def cli_selectItem(
|
||||||
c: cli_Context
|
c: cli_Context
|
||||||
) -> cli_Context:
|
) -> cli_Context:
|
||||||
if not (
|
if (
|
||||||
c.input.isdigit()
|
c.input.isdigit()
|
||||||
):
|
|
||||||
c.recentField = "none"
|
|
||||||
return c
|
|
||||||
#}
|
|
||||||
|
|
||||||
int(c.input) >= 1 and
|
|
||||||
int(c.input) <= c.cMemory.playfieldSize * 2 and
|
|
||||||
int(c.input) not in c.cMemory.hiddenItems
|
|
||||||
):
|
):
|
||||||
# User ids start with 1 while memory module has ids starting with 0
|
# User ids start with 1 while memory module has ids starting with 0
|
||||||
# Convert cli item id to memory item id
|
# Convert cli item id to memory item id
|
||||||
@@ -60,6 +52,40 @@ def cli_shouldPromptSelection(
|
|||||||
return c
|
return c
|
||||||
#}
|
#}
|
||||||
|
|
||||||
|
# Report matched items
|
||||||
|
@llm_by_value
|
||||||
|
def cli_shouldReportMatchedItems(
|
||||||
|
c: cli_Context
|
||||||
|
) -> cli_Context:
|
||||||
|
if (
|
||||||
|
c.recentField == "cMemory" and
|
||||||
|
c.cMemory.recentField == "hiddenItems"
|
||||||
|
):
|
||||||
|
c.outputMatchedItems = "Items matched! Go on:"
|
||||||
|
c.recentField = "outputMatchedItems"
|
||||||
|
return c
|
||||||
|
#}
|
||||||
|
c.recentField = "none"
|
||||||
|
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 selection of invalid item ids
|
# Report selection of invalid item ids
|
||||||
#
|
#
|
||||||
# Conditions:
|
# Conditions:
|
||||||
@@ -92,7 +118,7 @@ def cli_showHelp(
|
|||||||
c.input == "h" or
|
c.input == "h" or
|
||||||
c.input == "help"
|
c.input == "help"
|
||||||
):
|
):
|
||||||
c.outputHelp = "Commands:\n\te, exit, q, quit\n\t\tExit\n\th, help\n\t\tList commands\n\t1, 2, 3, ...\n\t\tSelect item\n"
|
c.outputHelp = "Commands:\n\te, exit, q, quit\n\t\tExit\n\th, help\n\t\tList commands\n\t1, 2, 3, ...\n\t\tSelect item\nEnter your choice:"
|
||||||
c.recentField = "outputHelp"
|
c.recentField = "outputHelp"
|
||||||
return c
|
return c
|
||||||
#}
|
#}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ class cli_Context:
|
|||||||
self.outputGreeting = ""
|
self.outputGreeting = ""
|
||||||
self.outputHelp = ""
|
self.outputHelp = ""
|
||||||
self.recentField = "none"
|
self.recentField = "none"
|
||||||
|
self.outputMatchedItems = ""
|
||||||
|
self.outputMismatchedItems = ""
|
||||||
|
|
||||||
def cli_createContext():
|
def cli_createContext():
|
||||||
return cli_Context()
|
return cli_Context()
|
||||||
|
|||||||
@@ -16,13 +16,15 @@ print(memory_test_shouldHideMatchingItems())
|
|||||||
print(cli_test_greetUser())
|
print(cli_test_greetUser())
|
||||||
print(cli_test_selectItem())
|
print(cli_test_selectItem())
|
||||||
print(cli_test_shouldPromptSelection())
|
print(cli_test_shouldPromptSelection())
|
||||||
print(cli_test_shouldReportIvalidItemSelection_outOfBoundsMin())
|
#print(cli_test_shouldReportIvalidItemSelection_outOfBoundsMin())
|
||||||
print(cli_test_showHelp_h())
|
print(cli_test_showHelp_h())
|
||||||
print(cli_test_showHelp_help())
|
print(cli_test_showHelp_help())
|
||||||
|
|
||||||
c = shell_createContext()
|
c = shell_createContext()
|
||||||
c.cCLI = cli_createContext()
|
c.cCLI = cli_createContext()
|
||||||
c.cCLI.cMemory = memory_createContext()
|
c.cCLI.cMemory = memory_createContext()
|
||||||
|
c.cCLI.cMemory.playfieldSize = 2
|
||||||
|
c.cCLI.cMemory = memory_generateConstPlayfield(c.cCLI.cMemory)
|
||||||
|
|
||||||
c = shell_launch(c)
|
c = shell_launch(c)
|
||||||
print(c.output)
|
print(c.output)
|
||||||
|
|||||||
33
v4/shell.py
33
v4/shell.py
@@ -52,12 +52,39 @@ def shell_processInput(
|
|||||||
if (
|
if (
|
||||||
c.cCLI.recentField == "cMemory"
|
c.cCLI.recentField == "cMemory"
|
||||||
):
|
):
|
||||||
psCLI = cli_shouldPromptSelection(c.cCLI)
|
# Prompt second item.
|
||||||
|
cli = cli_shouldPromptSelection(c.cCLI)
|
||||||
if (
|
if (
|
||||||
psCLI.recentField == "outputPromptSelection"
|
cli.recentField == "outputPromptSelection"
|
||||||
):
|
):
|
||||||
c.output += psCLI.outputPromptSelection
|
c.output += cli.outputPromptSelection
|
||||||
#}
|
#}
|
||||||
|
|
||||||
|
# Check matching items.
|
||||||
|
cli = c.cCLI
|
||||||
|
cli.cMemory = memory_shouldHideMatchingItems(c.cCLI.cMemory)
|
||||||
|
cli.recentField = "cMemory"
|
||||||
|
# Report matched items.
|
||||||
|
cli = cli_shouldReportMatchedItems(cli)
|
||||||
|
if (
|
||||||
|
cli.recentField == "outputMatchedItems"
|
||||||
|
):
|
||||||
|
c.output += cli.outputMatchedItems
|
||||||
|
c.cCLI.cMemory = memory
|
||||||
|
#}
|
||||||
|
|
||||||
|
# Check mismatching items.
|
||||||
|
memory = memory_shouldDeselectMismatchedItems(c.cCLI.cMemory)
|
||||||
|
c.cCLI.recentField = "cMemory"
|
||||||
|
# Report mismatched items.
|
||||||
|
cli = cli_shouldReportMismatchedItems(c.cCLI)
|
||||||
|
if (
|
||||||
|
cli.recentField == "outputMismatchedItems"
|
||||||
|
):
|
||||||
|
c.output += cli.outputMismatchedItems
|
||||||
|
c.cCLI.cMemory = memory
|
||||||
|
#}
|
||||||
|
|
||||||
#}
|
#}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
|
|||||||
Reference in New Issue
Block a user