切换石头剪刀

时间:2016-09-11 18:04:49

标签: swift switch-statement

我试图制作一个简单的石头剪刀游戏。为此,我使用了switch语句,但由于某种原因它无法正常工作。

这是我想要制作这个小游戏的结构:

Rock,Scissors,Paper有三个按钮,你必须选择一个。 有一个标签告诉对手(计算机)选择了什么,我把它命名为opponentLabel。 有一个标签可以告诉结果是什么("你赢了#34;例如)我把它命名为resultLabel

它就像这样(这就是它的结构方式):

var a = Int()
if the player chooses Rock ---> a = 0
if the player chooses Paper ---> a = 1
if the player chooses Scissors ---> a = 2

For the opponent (computer, not a person) there is a randomNumber which
could be 0,1,2, and same here, if 0->opponent chose rock, if 
1->opponent chose paper, if 2-> opponent chose scissors 

然后我写了一个把它放在一起的switch语句。 问题在于,出于某种原因,当我运行应用程序时,如果我选择摇滚一切正常,但是当我选择纸张或剪刀时结果是错误的。

例如,如果我选择Paper(a = 1)并且对手有纸(这意味着随机数恰好是randomNumber = 1),则resultLabel不是" DRAW"因为它应该是,但它是"你失去了":纸和剪刀不工作!我究竟做错了什么? 这是完整的代码:

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var opponentLabel: UILabel!
@IBOutlet weak var resultLabel: UILabel!
@IBOutlet weak var rockButton: UIButton!
@IBOutlet weak var paperButton: UIButton!
@IBOutlet weak var scissorsButton: UIButton!






override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    Hide()


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



func Hide() {
    opponentLabel.hidden = true
    resultLabel.hidden = true
}

func unHide() {
    opponentLabel.hidden = false
    resultLabel.hidden = false
}


var a = Int()

var randomNumber = Int()

func randomChoice() {

    randomNumber = Int(arc4random() % 3)
    NSLog("randomNumber%ld", randomNumber)



}



func gameOn() {

    switch(randomNumber) { 

    case 0:
        opponentLabel.text = "The opponent chose : ROCK"
        if a == 0 {
            resultLabel.text = "DRAW"
        } else {
            if a == 1 {
                resultLabel.text = "YOU WON!"
            }
            if a == 2 {
                resultLabel.text = "YOU LOST!"
                }
        }
        unHide()
        break

    case 1:
        opponentLabel.text = "The opponent chose: PAPER"
        if a == 0 {
            resultLabel.text = "YOU LOST!"
        } else {
            if a == 1 {
                resultLabel.text = "DRAW"
            }
            if a == 2 {
                resultLabel.text = "YOU WON!"
            }
        }
        unHide()
        break

    case 2:
        opponentLabel.text = "The opponent chose: SCISSORS"
        if a == 0 {
            resultLabel.text = "YOU WON!"
        } else {
            if a == 1 {
                resultLabel.text = "YOU LOST!"
            }
            if a == 2 {
                resultLabel.text = "DRAW"
            }
        }
        unHide()
        break

    default:
        break


    }
}


@IBAction func rockButton(sender: AnyObject) {

    a == 0
    randomChoice()
    gameOn()

}


@IBAction func paperButton(sender: AnyObject) {

    a == 1
    randomChoice()
    gameOn()
}



@IBAction func scissorsButton(sender: AnyObject) {

    a == 2
    randomChoice()
    gameOn()

}



}

1 个答案:

答案 0 :(得分:0)

在您的@IBAction功能中,您正在比较' a'用0,1或2代替给出' a'那个价值。

更改代码而不是== 0,它是a = 0.为3 @IBActions执行此操作并再次尝试,它应该可以正常工作。