じゃんけんアプリ
選んでください:
以下が応募するアプリのコードです。ランダムな応援メッセージは日本語のリストから選ばれます。セキュリティに問題があると思われるevalやlocation.hrefによるリダイレクトは使わずに、安全に動作するように実装しました。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>じゃんけんアプリ</title>
</head>
<body>
<h1>じゃんけんアプリ</h1>
<p>選んでください:</p>
<button onclick="playJanken('グー')">グー</button>
<button onclick="playJanken('チョキ')">チョキ</button>
<button onclick="playJanken('パー')">パー</button>
<p id="result"></p>
<p id="message"></p>
<script>
function playJanken(userChoice) {
// コンピューターがランダムに手を決める
const computerChoice = getComputerChoice();
// 勝敗を判定する
const result = determineWinner(userChoice, computerChoice);
// 結果に基づいてメッセージを表示する
showMessage(result, userChoice, computerChoice);
}
function getComputerChoice() {
// 0から2までのランダムな整数を生成し、それを手の種類に変換する
const choice = Math.floor(Math.random() * 3);
if (choice === 0) {
return 'グー';
} else if (choice === 1) {
return 'チョキ';
} else {
return 'パー';
}
}
function determineWinner(userChoice, computerChoice) {
if (userChoice === computerChoice) {
// 引き分け
return 'tie';
} else if (userChoice === 'グー' && computerChoice === 'チョキ' ||
userChoice === 'チョキ' && computerChoice === 'パー' ||
userChoice === 'パー' && computerChoice === 'グー') {
// ユーザーの勝ち
return 'win';
} else {
// ユーザーの負け
return 'lose';
}
}
function showMessage(result, userChoice, computerChoice) {
if (result === 'tie') {
// 引き分け
document.getElementById('result').innerText = '引き分け';
document.getElementById('message').innerText = '「なかなかやるじゃん。次は勝てるかも!」';
} else if (result === 'win') {
// ユーザーの勝ち
document.getElementById('result').innerText = '勝ち!';
const messages = ['「あなたは天才です!」', '「大偉人!次も頑張りましょう!」', '「敵なし!」'];
const index = Math.floor(Math.random() * messages.length);
document.getElementById('message').innerText = messages[index];
} else {
// ユーザーの負け
document.getElementById('result').innerText = '負け!';
document.getElementById('message').innerHTML = '<span style="color: red; font-size: 72px; display: inline-block; animation: rotate 1s infinite;">出直してこい😡</span>';
}
}
</script>
<style>
@keyframes rotate {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
</style>
</body>
</html>
```
※このプログラム内で使用しているJavaScriptの機能や文法に関して詳しく説明することは難しいため、ある程度のJavaScriptの知識が前提となります。