-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameFlow.cpp
More file actions
117 lines (95 loc) · 3.38 KB
/
gameFlow.cpp
File metadata and controls
117 lines (95 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// gameFlow.cpp
// AdvProgEX1
//
// Created by Alexander Shugaley on 01/11/2017.
// Copyright © 2017 Alexander Shugaley. All rights reserved.
//
#include "gameFlow.h"
using namespace std;
gameFlow::gameFlow(int argc, char* argv[]){
bool valid = this->parseCommandLineArgs(argc, argv);
if (valid){ //if the command line args are fine
validCommandParsing = true;
}
}
bool gameFlow::runMainFlow(){
string inputFilename(this->infile);
string outputFilename(this->outfile);
JigsawParser parser = JigsawParser(inputFilename, outputFilename); //parse the game
bool validParsing = parser.isInitialized(); //check if parsing worked
if (!validParsing){ //if not - write the errors
if (!parser.fileError()){
parser.writeErrorsToOutput();
}
return false;
}
vector<PuzzlePiece> pieces = parser.getCorrectInputPieces(); //get the pieces vector from the game
bool rotationAllowed = this->rotate; //check if we are in the rotation mode
if (rotationAllowed){ //start the solving algo in rotation mode
JigsawSolutionExistsRotationsAllowed puzzleCheck = JigsawSolutionExistsRotationsAllowed(pieces); //create new check puzzle
bool checkResult = puzzleCheck.checkIfPuzzleIsLegal(); //check trivical issues
if (!checkResult){
puzzleCheck.writeToFileFailedTests(outputFilename);
return false;
}
JigsawPuzzleRotations puzzle = JigsawPuzzleRotations(pieces); //create new puzzle
bool solved = puzzle.solveGame(); //run the algo for solution
puzzle.printSolutionToFile(outputFilename); //print the solution of 'Can't solve"
return solved;
}
else{ //start the algo in calssic mode
JigsawSolutionExistsChecks puzzleCheck = JigsawSolutionExistsChecks(pieces);
bool checkResult = puzzleCheck.checkIfPuzzleIsLegal();
if (!checkResult){
puzzleCheck.writeToFileFailedTests(outputFilename);
return false;
}
JigsawPuzzle puzzle = JigsawPuzzle(pieces);
bool solved = puzzle.solveGame();
puzzle.printSolutionToFile(outputFilename);
return solved;
}
}
bool gameFlow::rotateCommandExists(int argc, char* argv[]){
string rotateCommandString = "-rotate";
for (int i = 1; i < argc; i++){
if (rotateCommandString.compare(argv[i]) == 0){
return true;
}
}
return false;
}
bool gameFlow::parseCommandLineArgs(int argc, char* argv[]){
if (argc < 3 || argc > 4){
cout << WRONG_ARGS << endl;
return false;
}
if (argc == 4){
bool rotateExists = this->rotateCommandExists(argc, argv);
if (!rotateExists){
cout << WRONG_ARGS << endl;
return false;
}
this->rotate = true;
string rotationFlagString = "-rotate";
if (rotationFlagString.compare(argv[1]) == 0){
this->infile = argv[2];
this->outfile = argv[3];
} else if (rotationFlagString.compare(argv[2]) == 0){
this->infile = argv[1];
this->outfile = argv[3];
} else{
this->infile = argv[1];
this->outfile = argv[2];
}
}
else{
this->infile = argv[1];
this->outfile = argv[2];
}
return true;
}
bool gameFlow::getValidCommandParsing(){
return this->validCommandParsing;
}