Build Rock, Paper and Scissors game in ReactJS
Tags: react, javascript, webdev
Published: 2021-01-25 16:58:23

Hi there,

I was thinking to make something good which covers some react concepts as well as good styling(UI). 

I found one challenge of creating Rock, Paper and scissors on [Frontend Mentor](https://www.frontendmentor.io/). You can see it [here](https://www.frontendmentor.io/challenges/rock-paper-scissors-game-pTgwgvgH
). 

Let's solve this challenge.

From this tutorial you will learn,
-- A new fast way to create react app
-- React Hooks
-- Modal (Best Practice)
-- Creating Countdown
-- Sass styling
-- Advanced Conditional Rendering
-- React-router functionalities

Now for the styling part it will be good if you watch the video format of this tutorial.

{% youtube tCSaSDgz2Hw %}

### Let's start creating our app

In this tutorial I'm using [parcel](https://parceljs.org/) to create react app instead of create-react-app.

Go to this page [createdev](https://createapp.dev/parcel)

In this page select **react** in Main library.
Select **Sass** in Styling.
and for linting choose whichever you are using.
then just write project name and download the project.

When you unzip this you will find whole folder structure.
so, after unzip do `npm install` so it will install all the libraries listed in the package.json file.

Also, we will need two more libraries so install,
`npm install react-router-dom node-sass`

Start your app by,
`npm start`
>Parcel will build much faster then ordinary create-react-app.

Our project setup is done.

Create two folders in src.
one folder which is *components* and another *scss* to store scss files.

Our Component Folder will contain:-
|-- Header.js (The header part where we display score)
|-- Play.js (Provides 3 choices)
|-- Game.js (Display the Result)
|-- Footer.js (Footer contains Rules button)
|-- Modal.js (Display Rules)

## Let's start with the App.js

First of all import all the components here except modal.

{% gist https://gist.github.com/codebucks27/2c69024cdf779d3b2898b9d38f09a216/d0b19c320728522dd081fdf642c912e72840b393 %}



**Line 9:** This state **myChoice** is to store our choice.

**Line 10:** This state **score** is to store score.

**Line 15:** Call header component here and pass score as props so that we can display score in the header part.

**Line 16 to 23:** Here we have used Switch and Route from react-router-dom. Now using this,
when path is at "/" then we will render play component else
when path is at "/game" we will render game component.
*Eventually when you set your choice we will change path from "/" to "/game"*
>Make sure to wrap your app component with BrowserRouter in index.js file.

**Line 18:** Play component will need setMyChoice method so pass it as props.

**Line 32:** Game component will need myChoice, score and setScore to decide the result.

**Line 25:** We have placed footer component.

## Header Component
Deconstruct score from the props and just display score inside the h1 tag.

```
import React from "react";

const Header = ({ score }) => {
  return (
    <div className="header">
      <h1>Score: {score}</h1>
    </div>
  );
};

export default Header;
```
## Play Component.

In this component we will provide three choices to choose from.
So create 3 Links.

{% gist https://gist.github.com/codebucks27/2c69024cdf779d3b2898b9d38f09a216/04b30ea2b204ca3ac3ff3df5baa0eda4e5d27223 %}

Make sure you have imported Link from "react-router-dom" as in Line no 2.

**Line no 11 to 17:** 
We have created Link and set it's path to="/game".
Now inside this link we have created one div, this div has special attribute which is *data-id* this attribute contains value like paper, rock or scissors. 
Now onClick there is new method which is setChoice which sets the choice.

**Line 5:** This setChoice methods takes e as parameter and extracts value from data-id attribute and setMyChoice state.

So, when we set this choice the path will change from "/" to "/game", and our choice will be passed down to the *game* component.

## Game Component

Let's create the game component.

{% gist https://gist.github.com/codebucks27/2c69024cdf779d3b2898b9d38f09a216 %}

**Line 4:** make sure to deconstruct all 3 ({ score, myChoice, setScore }) props.

**Line 6:** This house state will store the choice of house (or computer).

**Line 7:** playerWin state is to decide results which can be "win" "lose" or "draw".

**Line 11 to 14:** Now this newHousePick() method will select choices randomly from choices array. using Math.random()*3. and it will use setHouse() to set house state.

**Line 15:** we will use useEffect to call this newHousePik() method so that whenever game components loads it will select house choice from the beginning.

**Line 19:** This Result() function decides who wins.
Now for the conditions we all know how to play this game so you can read conditions easily.
if player wins then playerWin = "win" and score = score + 1.
if player loses then playerWin = "lose" and score = score - 1.
if player and house both chose same choice then playerWin = "draw"

**Line 43:** This useEffect calls Result() function. We will call this useEffect only when house has been chosen so put house at the input of useEffect.

**Line 49:** This display myChoice state. (our choice)

**Line 50:** Displays house choice.

**Line 53, 54, 55:**
These lines display result message using conditional rendering.
if player win then it will display *you win*.
if player lose then it will display *you lose*.
if game is draw then it will display *Draw*.

**Line 57:** This is Link which contains Play again button.
this button takes us back to path "/". and onClick it will reset the house state.

#### That's it whole logic part of our app is completed now you can start styling it.

If you want to create whole app same as provided in the frontend mentor challenge then do watch the video.
In the video you will learn new way to create Modal in react and also you'll learn to create and render countDown.

>I don't want to write whole styling part because it will only make this article more big, So it's better to watch video for it or you can directly see the code in github.

You can find Full-Code repository from [here](https://github.com/codebucks27/ReactJs-rock-paper-scissors-game).

### Thanks For Reading and Supporting.😄

Feel free to visit my youtube channel: 
####[@CodeBucks](https://www.youtube.com/channel/UCeYt6blRBKuNrEg_-282fSA)

Follow me on Twitter where I'm sharing lot's of useful resources!
####[@code.bucks](https://twitter.com/code_bucks) 😉