Be A React Dev

Game Action - Get Result Rock Paper Scissors

17-Jul-2023


Amazon Prime

typescript plus react

What you will be building

Please check this link: Rock Paper Scissors Game

This app uses components from Chakra UI . You should check it out.

Reference

We first looked at setDecisionComputer here.

Let’s get right into it.

actions/get-result.ts

interface Payload {
  computerDecision: {
    beats: string;
    value: string;
  };
  userDecision: string;
}

const getResult = (payload: Payload) => {
  const { computerDecision, userDecision } = payload;

  if (computerDecision.value === userDecision) {
    return {
      judgement: 'It’s a tie.',
      computer: 0,
      user: 0,
    };
  }

  if (computerDecision.beats === userDecision) {
    return {
      judgement: 'Computer wins…',
      computer: 1,
      user: 0,
    };
  }

  return {
    judgement: 'You win!!!',
    computer: 0,
    user: 1,
  };
};

export default getResult;

First we need to describe a typescript interface of expected types to be passed into this function. We need the computer decision and the user decision. The computer decision is a shape which contains the computer’s actual decision and what that decision beats. The user decision is the value the user chose.

The result returns an object made up of judgement, computer value and user value. The values are either 1 or zero depending on whether the user or computer won (a 1 is set) or lost (a 0 is set) whether there was a tie or one actor lost to the other.

And that’s it really.


Get Cool Swag

If you are finding this content useful (or maybe you are just a nice person or maybe you just like my merch) I would be greatful if you headed over to my shop and make a purchase or two. All proceeds will go towards making more courses.

If there’s merch missing that you would like, let me know (click this: Make Something For Me ) and I’ll try to make it for you.

that sux dead dog’s balls!

That Sux Dead Dog’s Balls!

You can buy this product or you can check out my shop of products.


About

The Author Guy

Find Me

Rob Welan <rob.welan@beareact.dev>FacebookKo-FiLinkedInMediumPatreonRSS FeedStack OverflowX

Tech In Use

Be A React Dev

© 2024 Be A React Dev. All rights reserved.