初めてのiPhoneアプリ開発①

macの使い方もいまいちわからないまま、本を片手にiPhoneアプリ開発をしてみました。
本を一通り読み終えたので、自分なりに試行錯誤して復習してみました。
何を作るのか?
本で学んだ知識を復習するのが目的であり、ちょっと楽しみたい?ということで、簡単なゲームを作ってみたいと考えました。
今回作ったのは「○×ゲーム」です。短時間でサクッと作れるかなーと思ったのが発端です。
構想
- 盤面はボタンで作り(力技)、ボタンを押された時に、盤面を保存している多次元配列を操作する。
- 操作後、必要なボタンのみ表示を変える。
- 最後に、多次元配列から勝敗を判定する。
ボタンの作成とか、勝敗判定とか何も考えずにゴリゴリ力技でソースを書いています。恥ずかしいソースの書き方です。
実際のソース等
こんな感じで作っていきました。ボタンの作り方が強引だと思います。
実際のソースは↓
class ViewController: UIViewController { var map :[[Int]] = [[0,0,0], [0, 0, 0], [0, 0, 0]] var turn = 1 @IBOutlet weak var mesLabel: UILabel! @IBAction func btn11(_ sender: UIButton) { gameMan(x: 1,y:1, sender: sender) } @IBAction func btn12(_ sender: UIButton) { gameMan(x: 1,y: 2, sender: sender) } @IBAction func btn13(_ sender: UIButton) { gameMan(x: 1,y: 3, sender: sender) } @IBAction func btn21(_ sender: UIButton) { gameMan(x: 2,y: 1, sender: sender) } @IBAction func btn22(_ sender: UIButton) { gameMan(x: 2,y: 2, sender: sender) } @IBAction func btn23(_ sender: UIButton) { gameMan(x: 2,y: 3, sender: sender) } @IBAction func btn31(_ sender: UIButton) { gameMan(x: 3,y: 1, sender: sender) } @IBAction func btn32(_ sender: UIButton) { gameMan(x: 3,y: 2, sender: sender) } @IBAction func btn33(_ sender: UIButton) { gameMan(x: 3,y: 3, sender: sender) } func gameMan(x:Int, y:Int, sender e:UIButton) { if map[x-1][y-1]==0 { //盤に置ける場合 map[x-1][y-1]=turn if turn==1 { e.setTitle ("○",for: .normal) turn=10 } else { e.setTitle ("×",for: .normal) turn=1 } } //メッセージ表示 switch turn { case 0: mesLabel.text = "○のターン!" case 1: mesLabel.text = "○のターン!" case 10: mesLabel.text = "×のターン!" default: break } //勝敗判定 if map[0][0] + map[0][1] + map[0][2] == 3 { mesLabel.text = "○の勝ち!" } if map[1][0] + map[1][1] + map[1][2] == 3 { mesLabel.text = "○の勝ち!" } if map[2][0] + map[2][1] + map[2][2] == 3 { mesLabel.text = "○の勝ち!" } if map[0][0] + map[1][1] + map[2][2] == 3 { mesLabel.text = "○の勝ち!" } if map[2][0] + map[1][1] + map[0][2] == 3 { mesLabel.text = "○の勝ち!" } if map[0][0] + map[0][1] + map[0][2] == 30 { mesLabel.text = "×の勝ち!" } if map[1][0] + map[1][1] + map[1][2] == 30 { mesLabel.text = "×の勝ち!" } if map[2][0] + map[2][1] + map[2][2] == 30 { mesLabel.text = "×の勝ち!" } if map[0][0] + map[1][1] + map[2][2] == 30 { mesLabel.text = "×の勝ち!" } if map[2][0] + map[1][1] + map[0][2] == 30 { mesLabel.text = "×の勝ち!" } } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
まとめ
ここから色々なアプリを作りながらレベルアップしていきたいです。まずは、スタートラインに立ったということでやっていきます!
ディスカッション
コメント一覧
まだ、コメントがありません