summaryrefslogtreecommitdiff
path: root/qml/pages/Goban.qml
blob: 420d22d6b8ee2020b5a058af34f84065d5c0382d (plain)
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
import QtQuick 2.0

import "../javascript/goban_util.js" as Actions

Item {

    /**
     * This property represent a case size on the board.
     * The value is calculated at initialization, and depends on the goban size.
     */
    property int caseSize

    /**
     * Booleans flags telling if the board is limited in each directions
     */
    property bool limitTop: true;
    property bool limitBottom: true;
    property bool limitLeft: true;
    property bool limitRight: true;

    /*
     * The current color to play :
     * - true for white
     * - false for black
     */
    property bool currentPlayer: true;

    property variant tree;

    /*
     * Start the game.
     *
     * Initialize the board with the stones, and set player color.
     */
    function start() {

        currentPlayer = true;

        for (var i = 0; i < goban.rows * goban.columns; i++) {
            repeater.itemAt(i).remove(false);
        }

        var initial

        i = 0;

        while (tree[i].AW === undefined && tree[i].AB === undefined) {
            i++;
        }

        initial = tree[i]

        var aw = initial.AW;
        if (aw !== undefined) {
            aw.forEach(function (pos) {
                goban.getItemAt(pos[0], pos[1]).put(currentPlayer, false);
//                Actions.addPiece(pos[0] + (pos[1] * goban.columns), goban, currentPlayer, false, true, true);
            });
        }

        var ab = initial.AB;
        if (ab !== undefined) {
            ab.forEach(function (pos) {
                goban.getItemAt(pos[0], pos[1]).put(!currentPlayer, false);
//                Actions.addPiece(pos[0] + (pos[1] * goban.columns), goban, !currentPlayer, false, true, true);
            });
        }
    }

    function setGoban(ret) {

        limitTop = ret.side.TOP;
        limitBottom = ret.side.BOTTOM;
        limitLeft = ret.side.LEFT;
        limitRight = ret.side.RIGHT;

        goban.columns = ret.size[0]
        goban.rows = ret.size[1]


        var maxWidth = width / ret.size[0]
        var maxHeight = height / ret.size[1]

        if (maxWidth > maxHeight)  {
            caseSize = maxHeight;
        } else {
            caseSize = maxWidth;
        }

        /*
         * Put the initials stones
         */
        tree = ret.tree;
        start();
    }

    /**
     * Handle a click on the goban.
     */
    function clickHandler(index) {

        if ( (!limitLeft && Actions.isFirstCol(index, goban.columns))
          || (!limitRight && Actions.isLastCol(index, goban.columns))
          || (!limitTop && Actions.isFirstRow(index, goban.columns))
          || (!limitBottom && Actions.isLastRow(index, goban.columns, goban.rows)) ) {

            return;
        }

        if (Actions.addPiece(index, goban, currentPlayer, true, false, false)) {
            currentPlayer = !currentPlayer;
        }

    }

    /**
     * Background
     */
    Image {
        width: goban.width + (caseSize / 2); height: goban.height + (caseSize / 2);
        source: "../content/gfx/board.png"
        anchors.centerIn: goban
    }

    /*
     * Horizontal lines
     */
    Repeater {
        model: goban.rows

        Rectangle {

            x: goban.x + (caseSize / 2)

            y: goban.y + (caseSize / 2) + (index * caseSize)

            width: goban.width - caseSize;

            color: "black"

            visible: (!((index === goban.rows - 1 && !limitBottom) || (index === 0 && !limitTop)))

            height: 1

        }
    }

    /*
     * Verticals lines
     */
    Repeater {
        model: goban.columns

        Rectangle {

            x: goban.x + (caseSize / 2) + (index * caseSize)

            y: goban.y + (caseSize / 2)

            height: goban.height - caseSize;

            color: "black"

            width: 1

            visible: (!((index === goban.columns - 1 && !limitRight) || (index === 0 && !limitLeft)));
        }
    }

    /*
     * The grid for the game.
     */
    Grid {
        id: goban
        anchors.centerIn: parent
        columns: 0
        rows : 0
        spacing: 0

        function getItemAt(x, y) {
            return repeater.itemAt(x + y * columns)
        }

        function getElementAtIndex(index) {
            return repeater.itemAt(index);
        }

        Repeater {
            model: goban.columns * goban.rows
            id : repeater

            Point{
                width: caseSize; height: caseSize
                id : piece

                MouseArea {

                    id: interactiveArea
                    anchors.fill: parent
                    onClicked: clickHandler(index);
                }

            }
        }
    }
}