2016-09-11 33 views
-2

私はプログラミングが初めてで、私は現在Swiftを勉強しようとしています。私はシンプルなロックペーパーのはさみゲームを作ろうとしています。これを行うにはswitch文を使用しましたが、何らかの理由で正しく動作しません。ロックペーパー - はさみアプリ(スイフト)?

ありロック、はさみのための3つのボタン、紙があり、そしてあなたがいずれかを選択する必要があります。

これは私がこの小さなゲームを作ることを考えた構造です。 相手(コンピュータ)が何を選んだのかを示すラベルがあり、それをopponentLabelという名前にしました。

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 

と: は、(例えば、「あなたは勝った」)と私はそれが

をresultLabel名前が付けられ、それがこのように動作します(これだけの方法、それは構造化されたのですが)、結果が何であるかを伝えるラベルがあります私はswitch文を書いてそれをまとめました。 問題は何らかの理由で、私はアプリを実行するとき、私はすべてがうまく動作するロックを選択したが、私は紙やはさみを選択すると結果が間違っています。

例えば、Paper(a = 1)を選択して相手が紙を持っている場合(randomNumber = 1ということを意味する)、resultLabelは想定されるように "DRAW"それは "あなたが失われた"です:紙とはさみはうまくいきません!私は間違って何をしていますか?ここで がフルコードである:あなたの@IBAction機能で

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() 

} 



} 
+0

デフォルトのケースを除いてbreakステートメントを避ける必要があります(アクションがないため) – FredericP

答えて

0

、あなたは「」0、1または2ではなく、「」その値を与えて比較しています。

== 0の代わりにコードを変更すると、a = 0になります。3つの@IBActionsに対してこれを行い、もう一度やり直すとうまくいくはずです。

+0

ありがとう@Aekon !!それは働いて、また、私は今何か新しいことを学んだ! – tommsyeah

関連する問題