Binary Puzzle Solver

I decided to make a binary puzzle solver, it will solve a puzzle with binary input by checking every possible input combination. I input a start and end for the puzzle then let the program solve it.

In the actual puzzle, you would see a 3X3 puzzle with on and off states for every block, you would click a block and in result you would toggle the block you clicked on plus every block next to it (excluding diagonals).

The code below gets stuck doing the function calculateresult() when I do it.

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
#include "stdafx.h"
#include "windows.h"
#include <iostream>

using namespace std;
//Declaration of Global Variables
bool run = 1;
bool originalbox[3][3];
bool wantedbox[3][3];
bool processingbox[3][3];
bool inputbox[3][3];
bool matrixequal;
int x;
int y;

void inputoriginalbox()
{
	while (x <= 2)
	{
		y = 0;
		while (y <= 2)
		{
			cout << "At " << x << "," << y << endl;
			cin >> originalbox[x][y];
			y++;
		}
		x++;
	}
}

void setprocessingbox()
{
	while (x <= 2)
	{
		y = 0;
		while (y <= 2)
		{
			processingbox[x][y] = originalbox[x][y];
			y++;
		}
		x++;
	}
}

void resetcursor()
{
	x = 0;
	y = 0;
}

void inputwantedbox()
{
	while (x <= 2)
	{
		y = 0;
		while (y <= 2)
		{
			cout << "At " << x << "," << y << endl;
			cin >> wantedbox[x][y];
			y++;
		}
		x++;
	}
}

bool toggle(bool a)
{
	if (a)
		return 0;
	else
		return 1;
}

void hitpoint(int funcx,int funcy)
{
	//In the actual puzzle, you would see a 3X3 puzzle, you would click a block and in result
	//you would toggle the block you clicked on plus every block next to it (excluding diagonals)
	int x2 = funcx;
	int y2 = funcy;
	toggle(processingbox[funcx][funcy]);
	if (funcx > 0)
	{
		x2 = funcx - 1;
		toggle(processingbox[x2][y2]);
	}
	if (funcx < 2)
	{
		x2 = funcx + 1;
		toggle(processingbox[x2][y2]);
	}
	x2 = funcx;
	if (funcy > 0)
	{
		y2 = funcy - 1;
		toggle(processingbox[x2][y2]);
	}
	if (funcy < 2)
	{
		y2 = funcy + 1;
		toggle(processingbox[x2][y2]);
	}
}

void nextnumber()
{
	x++;
	if (x == 3)
	{
		y++;
		x = 0;
	}
}

void outputarray()
{
	while (x <= 2)
	{
		y = 0;
		while (y <= 2)
		{
			cout << "At " << x << "," << y << endl;
			cout << inputbox[x][y] << endl;
			y++;
		}
		x++;
	}
}

bool checkmatrixequal()
{
	while (x <= 2)
	{
		y = 0;
		while (y <= 2)
		{
			if (processingbox[x][y] != wantedbox[x][y])
				return 0;
			y++;
		}
		x++;
	}
	return 1;
}

void calculateresult()
{
	while(!matrixequal)
	{
		//This part is based on binary counting, it checks every possible solution for the right answer
		//The weird thing is that the y never goes past zero when I debug it.
		inputbox[x][y] = toggle(inputbox[x][y]);
		hitpoint(x,y);
		if (inputbox[x][y] == 0)
			nextnumber();
		else
			resetcursor();
		matrixequal = checkmatrixequal();
	}
}

void main()
{
	cout << "Mini-Game Solver" << endl;
	Sleep(500);
	//This program won't have an end yet.  This solves a basic binary puzzle, the rules can be fount in the hitpoint() function
	while (run)
	{
		cout << "What does the original box look like?" << endl;
		resetcursor();
		inputoriginalbox();
		resetcursor();
		setprocessingbox();
		cout << "What box do you want?" << endl;
		resetcursor();
		inputwantedbox();
		cout << "Processing..." << endl;
		resetcursor();
		//calculateresult is the part that doesn't end
		calculateresult();
		cout << "Done Processing" << endl;
		resetcursor();
		outputarray();
	}
}
First of all, never ever ever put main at the bottom of a program... second, make it "int main()". And last but not least, move your functions to the bottom of your program and leave prototypes at the top.
Why would you say never put main() at the bottom of the program?
You would be much better off if your program didn't use all of those global
variables. Maybe then it would be easier to figure out why y never goes
past zero.

(Does this even work???)
jsmith, because... if his functions use other functions that come afterward, they will not have been declared yet and he will get errors. It's best to use prototypes so you can call a function anywhere.
@packetpirate,
Besides being able to return a value, what are the advantages of using "int main()" over "void main()"?

@jsmith
I was thinking about removing the global values halfway through making this, but then I would just have one long main() function. I can only figure out how to change the variables run and matrixequal in a way which doesn't break the program or change the length of the main() function drastically.
And no, it doesn't work, that's why I'm posting here. I would've said "need help on multidimensional arrays" in the title to make it obvious that I need help but I figured that it might be blocked or rejected. (the admin made it very clear that I wasn't supposed to do that). Then I wouldn't have gotten anything done.
@Arbruteblade:
You don't have to have one long main function if you remove the globals. Read up on how
to pass parameters to functions. I actually wrote your program with no global variables
(one static local to a function though) using two functions and a class, and the entire
program is less than 80 lines of source file (though I skipped the input of the starting
grid and target grid). I'll post it tomorrow.


@packetpirate:
If function A calls B and B calls A (directly or indirectly), then yes, A has to be prototyped
before B and implemented after B (or vice versa). But that has nothing to do with the
location of main() in the source file.
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
#include <bitset>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <set>

template< size_t Width, size_t Height >
struct Grid
{
    enum { W = Width, H = Height };

    Grid()                           { bits.flip(); }
    void set( size_t w, size_t h )   { bits.set( make_index( w, h ) ); }
    void reset( size_t w, size_t h ) { bits.reset( make_index( w, h ) ); }
    void flip( size_t w, size_t h )  { bits.flip( make_index( w, h ) ); }
    bool zero() const                { return bits.none(); }
    uint32_t hash() const            { return bits.to_ulong(); }

  private:
    size_t make_index( size_t w, size_t h ) const { return h * W + w; }

    std::bitset<W*H> bits;
};

typedef Grid< 3, 3 > grid_t;

grid_t flip( grid_t grid, size_t w, size_t h ) {
    grid.flip( w, h );                               // Center
    if( w )                 grid.flip( w - 1, h );   // Left
    if( h )                 grid.flip( w, h - 1 );   // Top
    if( w < grid_t::W - 1 ) grid.flip( w + 1, h );   // Right
    if( h < grid_t::H - 1 ) grid.flip( w, h + 1 );   // Bottom
    return grid;
}

void solve( std::deque< grid_t > grid, size_t depth = 0 ) {
    static std::set<uint32_t> seen;

    std::deque< grid_t > new_grids;
    for( std::deque<grid_t>::iterator g = grid.begin(); g != grid.end(); ++g ) {
        if( g->zero() ) {
            std::cout << "Solved in " << depth << " moves." << std::endl;
            exit( 0 );
        }

        for( size_t w = 0; w < grid_t::W; ++w )
            for( size_t h = 0; h < grid_t::H; ++h ) {
                grid_t candidate( flip( *g, w, h ) );
                if( seen.find( candidate.hash() ) == seen.end() ) {
                    seen.insert( candidate.hash() );
                    new_grids.push_back( candidate );
                }
            }
    }

    solve( new_grids, depth + 1 );
}

int main() {
    std::cout << "Mini-Game Solver" << std::endl;
    solve( std::deque<grid_t>( 1, grid_t() ) );
    std::cout << "No solution found." << std::endl;
}


This isn't the most robust of code, but it will find the optimal solution (solution in the fewest number of moves).
Here's my final version:
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
// Mini-solver.cpp : main project file.

#include "stdafx.h"
#include "windows.h"
#include <iostream>

using namespace std;
//Declaration of Global Variables
bool originalbox[3][3];
bool wantedbox[3][3];
bool processingbox[3][3];
bool inputbox[3][3];
bool matrixequal;
int x;
int y;

void inputoriginalbox()
{
	while (x <= 2)
	{
		y = 0;
		while (y <= 2)
		{
			cout << "At " << x << "," << y << endl;
			cin >> originalbox[x][y];
			y++;
		}
		x++;
	}
}

void setprocessingbox()
{
	while (x <= 2)
	{
		y = 0;
		while (y <= 2)
		{
			processingbox[x][y] = originalbox[x][y];
			y++;
		}
		x++;
	}
}

void resetcursor()
{
	x = 0;
	y = 0;
}

void inputwantedbox()
{
	while (x <= 2)
	{
		y = 0;
		while (y <= 2)
		{
			cout << "At " << x << "," << y << endl;
			cin >> wantedbox[x][y];
			y++;
		}
		x++;
	}
}

bool toggle(bool a)
{
	if (a)
		return 0;
	else
		return 1;
}

void hitpoint(int funcx,int funcy)
{
	//In the actual puzzle, you would see a 3X3 puzzle, you would click a block and in result
	//you would toggle the block you clicked on plus every block next to it (excluding diagonals)
	int x2 = funcx;
	int y2 = funcy;
	processingbox[funcx][funcy] = toggle(processingbox[funcx][funcy]);
	if (funcx > 0)
	{
		x2 = funcx - 1;
		processingbox[x2][y2] = toggle(processingbox[x2][y2]);
	}
	if (funcx < 2)
	{
		x2 = funcx + 1;
		processingbox[x2][y2] = toggle(processingbox[x2][y2]);
	}
	x2 = funcx;
	if (funcy > 0)
	{
		y2 = funcy - 1;
		processingbox[x2][y2] = toggle(processingbox[x2][y2]);
	}
	if (funcy < 2)
	{
		y2 = funcy + 1;
		processingbox[x2][y2] = toggle(processingbox[x2][y2]);
	}
}

void nextnumber()
{
	x++;
	if (x == 3)
	{
		y++;
		x = 0;
	}
}

void outputarray()
{
	while (x <= 2)
	{
		y = 0;
		cout << endl;
		while (y <= 2)
		{
			cout << inputbox[x][y];
			y++;
		}
		x++;
	}
}

bool checkmatrixequal()
{
	int y2 = 0;
	int x2 = 0;
	while (x2 <= 2)
	{
		y2 = 0;
		while (y2 <= 2)
		{
			if (processingbox[x2][y2] != wantedbox[x2][y2])
				return 0;
			y2++;
		}
		x2++;
	}
	return 1;
}

void calculateresult()
{
	matrixequal = checkmatrixequal();
	while(!matrixequal)
	{
		//This part is based on binary counting, it checks every possible solution for the right answer
		//The weird thing is that the y never goes past zero when I debug it.
		inputbox[x][y] = toggle(inputbox[x][y]);
		hitpoint(x,y);
		if (inputbox[x][y] == 0)
			nextnumber();
		else
			resetcursor();
		matrixequal = checkmatrixequal();
	}
}

void reset()
{
	while (x <= 2)
	{
		y = 0;
		while (y <= 2)
		{
			originalbox[x][y] = 0;
			y++;
		}
		x++;
	}
	resetcursor();
	while (x <= 2)
	{
		y = 0;
		while (y <= 2)
		{
			inputbox[x][y] = 0;
			y++;
		}
		x++;
	}
	resetcursor();
	while (x <= 2)
	{
		y = 0;
		while (y <= 2)
		{
			processingbox[x][y] = 0;
			y++;
		}
		x++;
	}
	resetcursor();
	while (x <= 2)
	{
		y = 0;
		while (y <= 2)
		{
			wantedbox[x][y] = 0;
			y++;
		}
		x++;
	}
	resetcursor();
}

void main()
{
	cout << "Mini-Game Solver" << endl;
	Sleep(500);
	bool run = 1;
	//This program won't have an end yet.  This solves a basic binary puzzle, the rules can be fount in the hitpoint() function
	while (run)
	{
		cout << "What does the original box look like?" << endl;
		resetcursor();
		inputoriginalbox();
		resetcursor();
		setprocessingbox();
		cout << "What box do you want?" << endl;
		resetcursor();
		inputwantedbox();
		cout << "Processing..." << endl;
		resetcursor();
		//calculateresult is the part that doesn't end
		calculateresult();
		cout << "Done Processing" << endl;
		resetcursor();
		outputarray();
		cout << endl << "Rerun? (0 or 1)" << endl;
		cin >> run;
		if (run)
			reset();
	}
}

I finally figured it out, I was using y = 0 in my checkmatrixequal() function, the final code has 234 lines of code.

@jsmith nice job on that code, it's much better than mine.
Topic archived. No new replies allowed.