Skip to content

Commit

Permalink
Add number-guessing game (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukritmalpani authored Oct 25, 2023
1 parent db1503c commit 4e3fb64
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions packages/kitchen-sink/src/examples/number-guessing.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, { useState, FormEvent } from 'react';
import { block } from 'million/react';
interface NumberGuessingGameProps {}

const NumberGuessingGame: React.FC<NumberGuessingGameProps> = block(() => {
const [systemNumber] = useState(() => Math.floor(Math.random() * 10000) + 1);
const [userNumber, setUserNumber] = useState<number>(0);
const [attempts, setAttempts] = useState<number>(0);
const [message, setMessage] = useState<string>(
'Guess the number between 1 and 10000!',
);

const handleGuess = (e: FormEvent) => {
e.preventDefault();
if (userNumber === systemNumber) {
setMessage(`Congratulations, you win in ${attempts} moves!`);
} else {
setAttempts(attempts + 1);
setMessage(
userNumber > systemNumber
? 'Please decrease your value'
: 'Please increase your value',
);
}
};

return (
<div>
<h1>Number Guessing Game</h1>
<p>{message}</p>
<p>Moves taken: {attempts}</p>
<form onSubmit={handleGuess}>
<input
type="number"
value={userNumber}
onChange={(e) => setUserNumber(parseInt(e.target.value))}
/>
<button type="submit">Guess</button>
</form>
<h3>
Fun Fact : This game will take max 14 moves to win . Find out how :)
</h3>
</div>
);
});

export default NumberGuessingGame;

2 comments on commit 4e3fb64

@vercel
Copy link

@vercel vercel bot commented on 4e3fb64 Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

sink – ./packages/kitchen-sink

sink-git-main-millionjs.vercel.app
sink.million.dev
million-kitchen-sink-atit.vercel.app
sink-millionjs.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 4e3fb64 Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

million-kitchen-sink – ./packages/kitchen-sink

million-kitchen-sink-millionjs.vercel.app
million-kitchen-sink-git-main-millionjs.vercel.app
million-kitchen-sink.vercel.app

Please sign in to comment.