Commit 236dbce9 authored by Johannes Bergmeier's avatar Johannes Bergmeier

- Removed deprecated class SKSolver

- Using SKGraph directly now

svn path=/trunk/KDE/kdegames/ksudoku/; revision=1120693
parent 3b1e5dba
......@@ -229,7 +229,7 @@ bool GameVariantDelegate::eventFilter(QObject* watched, QEvent* event) {
///////////////////////////////////////////////////////////////////////////////
SudokuGame::SudokuGame(const QString& name, uint order, GameVariantCollection* collection)
: GameVariant(name, collection), m_order(order), m_solver(0)
: GameVariant(name, collection), m_order(order), m_graph(0)
{
// TODO load from settings
m_symmetry = 0;
......@@ -249,26 +249,24 @@ bool SudokuGame::canStartEmpty() const {
}
Game SudokuGame::startEmpty() const {
if(!m_solver) {
GraphSudoku *graph = new GraphSudoku(m_order);
graph->init();
m_solver = new SKSolver(graph);
if(!m_graph) {
m_graph = new GraphSudoku(m_order);
m_graph->init();
}
Puzzle* puzzle = new Puzzle(m_solver, false);
Puzzle* puzzle = new Puzzle(m_graph, false);
puzzle->init();
return Game(puzzle);
}
Game SudokuGame::createGame(int difficulty) const {
if(!m_solver) {
GraphSudoku *graph = new GraphSudoku(m_order);
graph->init();
m_solver = new SKSolver(graph);
if(!m_graph) {
m_graph = new GraphSudoku(m_order);
m_graph->init();
}
Puzzle* puzzle = new Puzzle(m_solver, true);
Puzzle* puzzle = new Puzzle(m_graph, true);
puzzle->init(difficulty, m_symmetry);
return Game(puzzle);
......@@ -284,7 +282,7 @@ KsView* SudokuGame::createView(const Game& /*game*/) const {
///////////////////////////////////////////////////////////////////////////////
RoxdokuGame::RoxdokuGame(const QString& name, uint order, GameVariantCollection* collection)
: GameVariant(name, collection), m_order(order), m_solver(0)
: GameVariant(name, collection), m_order(order), m_graph(0)
{
// TODO load from settings
m_symmetry = 0;
......@@ -304,26 +302,24 @@ bool RoxdokuGame::canStartEmpty() const {
}
Game RoxdokuGame::startEmpty() const {
if(!m_solver) {
GraphRoxdoku *graph = new GraphRoxdoku(m_order);
graph->init();
m_solver = new SKSolver(graph);
if(!m_graph) {
m_graph = new GraphRoxdoku(m_order);
m_graph->init();
}
Puzzle* puzzle = new Puzzle(m_solver, false);
Puzzle* puzzle = new Puzzle(m_graph, false);
puzzle->init();
return Game(puzzle);
}
Game RoxdokuGame::createGame(int difficulty) const {
if(!m_solver) {
GraphRoxdoku *graph = new GraphRoxdoku(m_order);
graph->init();
m_solver = new SKSolver(graph);
if(!m_graph) {
m_graph = new GraphRoxdoku(m_order);
m_graph->init();
}
Puzzle* puzzle = new Puzzle(m_solver, true);
Puzzle* puzzle = new Puzzle(m_graph, true);
puzzle->init(difficulty, m_symmetry);
return Game(puzzle);
......@@ -340,7 +336,7 @@ KsView* RoxdokuGame::createView(const Game& /*game*/) const {
CustomGame::CustomGame(const QString& name, const KUrl& url,
GameVariantCollection* collection)
: GameVariant(name, collection), m_url(url), m_solver(0)
: GameVariant(name, collection), m_url(url), m_graph(0)
{ }
bool CustomGame::canConfigure() const {
......@@ -356,26 +352,24 @@ bool CustomGame::canStartEmpty() const {
}
Game CustomGame::startEmpty() const {
if(!m_solver) {
m_solver = ksudoku::Serializer::loadCustomShape(m_url, 0, 0);
if(!m_solver) return Game();
if(!m_graph) {
m_graph = ksudoku::Serializer::loadCustomShape(m_url, 0, 0);
if(!m_graph) return Game();
}
Puzzle* puzzle = new Puzzle(m_solver, false);
Puzzle* puzzle = new Puzzle(m_graph, false);
puzzle->init();
return Game(puzzle);
}
Game CustomGame::createGame(int difficulty) const {
if(!m_solver) {
m_solver = ksudoku::Serializer::loadCustomShape(m_url, 0, 0);
if(!m_solver) return Game();
if(!m_graph) {
m_graph = ksudoku::Serializer::loadCustomShape(m_url, 0, 0);
if(!m_graph) return Game();
}
Puzzle* puzzle = new Puzzle(m_solver, true);
Puzzle* puzzle = new Puzzle(m_graph, true);
puzzle->init(difficulty, 1);
return Game(puzzle);
......
......@@ -26,7 +26,7 @@
#include <QItemDelegate>
#include <QAbstractListModel>
class SKSolver;
class SKGraph;
namespace ksudoku {
class KsView;
......@@ -138,7 +138,7 @@ private:
uint m_order;
uint m_symmetry;
mutable SKSolver* m_solver;
mutable SKGraph *m_graph;
};
class RoxdokuGame : public GameVariant {
......@@ -157,7 +157,7 @@ private:
uint m_order;
uint m_symmetry;
mutable SKSolver* m_solver;
mutable SKGraph* m_graph;
};
class CustomGame : public GameVariant {
......@@ -174,7 +174,7 @@ public:
private:
KUrl m_url;
mutable SKSolver* m_solver;
mutable SKGraph* m_graph;
};
}
......
......@@ -65,6 +65,7 @@
#include "settings.h"
#include "config.h"
using namespace ksudoku;
void KSudoku::onCompleted(bool isCorrect, const QTime& required, bool withHelp) {
if(!isCorrect) {
......
......@@ -21,8 +21,6 @@
#include "ksudokugame.h"
#include "sksolver.h"
#include "puzzle.h"
#include "history.h"
......@@ -219,13 +217,6 @@ Puzzle* Game::puzzle() const {
return m_private->puzzle;
}
bool Game::hasSolver()
{
if(!m_private)
return 0;
return m_private->puzzle->hasSolver();
}
void Game::setUrl(const KUrl& url) {
if(!m_private) return;
......@@ -351,7 +342,7 @@ bool Game::giveHint() {
// i = RANDOM(size());
// } while (m_private->isGiven(i));
int start = RANDOM(size());
int start = rand() % size();
int i;
for(i = start; i < size(); ++i)
if(!given(i))
......
......@@ -123,9 +123,6 @@ public:
Game& operator=(const Game& game);
public:
bool hasSolver();
int order() const;
bool marker(int index, int value) const;
......
......@@ -20,7 +20,6 @@
***************************************************************************/
#include "serializer.h"
//#include "sksolver.h"
#include "ksudokugame.h"
#include "puzzle.h"
......@@ -86,8 +85,8 @@ Game Serializer::deserializeGame(QDomElement element) {
}
Puzzle* Serializer::deserializePuzzle(QDomElement element) {
bool hasSolver = false;
SKSolver* solver = 0;
bool hasGraph = false;
SKGraph* graph = 0;
bool hasValues = false;
QString valuesStr;
bool hasSolution = false;
......@@ -98,16 +97,16 @@ Puzzle* Serializer::deserializePuzzle(QDomElement element) {
while (!child.isNull()) {
if(child.isElement()) {
if(child.nodeName() == "graph") {
if(hasSolver) {
delete solver;
if(hasGraph) {
delete graph;
return 0;
}
solver = deserializeGraph(child.toElement());
hasSolver = true;
graph = deserializeGraph(child.toElement());
hasGraph = true;
} else if(child.nodeName() == "values") {
if(hasValues) {
delete solver;
delete graph;
return 0;
}
......@@ -116,7 +115,7 @@ Puzzle* Serializer::deserializePuzzle(QDomElement element) {
} else if(child.nodeName() == "solution") {
// TODO remove deserialization of solution, it is no longer required
if(hasSolution) {
delete solver;
delete graph;
return 0;
}
......@@ -127,30 +126,30 @@ Puzzle* Serializer::deserializePuzzle(QDomElement element) {
child = child.nextSibling();
}
if(!solver) return 0;
if(valuesStr.length() != solver->g->size()) {
delete solver;
if(!graph) return 0;
if(valuesStr.length() != graph->size()) {
delete graph;
return 0;
}
// TODO remove deserialization of solution, it is no longer required
if(solutionStr.length() != 0 && solutionStr.length() != solver->g->size()) {
delete solver;
if(solutionStr.length() != 0 && solutionStr.length() != graph->size()) {
delete graph;
return 0;
}
Puzzle* puzzle = new Puzzle(solver, hasSolution);
Puzzle* puzzle = new Puzzle(graph, hasSolution);
QByteArray values;
values.resize(solver->g->size());
for(int i = 0; i < solver->g->size(); ++i) {
values.resize(graph->size());
for(int i = 0; i < graph->size(); ++i) {
values[i] = Symbols::ioSymbol2Value(valuesStr[i]);
}
// TODO remove deserialization of solution, it is no longer required
QByteArray solution;
if(solutionStr.length() != 0) {
solution.resize(solver->g->size());
for(int i = 0; i < solver->g->size(); ++i) {
solution.resize(graph->size());
for(int i = 0; i < graph->size(); ++i) {
solution[i] = Symbols::ioSymbol2Value(solutionStr[i]);
}
}
......@@ -173,7 +172,7 @@ static int readInt(QDomElement element, const QString& name, int* err)
return num;
}
SKSolver* Serializer::deserializeGraph(QDomElement element) {
SKGraph* Serializer::deserializeGraph(QDomElement element) {
bool noFailure = true;
QString orderStr = element.attribute("order");
......@@ -191,13 +190,11 @@ SKSolver* Serializer::deserializeGraph(QDomElement element) {
if(type == "sudoku") {
GraphSudoku *graph = new GraphSudoku(order);
graph->init();
SKSolver *solver = new SKSolver(graph);
return solver;
return graph;
} else if(type == "roxdoku") {
GraphRoxdoku *graph = new GraphRoxdoku(order);
graph->init();
SKSolver *solver = new SKSolver(graph);
return solver;
return graph;
} else if(type == "custom") {
int err=0;
int ncliques = readInt(element,"ncliques", &err);
......@@ -224,12 +221,10 @@ SKSolver* Serializer::deserializeGraph(QDomElement element) {
child = child.nextSibling();
}
GraphCustom* gc = new GraphCustom();
gc->init(name.toLatin1(), order, sizeX, sizeY, sizeZ, ncliques, cliques.toLatin1());
if(gc->valid==false) return 0;
SKSolver* solver = new SKSolver(gc);
return solver;
GraphCustom* graph = new GraphCustom();
graph->init(name.toLatin1(), order, sizeX, sizeY, sizeZ, ncliques, cliques.toLatin1());
if(graph->valid==false) return 0;
return graph;
}
return 0;
......@@ -294,7 +289,7 @@ HistoryEvent Serializer::deserializeComplexHistoryEvent(QDomElement /*element*/)
return HistoryEvent();
}
SKSolver* Serializer::loadCustomShape(const KUrl& url, QWidget* window, QString *errorMsg) {
SKGraph *Serializer::loadCustomShape(const KUrl &url, QWidget* window, QString *errorMsg) {
if ( url.isEmpty() ) return 0;
QString tmpFile;
bool success = false;
......@@ -374,24 +369,6 @@ Game Serializer::load(const KUrl& url, QWidget* window, QString *errorMsg) {
child = child.nextSibling();
}
// SKSolver* sk = (SKSolver*) game.puzzle()->solver();
// QString name = ((GraphCustom*)sk->g)->name;
// KSudoku* p = (KSudoku*) window;
// if(!p->shapes().contains(name))
// {
// KStandardDirs myStdDir;
// const QString destDir = myStdDir.saveLocation("data", /* TODO PORT kapp->instanceName() +*/ "ksudoku/", true);
// KStandardDirs::makeDir(destDir);
//
// QString path = destDir + name + ".xml";
// KUrl url;
// url.setPath(path);
//
// Serializer::storeCustomShape( sk, url ,window );
// p->updateShapesList();
//
// }
return game;
}
......@@ -409,7 +386,7 @@ bool Serializer::serializePuzzle(QDomElement& parent, const Puzzle* puzzle) {
QDomDocument doc = parent.ownerDocument();
QDomElement element = doc.createElement("puzzle");
serializeGraph(element, puzzle->solver());
serializeGraph(element, puzzle->graph());
for(int i = 0; i < puzzle->size(); ++i) {
contentStr += Symbols::ioValue2Symbol(puzzle->value(i));
......@@ -433,16 +410,16 @@ bool Serializer::serializePuzzle(QDomElement& parent, const Puzzle* puzzle) {
return true;
}
bool Serializer::serializeGraph(QDomElement& parent, const SKSolver* puzzle) {
bool Serializer::serializeGraph(QDomElement &parent, const SKGraph *graph) {
QDomElement element = parent.ownerDocument().createElement("graph");
element.setAttribute("order", puzzle->g->order());
element.setAttribute("order", graph->order());
//element.setAttribute("size", puzzle->size());
GameType type = puzzle->g->type();
GameType type = graph->type();
element.setAttribute("type" , (type == TypeSudoku) ? "sudoku" : (type == TypeRoxdoku) ? "roxdoku" : "custom");
if(type == TypeCustom)
{
GraphCustom* g = (GraphCustom*) puzzle->g;
GraphCustom* g = (GraphCustom*) graph;
element.setAttribute("ncliques", (int) g->cliques.size());
element.setAttribute("name", g->name);
element.setAttribute("sizeX", g->sizeX());
......@@ -567,19 +544,4 @@ bool Serializer::store(const Game& game, const KUrl& url, QWidget* window) {
return true;
}
bool Serializer::storeCustomShape(const SKSolver* solver, const KUrl& /*url*/, QWidget* /*window*/) {
QDomDocument doc( "ksudoku-graph" );
QDomElement root = doc.createElement( "ksudoku-graph" );
doc.appendChild( root );
serializeGraph(root, solver);
KTemporaryFile tmp;
//(*tmp.textStream()) << doc.toString(); //TODO PORT
tmp.close();
//KIO::NetAccess::upload(tmp.name(), url, window); //TODO PORT
//tmp.unlink(); //TODO PORT
return true;
}
}
......@@ -24,7 +24,7 @@
#include <QList>
class SKSolver;
class SKGraph;
class QDomElement;
class KUrl;
class QWidget;
......@@ -39,23 +39,22 @@ class Serializer {
public:
static Game deserializeGame(QDomElement element);
static Puzzle* deserializePuzzle(QDomElement element) ;
static SKSolver* deserializeGraph(QDomElement element);
static SKGraph* deserializeGraph(QDomElement element);
static QList<HistoryEvent> deserializeHistory(QDomElement element);
static HistoryEvent deserializeSimpleHistoryEvent(QDomElement element);
static HistoryEvent deserializeComplexHistoryEvent(QDomElement element);
static Game load(const KUrl& url, QWidget* window, QString* errorMsg = 0);
static SKSolver* loadCustomShape(const KUrl& url, QWidget* window, QString* errorMsg = 0);
static SKGraph* loadCustomShape(const KUrl& url, QWidget* window, QString* errorMsg = 0);
static bool serializeGame(QDomElement& parent, const Game& game);
static bool serializePuzzle(QDomElement& parent, const Puzzle* puzzle);
static bool serializeGraph(QDomElement& parent, const SKSolver* solver);
static bool serializeGraph(QDomElement& parent, const SKGraph* graph);
static bool serializeHistory(QDomElement& parent, const Game& game);
static bool serializeHistoryEvent(QDomElement& parent, const HistoryEvent& event);
static bool store(const Game& game, const KUrl& url, QWidget* window);
static bool storeCustomShape(const SKSolver* solver, const KUrl& url, QWidget* window);
};
}
......
......@@ -29,7 +29,6 @@
#include "ksview.moc"
#include "sksolver.h"
#include "puzzle.h"
#ifdef OPENGL_SUPPORT
......
......@@ -29,7 +29,6 @@
//Added by qt3to4:
#include <QWheelEvent>
#include <QMouseEvent>
#include "sksolver.h"
#include "ArcBall.h"
#include "ksudokugame.h"
......
......@@ -29,7 +29,6 @@
#include <kdebug.h>
#include "sksolver.h"
#include "puzzle.h"
#include "gameactions.h"
......@@ -366,11 +365,11 @@ void View2DScene::init(const Game& game) {
m_cellLayer->setHandlesChildEvents(false);
addItem(m_cellLayer);
SKGraph* g = m_game.puzzle()->solver()->g;
SKGraph* g = m_game.puzzle()->graph();
m_cells.resize(m_game.size());
m_cursorPos = -1;
for(int i = 0; i < m_game.size(); ++i) {
bool notConnectedNode = ((GraphCustom*) m_game.puzzle()->solver()->g)->optimized_d[i] == 0;
bool notConnectedNode = ((GraphCustom*) m_game.puzzle()->graph())->optimized_d[i] == 0;
if(notConnectedNode) {
m_cells[i] = 0;
continue;
......@@ -416,7 +415,7 @@ void View2DScene::init(const Game& game) {
void View2DScene::setSceneSize(const QSize& size) {
m_background->setPixmap(Renderer::instance()->renderBackground(size));
SKGraph* g = m_game.puzzle()->solver()->g;
SKGraph* g = m_game.puzzle()->graph();
setSceneRect(QRectF(0, 0, size.width(), size.height()));
int width = size.width() / (g->sizeX()+1);
......@@ -558,7 +557,7 @@ void View2DScene::flipMarkValue(int value, int cell) {
}
void View2DScene::moveCursor(int dx, int dy) {
SKGraph* g = m_game.puzzle()->solver()->g;
SKGraph* g = m_game.puzzle()->graph();
QPoint oldPos = m_cells[m_cursorPos]->pos();
QPoint relPos;
int newCursorPos = -1;
......
set(ksudoku_logic_SRCS
skgraph.cpp
sksolver.cpp
puzzle.cpp
)
......
......@@ -29,23 +29,23 @@
namespace ksudoku {
Puzzle::Puzzle(SKSolver* solver, bool withSolution)
Puzzle::Puzzle(SKGraph *graph, bool withSolution)
: m_withSolution(withSolution)
, m_solver(solver)
, m_graph(graph)
, m_difficulty(0)
, m_symmetry(0)
, m_initialized(false)
{ }
int Puzzle::value(int x, int y, int z) const {
Item *item = m_solver->g->board()->itemAt(x, y, z);
Item *item = m_graph->board()->itemAt(x, y, z);
if(item && m_puzzle2.ruleset())
return static_cast<ChoiceItem*>(item)->value(&m_puzzle2);
return 0;
}
int Puzzle::solution(int x, int y, int z) const {
Item *item = m_solver->g->board()->itemAt(x, y, z);
Item *item = m_graph->board()->itemAt(x, y, z);
if(item && m_solution2.ruleset())
return static_cast<ChoiceItem*>(item)->value(&m_solution2);
return 0;
......@@ -57,14 +57,14 @@ bool Puzzle::init() {
if(m_withSolution)
return false;
m_puzzle2 = Problem(m_solver->g->rulset());
m_puzzle2 = Problem(m_graph->rulset());
return true;
}
bool Puzzle::createPartial(Solver *solver) {
// TODO after finding a solution try to find simpler ones
const ItemBoard *board = m_solver->g->board();
const ItemBoard *board = m_graph->board();
for(;;) {
ChoiceItem *choiceItem;
for(;;) {
......@@ -117,7 +117,7 @@ bool Puzzle::init(int difficulty, int symmetry) {
if(m_initialized) return false;
Solver solver;
solver.setLimit(2);
solver.loadEmpty(m_solver->g->rulset());
solver.loadEmpty(m_graph->rulset());
if(createPartial(&solver)) {
return true;
......@@ -129,12 +129,12 @@ bool Puzzle::init(int difficulty, int symmetry) {
int Puzzle::init(const QByteArray& values) {
if(m_initialized) return -1;
m_puzzle2 = Problem(m_solver->g->rulset());
for(int x = 0; x < m_solver->g->sizeX(); ++x) {
for(int y = 0; y < m_solver->g->sizeY(); ++y) {
for(int z = 0; z < m_solver->g->sizeZ(); ++z) {
int value = values[m_solver->g->cellIndex(x, y, z)];
Item *item = m_solver->g->board()->itemAt(x, y, z);
m_puzzle2 = Problem(m_graph->rulset());
for(int x = 0; x < m_graph->sizeX(); ++x) {
for(int y = 0; y < m_graph->sizeY(); ++y) {
for(int z = 0; z < m_graph->sizeZ(); ++z) {
int value = values[m_graph->cellIndex(x, y, z)];
Item *item = m_graph->board()->itemAt(x, y, z);
if(item && value)
static_cast<ChoiceItem*>(item)->setValue(&m_puzzle2, value);
}
......
......@@ -22,8 +22,8 @@
#ifndef _KSUDOKUPUZZLE_H_
#define _KSUDOKUPUZZLE_H_
#include "sksolver.h"
#include "ksudoku_types.h"
#include "solver.h"
#include "skgraph.h"
class QChar;
......@@ -35,8 +35,8 @@ public:
* @param[in] sovler The solver used for this puzzle (the GameType) (not deleted when Puzzle is deleted)
* @param[in] withSolution Whether a the solution for this puzzle should be stored
*/
explicit Puzzle(SKSolver* solver, bool withSolution = true);
explicit Puzzle(SKGraph* graph, bool withSolution = true);
public:
/**
* Creates a puzzle without any content
......@@ -44,9 +44,8 @@ public:
bool init();
/**
* Creates a new puzzle based on the solver
* @param[in] difficulty The difficulty of the new game (for valid values see
* code of @c SKSolver)
* @param[in] symmetry The symmetry (for valid values see code of @c SKSolver)
* @param[in] difficulty The difficulty of the new game.
* @param[in] symmetry The symmetry
*/
bool init(int difficulty, int symmetry);
/**
......@@ -62,43 +61,39 @@ public:
* Return game type
*/