21点游戏 分为人机对战和人人对战
玩家每次抽一张牌 牌的点数为1-10点随机数 谁更接近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); console.log(`你抽取到的点数为:${point}`); player.titlePoint += point; } let judge = 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; 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; 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() * 7 + 15); console.log(`你的总点数为${player.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 (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(""); } } } } if (player1.titlePoint > 21) { console.log("玩家A已爆炸!"); console.log("玩家B获胜"); player2.victory++; } else if (player2.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: { useSys = false; } break; } } clear(); console.log("Thank you for playing"); } main();
变秃了 也变强了