@@ -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 |
@@ -1,8 +1,12 @@ | |||||
#include "memory_Context.h" | |||||
#include <iostream> | #include <iostream> | ||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
extern memory_Context memory_createContext(); | |||||
int main() { | int main() { | ||||
memory_createContext(); | |||||
std::cout << | std::cout << | ||||
"check" //memory_test_generateConstPlayfield() | "check" //memory_test_generateConstPlayfield() | ||||
<< std::endl | << std::endl | ||||
@@ -1,6 +1,7 @@ | |||||
#include <map> | #include <map> | ||||
#include <string> | #include <string> | ||||
#include "entities.h" | |||||
#include <vector> | |||||
#include "memory_Context.h" | |||||
//////////////// | //////////////// | ||||
// Client initiated input | // Client initiated input | ||||
@@ -8,7 +9,7 @@ | |||||
// Generate constant playfield | // Generate constant playfield | ||||
memory_Context memory_generateConstPlayfield( | memory_Context memory_generateConstPlayfield( | ||||
memory_Context& c | |||||
memory_Context c | |||||
) { | ) { | ||||
std::map<int, int> idGroups = { }; | std::map<int, int> idGroups = { }; | ||||
auto id = 0; | auto id = 0; | ||||
@@ -24,14 +25,14 @@ memory_Context memory_generateConstPlayfield( | |||||
} | } | ||||
// Select item | // Select item | ||||
memory_Context memory_selectItem( | memory_Context memory_selectItem( | ||||
memory_Context& c | |||||
memory_Context c | |||||
) { | ) { | ||||
if ( | if ( | ||||
c.selectedItems.size() == 2 | c.selectedItems.size() == 2 | ||||
) { | ) { | ||||
c.selectedItems = []; | c.selectedItems = []; | ||||
} | |||||
c.selectedItems.append(c.selectedId); | c.selectedItems.append(c.selectedId); | ||||
c.recentField = "selectedItems"; | c.recentField = "selectedItems"; | ||||
return c; | return c; | ||||
@@ -47,18 +48,16 @@ memory_Context memory_selectItem( | |||||
// 0. Two items has just been selected | // 0. Two items has just been selected | ||||
// 1. The same item has been selected twice | // 1. The same item has been selected twice | ||||
// 1. Selected items are of different groups | // 1. Selected items are of different groups | ||||
memory_Context memory_shouldDeselectMismatchedItems( | 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; | return c; | ||||
} | |||||
if ( | if ( | ||||
c.selectedItems[0] == c.selectedItems[1] | c.selectedItems[0] == c.selectedItems[1] | ||||
) { | ) { | ||||
@@ -66,6 +65,7 @@ memory_Context memory_shouldDeselectMismatchedItems( | |||||
c.mismatchedItems.append(c.selectedItems[0]); | c.mismatchedItems.append(c.selectedItems[0]); | ||||
c.recentField = "mismatchedItems"; | c.recentField = "mismatchedItems"; | ||||
return c; | return c; | ||||
} | |||||
if ( | if ( | ||||
c.playfieldItems[c.selectedItems[0]] != c.playfieldItems[c.selectedItems[1]] | 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.mismatchedItems.append(c.selectedItems[1]); | ||||
c.recentField = "mismatchedItems"; | c.recentField = "mismatchedItems"; | ||||
return c; | return c; | ||||
c.recentField = None; | |||||
} | |||||
c.recentField = "none"; | |||||
return c; | return c; | ||||
} | } | ||||
@@ -82,9 +83,8 @@ memory_Context memory_shouldDeselectMismatchedItems( | |||||
// | // | ||||
// Conditions: | // Conditions: | ||||
// 1. Matching items have just been hidden and all items are hidden now | // 1. Matching items have just been hidden and all items are hidden now | ||||
memory_Context memory_shouldDetectVictory( | memory_Context memory_shouldDetectVictory( | ||||
memory_Context& c | |||||
memory_Context c | |||||
) { | ) { | ||||
if ( | if ( | ||||
c.recentField == "hiddenItems" && | c.recentField == "hiddenItems" && | ||||
@@ -93,7 +93,8 @@ memory_Context memory_shouldDetectVictory( | |||||
c.victory = True; | c.victory = True; | ||||
c.recentField = "victory"; | c.recentField = "victory"; | ||||
return c; | return c; | ||||
c.recentField = None; | |||||
} | |||||
c.recentField = "none"; | |||||
return c; | return c; | ||||
} | } | ||||
@@ -101,9 +102,8 @@ memory_Context memory_shouldDetectVictory( | |||||
// | // | ||||
// Conditions: | // Conditions: | ||||
// 1. Two items are selected and they are of the same group | // 1. Two items are selected and they are of the same group | ||||
memory_Context memory_shouldHideMatchingItems( | memory_Context memory_shouldHideMatchingItems( | ||||
memory_Context& c | |||||
memory_Context c | |||||
) { | ) { | ||||
if ( | if ( | ||||
c.recentField == "selectedItems" && | c.recentField == "selectedItems" && | ||||
@@ -114,7 +114,8 @@ memory_Context memory_shouldHideMatchingItems( | |||||
c.hiddenItems.append(c.selectedItems[1]); | c.hiddenItems.append(c.selectedItems[1]); | ||||
c.recentField = "hiddenItems"; | c.recentField = "hiddenItems"; | ||||
return c; | return c; | ||||
c.recentField = None; | |||||
} | |||||
c.recentField = "none"; | |||||
return c; | return c; | ||||
} | } | ||||
@@ -32,7 +32,7 @@ def memory_selectItem( | |||||
len(c.selectedItems) == 2 | len(c.selectedItems) == 2 | ||||
): | ): | ||||
c.selectedItems = [] | c.selectedItems = [] | ||||
#} | |||||
c.selectedItems.append(c.selectedId) | c.selectedItems.append(c.selectedId) | ||||
c.recentField = "selectedItems" | c.recentField = "selectedItems" | ||||
return c | return c | ||||
@@ -56,9 +56,9 @@ def memory_shouldDeselectMismatchedItems( | |||||
c.recentField == "selectedItems" and | c.recentField == "selectedItems" and | ||||
len(c.selectedItems) == 2 | len(c.selectedItems) == 2 | ||||
): | ): | ||||
c.recentField = None | |||||
c.recentField = "none" | |||||
return c | return c | ||||
#} | |||||
if ( | if ( | ||||
c.selectedItems[0] == c.selectedItems[1] | c.selectedItems[0] == c.selectedItems[1] | ||||
): | ): | ||||
@@ -66,7 +66,7 @@ def memory_shouldDeselectMismatchedItems( | |||||
c.mismatchedItems.append(c.selectedItems[0]) | c.mismatchedItems.append(c.selectedItems[0]) | ||||
c.recentField = "mismatchedItems" | c.recentField = "mismatchedItems" | ||||
return c | return c | ||||
#} | |||||
if ( | if ( | ||||
c.playfieldItems[c.selectedItems[0]] != c.playfieldItems[c.selectedItems[1]] | c.playfieldItems[c.selectedItems[0]] != c.playfieldItems[c.selectedItems[1]] | ||||
): | ): | ||||
@@ -75,8 +75,8 @@ def memory_shouldDeselectMismatchedItems( | |||||
c.mismatchedItems.append(c.selectedItems[1]) | c.mismatchedItems.append(c.selectedItems[1]) | ||||
c.recentField = "mismatchedItems" | c.recentField = "mismatchedItems" | ||||
return c | return c | ||||
c.recentField = None | |||||
#} | |||||
c.recentField = "none" | |||||
return c | return c | ||||
#} | #} | ||||
@@ -95,8 +95,8 @@ def memory_shouldDetectVictory( | |||||
c.victory = True | c.victory = True | ||||
c.recentField = "victory" | c.recentField = "victory" | ||||
return c | return c | ||||
c.recentField = None | |||||
#} | |||||
c.recentField = "none" | |||||
return c | return c | ||||
#} | #} | ||||
@@ -117,7 +117,7 @@ def memory_shouldHideMatchingItems( | |||||
c.hiddenItems.append(c.selectedItems[1]) | c.hiddenItems.append(c.selectedItems[1]) | ||||
c.recentField = "hiddenItems" | c.recentField = "hiddenItems" | ||||
return c | return c | ||||
c.recentField = None | |||||
#} | |||||
c.recentField = "none" | |||||
return c | return c | ||||
#} | #} |
@@ -1,7 +1,5 @@ | |||||
#include <map> | |||||
#include <string> | |||||
#include "entities.h" | |||||
#include "memory_Context.h" | |||||
memory_Context memory_createContext() { | |||||
return memory_Context(); | |||||
} |
@@ -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 |
@@ -4,8 +4,8 @@ class memory_Context: | |||||
self.mismatchedItems = [] | self.mismatchedItems = [] | ||||
self.playfieldItems = {} | self.playfieldItems = {} | ||||
self.playfieldSize = 0 | self.playfieldSize = 0 | ||||
self.recentField = None | |||||
self.selectedId = None | |||||
self.recentField = "none" | |||||
self.selectedId = -1 | |||||
self.selectedItems = [] | self.selectedItems = [] | ||||
self.victory = False | self.victory = False | ||||
@@ -1,6 +1,7 @@ | |||||
#include <map> | #include <map> | ||||
#include <string> | #include <string> | ||||
#include "entities.h" | |||||
#include <vector> | |||||
#include "memory_Context.h" | |||||
std::string memory_test_generateConstPlayfield( | std::string memory_test_generateConstPlayfield( | ||||
) { | ) { | ||||
@@ -3,7 +3,8 @@ from Function import * | |||||
def includes(): | def includes(): | ||||
return """#include <map> | return """#include <map> | ||||
#include <string> | #include <string> | ||||
#include "entities.h" | |||||
#include <vector> | |||||
#include "memory_Context.h" | |||||
""" | """ | ||||
def replaceAnd(s): | def replaceAnd(s): | ||||
@@ -91,6 +92,11 @@ def translateStatement(s, state): | |||||
state.isIf = True | state.isIf = True | ||||
return s | return s | ||||
# Keep "if not (" | |||||
if ss == "if not (": | |||||
state.isIf = True | |||||
return f"{indentation}if !(" | |||||
# ): -> } | # ): -> } | ||||
if ss == "):": | if ss == "):": | ||||
state.isIf = False | state.isIf = False | ||||
@@ -129,8 +135,8 @@ class CPP: | |||||
for i in range(0, len(self.fn.parameters)): | for i in range(0, len(self.fn.parameters)): | ||||
p = translateParameter(self.fn.parameters[i]) | p = translateParameter(self.fn.parameters[i]) | ||||
# Make Context passed by reference. | # 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) | params.append(p) | ||||
strparams = "\n".join(params) | strparams = "\n".join(params) | ||||
if (len(strparams) > 0): | if (len(strparams) > 0): | ||||