티스토리 뷰

알고리즘

iOS 가위바위보 어플

이온시옥 2018. 12. 8. 21:12
반응형

//

//  ViewController.swift

//  My Application

//

//  Created by Eon on 03/12/2018.

//  Copyright © 2018 EonseokYim. All rights reserved.

//


import UIKit

import GameplayKit


class ViewController: UIViewController {


    let randomSource = GKARC4RandomSource()

    

    @IBOutlet weak var computerImageView: UIImageView!

    @IBOutlet weak var playerImageView: UIImageView!

    @IBOutlet weak var messageLabel: UILabel!

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        /*

        let label = UILabel(frame: CGRect(x: 30, y: 30, width: 100, height: 24))

        label.textAlignment = .center

        label.textColor = UIColor.red

        label.text = "Hello iOS ...!"

        

        self.view.addSubview(label)

        */

        let angle:CGFloat = CGFloat((180.0 * Double.pi) / 180.0)

        computerImageView.transform = CGAffineTransform(rotationAngle: angle)

    }


    @IBAction func tapStart() {

        computerImageView.image = UIImage(named: "rock.png")

        playerImageView.image = UIImage(named: "rock.png")

        messageLabel.text = "가위 바위 ..."

        

    }

    @IBAction func tapScissors() {

        playerImageView.image = UIImage(named: "scissors.png")

        doComputer(0)

    }

    @IBAction func tapRock() {

        playerImageView.image = UIImage(named: "rock.png")

        doComputer(1)


    }

    @IBAction func tapPaper() {

        playerImageView.image = UIImage(named: "paper.png")

        doComputer(2)


    }

    

    func doComputer( _ player:Int) {

        let computer = randomSource.nextInt(upperBound: 3)

        var msg = ""

        switch computer {

        case 0: //가위

            computerImageView.image = UIImage(named: "scissors.png")

            if player == 0 {

               msg = "비김"

            }

            else if player == 1 {

                msg = "승!!"

            }

            else if player == 2 {

                msg = "패ㅠㅠ"

            }

        case 1: //바위

            computerImageView.image = UIImage(named: "rock.png")

            if player == 0 {

                msg = "패ㅠㅠ"

            }

            else if player == 1 {

                msg = "비김"

            }

            else if player == 2 {

                msg = "승!!"

            }

        case 2: //보

            computerImageView.image = UIImage(named: "paper.png")

            if player == 0 {

                msg = "승!!"

            }

            else if player == 1 {

                msg = "패ㅠㅠ"

            }

            else if player == 2 {

                msg = "비김"

            }

        default:

            break

        }

        messageLabel.text = msg

    }

    


}


반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
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
글 보관함