Михаил Капелько 6 months ago
parent
commit
d046c67108
5 changed files with 162 additions and 14 deletions
  1. +91
    -0
      v4/\
  2. +36
    -10
      v4/cli.py
  3. +2
    -0
      v4/cli_Context.py
  4. +3
    -1
      v4/main.py
  5. +30
    -3
      v4/shell.py

+ 91
- 0
v4/\ View 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
#}

+ 36
- 10
v4/cli.py View File

@@ -20,16 +20,8 @@ def cli_greetUser(
def cli_selectItem(
c: cli_Context
) -> cli_Context:
if not (
if (
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
# Convert cli item id to memory item id
@@ -60,6 +52,40 @@ def cli_shouldPromptSelection(
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
#
# Conditions:
@@ -92,7 +118,7 @@ def cli_showHelp(
c.input == "h" or
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"
return c
#}


+ 2
- 0
v4/cli_Context.py View File

@@ -5,6 +5,8 @@ class cli_Context:
self.outputGreeting = ""
self.outputHelp = ""
self.recentField = "none"
self.outputMatchedItems = ""
self.outputMismatchedItems = ""

def cli_createContext():
return cli_Context()

+ 3
- 1
v4/main.py View File

@@ -16,13 +16,15 @@ print(memory_test_shouldHideMatchingItems())
print(cli_test_greetUser())
print(cli_test_selectItem())
print(cli_test_shouldPromptSelection())
print(cli_test_shouldReportIvalidItemSelection_outOfBoundsMin())
#print(cli_test_shouldReportIvalidItemSelection_outOfBoundsMin())
print(cli_test_showHelp_h())
print(cli_test_showHelp_help())

c = shell_createContext()
c.cCLI = cli_createContext()
c.cCLI.cMemory = memory_createContext()
c.cCLI.cMemory.playfieldSize = 2
c.cCLI.cMemory = memory_generateConstPlayfield(c.cCLI.cMemory)

c = shell_launch(c)
print(c.output)


+ 30
- 3
v4/shell.py View File

@@ -52,12 +52,39 @@ def shell_processInput(
if (
c.cCLI.recentField == "cMemory"
):
psCLI = cli_shouldPromptSelection(c.cCLI)
# Prompt second item.
cli = cli_shouldPromptSelection(c.cCLI)
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


Loading…
Cancel
Save