JS-21點遊戲

//21點遊戲
let readline=require("readline-sync");
//清屏函數
let clear=()=>process.stdout.write(process.platform==='win32'?'\x1Bc':'\x1B[2J\x1B[3J\x1B[H');
//抽排函數 接收一個參數 玩家對象
let takeCard=function(player){
let point=Math.floor(Math.random()*10+1);//隨機1-10的個數
console.log(`你抽到的點數是:${point}`);
player.titlePoint+=point;//改變玩家的總點數
}
//結果比較
let judeg=function(player1,player2){
if(player1.titlePoint>player2.titlePoint){
return 1;
}else if(player1.titlePoint<player2.titlePoint){
return 2;
}else{
return 0;
}
}
//遊戲內容
let main=function(){
let useSys=true;//是否使用系統 默認值true表明一直使用
while (useSys){
clear();
console.log("歡迎來到21點遊戲");
console.log("請選擇遊戲選項: 1.單人模式 2.雙人模式 3.遊戲說明 4.退出遊戲");
let funcSelect=parseInt(readline.question(""));
switch (funcSelect){
//單人
case 1:
{
//初始化兩個對象,一個玩家一個電腦
let player={victory:0,titlePoint:0};
let computer={victory:0,titlePoint:0};
let continuePlay=true;//是否繼續單人模式
while(continuePlay){
let isTakeCard=true;//玩家是否有資格抽取下一張牌 默認true 有資格抽取
//抽牌循環
while (isTakeCard){
clear();
console.log(`當前的總點數爲:${player.titlePoint}`);
console.log("是否抽取?(Y/N)");
let isTake=readline.question("");
switch(isTake){
case "Y":
case "y":
takeCard(player);//誒次抽排都要判斷是否超過21點,超過直接失敗
//將isTakeCard值改成fales
if(player.titlePoint>21){
console.log("失敗!");
readline.question("");
isTakeCard=false;
break;
}
console.log("\n按回車鍵繼續");
readline.question("");
break;
case "N":
case "n":
isTakeCard=false;
break;
default:
console.log("您輸入的有誤,請從新輸入");
readline.question("");
}
}
//若是退出上面的循環,要麼是玩家輸了,要麼是玩家主動退出
//因此先判斷玩家的總點數是否大於21點
if(player.titlePoint>21){
console.log("電腦獲勝");
computer.victory++;
}else{
//若是沒有進入上面的if,說明是主動退出
//那咱們就讓電腦對象生成總點數,而後將兩個對象傳入judge函數比較
computer.titlePoint=Math.floor(Math.random()*21+1);
console.log(`您的總點數爲:${player.titlePoint},電腦的總點數爲:${computer.titlePoint}`);
let result=judeg(player,computer);
switch(result){
case 0:
console.log("本局爲平局,不加分");
break;
case 1:
console.log("玩家獲勝");
player.victory++;
break;
case 2:
console.log("電腦獲勝");
computer.victory++;
break;
}
}
//打印信息
console.log("\n當前比分以下:");
console.log(`玩家:${player.victory} vs 電腦:${computer.victory}`);
readline.question("");
//問玩家是否繼續 不繼續就將continuePlay改爲false
while(true){
console.log("是否繼續遊戲?(Y/N)");
let isAgain=readline.question("");
//繼續單人模式 不繼續直接將continuePlay改爲fales
if(isAgain==="Y"||isAgain==="y"){
player.titlePoint=0;
computer.titlePoint=0;
break;
}else if(isAgain==="N"||isAgain==="n"){
continuePlay=false;
break;
}else{
console.log("輸入有誤,請從新輸入\n");
}
}
}
//可以來到這裏,所名玩家輸入的n,這時候continuePlay的值爲false
//打印結果就能夠了
clear();
console.log("最終比分以下:");
console.log(`玩家:${player.victory} vs 電腦:${computer.victory}`);
console.log("\n按回車鍵返回");
readline.question("");
}
break;
//雙人
case 2:
{
//和單人模式同樣,仍是初始化對象,不過多了一個isTakeCare屬性
let player1={victory:0,titlePoint:0,isTakeCard:true};
let player2={victory:0,titlePoint:0,isTakeCard:true};
//continuePlay變量表示是否繼續雙人模式
let continuePlay=true;
while(continuePlay){
//根據兩個對象的isTakePlay屬性值判斷是否抽牌,只要兩個對象有一個屬性值爲true就會進入while循環
while(player1.isTakeCard||player2.isTakeCard){
//根據玩家1的isTakeCard屬性斷定玩家1是否抽牌
if(player1.isTakeCard){
let inputValidate=true;//這個變量是輸入驗證 用於後面驗證玩家的輸入
while(inputValidate){
clear();
//在抽牌以前打印當前點數信息,而後在詢問玩家是否抽取點數
console.log("總點數:");
console.log(`玩家A:${player1.titlePoint} vs 玩家B:${player2.titlePoint}`);
console.log("玩家A 是否抽牌?(Y/N)");
let isTake = readline.question("");
switch(isTake){
case "Y":
case "y":
inputValidate=false;//作輸入驗證
takeCard(player1);
readline.question("");
//大於21,將兩個玩家的isTakeCard都修改成false,這樣就能夠退出上上個while循環
if(player1.titlePoint>21){
player1.isTakeCard=false;
player2.isTakeCard=false;
}
break;
case "N":
case "n":
//表明玩家不抽牌,直接將isTakeCard屬性改成false
inputValidate=false;
player1.isTakeCard=false;
break;
default:
console.log("您輸入的有誤,請從新輸入");
readline.question("");
}
}
}
//和上面相同
if(player2.isTakeCard){
let inputValidate=true;
while(inputValidate){
clear();
console.log("總點數:");
console.log(`玩家A:${player1.titlePoint} vs 玩家B:${player2.titlePoint}`);
console.log("玩家B 是否抽牌?(Y/N)");
let isTake=readline.question("");
switch(isTake){
case "Y":
case "y":
inputValidate=false;
takeCard(player2);
readline.question("");
if(player2.titlePoint>21){
player1.isTakeCard=false;
player2.isTakeCard=false;
}
break;
case "N":
case "n":
inputValidate=false;
player2.isTakeCard=false;
break;
default:
console.log("您輸入的有誤,請從新輸入");
readline.question("");
}
}
}
}
//代碼來到這裏說明兩個玩家isTakeCard都爲fales,接下來直接比較
if(player1.titlePoint>21){
console.log("玩家A失敗!");
console.log("玩家B獲勝!");
player2.victory++;
}else if(player2.titlePoint>21){
console.log("玩家A獲勝!");
console.log("玩家B失敗!");
player1.victory++;
}else{
let result=judeg(player1,player2);
switch(result){
case 0:
console.log("平局,不加分");
break;
case 1:
console.log("玩家A獲勝");
player1.victory++;
break;
case 2:
console.log("玩家B獲勝");
player2.victory++;
break;
}
}
console.log("\n當前比分以下:");
console.log(`玩家A:${player1.victory} vs 玩家B:${player2.victory}`);
readline.question("");
while(true){
console.log("是否繼續遊戲?(Y/N)");
let isAgain=readline.question("");
if (isAgain==="Y"||isAgain==="y"){
player1.titlePoint=0;
player2.titlePoint=0;
player1.isTakeCard=true;
player2.isTakeCard=true;
break;
}else if(isAgain==="N"||isAgain==="n"){
continuePlay=false;
break;
}else{
console.log("您輸入的有誤,請從新輸入\n");
}
}
}
clear();
console.log("最終比分以下:");
console.log(`玩家A:${player1.victory} vs 玩家B:${player2.victory}`);
console.log("\n按回車鍵返回");
readline.question("");
}
break;
//遊戲規則
case 3:
{
clear();
console.log("單人遊戲規則:");
console.log("單人模式下玩家能夠連續抽牌,每回合抽取的點數爲1-10點鐘任意一點數");
console.log("全部點數相加不能超過21點,而後總點數和電腦點數進行比較,誰更接近21點獲勝\n");
console.log("雙人遊戲規則:");
console.log("兩名玩家輪流抽牌,不超過21點的前提下進行總點數的比較,誰更接近21點獲勝\n");
console.log("按回車鍵返回");
readline.question("");
}
break;
//退出
case 4:
{
//退出遊戲 將useSys修改爲false
console.log("感謝您的使用")
useSys=false;
}
break;
}
}
clear();
console.log("Thank you for playing");
}
main();
相關文章
相關標籤/搜索