-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.qml
234 lines (150 loc) · 6.98 KB
/
main.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
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
ApplicationWindow {
visible: true
title: qsTr("Noughts and Crosses (2 players mode)")
width: 400
height: 400
minimumWidth: width
minimumHeight: height
maximumWidth: minimumWidth
maximumHeight: minimumHeight
StackView {
id: stack
anchors.fill: parent
initialItem: startPage
End {
id: endPage
onClicked: {
// Go back to start page
stack.pop()
stack.pop()
}
}
Field {
id: gameField
onClicked: {
// Game is not ended
if (gameField.done === false) {
// Free cell clicked
if (gameEngine.cellClicked(x, y)) {
// Player 1 is Noughts?
if (gameEngine.getDeterminant()) {
// Clicked by Player 1 - insert cross
// Clicked by Player 2 - insert nought
(gameEngine.getPlayer() === 1) ? gameField.getCell(x, y).setCross() :
gameField.getCell(x, y).setNought()
} else {
// Clicked by Player 1 - insert nought
// Clicked by Player 2 - insert cross
(gameEngine.getPlayer() === 1) ? gameField.getCell(x, y).setNought() :
gameField.getCell(x, y).setCross()
}
// Step count now > 4?
// (if so - there might be winner)
if (gameEngine.getStep() > 4) {
// If there is winning line
if (gameEngine.checkResult()) {
// Check whose line is this and
// change Result page color and text
// If Player 1
if (gameEngine.getPlayer() === 1) {
// Does he Noughts or Crosses?
gameEngine.getDeterminant() ? endPage.crossWin() :
endPage.noughtWin()
// Else Player 2
} else {
// Same but Player 2
gameEngine.getDeterminant() ? endPage.noughtWin() :
endPage.crossWin()
}
// Visual effects
for (var i = 0; i < 9; ++i) {
// Highlight winning line cells
if (gameEngine.getWinData(0) === i ||
gameEngine.getWinData(1) === i ||
gameEngine.getWinData(2) === i) {
gameField.highlightCell(i)
// And disable other
} else {
gameField.disableCell(i)
}
}
// Game ended
gameField.done = true;
} else {
// If all cells are marked
// and we still have no winner
if (gameEngine.getStep() === 9) {
// Disable them all
for (var k = 0; k < 9; ++k) {
gameField.disableCell(k)
}
// Show result page with "DRAW" message
endPage.draw()
// Game ended
gameField.done = true;
}
}
}
// If already marked cell was clicked
} else {
// Player 1 Noughts or Crosses?
if (gameEngine.getDeterminant()) {
// If Nought in cell - green color
(gameEngine.getData(x + y * 3) === 1) ? gameField.hintColor = "#0F0" :
gameField.hintColor = "#F00"
} else {
// Else red color
(gameEngine.getData(x + y * 3) === 1) ? gameField.hintColor = "#F00" :
gameField.hintColor = "#0F0"
}
// If clicked cell is already players cell
if (gameEngine.getData(x + y * 3) === gameEngine.getPlayer()) {
gameField.hintText = "This cell is<br>ALREADY<br>your!"
// If enemies cell
} else {
gameField.hintText = "This cell is<br>NOT<br>your!"
}
// Show hint
gameField.hintVisibility = true
}
} else {
// Clear game data
gameField.done = false
// Clear field
gameEngine.refreshField()
// Clear marks
gameField.clear()
// Show reusl page
stack.push(endPage)
}
}
// Hint clicked
onHintClicked: {
// Dismiss hint
gameField.hintVisibility = false
}
}
Start {
id: startPage
// Clicked Noughts cell
onNoughtClicked: {
// Player 1 is now Noughts
// Player 2 - Crosses
gameEngine.setDeterminant(true);
// Show game field
stack.push(gameField)
}
// Clicked cross cell
onCrossClicked: {
// Player 1 is now Crosses
// Player 2 - Noughts
gameEngine.setDeterminant(false);
// Show game field
stack.push(gameField)
}
}
}
}