0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Swift】じゃんけんの対話プログラム

Posted at

じゃんけんの対話プログラム

コードの基本的なスキルだけで、じゃんけんゲームを実装する。

動作環境

  • iPadアプリ「Swift Playgrounds」
  • 対話ブック

じゃんけんのロジック

// じゃんけんの仕組み
let hands = ["✊", "✌️", "🖐️"]
let randomNumber = Int.random(in: 0...2)
let enemyHand = hands[randomNumber]

// 勝敗を判定する
show("じゃんけん...")
pauseUntilTapped(message: "ぽん!")
show("コンピュータは\(enemyHand)を出しました。")

コードで勝負する

// じゃんけんの仕組み
let hands = ["✊", "✌️", "🖐️"]
let randomNumber = Int.random(in: 0...2)
let enemyHand = hands[randomNumber]

show("じゃんけん...")
let yourHand = askForChoice("何を出す?", strings: hands)

// 勝敗を判定する
pauseUntilTapped(message: "勝負する!")
show("コンピュータは\(enemyHand)を出しました。")

if yourHand == "✊" {
    if enemyHand == "✌️" {
        show("すごい!あなたの勝ち☀️")
    } else if enemyHand == "✊" {
        show("うーん、引き分け☁️")
    } else {
        show("残念、あなたの負け🌧️")
    }  
} 

if yourHand == "✌️" {
    if enemyHand == "🖐️" {
        show("すごい!あなたの勝ち☀️")
    } else if enemyHand == "✌️" {
        show("うーん、引き分け☁️")
    } else {
        show("残念、あなたの負け🌧️")
    }  
} 

if yourHand == "🖐️" {
    if enemyHand == "✊" {
        show("すごい!あなたの勝ち☀️")
    } else if enemyHand == "🖐️" {
        show("うーん、引き分け☁️")
    } else {
        show("残念、あなたの負け🌧️")
    }  
} 

相手を選ぶ

// 相手を選択する
show("相手を選んでください")
let enemies = ["🤖", "👻", "🧚", "😈"]
let enemy = askForChoice(strings: enemies)

if enemy == "🤖" {
    show("じゃんけんモードを開始しマス。")
} else if enemy == "👻" {
    show("おばけだぞ〜")
} else if enemy == "🧚" {
    show("じゃんけんしましょ♪")
} else {
    show("負けないぞ!")
}

// じゃんけんの仕組み
let hands = ["✊", "✌️", "🖐️"]
let randomNumber = Int.random(in: 0...2)
let enemyHand = hands[randomNumber]

show("じゃんけん...")
let yourHand = askForChoice("何を出す?", strings: hands)

// 勝敗を判定する
pauseUntilTapped(message: "勝負する!")
show("\(enemy)\(enemyHand)です。")

if yourHand == "✊" {
    if enemyHand == "✌️" {
        show("すごい!あなたの勝ち☀️")
    } else if enemyHand == "✊" {
        show("うーん、引き分け☁️")
    } else {
        show("残念、あなたの負け🌧️")
    }  
} 

if yourHand == "✌️" {
    if enemyHand == "🖐️" {
        show("すごい!あなたの勝ち☀️")
    } else if enemyHand == "✌️" {
        show("うーん、引き分け☁️")
    } else {
        show("残念、あなたの負け🌧️")
    }  
} 

if yourHand == "🖐️" {
    if enemyHand == "✊" {
        show("すごい!あなたの勝ち☀️")
    } else if enemyHand == "🖐️" {
        show("うーん、引き分け☁️")
    } else {
        show("残念、あなたの負け🌧️")
    }  
} 
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?