-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGameBoardToggle.js
151 lines (138 loc) · 3.33 KB
/
GameBoardToggle.js
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
var rows = 1;
var columns = 1;
var grids = new Array(rows * columns);
var circles = new Array(rows * columns);
var verLines = new Array(rows * columns);
var horLines = new Array(rows * columns);
var components = new Array(3)
var gridLength = 0;
function index(x, y) {
return x * columns + y
}
function createBoard(n, m, length) {
// Remove previous blocks
for (var i = 0; i < rows * columns; ++i) {
if (grids[i] != null)
grids[i].destroy();
if (circles[i] != null)
circles[i].destroy();
if (verLines[i] != null)
verLines[i].destroy();
if (horLines[i] != null)
horLines[i].destroy();
}
rows = n;
columns = m;
gridLength = length;
grids = new Array(rows * columns);
circles = new Array(rows * columns);
verLines = new Array(rows * columns);
horLines = new Array(rows * columns);
for (var i = 0; i < rows; ++i)
for (var j = 0; j < columns; ++j) {
var id = index(i, j);
grids[id] = createObject(0, "Grid", i, j);
grids[id].state = "shown";
}
for (var i = 0; i < rows; ++i)
for (var j = 0; j < columns; ++j) {
var id = index(i, j);
if (i + 1 < rows) {
verLines[id] = createObject(2, "Line", i, j);
verLines[id].vertical = true;
} else {
verLines[id] = null;
}
if (j + 1 < columns) {
horLines[id] = createObject(2, "Line", i, j);
horLines[id].vertical = false;
} else {
horLines[id] = null;
}
}
for (var i = 0; i < rows; ++i)
for (var j = 0; j < columns; ++j) {
var id = index(i, j);
circles[id] = createObject(1, "Circle", i, j);
}
}
function createObject(index, name, x, y) {
if (components[index] == null)
components[index] = Qt.createComponent("Board" + name + ".qml");
if (components[index].status == Component.Ready) {
var obj = components[index].createObject(board);
if (obj == null) {
console.log("error creating " + name + ": " + components[index].errorString());
return null;
}
// obj.length = gridLength;
obj.posY = x;
obj.posX = y;
return obj;
} else {
console.log("error loading " + name + ": " + components[index].errorString());
return null;
}
}
function hideAll() {
for (var i = 0; i < rows; ++i)
for (var j = 0; j < columns; ++j) {
hideLine(i, j, i + 1, j);
hideLine(i, j, i, j + 1);
hideCircle(i, j);
hideGrid(i, j);
}
}
function hideGrid(x, y) {
var id = index(x, y);
if (grids[id] != null) {
grids[id].state = "hidden"
}
}
function showCircle(x, y, color) {
var id = index(x, y)
if (circles[id] != null) {
circles[id].color = color
circles[id].state = "shown"
}
}
function hideCircle(x, y) {
var id = index(x, y);
if (circles[id] != null) {
circles[id].state = "hidden"
}
}
function showLine(x1, y1, vertical, color) {
var id = index(x1, y1);
if (!vertical) {
if (horLines[id] != null) {
horLines[id].color = color;
horLines[id].state = "shown";
}
} else if (verLines[id] != null) {
verLines[id].color = color;
verLines[id].state = "shown";
}
}
function hideLine(x1, y1, vertical) {
var id = index(x1, y1);
if (!vertical) {
if (horLines[id] != null) {
horLines[id].state = "hidden";
}
} else if (verLines[id] != null) {
verLines[id].state = "hidden";
}
}
function changeGridColor(x, y, color) {
var id = index(x, y);
if (grids[id] != null) {
grids[id].color = color;
}
}
function ripple(x, y) {
var id = index(x, y);
if (grids[id] != null) {
grids[id].ripple();
}
}