Coder Social home page Coder Social logo

anushkachauhxn / fm-rock-paper-scissors-lizard-spock Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 1.34 MB

The Big Band Theory "rock paper scissors lizard spock" game built using ReactJS, Redux and Material UI. My solution to a Frontend Mentor Challenge.

Home Page: https://anushkachauhxn.github.io/fm-rock-paper-scissors-lizard-spock

HTML 3.37% CSS 40.17% JavaScript 56.45%
react css css-animations css3 javascript javascript-game material-ui mui react-app react-applications

fm-rock-paper-scissors-lizard-spock's Introduction

๐ŸŽฏ Frontend Mentor - Rock, Paper, Scissors, Lizard, Spock Challenge

This is a solution to the Rock, Paper, Scissors challenge on Frontend Mentor. Frontend Mentor challenges help you improve your coding skills by building realistic projects.

โžก๏ธ Frontend Mentor Projects: https://github.com/anushkachauhxn/frontend-mentor-projects

๐Ÿ“œ Table of contents

๐Ÿ“ Overview

The challenge

  • Your challenge is to build out this Rock, Paper, Scissors game and get it looking as close to the design as possible.

  • Your users should be able to:

    • โ˜‘๏ธ View the optimal layout for the game depending on their device's screen size
    • โ˜‘๏ธ Play Rock, Paper, Scissors against the computer
    • โ˜‘๏ธ Maintain the state of the score after refreshing the browser (optional)
    • โ˜‘๏ธ Bonus: Play Rock, Paper, Scissors, Lizard, Spock against the computer (optional)

Screenshot

Screenshot 1

Screenshot 2

Links

  • Solution URL: Link
  • Live Site URL: Link

๐Ÿ’ก My process

Built with

  • React JS
  • React Router
  • Redux
  • Material UI
  • CSS custom properties

What I learned

๐Ÿ˜Ž Proud of this CSS:

The all CSS winner animation!

/* Animation On Result */
.game__side button {
  position: relative;
}

.game__side button::before,
.game__side button::after {
  content: "";
  position: absolute;
  z-index: -1;
  border-radius: 50%;
  animation: ripple 0.75s cubic-bezier(0.65, 0, 0.34, 1) 0s 1;
}

.game__side button::before {
  height: calc(332px * 1.75);
  width: calc(332px * 1.75);
  border: 84px solid hsl(223, 44%, 21%); /* Outer Ring */
}
.game__side button::after {
  height: calc(332px * 1.28);
  width: calc(332px * 1.28);
  background: hsl(224, 33%, 28%); /* Inner Ring */
  border: 78px solid hsl(223, 39%, 24%); /* Middle Ring */
}

@keyframes ripple {
  from {
    transform: scale3d(0, 0, 1);
  }
  to {
    transform: scale3d(1, 1, 1);
  }
}

๐Ÿ˜Ž Proud of this JS:

The whole Redux store structure and the actions have to be precise. The timing for calling the functions should be well thought.

gameSlice.js :

const winners = [
  // winners[userVal] = [compVals that make user win]
  [2, 3],
  [0, 4],
  [1, 3],
  [1, 4],
  [0, 2],
];

export const gameSlice = createSlice({
  name: "game",
  initialState: {
    userVal: null,
    compVal: -1,
    result: null,
    score: 0,
  },
  reducers: {
    setUserVal: (state, action) => {
      state.userVal = action.payload;
    },
    setCompVal: (state) => {
      let res = Math.floor(Math.random() * 5);

      for (let i = 1; res === state.userVal; i++) {
        // To avoid draws
        res = Math.floor(Math.random() * 5);
        if (i > 5) {
          alert("Something went wrong.");
          break;
        }
      }
      state.compVal = res;
    },
    calculateResult: (state) => {
      const compVals = winners[state.userVal];

      if (compVals.includes(state.compVal)) {
        state.result = 1; // user wins
      } else {
        state.result = -1; // comp wins
      }
    },
    updateScore: (state) => {
      state.score = parseInt(state.score);
      state.score += parseInt(state.result);
      if (state.score < 0) {
        state.score = 0;
      }
      sessionStorage.setItem("score", state.score);
    },
    setScore: (state) => {
      // Getting the score after page has been refreshed
      state.score = sessionStorage.getItem("score") || 0;
    },
    resetGame: (state) => {
      state.userVal = null;
      state.compVal = -1;
      state.result = null;
    },
  },
});

Main.js :

const openGame = (usersPick) => {
  dispatch(setUserVal(usersPick));

  setTimeout(() => {
    dispatch(setCompVal());

    setTimeout(() => {
      dispatch(calculateResult());
      dispatch(updateScore());
    }, 500);
  }, 1000);

  history.push("/game");
};

๐Ÿ”Ž Useful resources

๐Ÿงฉ Stackoverflow:

๐Ÿ“– mdn:

๐Ÿง  Inspiration for Animation:

โšก Deployment Bug:

โญ Author

fm-rock-paper-scissors-lizard-spock's People

Contributors

anushkachauhxn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.