This commit is contained in:
Михаил Капелько
2024-04-24 22:44:36 +03:00
parent 3a24d68174
commit fc82f332ed
9 changed files with 71 additions and 42 deletions

View File

@@ -1 +1 @@
c++ -o test_memory_C++ -std=c++11 main.cpp
c++ -o test_memory_C++ -std=c++11 -I. memory.cpp memory_Context.cpp main.cpp

View File

@@ -1,8 +1,12 @@
#include "memory_Context.h"
#include <iostream>
#include <string>
#include <vector>
extern memory_Context memory_createContext();
int main() {
memory_createContext();
std::cout <<
"check" //memory_test_generateConstPlayfield()
<< std::endl

View File

@@ -1,6 +1,7 @@
#include <map>
#include <string>
#include "entities.h"
#include <vector>
#include "memory_Context.h"
////////////////
// Client initiated input
@@ -8,7 +9,7 @@
// Generate constant playfield
memory_Context memory_generateConstPlayfield(
memory_Context& c
memory_Context c
) {
std::map<int, int> idGroups = { };
auto id = 0;
@@ -24,14 +25,14 @@ memory_Context memory_generateConstPlayfield(
}
// Select item
memory_Context memory_selectItem(
memory_Context& c
memory_Context c
) {
if (
c.selectedItems.size() == 2
) {
c.selectedItems = [];
}
c.selectedItems.append(c.selectedId);
c.recentField = "selectedItems";
return c;
@@ -47,18 +48,16 @@ memory_Context memory_selectItem(
// 0. Two items has just been selected
// 1. The same item has been selected twice
// 1. Selected items are of different groups
memory_Context memory_shouldDeselectMismatchedItems(
memory_Context& c
memory_Context c
) {
if not (;
c.recentField == "selectedItems" &&;
c.selectedItems.size() == 2;
if !(
c.recentField == "selectedItems" &&
c.selectedItems.size() == 2
) {
c.recentField = None;
c.recentField = "none";
return c;
}
if (
c.selectedItems[0] == c.selectedItems[1]
) {
@@ -66,6 +65,7 @@ memory_Context memory_shouldDeselectMismatchedItems(
c.mismatchedItems.append(c.selectedItems[0]);
c.recentField = "mismatchedItems";
return c;
}
if (
c.playfieldItems[c.selectedItems[0]] != c.playfieldItems[c.selectedItems[1]]
) {
@@ -74,7 +74,8 @@ memory_Context memory_shouldDeselectMismatchedItems(
c.mismatchedItems.append(c.selectedItems[1]);
c.recentField = "mismatchedItems";
return c;
c.recentField = None;
}
c.recentField = "none";
return c;
}
@@ -82,9 +83,8 @@ memory_Context memory_shouldDeselectMismatchedItems(
//
// Conditions:
// 1. Matching items have just been hidden and all items are hidden now
memory_Context memory_shouldDetectVictory(
memory_Context& c
memory_Context c
) {
if (
c.recentField == "hiddenItems" &&
@@ -93,7 +93,8 @@ memory_Context memory_shouldDetectVictory(
c.victory = True;
c.recentField = "victory";
return c;
c.recentField = None;
}
c.recentField = "none";
return c;
}
@@ -101,9 +102,8 @@ memory_Context memory_shouldDetectVictory(
//
// Conditions:
// 1. Two items are selected and they are of the same group
memory_Context memory_shouldHideMatchingItems(
memory_Context& c
memory_Context c
) {
if (
c.recentField == "selectedItems" &&
@@ -114,7 +114,8 @@ memory_Context memory_shouldHideMatchingItems(
c.hiddenItems.append(c.selectedItems[1]);
c.recentField = "hiddenItems";
return c;
c.recentField = None;
}
c.recentField = "none";
return c;
}

View File

@@ -32,7 +32,7 @@ def memory_selectItem(
len(c.selectedItems) == 2
):
c.selectedItems = []
#}
c.selectedItems.append(c.selectedId)
c.recentField = "selectedItems"
return c
@@ -56,9 +56,9 @@ def memory_shouldDeselectMismatchedItems(
c.recentField == "selectedItems" and
len(c.selectedItems) == 2
):
c.recentField = None
c.recentField = "none"
return c
#}
if (
c.selectedItems[0] == c.selectedItems[1]
):
@@ -66,7 +66,7 @@ def memory_shouldDeselectMismatchedItems(
c.mismatchedItems.append(c.selectedItems[0])
c.recentField = "mismatchedItems"
return c
#}
if (
c.playfieldItems[c.selectedItems[0]] != c.playfieldItems[c.selectedItems[1]]
):
@@ -75,8 +75,8 @@ def memory_shouldDeselectMismatchedItems(
c.mismatchedItems.append(c.selectedItems[1])
c.recentField = "mismatchedItems"
return c
c.recentField = None
#}
c.recentField = "none"
return c
#}
@@ -95,8 +95,8 @@ def memory_shouldDetectVictory(
c.victory = True
c.recentField = "victory"
return c
c.recentField = None
#}
c.recentField = "none"
return c
#}
@@ -117,7 +117,7 @@ def memory_shouldHideMatchingItems(
c.hiddenItems.append(c.selectedItems[1])
c.recentField = "hiddenItems"
return c
c.recentField = None
#}
c.recentField = "none"
return c
#}

View File

@@ -1,7 +1,5 @@
#include <map>
#include <string>
#include "entities.h"
#include "memory_Context.h"
memory_Context memory_createContext() {
return memory_Context();
}

19
v3/memory_Context.h Normal file
View File

@@ -0,0 +1,19 @@
#include <map>
#include <string>
#include <vector>
#ifndef memory_Context_HEADER
#define memory_Context_HEADER
struct memory_Context {
std::vector<int> hiddenItems;
std::vector<int> mismatchedItems;
std::map<int, int> playfieldItems;
int playfieldSize = 0;
std::string recentField = "none";
int selectedId = -1;
std::vector<int> selectedItems;
bool victory = false;
};
#endif // memory_Context_HEADER

View File

@@ -4,8 +4,8 @@ class memory_Context:
self.mismatchedItems = []
self.playfieldItems = {}
self.playfieldSize = 0
self.recentField = None
self.selectedId = None
self.recentField = "none"
self.selectedId = -1
self.selectedItems = []
self.victory = False

View File

@@ -1,6 +1,7 @@
#include <map>
#include <string>
#include "entities.h"
#include <vector>
#include "memory_Context.h"
std::string memory_test_generateConstPlayfield(
) {

View File

@@ -3,7 +3,8 @@ from Function import *
def includes():
return """#include <map>
#include <string>
#include "entities.h"
#include <vector>
#include "memory_Context.h"
"""
def replaceAnd(s):
@@ -91,6 +92,11 @@ def translateStatement(s, state):
state.isIf = True
return s
# Keep "if not ("
if ss == "if not (":
state.isIf = True
return f"{indentation}if !("
# ): -> }
if ss == "):":
state.isIf = False
@@ -129,8 +135,8 @@ class CPP:
for i in range(0, len(self.fn.parameters)):
p = translateParameter(self.fn.parameters[i])
# Make Context passed by reference.
if "Context" in p:
p = p.replace("Context", "Context&")
#if "Context" in p:
# p = p.replace("Context", "Context&")
params.append(p)
strparams = "\n".join(params)
if (len(strparams) > 0):