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()*20+1);
console.log(`你抽取到的点数为:${point}`)
player.titlepoint += point
}
let judge = function(player1,player2){
if(player1.titlepoint>player2.titlepoint){
return 1;
}else if(player1.titlepoint
return 2;
}else{
return 0
}
}
let main = function(){
let userSys = true;
while(userSys){
clear();
console.log("欢迎来到21点的世界")
console.log("请选择游戏选项: 1.单人游戏 2.双人游戏 3.游戏说明 4退出游戏")
let funSelect = parseInt(readline.question(""))
switch(funSelect){
case 1:
{
let player = {victory:0,titlepoint:0};
let computer = {victory:0,titlepoint:0}
let continueplay = true;
while(continueplay){
let isTakeCard = true;
while(isTakeCard){
clear();
console.log(`当前的总分数为:${player.titlepoint}`);
console.log("是否要抽取一张牌?(Y/N)")
let isTake = readline.question("");
switch(isTake){
case "y":
case "Y":
takecard(player);
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("")
}
}
if(player.titlepoint>21){
console.log("电脑获胜");
computer.victory++;
}
else{
computer.titlepoint = Math.floor(Math.random()*2+5);
console.log(`你的总点数为${play.titlepoint},电脑的总点数为${computer.titlepoint}`);
let result = judge(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("")
while(true){
console.log("是否继续游戏?(Y/N)");
let isAgain = readline.question("");
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");
}
}
}
clear();
console.log("最终比分如下:")
console.log(`玩家:${player.victory} vs 电脑:${computer.victory}`);
console.log("\n按回车健返回");
readline.question("");
}
break;
case 2:
{
let player1 = {victory:0,titlepoint:0,isTakeCard:true};
let player2 = {victory:0,titlepoint:0,isTakeCard:true};
let continueplay = true;
while(continueplay){
while(player1.isTakeCard || player2.isTakeCard){
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("")
if(player1.titlepoint>21){
player1.isTakeCard = false;
player2.isTakeCard = false;
}
break;
case "n":
case "N":
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(player1.titlepoint>21){
player1.isTakeCard = false;
player2.isTakeCard = false;
}
break;
case "n":
case "N":
inputValidate = false;
player2.isTakeCard = false;
break;
default:
console.log("输入有误,请重新输入");
readline.question("");
}
}
}
}
if(player1.titlepoint>21){
console.log("玩家A已爆炸!");
console.log("玩家B获胜");
player1.victory++;
}
else if(player1.titlepoint>21){
console.log("玩家B已爆炸!");
console.log("玩家A获胜");
player1.victory++;
}
else {
let result = judge(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:{
userSys = false;
}
break;
}
}
clear();
console.log("Thank you for playing");
}
main();
转载于:https://www.cnblogs.com/akangwx0624/p/11147048.html