回答編集履歴
1
修正
test
CHANGED
@@ -13,3 +13,221 @@
|
|
13
13
|
上記のコードを削除して、数回試したところ数がオーバーしてもゲームが続けられるという事はありませんでした。
|
14
14
|
|
15
15
|
確認してみてください。
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
---
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
コードの中に同じ様な記述が複数あったのでまとめてみました、参考にしてみてください。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
```swift
|
28
|
+
|
29
|
+
import UIKit
|
30
|
+
|
31
|
+
import SpriteKit
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
class ViewController: UIViewController {
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
@IBOutlet weak var start: UIButton!
|
40
|
+
|
41
|
+
@IBOutlet weak var one : UIButton!
|
42
|
+
|
43
|
+
@IBOutlet weak var two : UIButton!
|
44
|
+
|
45
|
+
@IBOutlet weak var three: UIButton!
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
@IBOutlet weak var kekka : UILabel!
|
50
|
+
|
51
|
+
@IBOutlet weak var agote : UILabel!
|
52
|
+
|
53
|
+
@IBOutlet weak var konkai: UILabel!
|
54
|
+
|
55
|
+
@IBOutlet weak var serifu: UILabel!
|
56
|
+
|
57
|
+
@IBOutlet weak var make : UILabel!
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
var teto = 0
|
62
|
+
|
63
|
+
var X = 0
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
//開始ボタン
|
68
|
+
|
69
|
+
@IBAction func starta(sender: AnyObject) {
|
70
|
+
|
71
|
+
teto = 0
|
72
|
+
|
73
|
+
[make].forEach { $0.hidden = true }
|
74
|
+
|
75
|
+
[one, two, three, agote, kekka, serifu, konkai].forEach { $0.hidden = false }
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
X = Int(arc4random_uniform(20))+10
|
80
|
+
|
81
|
+
if X % 4 == 1{
|
82
|
+
|
83
|
+
return
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
self.start.hidden = true
|
90
|
+
|
91
|
+
konkai.text = String(X)
|
92
|
+
|
93
|
+
if X % 4 == 0 {
|
94
|
+
|
95
|
+
teto = 3
|
96
|
+
|
97
|
+
} else {
|
98
|
+
|
99
|
+
teto = (X-4)%4-1
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
agote.text = String(teto)
|
104
|
+
|
105
|
+
kekka.text = String(teto)
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
// 1ボタン
|
112
|
+
|
113
|
+
@IBAction func onea(sender: AnyObject) {
|
114
|
+
|
115
|
+
setTeto(player1Teto: 1, player2Teto: 3)
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
// 2ボタン
|
122
|
+
|
123
|
+
@IBAction func twoa(sender: AnyObject) {
|
124
|
+
|
125
|
+
setTeto(player1Teto: 2, player2Teto: 2)
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
// 3ボタン
|
132
|
+
|
133
|
+
@IBAction func threea(sender: AnyObject) {
|
134
|
+
|
135
|
+
setTeto(player1Teto: 3, player2Teto: 1)
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
func setTeto(player1Teto teto1: Int, player2Teto teto2: Int) {
|
142
|
+
|
143
|
+
setPlayer1(teto1)
|
144
|
+
|
145
|
+
if X <= teto {
|
146
|
+
|
147
|
+
gemeOver()
|
148
|
+
|
149
|
+
} else {
|
150
|
+
|
151
|
+
setPlayer2(teto2)
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
func setPlayer1(player1Teto: Int) {
|
160
|
+
|
161
|
+
teto += player1Teto
|
162
|
+
|
163
|
+
kekka.text = String(teto)
|
164
|
+
|
165
|
+
serifu.text = "考え中"
|
166
|
+
|
167
|
+
[one, two, three].forEach { $0.hidden = true }
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
func setPlayer2(player2Teto: Int) {
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
// 3秒後にメインスレッドで実行
|
178
|
+
|
179
|
+
dispatch_async_main_after(3) {
|
180
|
+
|
181
|
+
self.teto += player2Teto
|
182
|
+
|
183
|
+
self.kekka.text = String(self.teto)
|
184
|
+
|
185
|
+
self.agote.text = String(player2Teto)
|
186
|
+
|
187
|
+
self.serifu.text = "君の番だよ"
|
188
|
+
|
189
|
+
[self.one, self.two, self.three].forEach { $0.hidden = false }
|
190
|
+
|
191
|
+
if self.teto > self.X - 1 {
|
192
|
+
|
193
|
+
[self.two, self.three].forEach { $0.hidden = true }
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
if self.teto > self.X - 2 {
|
198
|
+
|
199
|
+
self.three.hidden = true
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
func gemeOver() {
|
210
|
+
|
211
|
+
make.text = "You have lose"
|
212
|
+
|
213
|
+
[make, start].forEach { $0.hidden = false }
|
214
|
+
|
215
|
+
[one, two, three, agote, kekka, serifu, konkai].forEach { $0.hidden = true }
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
// メインスレッドで遅延実行
|
224
|
+
|
225
|
+
func dispatch_async_main_after(minute: Double, block: () -> ()) {
|
226
|
+
|
227
|
+
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(minute * Double(NSEC_PER_SEC)))
|
228
|
+
|
229
|
+
dispatch_after(delayTime, dispatch_get_main_queue(), block)
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
```
|