1.5.2 • Published 1 year ago

rpsjs v1.5.2

Weekly downloads
2
License
MIT
Repository
github
Last release
1 year ago

RockPaperScissorsJS


Table of content

Introduction


RockPaperScissorsJS is a JavaScript library that simplifies the process of creating the popular rock paper scissors game. It's easy to use and is available in a JavaScript class format, where your Play instance will be considered an object.

To get started, you can create a new instance of Play like this:

  const player = new Play(); 

The Roll(choice) function is where most of the game logic happens. It takes an argument representing the user's guess, which can be one of three options: rock, paper, or scissors. Here's an example of how to use Roll():

console.log(player.Roll('paper'));  

The Roll() function generates a random guess for the computer and compares it to the user's guess. It then returns an object with three properties: user, computer, and won. The user property represents the user's guess, the computer property represents the computer's guess, and the won property is a Boolean value that indicates whether the user won (true), lost (false), or tied (draw).

Here's an example of what the output from Roll() might look like:

{
  user: 'paper',
  computer: 'scissors',
  won: false
}

getting started


getting started you will first require the library which can be done by using the script tags or imported on a node project

Browser or CDN

To use RockPaperScissorsJS in your browser, you can include it directly from a CDN. For example, you can use the following script tag in your HTML file:

   <script src="./path/to/rockPaperScissors.JS"> </script> // get the library from the path where it's located

or if you are using a CDN then you can use the following

   <script src="https://cdn.jsdelivr.net/npm/rpsjs@1.5.0/rockPaperScissors.min.js"></script> 

place the CDN link onto the header

Node.js

To use RockPaperScissorsJS in your Node.js project, you can install it via NPM. Simply run the following command in your project directory:

npm install rpsjs

Once installed, you can import the library into your Node.js module using either require() or import statements:

// Using require()
const { Play } = require('rpsjs');
// Using import statement
import { Play } from 'rpsjs';

Downloading the library

Alternatively, you can also download the library as a ZIP file from the GitHub repository and include it in your project directory. This can be useful if you want to modify the source code or use a specific version of the library.

After downloading the ZIP file, extract its contents into your project directory and include the rockPaperScissors.min.js file in your HTML file or require/import it into your Node.js project as shown above.

then crate an object to use

    const player = new Play();// you can name it anything other than player

you can find the demos here

API functions

there are 6 major functions;

       Roll(user)
       Compute()
       Won(user, computer)
       Multiplayer(user,computer)
       Scores() // get users scores
       Clear() // clear scores and plays

Note that the functions begin with a capital letter.

Roll

Roll(user) This function takes a user's guess as an argument and generates a random computer guess. It then returns an object containing the user's guess, computer's guess, and whether the user won or lost.

const player = new Play();

console.log(player.Roll('paper'));

/*
{
  user: 'rock',
  computer: 'scissors',
  won: true
}
*/

the computer guess is randomly made.

Compute

Compute() This function generates a random computer guess and returns it.

const player = new Play();

console.log(player.Compute()); // 'rock', 'paper', or 'scissors'

you can do a lot more with this just check the examples here

Multiplayer

Multiplayer(player1 = this.user, player2 = this.friend) This function allows you to play Rock Paper Scissors with another player. The function takes two arguments, player1 and player2, which represent the choices made by the two players. By default, the function uses the user and friend choices stored in the Play object.

player1: The choice made by the first player. This can be either 'rock', 'paper', or 'scissors'. player2: The choice made by the second player. This can also be either 'rock', 'paper', or 'scissors'. The function returns an object that contains the game result.

Here's an example of how to use the Multiplayer function with arguments:

const player = new Play();
this.user = 'rock';
this.player = 'paper'
console.log(player.Multiplayer('rock', 'paper'));
// or  player.Multiplayer()

/*
{
  user: 'rock',
  friend: 'paper',
  won: false
}
*/

won

Won(user, computer) This function takes a user's guess and computer's guess as arguments and determines whether the user won, lost, or tied. It also updates the user's score internally.

const player = new Play('rock');

console.log(player.Won('rock', 'paper')); // false
console.log(player.Won('rock', 'scissors')); // true
console.log(player.Scores()); // { user: 1, computer: 0 }

Score

Scores() This function returns an object containing the user's score and computer's score.

const player = new Play();

console.log(player.Scores()); // { user: 0, computer: 0 }

Clear

Clear() This function resets the user's score and computer's score.

const player = new Play(10);

player.Won('rock', 'scissors');
console.log(player.Scores()); // { user: 1, computer: 0 }

player.Clear();
console.log(player.Scores()); // { user: 0, computer: 0 }

extra


Here are some additional resources you may find helpful:

  • Demo page - See the library in action with this simple demo page.
  • Documentation - Learn more about the available functions and how to use them.
  • GitHub repository - Clone or fork the source code to modify or contribute to the project.

If you have any questions or issues, feel free to create a new issue on the GitHub repositoryor reach out to the creator bethropolis.

License


Licensed under my favourate License, MIT License.