001: /*
002: 
003:   File: main.cpp
004: 
005:   (c) 2003 by Oliver Ehli
006: 
007:      This program is free software; you can redistribute it and/or modify
008:      it under the terms of the GNU General Public License as published by
009:      the Free Software Foundation; either version 2 of the License, or
010:      (at your option) any later version.
011: 
012:      This program is distributed in the hope that it will be useful,
013:      but WITHOUT ANY WARRANTY; without even the implied warranty of
014:      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015:      GNU General Public License for more details.
016: 
017:      You should have received a copy of the GNU General Public License
018:      along with this program; if not, write to the Free Software
019:      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: 
021:      See File LICENSE for more Information.
022: 
023: */
024: 
025: #include <stdio.h>
026: #include <fstream.h>
027: 
028: #include "Puzzle.h"
029: #include "Piece.h"
030: #include "Solver.h"
031: #include "InventorWriter.h"
032: 
033: 
034: 
035: #ifdef DEBUG_BIN
036: 
037: void dumpPieces(Piece *pieces[])
038: {
039:   ofstream iv_out;
040:   iv_out.open("pieces.iv");
041: 
042:   iv_out << InventorWriter::header;
043: 
044:   for(int i=0; i<NUM_PCS; i++) {
045:     iv_out << InventorWriter::indent;
046:     iv_out << InventorWriter::beginGroup;
047: 
048:     iv_out << InventorWriter::indent;
049:     iv_out << InventorWriter::indent;
050: 
051:     InventorWriter::writeTranslation(iv_out, 0.0,
052:                                      i%3 * 5 * InventorWriter::baseUnit,
053:                                      i/3 * 5 * InventorWriter::baseUnit);
054: 
055: //     (pieces[i])->writeDescriptionAsIV(iv_out, 2, i);
056:     (pieces[i])->writePieceAsIV(iv_out, 2, i);
057:     iv_out << InventorWriter::indent;
058:     iv_out << InventorWriter::endGroup;
059:   }
060: 
061:   iv_out << InventorWriter::footer;
062:   iv_out.close();
063: 
064: }
065: 
066: 
067: void dumpRotatedPieces(Piece *pieces[])
068: {
069:   ofstream iv_out;
070:   char filename[128];
071: 
072:   for(int i=0; i<NUM_PCS; i++) {
073:     sprintf(filename, "rotatedPieces-%02d.iv", i);
074:     iv_out.open(filename);
075: 
076:     iv_out << InventorWriter::header;
077: 
078:     (pieces[i])->writeRotatedPiecesAsIV(iv_out, 1, i);
079: 
080:     iv_out << InventorWriter::footer;
081:     iv_out.close();
082:   }
083: 
084: }
085: 
086: void dumpPiecePositions(Piece *pieces[])
087: {
088:   ofstream iv_out;
089:   char filename[128];
090: 
091: //   for(int i=0; i<NUM_PCS; i++) {
092:    int i=0;{
093:     sprintf(filename, "piecePositions-%02d.iv", i);
094:     iv_out.open(filename);
095: 
096:     iv_out << InventorWriter::header;
097: 
098:     (pieces[i])->writePiecePositionsAsIV(iv_out, 1, i);
099: 
100:     iv_out << InventorWriter::footer;
101:     iv_out.close();
102:   }
103: 
104: }
105: 
106: #endif
107: 
108: 
109: int main(int argc, char **argv)
110: {
111:   Piece *pieces[NUM_PCS+1];
112: 
113:   pieces[0] = new Piece(0, Puzzle::getPieces()[0].bbox,
114:                         Puzzle::getPieces()[0].positions, true);
115:   for(int i=1; i<NUM_PCS; i++)
116:     pieces[i] = new Piece(i, Puzzle::getPieces()[i].bbox,
117:                           Puzzle::getPieces()[i].positions);
118: 
119: //   pieces[NUM_PCS-1] = new Piece(NUM_PCS-1, Puzzle::getPieces()[NUM_PCS-1].bbox,
120: //                               Puzzle::getPieces()[NUM_PCS-1].positions, false, true);
121: 
122:   pieces[NUM_PCS] = new Piece(NUM_PCS-1, Puzzle::getPieces()[NUM_PCS-1].bbox,
123:                               Puzzle::getPieces()[NUM_PCS-1].positions, false, true);
124: 
125: #ifdef DEBUG_BIN
126:   dumpPieces(pieces);
127:   dumpRotatedPieces(pieces);
128:   dumpPiecePositions(pieces);
129: #endif
130: 
131:   if(argc>1 && *argv[1] == 'c') {
132:     pieces[0]->dumpPositionInfo();
133: 
134:     cout << "int positionIndex[NUM_PCS+1][NUM_LO_BITS] = {" << endl;
135:     for(int i=0; i<NUM_PCS+1; i++)
136:       pieces[i]->dumpOuccupanceIndexInfo();
137:     cout << "};" << endl;
138: 
139:     cout << "long long positions[NUM_PCS+1][NUM_LO_BITS][24] = {" << endl;
140:     for(int i=0; i<NUM_PCS+1; i++)
141:       pieces[i]->dumpOuccupanceInfo();
142:     cout << "};" << endl;
143: 
144: 
145:     cout << "int positionIndex32[NUM_PCS+1][NUM_HI_BITS] = {" << endl;
146:     for(int i=0; i<NUM_PCS+1; i++)
147:       pieces[i]->dumpOuccupanceIndexInfo(true);
148:     cout << "};" << endl;
149: 
150:     cout << "int positions32[NUM_PCS+1][NUM_HI_BITS][24] = {" << endl;
151:     for(int i=0; i<NUM_PCS+1; i++)
152:       pieces[i]->dumpOuccupanceInfo(true);
153:     cout << "};" << endl;
154: 
155: 
156: 
157: 
158:     cout << "int altPositionIndex[NUM_PCS+1][NUM_LO_BITS] = {" << endl;
159:     for(int i=0; i<NUM_PCS+1; i++)
160:       pieces[i]->dumpOuccupanceIndexInfo();
161:     cout << "};" << endl;
162: 
163:     cout << "long long altPositions[NUM_PCS+1][NUM_LO_BITS][24] = {" << endl;
164:     for(int i=0; i<NUM_PCS+1; i++)
165:       pieces[i]->dumpOuccupanceInfo();
166:     cout << "};" << endl;
167: 
168: 
169:     cout << "int altPositionIndex32[NUM_PCS+1][NUM_HI_BITS] = {" << endl;
170:     for(int i=0; i<NUM_PCS+1; i++)
171:       pieces[i]->dumpOuccupanceIndexInfo(true);
172:     cout << "};" << endl;
173: 
174:     cout << "int altPositions32[NUM_PCS+1][NUM_HI_BITS][24] = {" << endl;
175:     for(int i=0; i<NUM_PCS+1; i++)
176:       pieces[i]->dumpOuccupanceInfo(true);
177:     cout << "};" << endl;
178: 
179: 
180:     return 0;
181:   }
182: 
183:   for(int i=0; i<NUM_PCS; i++)
184:     cout << "piece " << i << ": "
185:          << pieces[i]->getNumPositions() << " possible Positions" << endl;
186: 
187:   Solver *solver = new Solver(pieces);
188: 
189:   solver->solve();
190: 
191: }