Home / utk / cs140 / sp20 / lab9 / ss_solver.cpp
Directory Listing
ss_solver.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * INSTRUCTOR-GIVEN TEMPLATE
 * COSC 140: Lab 9B - Shape Shifter!
 *
 * Description:
 *     A simple introduction to recursion. Brute force through all possible
 *     combinations of a puzzle until a valid solution is found. If none are
 *     discovered, print nothing. If a solution does exist, print out the
 *     pieces and the positions where they would solve the puzzle.
 *
 * Author:
 *     Your Name
 */

//C++
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <map>
#include <algorithm>
#include <sstream>

//C
#include <stdio.h>
#include <stdlib.h>

using namespace std;

// ----------------------------------------------------------------------------
// Class/Struct Function Prototypes                                        {{{1
// ----------------------------------------------------------------------------

/*
 * Class for ShapeShifter. Has a grid of the initial setup that you will try to
 * put pieces into. Also has a struct called "shape" that stores a shape's
 * grid, width, and height.
 */

typedef unsigned int uint;

class ShapeShifter {
	struct shape {
		//Constructors
		shape();
		shape(const string&);
		
		//Variables
		vector< vector<bool> > grid;
		uint width, height;
		string text;
	};
		
	public:
		//Constructors
		ShapeShifter();
		ShapeShifter(const vector<string>&);
		
		//Dealing with shapes
		void add_shape(const string&);
		bool apply_shape(uint, uint, uint);
		
		//Solving functions
		bool all_one();
		bool find_solution(uint = 0);
		void print_solution();
		
	private:
		//All shapes to be inserted into the grid
		vector<shape> shapes;
		
		//Game Grid
		vector< vector<bool> > grid;
		
		//(x, y) points for each shape when the solution is found.
		deque< pair<uint, uint> > solution;
		
		//Game grid size
		uint width, height;
};

// ----------------------------------------------------------------------------
// Class/Struct Function Declarations                                      {{{1
// ----------------------------------------------------------------------------

/* Shape Sub-Struct */

/*
 * ShapeShifter::shape constructor (Default)                               {{{2
 *
 * Leave this blank.
 */

ShapeShifter::shape::shape() {
	/* Leave Empty */
}

/*
 * ShapeShifter::shape constructor (construct via string)                  {{{2
 *
 * Takes a string via "line" and makes a grid out of it.
 */

ShapeShifter::shape::shape(const string& line) {
	/*
	 * - Split the string by space (probably via istringstream).
	 *   Put each row into a temporary vector<string>.
	 *
	 * - Go through each char in each row and set "grid" up.
	 *
	 * - Set width and height
	 *
	 * - Store the original string ("line") in "text".
	 */
}

/* ShapeShifter Class */

/*
 * ShapeShifter constructor (Default)                                      {{{2
 *
 * Leave this blank.
 */

ShapeShifter::ShapeShifter() {
	/* Leave Empty */
}

/*
 * ShapeShifter constructor (construct via vector of strings)              {{{2
 *
 * Takes strings read into a vector "rows" and defines the initial grid of the
 * ShapeShifter object with it (stored as a 2D vector of bools in "grid").
 */

ShapeShifter::ShapeShifter(const vector<string>& rows) {

}

/*
 * add_shape                                                               {{{2
 *
 * Creates a single shape based on a string in "line" and puts it into the
 * "shapes" vector.
 */

void ShapeShifter::add_shape(const string& line) {

}

/*
 * apply_shape                                                             {{{2
 *
 * Inserts a shape into the game grid. The shape is in "shapes" at index
 * "shape_id" and it is inserted at the top-left corner, but shifted by
 * "x" and "y". If the shape shifted by "x" and "y" goes out of the game board,
 * make sure nothing happens. This function may be used twice to "undo" a shape
 * apply.
 *
 * (ex. Assume the given grid "100 101 000" and shape "111".
 *
 *    apply_shape at (x: 0, y: 0)      apply_shape at (x: 0, y: 1)
 *      100   111   011                  100         100
 *      101 +     = 101                  101 + 111 = 010
 *      000         000                  000         000
 */

bool ShapeShifter::apply_shape(uint shape_id, uint x, uint y) {
	/*
	 * - Check if the shape can fit in the grid when shifted by "x" and "y".
	 * - Invert all parts of the grid where the shape would be red at.
	 */

	return false;
}

/*
 * all_one                                                                 {{{2
 *
 * Checks if all cells in the grid are "1". Returns TRUE if so. Otherwise, it
 * returns FALSE.
 */

bool ShapeShifter::all_one() {
	return false;
}

/*
 * find_solution                                                           {{{2
 *
 * Recursively traverse and try to find a solution. If called with no argument,
 * it'll start at the very first shape in the "shapes" vector and go through
 * each of them trying to apply in every possible position until a solution is
 * found. Returns TRUE upon a solution being found, and FALSE otherwise.
 */

bool ShapeShifter::find_solution(uint shape_id) {
	/*
	 * The recursive function.
	 *
	 * - With "shape_id", go through every possible position and try to insert
	 *   shape[shape_id] in.
	 *
	 * - If inserted and it isn't the last shape, call:
	 *     find_solution(shape_id + 1)
	 *
	 * - If inserted and it is the last shape, call "all_one". If true then
	 *   return true. Have the other calls to the function also handle this
	 *   return.
	 *
	 * - If you looped through all possible positions and it didn't work, undo
	 *   all changes done to the class involving this shape and return false.
	 */

	return false;
}

/*
 * print_solution                                                          {{{2
 *
 * Goes through the "solution" deque and prints out the coordinates of each
 * shape, along with the original string used to make up the shape.
 */

void ShapeShifter::print_solution() {

}

// ----------------------------------------------------------------------------
// Main Function                                                           {{{1
// ----------------------------------------------------------------------------

int main(int argc, char** argv) {
	/*
	 * - Create a vector of strings holding all argv after argv[0]
	 *
	 * - Create your ShapeShifter object.
	 *
	 * - Create all shapes.
	 *
	 * - Find a solution.
	 *
	 * - Print the result.
	 */

	return 0;
}