-
Notifications
You must be signed in to change notification settings - Fork 0
/
kenkenparser.cpp
96 lines (89 loc) · 2.54 KB
/
kenkenparser.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
#include "kenkenparser.h"
KenkenParser::KenkenParser(QObject *parent) : QObject(parent)
{
coordSearch.setPattern("(\\d+),(\\d+)");;
opSearch.setPattern("(\\d+)([\\+\\*-:])?$");;
}
KenkenParser::~KenkenParser()
{
}
void KenkenParser::parse(QFile &file)
{
QTextStream in(&file);
QString line;
while(!in.atEnd())
{
BorderRule rule;
line = in.readLine();
if(line.isEmpty())
break;
int pos = 0;
bool res = true;
while ((pos = coordSearch.indexIn(line, pos)) != -1)
{
rule.points << QPoint(coordSearch.cap(1).toInt(&res) - 1,
coordSearch.cap(2).toInt(&res) - 1);
pos += coordSearch.matchedLength();
points++;
}
opSearch.indexIn(line);
rule.setOperation(opSearch.cap(2));
rule.resultOp = opSearch.cap(1).toInt(&res);
if((rule.points.size() == 1 && rule.op != FIXED) || !res || rule.points.size() == 0)
{
emit fail("Error on line: " + line);
return;
}
rules.push_back(rule);
}
createGrids();
emit success(grid, rules);
}
void KenkenParser::createGrids()
{
int size = sqrt(points);
grid = new GridItem(size, size);
QVector<BorderRule> ruleWithoutFixed;
foreach(auto rule, rules)
{
Cell *cell = grid->getCell(rule.points[0].y(), rule.points[0].x());
cell->setOperation(rule.getOperation());
if(rule.op != FIXED)
ruleWithoutFixed.append(rule);
else
{
cell->setText(rule.resultOp);
cell->setFixedText(true);
}
foreach(QPoint point, rule.points)
setCellBorder(point, rule);
}
rules = ruleWithoutFixed;
}
void KenkenParser::setCellBorder(const QPoint &point, const BorderRule &rule)
{
int size = grid->getRows();
Cell *cell = grid->getCell(point.y(), point.x());
for(int dx = -1; dx <= 1; dx += 2)
{
if(point.x() + dx >= size || point.x() + dx < 0)
continue;
if(rule.points.contains(QPoint(point.x() + dx, point.y())))
continue;
if(dx == -1)
cell->setLeft(true);
else
cell->setRight(true);
}
for(int dy = -1; dy <= 1; dy += 2)
{
if(point.y() + dy >= size || point.y() + dy < 0)
continue;
if(rule.points.contains(QPoint(point.x(), point.y() + dy)))
continue;
if(dy == -1)
cell->setTop(true);
else
cell->setButtom(true);
}
}