const readline = require("readline-sync");
console.log("欢迎来到猜拳游戏:");
console.log("输入 1 为 剪刀\n输入 2 为石头\n输入 3 为布\n");
//电脑随机
function computer() {
let computer = parseInt(Math.random() * 3 + 1);
switch (computer) {
case 1:
console.log("电脑出的是:剪刀");
break;
case 2:
console.log("电脑出的是:石头");
break;
case 3:
console.log("电脑出的是:布");
}
return computer;
}
// 将数转换为石头剪刀布
function user(input) {
// let input = readline.question() - 0;
switch (input) {
case 1:
console.log("你出的是:剪刀");
break;
case 2:
console.log("你出的是:石头");
break;
case 3:
console.log("你出的是:布");
break;
}
return user;
}
//判断
function judge(yh, dn) {
if (yh == dn) {
console.log("平局");
} else if (yh - dn == -2 || yh - dn == 1) {
console.log("玩家获胜!");
} else {
console.log("电脑胜!");
}
}
console.log("请输入:");
let user3 = readline.question() - 0;
user(user3);
judge(user3, computer(computer));
原文地址:https://www.cnblogs.com/yehongrun/p/9092798.html