js Exercise: finger-guessing game
// finger-guessing game
// clear screen function
let clear = () => process.stdout.write (process.platform === 'win32' '\ x1Bc':? '\ X1B [2J \ x1B [3J \ x1B [H ' );
the let the readline = the require ( "the readline-Sync" );
the let Player = {
name: "Player", // default player name
victory: 0 // number player has won Board
};
the let COM = {
name: "computer" ,
victory: 0 // number PC Board victory
}
// winning player parameter determination function receives two digital mora mora digital computer
the let Judge = function (I, J) {
//Returns the number 01 represents 2,0 representative of draw computer represents the winning player wins
the let Result = J - I;
IF (Result === 0 )
{
return 0 ;
}
the else IF (Result> 0 )
{
IF (I == ===. 3. 1 && J = )
{
return 2 ;
}
the else {
return . 1 ;
}
}
the else {
IF (I && J === ===. 3. 1 )
{
return . 1 ;
}
the else{
Return 2 ;
}
}
}
// described function returns the corresponding digital words transmitted over
the let desc = function (I) {
the let STR = "" ;
Switch (I)
{
Case . 1: STR = "stone"; return STR ;
Case 2: STR = "scissors"; return STR;
Case . 3: = STR "cloth"; return STR;
}
}
the let main = function () {
Clear ();
the console.log ( "Please enter your player name: " );
the let playerNameReadline.question = ( "" );
player.name = playerName;
the let the USESYS = to true ;
the while (the USESYS)
{
the Clear ();
console.log ( "Welcome to the mora game," , player.name);
console.log ( "Please select the game options: 1. start game 2. game 3. Description quit the game" );
the let funcSelect = readline.question ( "" );
Switch (funcSelect)
{
// start the game
Case "1" :
the let PlayNow = true ; // set the game state is true
the while (PlayNow)
{
Clear ();
the console.log ( "you want to select the punches: 1. 2. stone scissors cloth 3. 4. Return" );
the let playerChoose = the parseInt (readline.question ( "" )) ;
Switch (playerChoose)
{
Case . 1 :
Case 2 :
Case . 3 :
{
// computer-generated random nonce 1-3 punch returned stone 1. 2. 3. scissors cloth
let computChoose = Math.floor (Math.random () * 3 + 1 );
Result the let = Judge (playerChoose, computChoose);
// The return result to output a different content
Switch (Result)
{
// tie
Case 0 :
the console.log ( "you are out:" , desc (playerChoose));
Console. log ( "the computer 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 ( "the computer is:" , desc (computChoose));
console.log ( "! victory players" );
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 ( "the computer is:" , desc (computChoose));
console.log ( "Computer victory!" );
com.victory ++ ;
console.log (player.name + ":" + player.victory + "VS" + com.name + ":" + com.victory);
the console.log ( "press ENTER to continue" );
readline.question ( "" );
BREAK ;
}
}
BREAK ;
Case . 4 :
playNow = false ; // return to the previous menu playNow only need to be replaced by false
BREAK ;
default :
console.log ( "input error, please re-enter, press the Enter key to continue" );
readline.question ( "" );
}
}
BREAK ;
// description of the game
Case "2" :
the 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" :
the USESYS = false ;
BREAK ;
default :
console.log ( "input error, please re-enter, press the enter key to continue" );
readline.question ( "" );
}
}
clear();
console.log("最终比分为:");
console.log(player.name + ":" + player.victory + " vs " + com.name + ":" + com.victory);
console.log("Thank you for playing,",player.name);
}
main();