1
1

More than 1 year has passed since last update.

JavaScriptでCPU対戦できる乱数ゲーム作った

Posted at

ヤッツィーというアメリカ発のダイスゲームを参考に、より簡単に楽しめるものをJavaScriptで作りました。(お手軽ヤッツィーとか呼んでます。)

htmlのbodyにそのまま突っ込んでデベロッパーツールで遊んでます。

ボーナス計算の処理や、CPUがダイスを振り直すか振り直さないかの判断、またどれを振り直すかの処理など凝って作りました。

ご意見等お待ちしております!

以下、ルールです。

image.png

            let a, b, c, i, r, score, bonus, cpuscore, cpubonus, ss, ts=0, cputs=0;
            let min, max;
            
            for(i=1;i<=3;i++){
                console.log("%d回目の挑戦\n\n", i);
                a = getRandomInt(1, 4);
                b = getRandomInt(1, 4);
                c = getRandomInt(1, 4);
                console.log("%d %d %d\n",a ,b, c);

                r = prompt('振り直しますか?,0~3');
                console.log(`${r}`);
                rerollDice(r);
                score = a + b + c;
                bonuscalc(a, b, c);
                ss = score + bonus;
                console.log("今回のスコアは%d点、ボーナスは%d点だったので、\nセクションスコアは%d点です!\n\n", score, bonus, ss);
                ts += ss;//player

                console.log("CPUの%d回目の挑戦\n\n", i);
                a = getRandomInt(1, 4);
                b = getRandomInt(1, 4);
                c = getRandomInt(1, 4);
                console.log("%d %d %d\n",a ,b, c);
                cpunoban();

                score = a + b + c;
                ss = score + bonus;
                console.log("今回のスコアは%d点、ボーナスは%d点だったので、\nセクションスコアは%d点です!\n\n", score, bonus, ss);
                cputs += ss;
            }
            console.log("3回の挑戦を終えました。結果は・・・\n\nあなたの得点は%d点、\nCPUの得点は%d点です!\n遊んでくれてありがとう!", ts, cputs);
            
            function getRandomInt(min, max) {
                min = Math.ceil(min);
                max = Math.floor(max);
                return Math.floor(Math.random() * ((max) - min) + min); 
                //The maximum is exclusive and the minimum is inclusive
            }
            function cpurrd(q){
                if(a==q){
				     a = getRandomInt(1, 4);
			    }
		        if(b==q){
			        b = getRandomInt(1, 4);
		        }
		        if(c==q){
			        c = getRandomInt(1, 4);
		        }
		        console.log("振り直した結果・・・%d %d %d\n\n", a, b ,c);
                bonuscalc(a, b, c);
            }
            function rerollDice(r){//ダイスを振り直す処理
                if(r==0){
                    console.log("あなたはダイスを振り直しませんでした。\n")
                    console.log("%d %d %d\n",a ,b, c);
                }else if(r==1){
                    a = getRandomInt(1, 4);
                    console.log("あなたは左のダイスを振り直します。\n");
                    console.log("振りなおした結果・・・%d %d %d\n", a, b, c);
                }else if(r==2){
                    b = getRandomInt(1, 4);
                    console.log("あなたは中央のダイスを振り直します。\n");
                    console.log("振りなおした結果・・・%d %d %d\n", a, b, c);
                }else if(r==3){
                    c = getRandomInt(1, 4);
                    console.log("あなたは右のダイスを振り直します。\n");
                    console.log("振りなおした結果・・・%d %d %d\n", a, b, c);
                }
            }
            function bonuscalc(a, b, c){//ボーナス計算
                if(a==b && b==c){
                    bonus = 10;
	            	console.log("ボーナス!!!ヤッツィー!!!+10点獲得!!!\n");
                }else if(a!=2 && b!=2 && c!=2){
                    bonus = 3;
			        console.log("ボーナス!リゴール!+3点獲得!\n");
                }else if(!(a==b || b==c || c==a)){
                    bonus = 5;
	            	console.log("ボーナス!!ストレート!!+5点獲得!!\n");
                }else{
                    bonus = 0;
                    console.log("ボーナスは得られませんでした。");
                }
                return bonus;
            }
            function cpunoban(){
                if(a==b && b===c && c==a){//ヤッツィー
                    console.log("CPUは振り直しませんでした。\n\n");
                    bonuscalc(a, b, c);
                }else if(a!=b && b!=c && c!=a){//ストレート
                    console.log("CPUは振り直しませんでした。\n\n");
                    bonuscalc(a, b, c);
                }else if(a!=2 && b!=2 && c!=2){//リゴール
                    console.log("CPUは振り直しませんでした。\n\n");
                    bonuscalc(a, b, c);
                }else if(a+b+c==7){//2,2,3の場合。else ifなので、1,3,3は除外。
			        console.log("CPU は3が出ている目を振り直します。\n\n");
			        cpurrd(3);
		        }else if(a+b+c==5){//2,2,1の場合。else ifなので1,1,3は除外。
			        console.log("CPU は1が出ている目を振り直します。\n\n");
			        cpurrd(1);
		        }else if(a+b+c==4){//1,1,2の場合。
		        	console.log("CPU は2が出ている目を振り直します。\n\n");
			        cpurrd(2);
		        }else if(a+b+c==8){//3,3,2の場合。
		        	console.log("CPU は2が出ている目を振り直します。\n\n");
			        cpurrd(2);
                }
            }            let a, b, c, i, r, score, bonus, cpuscore, cpubonus, ss, ts=0, cputs=0;
            let min, max;
            
            for(i=1;i<=3;i++){
                console.log("%d回目の挑戦\n\n", i);
                a = getRandomInt(1, 4);
                b = getRandomInt(1, 4);
                c = getRandomInt(1, 4);
                console.log("%d %d %d\n",a ,b, c);

                r = prompt('振り直しますか?,0~3');
                console.log(`${r}`);
                rerollDice(r);
                score = a + b + c;
                bonuscalc(a, b, c);
                ss = score + bonus;
                console.log("今回のスコアは%d点、ボーナスは%d点だったので、\nセクションスコアは%d点です!\n\n", score, bonus, ss);
                ts += ss;//player

                console.log("CPUの%d回目の挑戦\n\n", i);
                a = getRandomInt(1, 4);
                b = getRandomInt(1, 4);
                c = getRandomInt(1, 4);
                console.log("%d %d %d\n",a ,b, c);
                cputurn();

                score = a + b + c;
                ss = score + bonus;
                console.log("今回のスコアは%d点、ボーナスは%d点だったので、\nセクションスコアは%d点です!\n\n", score, bonus, ss);
                cputs += ss;
            }
            console.log("3回の挑戦を終えました。結果は・・・\n\nあなたの得点は%d点、\nCPUの得点は%d点です!\n遊んでくれてありがとう!", ts, cputs);
            
            function getRandomInt(min, max) {
                min = Math.ceil(min);
                max = Math.floor(max);
                return Math.floor(Math.random() * ((max) - min) + min); 
                //The maximum is exclusive and the minimum is inclusive
            }
            function cpurrd(q){
                if(a==q){
				     a = getRandomInt(1, 4);
			    }
		        if(b==q){
			        b = getRandomInt(1, 4);
		        }
		        if(c==q){
			        c = getRandomInt(1, 4);
		        }
		        console.log("振り直した結果・・・%d %d %d\n\n", a, b ,c);
                bonuscalc(a, b, c);
            }
            function rerollDice(r){//ダイスを振り直す処理
                if(r==0){
                    console.log("あなたはダイスを振り直しませんでした。\n")
                    console.log("%d %d %d\n",a ,b, c);
                }else if(r==1){
                    a = getRandomInt(1, 4);
                    console.log("あなたは左のダイスを振り直します。\n");
                    console.log("振りなおした結果・・・%d %d %d\n", a, b, c);
                }else if(r==2){
                    b = getRandomInt(1, 4);
                    console.log("あなたは中央のダイスを振り直します。\n");
                    console.log("振りなおした結果・・・%d %d %d\n", a, b, c);
                }else if(r==3){
                    c = getRandomInt(1, 4);
                    console.log("あなたは右のダイスを振り直します。\n");
                    console.log("振りなおした結果・・・%d %d %d\n", a, b, c);
                }
            }
            function bonuscalc(a, b, c){//ボーナス計算
                if(a==b && b==c){
                    bonus = 10;
	            	console.log("ボーナス!!!ヤッツィー!!!+10点獲得!!!\n");
                }else if(a!=2 && b!=2 && c!=2){
                    bonus = 3;
			        console.log("ボーナス!リゴール!+3点獲得!\n");
                }else if(!(a==b || b==c || c==a)){
                    bonus = 5;
	            	console.log("ボーナス!!ストレート!!+5点獲得!!\n");
                }else{
                    bonus = 0;
                    console.log("ボーナスは得られませんでした。");
                }
                return bonus;
            }
            function cputurn(){
                if(a==b && b===c && c==a){//ヤッツィー
                    console.log("CPUは振り直しませんでした。\n\n");
                    bonuscalc(a, b, c);
                }else if(a!=b && b!=c && c!=a){//ストレート
                    console.log("CPUは振り直しませんでした。\n\n");
                    bonuscalc(a, b, c);
                }else if(a!=2 && b!=2 && c!=2){//リゴール
                    console.log("CPUは振り直しませんでした。\n\n");
                    bonuscalc(a, b, c);
                }else if(a+b+c==7){//2,2,3の場合。else ifなので、1,3,3は除外。
			        console.log("CPU は3が出ている目を振り直します。\n\n");
			        cpurrd(3);
		        }else if(a+b+c==5){//2,2,1の場合。else ifなので1,1,3は除外。
			        console.log("CPU は1が出ている目を振り直します。\n\n");
			        cpurrd(1);
		        }else if(a+b+c==4){//1,1,2の場合。
		        	console.log("CPU は2が出ている目を振り直します。\n\n");
			        cpurrd(2);
		        }else if(a+b+c==8){//3,3,2の場合。
		        	console.log("CPU は2が出ている目を振り直します。\n\n");
			        cpurrd(2);
                }
            }
1
1
2

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
1
1