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

« back to all changes in this revision

Viewing changes to ksudoku/engine/script/boardprototype.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 "./boardprototype.h"
2
 
#include "item.h"
3
 
 
4
 
#include <QScriptEngine>
5
 
 
6
 
#include <QVariant>
7
 
 
8
 
#include "boardwrapper.h"
9
 
 
10
 
#ifndef KDE_USE_FINAL
11
 
Q_DECLARE_METATYPE(QVector<QScriptValue>)
12
 
#endif
13
 
BoardPrototype::BoardPrototype(QObject *parent)
14
 
        : QObject(parent)
15
 
{
16
 
}
17
 
 
18
 
BoardWrapper* BoardPrototype::thisBoard() const {
19
 
        return thisObject().data().toVariant().value<BoardWrapper*>();
20
 
}
21
 
 
22
 
#include <QDebug>
23
 
void BoardPrototype::setItem(const QScriptValue& item, int x, int y, int z, int w) {
24
 
        BoardWrapper *wrapper = thisBoard();
25
 
        wrapper->setItemAt(item, x, y, z, w);
26
 
}
27
 
 
28
 
QScriptValue BoardPrototype::items(int x, int y, int z, int w) {
29
 
        BoardWrapper *wrapper = thisBoard();
30
 
        ItemBoard *board = wrapper->board();
31
 
        Q_ASSERT(wrapper);
32
 
 
33
 
        // reimplement the ItemBoard::items functionality
34
 
        // NOTE must reflect changes in the ItemBoard class
35
 
        QVector<QScriptValue> items;
36
 
        int min0 = x, min1 = y, min2 = z, min3 = w;
37
 
        int max0 = x, max1 = y, max2 = z, max3 = w;
38
 
        if(min0 < 0) { min0 = 0; max0 = board->size(0)-1; }
39
 
        if(min1 < 0) { min1 = 0; max1 = board->size(1)-1; }
40
 
        if(min2 < 0) { min2 = 0; max2 = board->size(2)-1; }
41
 
        if(min3 < 0) { min3 = 0; max3 = board->size(3)-1; }
42
 
 
43
 
        for(int i3 = min3; i3 <= max3; ++i3) {
44
 
                for(int i2 = min2; i2 <= max2; ++i2) {
45
 
                        for(int i1 = min1; i1 <= max1; ++i1) {
46
 
                                for(int i0 = min0; i0 <= max0; ++i0) {
47
 
                                        items.append(wrapper->itemAt(i0, i1, i2, i3));
48
 
                                }
49
 
                        }
50
 
                }
51
 
        }
52
 
 
53
 
        return engine()->toScriptValue(items);
54
 
}
55
 
 
56
 
QScriptValue BoardPrototype::split(int x, int y, int z, int w) {
57
 
        BoardWrapper *wrapper = thisBoard();
58
 
        ItemBoard *board = wrapper->board();
59
 
        Q_ASSERT(board);
60
 
 
61
 
        int countX = 1, countY = 1, countZ = 1, countW = 1;
62
 
        int sizeX = board->size(0), sizeY = board->size(1), sizeZ = board->size(2), sizeW = board->size(3);
63
 
 
64
 
        if(x > 0) {
65
 
                if(sizeX % x) return context()->throwError(QString("blocks of size (%1,%1,%1,%1) don't fit into board "
66
 
                        "(x mismatch)").arg(x).arg(y).arg(z).arg(w));
67
 
                countX = sizeX / x;
68
 
                sizeX = x;
69
 
        }
70
 
        if(y > 0) {
71
 
                if(sizeY % y) return context()->throwError(QString("blocks of size (%1,%1,%1,%1) don't fit into board "
72
 
                        "(y mismatch)").arg(x).arg(y).arg(z).arg(w));
73
 
                countY = sizeY / y;
74
 
                sizeY = y;
75
 
        }
76
 
        if(z > 0) {
77
 
                if(sizeZ % z) return context()->throwError(QString("blocks of size (%1,%1,%1,%1) don't fit into board "
78
 
                        "(z mismatch)").arg(x).arg(y).arg(z).arg(w));
79
 
                countZ = sizeZ / z;
80
 
                sizeZ = z;
81
 
        }
82
 
        if(w > 0) {
83
 
                if(sizeW % w) return context()->throwError(QString("blocks of size (%1,%1,%1,%1) don't fit into board "
84
 
                        "(w mismatch)").arg(x).arg(y).arg(z).arg(w));
85
 
                countW = sizeW / w;
86
 
                sizeW = w;
87
 
        }
88
 
 
89
 
        QVector<QScriptValue> blocks;
90
 
        for(int ix = 0; ix < countX; ++ix) {
91
 
        for(int iy = 0; iy < countY; ++iy) {
92
 
        for(int iz = 0; iz < countZ; ++iz) {
93
 
        for(int iw = 0; iw < countW; ++iw) {
94
 
                QVector<QScriptValue> block;
95
 
                block.reserve(sizeX*sizeY*sizeZ*sizeW);
96
 
                for(int jx = 0; jx < sizeX; ++jx) {
97
 
                for(int jy = 0; jy < sizeY; ++jy) {
98
 
                for(int jz = 0; jz < sizeZ; ++jz) {
99
 
                for(int jw = 0; jw < sizeW; ++jw) {
100
 
                        QScriptValue item = wrapper->itemAt(ix*sizeX+jx, iy*sizeY+jy, iz*sizeZ+jz, iw*sizeW+jw);
101
 
                        block << item;
102
 
                }}}}
103
 
                blocks << engine()->toScriptValue(block);
104
 
        }}}}
105
 
 
106
 
        return engine()->toScriptValue(blocks);
107
 
}
108
 
 
109
 
#include "boardprototype.moc"
110