OverviewToday, I will use Node.JS to bring you a simple and interesting rock-paper-scissors game. Build Process
Related APIsLet's take a look at the relevant API first. We will understand the API in the order of calling, and the whole process will be clear. readlineRead data from a readable stream line by line Basic use
chalkChalk, a style library for node terminals, which can modify the color, bold, hidden, background color and other styles of the terminal output string const chalk = require('chalk') const logText = chalk.green(` Hello, let’s play games together! `) console.log(logText) clearClear screen command, node terminal clears the screen, clears the current terminal view display This is the easiest to use. Just execute the clear() method where you need to clear the screen. const clear = require('clear') clear() Additional instructions for steps// Define the instruction list, // Determine whether the instructions entered by the player are correct and the random output of the computer is taken from here const act = ['Scissors', 'Rock', 'Cloth'] // Determine the player input information based on the read stream // Listen for read stream input rl.on('line', function (input) { if (input === 'quit') { // If you enter [quit], execute the close() method rl.close() } else if (act.indexOf(input) !== -1) { // If the input string is in the instruction list // Randomly generate the corresponding computer instruction const idx = Math.floor((Math.random() * 3)) gamer = act[idx] // Determine whether the player has scored based on the scoring rule const curScore = scoreRule(input, gamer) // Accumulate scores score += curScore // Print information for this round let win = curScore === 1 ? 'The player wins this time' : curScore === -1 ? 'The computer wins this time' : 'It's a tie, it must be a coincidence' result = ` ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ Round ${num}: ------------------- The player played: ${input} The computer is out: ${gamer} ${win} ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ` // After writing to the stream, continue to the next round num++; console.log(result) // If 3 rounds have been played, execute the close() method if (num > 3) { rl.close() } } else { // Other inputs print the correct input prompt console.log(` !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! To continue the game, please enter: [Scissors], [Rock], [Paper] To exit the game, please enter: 【quit】 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! `) } }) Complete code// stone.js const readline = require('readline') const clear = require('clear') const chalk = require('chalk') const act = ['Scissors', 'Rock', 'Cloth'] let num = 1 let score = 0 let gamer = '' let result = '' const rl = readline.createInterface({ input: process.stdin, output: process.stdout }) clear() const beginText = chalk.green(` ============================================ To start the game, please enter: [Scissors], [Rock], [Paper] To exit the game, please enter: 【quit】 ============================================ `) console.log(beginText) rl.on('line', function (input) { if (input === 'quit') { rl.close() } else if (act.indexOf(input) !== -1) { const idx = Math.floor((Math.random() * 3)) gamer = act[idx] const curScore = scoreRule(input, gamer) score += curScore let win = curScore === 1 ? 'The player wins this time' : curScore === -1 ? 'The computer wins this time' : 'It's a tie, it must be a coincidence' result = ` ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ Round ${num}: ------------------- The player played: ${input} The computer is out: ${gamer} ${win} ※ ※ ※ ※ ※ ※ ※ ※ ※ ※ ` num++; console.log(result) if (num > 3) { rl.close() } } else { console.log(` !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! To continue the game, please enter: [Scissors], [Rock], [Paper] To exit the game, please enter: 【quit】 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! `) } }) // Listen for closing and exiting the process rl.on('close', function () { if (num > 3) { winText = score > 0 ? 'The player won the final victory' : score < 0 ? 'The player still lost in the end, come on' : 'An incredible draw' console.log(` ========================== This round ends, the player's total score is ${score} ${winText} ========================== `) } process.exit(0) }) function scoreRule(player, npc) { // Scissors, Paper // Rock, Scissors // Paper, Rock if (player === npc) { return 0 } if ((player === 'Scissors' && npc === 'Cloth') || (player === 'rock' && npc === 'scissors') || (player === 'cloth' && npc === 'stone')) { return 1 } else { return -1 } } Throwing out ideasIn the process of learning node, we will be exposed to more and more dependent modules and APIs, which also indirectly illustrates the power of the npm library. We can find and use the functions we want by searching. Don't get lost in the waves of APIs, we understand, just remember to use search skills when you need them. The [Rock, Paper, Scissors] here is so loud (it's almost unbearable to watch), but our learning process can start from the loud, and go deeper and deeper, and your delicate figure will surely be left on the top of the loud. Welcome to complain about me, welcome to go deeper into node, come on~ The above is the details of how to write the Node.JS version of the mini-game. For more information about the Node.JS version of the mini-game, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: MySQL Installer Community 5.7.16 installation detailed tutorial
>>: Detailed explanation on how to get the IP address of a docker container
In fact, we have been hearing a lot about web des...
This article uses an example to illustrate how to...
1. Introduction tr is used to convert or delete a...
Written in front Sometimes you need to install so...
Table of contents Scenario Core Issues Status mon...
Preface: Lynis is a security audit and hardening ...
This article discusses several major zero-copy te...
Preface Recently, I accidentally discovered MySQL...
Table of contents Install Pagoda Configure Python...
At the end of last year, I replaced the opensuse ...
Copy code The code is as follows: <pre> <...
The installation process is omitted (I installed ...
After installing MySQL, you will find that the ro...
Table of contents question: There are 2 tokens in...
Why is it stuck? There is a premise that must be ...