Михаил Капелько il y a 8 mois
Parent
révision
fa73ca6f0c
3 fichiers modifiés avec 106 ajouts et 20 suppressions
  1. +12
    -3
      v2/entities.py
  2. +91
    -16
      v2/functions.py
  3. +3
    -1
      v2/main.py

+ 12
- 3
v2/entities.py Voir le fichier

@@ -1,7 +1,16 @@
class MemoryContext:
playfieldItems = {}
playfieldSize = 0
selectedItems = []
def __init__(self):
self.playfieldItems = {}
self.playfieldSize = 0
self.recentField = None
self.selectedId = None
self.selectedItems = []

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()

+ 91
- 16
v2/functions.py Voir le fichier

@@ -2,7 +2,7 @@ from entities import *

def memory_generateConstPlayfield(
c: MemoryContext
) -> str:
) -> MemoryContext:
idGroups: dict[int, int] = { }
id = 0
for gid in range(0, c.playfieldSize):
@@ -12,15 +12,33 @@ def memory_generateConstPlayfield(
id += 1
#}
c.playfieldItems = idGroups
return "playfieldItems"
c.recentField = "playfieldItems"
return c
#}

# Select item
#
# Conditions:
# 0. Remove obsolete selected items
# 1. If selectedId is recent
# 2. If it's not recent
def memory_selectItem(
c: MemoryContext,
id: int
) -> str:
c.selectedItems.append(id)
return "selectedItems"
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.
@@ -29,9 +47,9 @@ def test_memory_generateConstPlayfield(
) -> str:
c = memory_createEmptyContext()
c.playfieldSize = 2
fieldName = memory_generateConstPlayfield(c)
c = memory_generateConstPlayfield(c)
if (
fieldName == "playfieldItems" and
c.recentField == "playfieldItems" and
len(c.playfieldItems) == 4 and
c.playfieldItems[0] == 0 and
c.playfieldItems[1] == 0 and
@@ -43,21 +61,78 @@ def test_memory_generateConstPlayfield(
return "ERR: memory_generateConstPlayfield"
#}

def test_memory_selectItem(
def test_memory_selectOneItem(
) -> str:
c = memory_createEmptyContext()
c.playfieldSize = 2
memory_generateConstPlayfield(c)
# Select the item with group 0.
fieldName = memory_selectItem(c, 0)
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 (
fieldName == "selectedItems" and
c.recentField == "selectedItems" and
len(c.selectedItems) == 1 and
c.selectedItems[0] == 0
):
return "OK: memory_selectItem"
return "OK: memory_selectOneItem"
#}
return "ERR: memory_selectOneItem"
#}

def test_memory_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: memory_selectTwoItems"
#}
return "ERR: memory_selectTwoItems"
#}

def test_memory_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: memory_selectThreeItems"
#}
return "ERR: memory_selectItem"
return "ERR: memory_selectThreeItems"
#}

+ 3
- 1
v2/main.py Voir le fichier

@@ -2,4 +2,6 @@ from entities import *
from functions import *

print(test_memory_generateConstPlayfield())
print(test_memory_selectItem())
print(test_memory_selectOneItem())
print(test_memory_selectTwoItems())
print(test_memory_selectThreeItems())

Chargement…
Annuler
Enregistrer