//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();