This assignment is about practicing React state. You will complete a Rock Paper Scissors game by filling in the missing pieces: using useState in the parent, passing state and handlers via props to child components, and handling user input. The game logic (random computer pick, win/loss calculation) is already implemented in src/utils/index.js—your job is to connect the UI to that logic using React state and props.
You must have played "Rock Paper Scissors" at least once. Here you will build it on the web: the user picks Rock, Paper, or Scissors; the computer picks at random; the app shows the result (Win / Lost / Peace).
Demo: Go to demo website
Submit: When you're done, submit your assignment on Discord using the /submit command.
master branch. Do not edit the solution branch; that holds the reference implementation."Your code here" to find every spot you must complete. You should only change those designated areas; leave the rest of the codebase as is.| File | What you need to do |
|---|---|
src/components/Result.js | Destructure the props: user1GameItem, user2GameItem, result. |
src/components/Player.js | Use the avatarUrl and name props for the <img> src and alt. |
src/components/Main.js | Pass the real state and handler into <Result> and <Choices> instead of the placeholder strings (e.g. user1GameItem, user2GameItem, result, gameItems, handleGameItemChange). |
src/components/Choices.js | Pass the handleGameItemChange prop through to each <ChoiceCard>. |
src/components/ChoiceCard.js | Call handleGameItemChange when the choice is clicked (e.g. in onClick). |
All of these are in the src/components/ folder (and the state lives in src/components/Main.js). The src/utils/index.js and src/App.js are provided; you don't need to change them for the assignment.
Repository: nauqh/m2-rock-paper-scissor
Branches:
master — This is where you code. All "Your code here" placeholders are on this branch.solution — Reference solution. Use it only to check answers; don't edit it.Get started:
YOUR_USERNAME with your GitHub username):git clone https://github.com/YOUR_USERNAME/m2-rock-paper-scissor.gitgit checkout masterTo view the solution later:
git checkout solution
src/utils/index.jsThe game logic is already written. Here is what the two helpers do.
getRandomGameItem(gamesItems)gamesItems is a list of items.gamesItems list.export const getRandomGameItem = (gamesItems) => {
const index = Math.floor(Math.random() * gamesItems.length); // index between 0 and gamesItems.length - 1
return gamesItems[index]; // return that item
};calculatorUserWinner(user1GameItem, user2GameItem)user1GameItem, user2GameItem — game objects, each with a game item id and a list of ids they can beat.user1GameItem = {
url: "/images/paper.png",
id: 0,
winItemIds: [1],
name: "Paper",
};"Win" / "Lost" / "Peace").export const calculatorUserWinner = (user1GameItem, user2GameItem) => {
if (user1GameItem.id === user2GameItem.id) {
return "Peace"; // same choice => tie
} else if (user1GameItem.winItemIds.includes(user2GameItem.id)) {
return "Win"; // player 1's winItemIds contains player 2's id => player 1 wins
} else {
return "Lost"; // otherwise player 1 loses
}
};Your task is to wire the UI in the components (using useState and props) so that when the user picks a choice, you call these utils and show the result.