-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitFormSolvePuzzle.cpp
207 lines (197 loc) · 6.25 KB
/
UnitFormSolvePuzzle.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
//---------------------------------------------------------------------------
/*
The Refrigerator Puzzle Solver, program to solve a refrigerator puzzle
Copyright (C) 2008 Richel Bilderbeek
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <cmath>
#include <cassert>
#include <vector>
#include <string>
#include <algorithm>
#include <boost/scoped_ptr.hpp>
#include "UnitPuzzlePiece.h"
#include "UnitFormSolvePuzzle.h"
#include "UnitFormAbout.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormSolvePuzzle *FormSolvePuzzle;
//---------------------------------------------------------------------------
__fastcall TFormSolvePuzzle::TFormSolvePuzzle(TComponent* Owner)
: TForm(Owner), mQuit(false)
{
//Tests
assert(IntPower(2,2)== 4);
assert(IntPower(2,3)== 8);
assert(IntPower(2,4)==16);
assert(IntPower(3,2)== 9);
assert(IntPower(3,3)==27);
assert(IntPower(3,4)==81);
//More tests
PuzzlePiece p0(
greenBeetleHead,
greenBeetleTail,
brownBeetleHead,
brownBeetleTail);
PuzzlePiece p1 = p0;
assert(p0 == p1);
p1.Rotate();
assert(p0 != p1);
PuzzlePiece p2 = p1;
assert(p1 == p2);
p2.Rotate();
assert(p1 != p2);
PuzzlePiece p3 = p2;
assert(p2 == p3);
p3.Rotate();
assert(p2 != p3);
PuzzlePiece p4 = p3;
assert(p3 == p4);
p4.Rotate();
assert(p3 != p4);
assert(p0 == p4);
}
//---------------------------------------------------------------------------
const int IntPower(const int base, const int exponent)
{
assert(exponent >= 0);
int result = 1;
for (int i=0; i!=exponent; ++i)
{
result *= base;
}
return result;
}
//---------------------------------------------------------------------------
const int Factorial(const int n)
{
assert(n>=0);
int result = 1;
for (int i=1; i<=n; ++i)
{
result*=i;
}
return result;
}
//---------------------------------------------------------------------------
void __fastcall TFormSolvePuzzle::ButtonQuitClick(TObject *Sender)
{
mQuit = true;
Close();
}
//---------------------------------------------------------------------------
void TFormSolvePuzzle::ShowSolution(const std::vector<PuzzlePiece>& v)
{
const int n = static_cast<int>(v.size());
for (int i=0; i!=n; ++i)
{
const int x = (i % 3) * 3;
const int y = (i / 3) * 3;
StringGrid->Cells[x+1][y ] = ToStr(v[i].mTop).c_str();
StringGrid->Cells[x+2][y+1] = ToStr(v[i].mRight).c_str();
StringGrid->Cells[x+1][y+2] = ToStr(v[i].mBottom).c_str();
StringGrid->Cells[x ][y+1] = ToStr(v[i].mLeft).c_str();
}
for (int i=n; i!=9; ++i)
{
const int x = (i % 3) * 3;
const int y = (i / 3) * 3;
StringGrid->Cells[x+1][y ] = "";
StringGrid->Cells[x+2][y+1] = "";
StringGrid->Cells[x+1][y+2] = "";
StringGrid->Cells[x ][y+1] = "";
}
}
//---------------------------------------------------------------------------
void __fastcall TFormSolvePuzzle::ButtonSolveClick(TObject *Sender)
{
const std::vector<PuzzlePiece> piecesOriginal = GetPieces();
std::vector<PuzzlePiece> pieces1d = piecesOriginal;
std::sort(pieces1d.begin(),pieces1d.end());
ProgressBar->Max = IntPower(4,9);
ProgressBar->Hint = ProgressBar->Max;
const int maxI = IntPower(4,9);
for (int i = 1; i!=maxI; ++i)
{
const std::vector<PuzzlePiece> solution = SolvePuzzle(std::vector<PuzzlePiece>(),pieces1d);
if (solution.empty() == false)
{
ShowSolution(solution);
ShowMessage("Solved!");
return;
}
Application->ProcessMessages();
if (mQuit == true) return;
ProgressBar->Position = i;
//Rotate a piece or more pieces
for (int index = 0; index != 9; ++index)
{
pieces1d[index].Rotate();
if (pieces1d[index].mNrotations != 0) break;
}
}
ShowMessage("Not solved...");
}
//---------------------------------------------------------------------------
const std::vector<PuzzlePiece> TFormSolvePuzzle::SolvePuzzle(
const std::vector<PuzzlePiece>& piecesUsed,
const std::vector<PuzzlePiece>& piecesLeft)
{
if (CheckBoxDisplay->Checked == true)
{
ShowSolution(piecesUsed);
Application->ProcessMessages();
this->Refresh();
Sleep(100);
}
if (piecesLeft.empty() == true) return piecesUsed;
assert(piecesUsed.size() + piecesLeft.size() == 9);
const int nUsed = static_cast<int>(piecesUsed.size());
const int nLeft = static_cast<int>(piecesLeft.size());
const int newIndex = nUsed;
for (int i=0; i!=nLeft; ++i)
{
//Check if the newcomer will fit
if (newIndex > 2
&& DoesFit(piecesUsed[newIndex-3].mBottom,piecesLeft[i].mTop)==false)
{
continue;
}
if (newIndex % 3 != 0
&& DoesFit(piecesUsed[newIndex-1].mRight,piecesLeft[i].mLeft)==false)
{
continue;
}
//Newcomer fits!
std::vector<PuzzlePiece> newPiecesUsed = piecesUsed;
std::vector<PuzzlePiece> newPiecesLeft = piecesLeft;
newPiecesUsed.push_back(newPiecesLeft[i]);
std::swap(newPiecesLeft[i],newPiecesLeft.back());
newPiecesLeft.pop_back();
const std::vector<PuzzlePiece> solution = SolvePuzzle(newPiecesUsed,newPiecesLeft);
if (solution.empty() == false) return solution;
}
return std::vector<PuzzlePiece>();
}
//---------------------------------------------------------------------------
void __fastcall TFormSolvePuzzle::ButtonAboutClick(TObject *Sender)
{
boost::scoped_ptr<TFormAbout> f(new TFormAbout(0));
f->ShowModal();
}
//---------------------------------------------------------------------------