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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
/*
This file is part of StroQ, Copyright (C) 2005 Luc Vo Van
StroQ 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 2, or (at your option) any
later version.
StroQ 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 StroQ; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Class : Puzzle
Author : Luc Vo Van
Original Date : 18.05.2005
Description : A Puzzle contains dimensions, Squares, hint positions
and can return a unique code which can be used for
its reconstruction. It is what the player will try to
solve.
*/
#include <qapplication.h>
#include <qpoint.h>
#include <qcanvas.h>
#include <stdlib.h>
#include <math.h>
#include "stroqconst.h"
#include "puzzle.h"
#include "playsquare.h"
#include "square.h"
#define MAX(a,b) (((a)>(b))?(a):(b))
/*
* The code format is as follows:
*
* 1) The first 6 bits are reserved for the bitwidth, which is how many bits
* are needed to represent the largest of the two coordinates
* ie: max(row,column)
* 2) The next bitwidth bits are the number of columns (width) of the puzzle
* 3) The next bitwidth bits are the number of height (height) of the puzzle
* 4) The next 2*bitwidth bits are the row and height of the start of a solution
* to the puzzle (hint 1)
* 5) The next 2*bitwidth bits are the row and height of the end of a solution
* to the puzzle (hint 2). This is of course the end of the solution hinted
* in the previous step
* 6) The next width*height bits are the content of the puzzle, 1 for black
* squares 0 for whites
* 7) The remaining bits are ignored as the puzzle code is right padded with 0s
* until it is a factor of 8.
*
* In brief:
* [bitwidth][width*][height*][row sol1*][col sol1*][row sol2*][col sol2*]
* ... [content]
* parameters noted with a * use 'bitwidth' bits.
*/
Puzzle::Puzzle(QString code)
{
QString ucode = code.upper();
QString bincode = "";
unsigned int offset = 0;
for(unsigned int i = 0; i < ucode.length(); i++)
{
getBin(bincode, ucode.at(i).unicode()-65, 4);
}
// Reads the bitwidth, first 6 bits
unsigned int bitwidth = evalBin(bincode.left(6));
offset += 6;
// Reads the dimensions
m_iWidth = evalBin(bincode.mid(offset, bitwidth));
offset += bitwidth;
m_iHeight = evalBin(bincode.mid(offset, bitwidth));
offset += bitwidth;
allocate();
// Reads the solution hints
m_qpSolutionStart = QPoint(
evalBin(bincode.mid(offset, bitwidth)),
evalBin(bincode.mid(offset + bitwidth, bitwidth)));
offset += 2* bitwidth;
m_qpSolutionEnd = QPoint(
evalBin(bincode.mid(offset, bitwidth)),
evalBin(bincode.mid(offset + bitwidth, bitwidth)));
offset += 2* bitwidth;
/*
printf("puzzlecode: %s bincode: %s bitwidth: %d\n",
code.latin1(), bincode.latin1(), bitwidth);
printSpec();
*/
// Copies the squares or playsquares, depending
for(int row = 1; row < getHeight() + 1; row++)
{
for(int column = 1; column < getWidth() + 1; column++)
{
if(bincode.at(offset) == QChar('1'))
setSquare(new Square(QPoint(column, row), Square::Black));
else
setSquare(new Square(QPoint(column, row), Square::White));
offset++;
}
}
}
Puzzle::Puzzle(int width, int height, QPoint solStart, QPoint solEnd,
Square::States defaultState /* = Square::Border */ )
{
m_iWidth = width;
m_iHeight = height;
m_qpSolutionStart = solStart;
m_qpSolutionEnd = solEnd;
allocate(defaultState);
}
Puzzle::Puzzle(Puzzle* originalPuzzle)
{
Square *origSquare;
m_iWidth = originalPuzzle->getWidth();
m_iHeight = originalPuzzle->getHeight();
m_qpSolutionStart = originalPuzzle->getSolutionStart();
m_qpSolutionEnd = originalPuzzle->getSolutionEnd();
allocate();
// Copies the squares or playsquares, depending
for(int row = 0; row < originalPuzzle->getHeight() + 2; row++)
{
for(int column = 0; column < originalPuzzle->getWidth() + 2; column++)
{
origSquare = originalPuzzle->getSquareAt(column, row);
setSquare(new Square(origSquare->getGridPos(),
origSquare->getState()));
}
}
}
Puzzle::Puzzle(Puzzle* originalPuzzle, QCanvas *canvas)
{
PlaySquare *origSquare;
m_iWidth = originalPuzzle->getWidth();
m_iHeight = originalPuzzle->getHeight();
m_qpSolutionStart = originalPuzzle->getSolutionStart();
m_qpSolutionEnd = originalPuzzle->getSolutionEnd();
allocate();
// Copies the squares or playsquares, depending
for(int column = 0; column < originalPuzzle->getWidth() + 2; column++)
{
for(int row = 0; row < originalPuzzle->getHeight() + 2; row++)
{
origSquare = (PlaySquare*)originalPuzzle->getSquareAt(column, row);
setSquare(new PlaySquare(canvas,
origSquare->getGridPos(),
origSquare->getState()));
}
}
}
Puzzle::Puzzle(QString code, QCanvas *canvas)
{
Puzzle *originalPuzzle = new Puzzle(code);
PlaySquare *origSquare;
m_iWidth = originalPuzzle->getWidth();
m_iHeight = originalPuzzle->getHeight();
m_qpSolutionStart = originalPuzzle->getSolutionStart();
m_qpSolutionEnd = originalPuzzle->getSolutionEnd();
allocate();
// Copies the squares or playsquares, depending
for(int column = 0; column < originalPuzzle->getWidth() + 2; column++)
{
for(int row = 0; row < originalPuzzle->getHeight() + 2; row++)
{
origSquare = (PlaySquare*)originalPuzzle->getSquareAt(column, row);
setSquare(new PlaySquare(canvas,
origSquare->getGridPos(),
origSquare->getState()));
}
}
delete originalPuzzle;
}
void Puzzle::allocate(Square::States filler /* = Square::Border*/ )
{
// Allocate the bidimensional array, including the Squares on the borders
// Allocate the row array
m_pArray = new Square**[getWidth()+2];
// Allocate the column array for each row
for (int column = 0; column < getWidth()+2; column++)
m_pArray[column] = new Square*[getHeight()+2];
// If filling with Border states, fill everything
// For each column in a row : allocate the Square
for (int column = 0; column < getWidth()+2; column++)
for (int row = 0; row < getHeight()+2; row++)
m_pArray[column][row] = new Square(QPoint(column, row),
Square::Border);
// If we want to fill with something other than a Border,
// the inside of the puzzle with the given State
if(filler != Square::Border)
{
// For each column in a row : allocate the Square
for (int column = 1; column < getWidth()+1; column++)
for (int row = 1; row < getHeight()+1; row++)
m_pArray[column][row] = new Square(QPoint(column, row),
filler);
}
}
Puzzle::~Puzzle()
{
// For each column in each row : delete the Square
for (int column = 0; column < getWidth()+2; column++)
for (int row = 0; row < getHeight()+2; row++)
delete m_pArray[column][row];
// Free the columns
for (int column = 0; column < getWidth()+2; column++)
delete m_pArray[column];
// Free the bidimensional array
delete m_pArray;
}
bool Puzzle::isCodeValid(QString code)
{
QString ucode = code.upper();
QString bincode = "";
unsigned int offset = 0, width, height;
for(unsigned int i = 0; i < ucode.length(); i++)
getBin(bincode, ucode.at(i).unicode()-65, 4);
unsigned int bitwidth = evalBin(bincode.left(6));
offset += 6;
width = evalBin(bincode.mid(offset, bitwidth));
if(width > MAX_SIDE)
return false;
offset += bitwidth;
height = evalBin(bincode.mid(offset, bitwidth));
if(height > MAX_SIDE)
return false;
offset += bitwidth;
// SolStart within the boundaries of the puzzle
if(evalBin(bincode.mid(offset, bitwidth)) > width)
return false;
if(evalBin(bincode.mid(offset + bitwidth, bitwidth)) > height)
return false;
offset += 2* bitwidth;
// SolEnd within the boundaries of the puzzle
if(evalBin(bincode.mid(offset, bitwidth)) > width)
return false;
if(evalBin(bincode.mid(offset + bitwidth, bitwidth)) > height)
return false;
offset += 2* bitwidth;
// Checks if there is enough square data following the header
if(bincode.length() - offset < (height*width))
return false;
// Everything seems to be fine
return true;
}
void Puzzle::setSquare(Square* square)
{
// If the square is already set, delete the previous square
int x = square->getGridPos().x();
int y = square->getGridPos().y();
if(m_pArray[x][y])
delete m_pArray[x][y];
m_pArray[x][y] = square;
}
Square* Puzzle::getSquareAt(int x, int y)
{
return m_pArray[x][y];
}
int Puzzle::getWidth()
{
return m_iWidth;
}
int Puzzle::getHeight()
{
return m_iHeight;
}
QPoint Puzzle::getSolutionStart()
{
return m_qpSolutionStart;
}
QPoint Puzzle::getSolutionEnd()
{
return m_qpSolutionEnd;
}
void Puzzle::invert()
{
// Copies the squares or playsquares, depending
for(int row = 1; row <= getHeight(); row++)
{
for(int column = 1; column <= getWidth(); column++)
{
getSquareAt(column, row)->toggle();
}
}
}
QString Puzzle::getCode()
{
/*
* Returns the code corresponding to this puzzle
* This is not really efficient, but keeps away from endian problems
* (ppc/x86), and I'm no super l337 hacker (far from it).
* Until some code checks are performed it's really easy to break
* the program here and in the constructor using the code
*/
int x = -1;
int y = -1;
int codewidth = 1;
QString code, tmpcode;
code = "";
tmpcode = "";
// Determine the code width (how many bits are needed to represent the width
x = MAX(getWidth(), getHeight());
while(x > (pow(2, codewidth)-1))
codewidth++;
// printf("We need %d bits\n", codewidth);
getBin(code, codewidth, 6);
// We reserve 6 bits for the width
getBin(code, getWidth(), codewidth);
getBin(code, getHeight(), codewidth);
// printf("width : %s\n", code.latin1());
// For the solutions
// Goes through every square and puts 0s until the solution where we
// put a 1
getBin(code, getSolutionStart().x(), codewidth);
getBin(code, getSolutionStart().y(), codewidth);
getBin(code, getSolutionEnd().x(), codewidth);
getBin(code, getSolutionEnd().y(), codewidth);
//printf("width,puzzle,sols1, sols2: %s\n", code.latin1());
for(y = 1; y<=getHeight() ; y++)
{
for(x = 1; x < getWidth() + 1; x++)
{
if(getSquareAt(x, y)->getState() == Square::Black)
code = code + "1";
else
code = code + "0";
}
}
// printf("width,puzzle : %s\n", code.latin1());
// 4 bits per letter
QString finalcode;
QString tmp;
unsigned int index = 0;
//printf("%s\n", code.latin1());
while(code.length() % 4 != 0)
code = code + "0";
// printf("%s (padded)\n", code.latin1());
while(index < code.length()-1)
{
finalcode = finalcode + QChar(65 + evalBin(code.mid(index, 4)));
index+=4;
}
// printf("%s => %s\n", code.latin1(), finalcode.latin1());
return finalcode;
}
void Puzzle::getBin(QString& res, int n)
{
QString tmpn;
if ((n == 0) || (n==1))
{
/* base case */
tmpn = tmpn.setNum(n);
res = res + tmpn;
}
else
{
/* recursive call */
getBin(res, n/2);
tmpn = tmpn.setNum(n%2);
res = res + tmpn;
}
}
void Puzzle::getBin(QString& res, int n, unsigned int length)
{
QString tmp = "";
getBin(tmp, n);
res = res + tmp.rightJustify(length, QChar('0'));
}
unsigned int Puzzle::evalBin(QString toeval)
{
int res = 0;
for(unsigned int i = 0; i<toeval.length(); i++)
{
if(toeval.at(toeval.length()-i-1) == QChar('1'))
res += (int) pow(2, i);
}
return res;
}
void Puzzle::printSpec()
{
printf("Puzzle spec: width:%d height:%d solstart:%d,%d solend:%d,%d\n",
getWidth(), getHeight(),
m_qpSolutionStart.x(), m_qpSolutionStart.y(),
m_qpSolutionEnd.x(), m_qpSolutionEnd.y());
}
|