回答編集履歴

1

ソースコードを追記

2015/03/23 02:01

投稿

退会済みユーザー
test CHANGED
@@ -5,3 +5,149 @@
5
5
  アプリのチュートリアルサイト([リンク](http://codewithchris.com/how-to-make-an-iphone-app/))からソースコードをダウンロードして検証してみてはいかがでしょうか。
6
6
 
7
7
  ![イメージ説明][WIDTH:600](6f1c715dacce41990b8ac755c3791a35.jpeg)
8
+
9
+
10
+
11
+ ちなみに、ViewController.swiftのソースコードは下記のとおりです。
12
+
13
+ ```lang-Swift
14
+
15
+ import UIKit
16
+
17
+
18
+
19
+ class ViewController: UIViewController {
20
+
21
+
22
+
23
+ @IBOutlet weak var firstCardImageView: UIImageView!
24
+
25
+ @IBOutlet weak var secondCardImageView: UIImageView!
26
+
27
+ @IBOutlet weak var playRoundButton: UIButton!
28
+
29
+ @IBOutlet weak var backgroundImageView: UIImageView!
30
+
31
+
32
+
33
+ @IBOutlet weak var playerScore: UILabel!
34
+
35
+ @IBOutlet weak var enemyScore: UILabel!
36
+
37
+
38
+
39
+ var cardNamesArray:[String] = ["ace", "card2", "card3", "card4", "card5", "card6", "card7", "card8", "card9", "card10", "jack", "queen", "king"]
40
+
41
+
42
+
43
+ var playerScoreTotal = 0
44
+
45
+ var enemyScoreTotal = 0
46
+
47
+
48
+
49
+ override func viewDidLoad() {
50
+
51
+ super.viewDidLoad()
52
+
53
+ // Do any additional setup after loading the view, typically from a nib.
54
+
55
+
56
+
57
+ }
58
+
59
+
60
+
61
+ override func didReceiveMemoryWarning() {
62
+
63
+ super.didReceiveMemoryWarning()
64
+
65
+ // Dispose of any resources that can be recreated.
66
+
67
+ }
68
+
69
+
70
+
71
+ @IBAction func playRoundTapped(sender: UIButton) {
72
+
73
+
74
+
75
+ // Randomize a number for the first imageview
76
+
77
+ var firstRandomNumber:Int = Int(arc4random_uniform(13))
78
+
79
+
80
+
81
+ // Construct a string with the random number
82
+
83
+ var firstCardString:String = self.cardNamesArray[firstRandomNumber]
84
+
85
+
86
+
87
+ // Set the first card image view to the asset corresponding to the randomized number
88
+
89
+ self.firstCardImageView.image = UIImage(named: firstCardString)
90
+
91
+
92
+
93
+ // Randomize a number for the second imageview
94
+
95
+ var secondRandomNumber:Int = Int(arc4random_uniform(13))
96
+
97
+
98
+
99
+ // Construct a string with the random number
100
+
101
+ var secondCardString:String = self.cardNamesArray[secondRandomNumber]
102
+
103
+
104
+
105
+ // Set the second card image view to the asset corresponding to the randomized number
106
+
107
+ self.secondCardImageView.image = UIImage(named: secondCardString)
108
+
109
+
110
+
111
+ // Determine the higher card
112
+
113
+ if firstRandomNumber > secondRandomNumber {
114
+
115
+
116
+
117
+ // TODO: first card is larger
118
+
119
+ playerScoreTotal += 1
120
+
121
+ self.playerScore.text = String(playerScoreTotal)
122
+
123
+ }
124
+
125
+ else if firstRandomNumber == secondRandomNumber {
126
+
127
+
128
+
129
+ // TODO: numbers are equal
130
+
131
+ }
132
+
133
+ else {
134
+
135
+
136
+
137
+ // TODO: second card is larger
138
+
139
+ enemyScoreTotal += 1
140
+
141
+ self.enemyScore.text = String(enemyScoreTotal)
142
+
143
+ }
144
+
145
+ }
146
+
147
+
148
+
149
+ }
150
+
151
+
152
+
153
+ ```