じゃんけんの対話プログラム
コードの基本的なスキルだけで、じゃんけんゲームを実装する。
動作環境
- 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("残念、あなたの負け🌧️")
}
}