JavaScript game of mora

// finger-guessing game
// clear screen function
let clear = () => process.stdout.write(process.platform === 'win32' ? '\x1Bc' : '\x1B[2J\x1B[3J\x1B[H');
let readline = require("readline-sync");
let player = {
name: "Players", // default player name
victory: 0 // number of players innings victory
};
let com = {
name: "Computer"
victory: 0 // number of computer innings victory
}
// function receives two parameters determining the outcome of a player number Mora Mora digital computer
let judge = function(i,j){
// returns the number 01 represents the 2,0 on behalf of the player wins 1 draw 2 represents the computer wins
let result = j - i;
if(result === 0)
{
return 0;
}
else if(result > 0)
{
if(i === 1 && j === 3)
{
return 2;
}
else{
return 1;
}
}
else{
if(i === 3 && j === 1)
{
return 1;
}
else{
return 2;
}
}
}
// returns the corresponding function describing the digital words transmitted over
let desc = function(i){
let str = "";
switch(i)
{
case 1: str = "stone"; return str;
case 2: str = "scissors"; return str;
case 3:str = "布";return str;
}
}
let main = function(){
clear();
console.log ( "Please enter your player name:");
let playerName = readline.question("");
player.name = playerName;
let useSys = true;
while(useSys)
{
clear();
console.log ( "Welcome to the mora game,", player.name);
console.log ( "Please select the game options: 1. Start Game 2. Game Description 3. Exit the game");
let funcSelect = readline.question("");
switch(funcSelect)
{
// Start game
case "1":
let playNow = true; // set the game state to true
while(playNow)
{
clear();
console.log ( "Please select you want out of boxing: 1. 2. stone scissors cloth 3. 4. Return");
let playerChoose = parseInt(readline.question(""));
switch(playerChoose)
{
case 1:
case 2:
case 3:
{
// return punches 1-3 computer-generated random nonce stones 1. 2. 3. scissors cloth
let computChoose = Math.floor(Math.random()*3 + 1);
let result = judge(playerChoose,computChoose);
// outputs different content based on the results returned
switch(result)
{
// draw
case 0:
console.log ( "You are out:", desc (playerChoose));
console.log ( "computer that is:", desc (computChoose));
console.log ( "round tie, do not score");
console.log(player.name + ":" + player.victory + " vs " + com.name + ":" + com.victory);
console.log ( "press the Enter key to continue");
readline.question("");
break;
// player wins
case 1:
console.log ( "You are out:", desc (playerChoose));
console.log ( "computer that is:", desc (computChoose));
console.log ( "player has won!");
player.victory++;
console.log(player.name + ":" + player.victory + " vs " + com.name + ":" + com.victory);
console.log ( "press the Enter key to continue");
readline.question("");
break;
// Computer wins
case 2:
console.log ( "You are out:", desc (playerChoose));
console.log ( "computer that is:", desc (computChoose));
console.log ( "Computer victory!");
com.victory++;
console.log(player.name + ":" + player.victory + " vs " + com.name + ":" + com.victory);
console.log ( "press the Enter key to continue");
readline.question("");
break;
}
}
break;
case 4:
playNow = false; // return to the previous menu only need to be replaced by false playNow
break;
default:
console.log ( "input error, please re-enter, press the Enter key to continue");
readline.question("");
}
 
}
break;
// game instructions
case "2":
clear();
console.log ( "and usually on the same game we play mora");
console.log ( "players choose a rock, scissors or cloth, and then compare the computer out of the fist");
the console.log ( "comparison rules are as follows:");
the console.log ( "Buck stone rock Kebu g Scissors Scissors");
the console.log ( "Press ENTER to return");
readline.question("");
break;
// exit the game
case "3":
useSys = false;
break;
default:
console.log ( "input error, please re-enter, press the Enter key to continue");
readline.question("");
}
}
clear();
the console.log ( "final score is:");
console.log(player.name + ":" + player.victory + " vs " + com.name + ":" + com.victory);
console.log("Thank you for playing,",player.name);
}
main();

Guess you like

Origin www.cnblogs.com/fatingGoodboy/p/11409857.html