LoginSignup
0
0

More than 3 years have passed since last update.

パソコンとジャンケンゲーム

Last updated at Posted at 2021-02-20

//じゃんけんの手の番号を設定
const GU = 1;
const CHOKI = 2;
const PA = 3;

let msgStr = '半角数字で1~3の数字を入力してください。\n\n';
msgStr += GU + ':グー\n';
msgStr += CHOKI + ':チョキ\n';
msgStr += PA + ':パー';
let hum = prompt(msgStr);
hum =parseInt(hum);
//入力値のチェック
if(hum !== GU && hum !== CHOKI && hum !== PA) {
alert('入力値をうまく認識できませんでした。ブラウザを再読み込みすると、もう一度挑戦できます。')
} else {
//正しい入力値の場合、コンピューターの手を作る
let com;
com = Math.random() * 3;
com = Math.floor(com) + 1;
//コンピューターの名前
let comHandName = '';
switch (com) {
case GU:
comHandName = 'グー';
break;
case CHOKI:
comHandName = 'チョキ';
break;
case PA:
comHandName = 'パー';
break;
}
//結果の判定
let msgResult = '';
if(hum == com){
msgResult = '結果はあいこでした';
} else {
if((com == GU && hum == PA) || (com == CHOKI && hum == GU) || (com == PA && hum == CHOKI)){
msgResult ='あなたの勝ちです';
} else {
msgResult = 'あなたの負けです';
}
}
//最終的な結果表示
msgResult += 'コンピューターの出した手は「' + comHandName + '」でした。';
alert(msgResult);
}

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