With node.js modules, async, HTTP, etc., let’s play a simple rock-paper-scissors game.

Demand analysis

This little rock-Paper-Scissors game is a form of human-computer interaction. We randomly input rock/Scissors/paper, and then the computer randomly generates rock/scissors/paper, compares the wins and losses, and outputs the results.

There are two problems here. The output we know can be printed through console.log(), so how do we input it? Then how does Node.js get our input?

Nodeindex. js rock (” rock “);

Argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv: argv

console.log(process.argv)
Copy the code

The initial release

Without further ado, go directly to the code:

// spr.js
// Rock-paper-scissors games
// Command line command: node spr.js rock
// argv gets the behavior typed after the command line node
// let playerAction = process.argv[process.argv.length - 1]; // The last input
let playerAction = process.argv[2]; // Can also be obtained directly by subscript
console.log('You're out.', playerAction)
if(playerAction ! ='rock'&& playerAction ! ='paper'&& playerAction ! ='scissor') {
  console.log('Please enter Rock or paper or scissor')}else {
  // The computer generates rock-paper-scissors from random numbers
  let computerAction;
  let random = Math.random() * 3;
  if (random < 1) {
    console.log('The computer made a stone')
    computerAction = 'rock'
  } else if (random > 2) {
    console.log('Computer made scissors')
    computerAction = 'scissor'
  } else {
    console.log('Computer out of the cloth')
    computerAction = 'paper'
  }

  // Compare winners and losers
  if (computerAction === playerAction) {
    console.log('draw')}else if (
    (computerAction == 'rock' && playerAction == 'scissor') ||
    (computerAction == 'scissor' && playerAction == 'paper') ||
    (computerAction == 'paper' && playerAction == 'rock')) {console.log('You lost')}else {
    console.log('You won')}}Copy the code

So run and play:

node spr.js rock
node spr.js scissor
node spr.js paper
Copy the code

Module encapsulation

In node.js beginners series (2) node.js CommonJS module specification, so now to the game package, and then to achieve a new requirement: computer also has mood, when we win more than 3 times, the computer’s mood will come up, no longer play.

Exports a new game.js file that exports the game module via module.exports:

// game.js
module.exports = function (playerAction) {
  if (['rock'.'scissor'.'paper'].indexOf(playerAction) == -1) {
    throw new Error('Please enter Rock or paper or scissor');
  }
  // The computer generates rock-paper-scissors from random numbers
  var computerAction;
  var random = Math.random() * 3
  if (random < 1) {
    computerAction = 'rock'
    console.log('The computer made a stone')}else if (random > 2) {
    computerAction = 'scissor'
    console.log('Computer made scissors')}else {
    computerAction = 'paper'
    console.log('Computer out of the cloth')}// Compare winners and losers
  if (computerAction === playerAction) {
    console.log('draw')
    return 0;
  } else if (
    (computerAction == 'rock' && playerAction == 'scissor') ||
    (computerAction == 'scissor' && playerAction == 'paper') ||
    (computerAction == 'paper' && playerAction == 'rock')) {console.log('You lost')
    return -1;
  } else {
    console.log('You won')
    return 1; }}Copy the code

Create index.js and load the game.js module via require:

// argv gets the behavior typed after the command line node
var playerAction = process.argv[process.argv.length - 1];
console.log(playerAction);

// Introduce the rock-paper-scissors game module via require
const game = require('./game.js')
const result = game(playerAction);
console.log(result)
Copy the code

Run it and see:

Node.js keeps typing

Now to count the number of times we win, we need a method that allows us to keep typing:

// Get standard input for the process
process.stdin.on('data'.(buffer) = > {
  // The callback is a buffer that needs to be processed into a string
  const action = buffer.toString().trim();
  console.log(action)
})
Copy the code

The terminal runs the command: node index.js, and you can see that the input can continue.

Count the number of wins and the computer stops playing after three, then kill the process with process.exit() :

const game = require('./game.js')
var winCount = 0;
// Get standard input for the process
process.stdin.on('data'.(buffer) = > {
  // The callback is a buffer that needs to be processed into a string
  const action = buffer.toString().trim();
  const result = game(action);
  if (result == 1) {
    winCount++
    if (winCount == 3) {
      console.log('I don't want to play! Hum! '); process.exit(); }}})Copy the code

As you can see, after a little bit of back-and-forth with the computer and then winning the computer three times, the process ends.

Ok, here is such a simple rock paper scissors small game to achieve, the next we come to the rock paper scissors small game into a web version of it!

Here’s the code