~ubuntu-branches/ubuntu/quantal/kdegames/quantal

« back to all changes in this revision

Viewing changes to ksudoku/engine/constraint.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:50 UTC
  • mfrom: (1.3.14)
  • Revision ID: package-import@ubuntu.com-20111215141750-6tj6brf4azhrt915
Tags: 4:4.7.90-0ubuntu1
new upstream beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "constraint.h" 
2
 
 
3
 
#include "ruleset.h"
4
 
 
5
 
#include <iostream>
6
 
 
7
 
/**
8
 
 * \class ConstraintHelper
9
 
 *
10
 
 * \brief ConstraintHelper is the base for all constraint helpers.
11
 
 *
12
 
 * Constraint-helpers do the actual validation against the rules of a constraint
13
 
 * and optionaly helps to resolve the constraint
14
 
 */
15
 
 
16
 
/**
17
 
 * \fn void apply(IHelperTarget* target) const
18
 
 * Applies the helper to the \a target (which is a solver in most cases).
19
 
 *
20
 
 * This includes validation and changing the targets problem in a way the limits
21
 
 * the possible configurations towards the solution(s) of the problem.
22
 
 * Needs to be implemented by derived classes.
23
 
 */
24
 
 
25
 
/**
26
 
 * \fn Constraint* constraint() const
27
 
 * Returns the constraint the helper belongs to.
28
 
 *
29
 
 * Needs to be implemented by derived classes.
30
 
 */
31
 
 
32
 
/**
33
 
 * Constructs a new constraint-helper.
34
 
 */
35
 
ConstraintHelper::ConstraintHelper()
36
 
        : m_entry(this)
37
 
{
38
 
}
39
 
 
40
 
/**
41
 
 * Registers the helper at \a ruleset.
42
 
 */
43
 
void ConstraintHelper::setup(Ruleset* ruleset) {
44
 
        ruleset->addHelper(this);
45
 
        m_entry.setup(ruleset);
46
 
}
47
 
 
48
 
/**
49
 
 * Called when an \a item associated with the helper changed.
50
 
 *
51
 
 * Reactivates the helper in \a problem if it is really affected
52
 
 * by the change in item.
53
 
 */
54
 
void ConstraintHelper::itemChanged(Problem* problem, const Item* item) const {
55
 
        if(!constraint()->reallyAffectsItem(item, problem)) return;
56
 
        
57
 
        m_entry.activate(problem);
58
 
}
59
 
 
60
 
/**
61
 
 * Helps to resolve the constraint.
62
 
 *
63
 
 * This method calls apply() and afterwards set this helper to idle
64
 
 */
65
 
bool ConstraintHelper::resolve(Problem *problem) const {
66
 
        bool noerror = apply(problem);
67
 
        m_entry.resolve(problem);
68
 
        return noerror;
69
 
}
70
 
 
71
 
/**
72
 
 * Returns whether the constraint waits for solving.
73
 
 */
74
 
bool ConstraintHelper::isPending(Problem* problem) const {
75
 
        return m_entry.isPending(problem);
76
 
}
77
 
 
78
 
/**
79
 
 * \class Constraint
80
 
 *
81
 
 * \brief Constraint is the base for all constraints in a graph.
82
 
 *
83
 
 * A constraint is the specific application of a rule. For example
84
 
 * in sudoku a constraint may limit all cells in a specific row
85
 
 * to contain each value only once.
86
 
 */
87
 
 
88
 
/**
89
 
 * \fn QVector<int> Constraint::affectedNodes() const
90
 
 * Returns a list of all nodes that may be affected by the constraint.
91
 
 */
92
 
 
93
 
/**
94
 
 * Constructs a new constraint.
95
 
 */
96
 
Constraint::Constraint() {
97
 
}
98
 
 
99
 
/**
100
 
 * Returns true if the constraint really affects \a item.
101
 
 * Reimplement this if the nodes affected by constraint might change 
102
 
 * depending on the state of \a puzzle.
103
 
 */
104
 
bool Constraint::reallyAffectsItem(const Item *item, Problem *puzzle) const {
105
 
        Q_UNUSED(item);
106
 
        Q_UNUSED(puzzle);
107
 
        return true;
108
 
}
109
 
 
110
 
/**
111
 
 * Reimplement this to initilazies the constraint and registrater it
112
 
 * to the graph.
113
 
 */
114
 
void Constraint::init(Ruleset *rules) {
115
 
        Q_UNUSED(rules);
116
 
}
117
 
 
118
 
#include "constraint.moc"