Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created May 24, 2023 07:53
Show Gist options
  • Save codecademydev/e3cdb2b70b17dbd7c4ba34c97942549b to your computer and use it in GitHub Desktop.
Save codecademydev/e3cdb2b70b17dbd7c4ba34c97942549b to your computer and use it in GitHub Desktop.
Codecademy export
import React, { useState } from "react";
import styles from "./Game.module.css";
const CHOICES = [
{ name: "rock", emoji: "✊" },
{ name: "paper", emoji: "✋" },
{ name: "scissors", emoji: "✌️" },
];
const choiceStyles = {
display: "flex",
alignItems: "center",
marginBottom: 40
};
const emojiStyles = {
fontSize: 64,
marginRight: 20
};
const nameStyles = {
margin: 0,
fontSize: 24,
color: "#FFF"
};
const resultStyle = {
marginTop: 40,
fontSize: 48,
color: "#FFF"
};
function Game() {
const [playerChoice, setPlayerChoice] = useState(null);
const [codeyChoice, setCodeyChoice] = useState(null);
const [result, setResult] = useState(null);
function handlePlayerChoice(choice) {
const codeyChoice = CHOICES[Math.floor(Math.random() * CHOICES.length)];
setPlayerChoice(choice);
setCodeyChoice(codeyChoice);
if (choice.name === codeyChoice.name) {
setResult("Tie!");
} else if (
(choice.name === "rock" && codeyChoice.name === "scissors") ||
(choice.name === "paper" && codeyChoice.name === "rock") ||
(choice.name === "scissors" && codeyChoice.name === "paper")
) {
setResult("You win!");
} else {
setResult("You lose!");
}
}
function resetGame() {
setPlayerChoice(null);
setCodeyChoice(null);
setResult(null);
}
return (
<div className={styles.container}>
<h1 style={{ fontSize: 48, marginTop: 0 }}>Rock Paper Scissors</h1>
<div className={styles.choices}>
{CHOICES.map((choice) => (
<button
className={styles.button}
key={choice.name}
onClick={() => handlePlayerChoice(choice)}
aria-label={choice.name}
>
{choice.emoji}
</button>
))}
</div>
{playerChoice && codeyChoice && (
<div className={styles.results}>
<div style={choiceStyles}>
<span style={emojiStyles}>{playerChoice.emoji}</span>
<p style={nameStyles}>You chose {playerChoice.name}</p>
</div>
<div style={choiceStyles}>
<span style={emojiStyles}>{codeyChoice.emoji}</span>
<p style={nameStyles}>The computer chose {codeyChoice.name}</p>
</div>
<h2 style={resultStyle}>{result}</h2>
<button className={styles.button} onClick={resetGame}>Play again</button>
</div>
)}
</div>
);
}
export default Game;
.container {
display: flex;
flex-direction: column;
align-items: center;
font-family: "Courier New", Courier, monospace;
background-color: #000000;
color: #ffffff;
padding: 40px;
}
.choices {
display: flex;
justify-content: center;
margin: 40px 0;
}
.button {
font-size: 48px;
background-color: #333333;
color: #ffffff;
border: 4px solid #ffffff;
border-radius: 8px;
padding: 20px 40px;
margin: 0 20px;
cursor: pointer;
transition: all 0.2s ease-in-out;
}
.results {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 40px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment