-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoard.qml
295 lines (254 loc) · 9.9 KB
/
Board.qml
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import QtQuick 2.9
import QtQuick.Controls 2.5
import QtQuick.Dialogs 1.2
Item {
id: board
//Wrong Choose dialog
Dialog {
id: wrongChoose
title: persian.checked ? "خطا" : "Error"
Text {
anchors.horizontalCenter: parent.horizontalCenter
id: errText
text: "An error!"
}
standardButtons: Dialog.Close
}
//Promotion dialog
PromotionDialog {
id: promotion
}
//Promotion control
Connections {
target: bknd
onPromotion: {
promotion.open()
}
}
//End of game dialog
EndOfGameDialog {
id: endOfGame
}
//Board image
Image {
id: boardImage
anchors.fill: parent
source: "qrc:/Assets/Images/Board.jpg"
//A grid of buttons for choosing move src
GridView {
id: src
//Not scrollable
interactive: false
anchors.centerIn: boardImage
//Ignoring edge of board photo
width: boardImage.width - boardImage.width * 0.096
height: boardImage.height - boardImage.height * 0.097
cellWidth: width / 8
cellHeight: height / 8
model: 64
//Content
delegate: Rectangle {
id: cellRec
width: src.width / 8
height: src.height / 8
border.width: bknd.isMoved(index) ? 3.4 : 0.5
border.color: bknd.isMoved(index) ? "#42e7ac" : "black"
color: "#00000000"
//Cells
Button {
id: srcCell
anchors.fill: parent
flat: true
onClicked: {
switch (bknd.choose(index)) {
//OK
case 2:
//highlight canGo and canHit
//Used "Connections" to prevent of crowdness here
break
//Unaccessable
case 1:
errText.text = persian.checked ? "نوبت این رنگ نیست!" : "It's not turn of this color!"
wrongChoose.open()
break
//Empty
case 0:
errText.text
= persian.checked ? "این خانه خالی است!" : "This tile is empty!"
wrongChoose.open()
break
}
}
//Highlight canGo and canHit cells on choose
Connections {
target: bknd
onChoosen: {
//Shows dest choose buttons
srcCell.visible = false
dest.visible = true
//Get destination cells state
switch (bknd.cellState(index)) {
//Is selected
case 3:
cellRec.color = "#bca972"
cellRec.border.color = "black"
cellRec.border.width = 0.5
break
//Can hit
case 2:
cellRec.color = "#811717"
cellRec.border.color = "black"
cellRec.border.width = 0.5
break
//Can go
case 1:
cellRec.color = "#a4883d"
cellRec.border.color = "black"
cellRec.border.width = 0.5
break
//Unavailable
case 0:
//Can set a color for unavailabe cells
}
}
}
//Unchoose the piece
Connections {
target: bknd
onUnchoosen: {
srcCell.visible = true
dest.visible = false
cellRec.color = "#00000000"
//Heighlight passed cells
cellRec.border.color = bknd.isMoved(
index) ? "#42e7ac" : "balck"
cellRec.border.width = bknd.isMoved(
index) ? 3.4 : 0.5
}
}
}
//Chessmen icons
Image {
id: chessmanIcon
source: bknd.getIcon(index)
width: parent.width * 0.95
height: parent.height * 0.95
//_____________________moving animations
OpacityAnimator {
id: destOpasity
target: chessmanIcon
duration: 400
from: 1
to: 0
}
ParallelAnimation {
id: anim
animations: [
NumberAnimation {
id: pieceXAnim
target: chessmanIcon
property: "x"
from: chessmanIcon.x
duration: 400
},
NumberAnimation {
id: pieceYAnim
target: chessmanIcon
property: "y"
from: chessmanIcon.y
duration: 400
}
]
onStopped: {
moveSound.play()
mystack.replace("GamePage.qml")
}
}
Connections {
target: bknd
onMoved: {
//____________________________________________________ unchoose
srcCell.visible = true
dest.visible = false
cellRec.color = "#00000000"
//Heighlight passed cells
cellRec.border.color = bknd.isMoved(
index) ? "#42e7ac" : "balck"
cellRec.border.width = bknd.isMoved(
index) ? 3.4 : 0.5
//___________________________________________________ animations
if (bknd.getDestIndex() === index)
destOpasity.start()
//did not word
// if (chessmanIcon.source === bknd.getHitPiece())
// mystack.pop()
if (bknd.getSrcIndex() === index) {
pieceXAnim.to = (bknd.getDestJ() - bknd.getSrcJ(
)) * cellRec.width
pieceYAnim.to = (bknd.getDestI() - bknd.getSrcI(
)) * cellRec.height
anim.start()
}
}
}
} //Image End
} //CellRec End
} //GridView End
//New grid buttons for move destination
GridView {
id: dest
//Not scrollable
interactive: false
visible: false
anchors.centerIn: boardImage
//Ignoring edges of board photo
width: boardImage.width - boardImage.width * 0.096
height: boardImage.height - boardImage.height * 0.097
cellWidth: this.width / 8
cellHeight: this.height / 8
model: 64
//Content
delegate: Rectangle {
color: "#00000000"
width: dest.width / 8
height: dest.height / 8
Button {
id: destCell
anchors.fill: parent
flat: true
onClicked: {
//If piece can go there
if (bknd.move(index)) {
} //If piece can't go to dest and dest is not its current cell
else if (!bknd.unchoosePiece(index)) {
errText.text
= persian.checked ? "نمیتوان به آنجا رفت!" : "Can't move there!"
wrongChoose.open()
}
}
}
}
}
}
Component.onCompleted: {
switch (bknd.gameStatus()) {
////NORMAL
case 0:
bknd.checkRandomMove()
break
//CHECKED
case 1:
errText.text = persian.checked ? "شما کیش شدید!" : "You're checked!"
wrongChoose.title = persian.checked ? "کیش" : "Checked"
wrongChoose.open()
bknd.checkRandomMove()
break
//STALEMATE
case 2:
//CHECKMATE
case 3:
endOfGame.open()
break
}
}
}