- Get started by editing
- src/app/page.tsx
-
- Docs{' '} - - -> - -
-- Find in-depth information about Next.js features and API. -
- + function generateToss() { + const random = Math.random(); + if (random < 0.5) { + return "Heads"; + } else { + return "Tails"; + } + } - -- Learn{' '} - - -> - -
-- Learn about Next.js in an interactive course with quizzes! -
- + function generateComputerChoice() { + const random = Math.random(); + if (random < 0.5) { + return "Batting"; + } else { + return "Bowling"; + } + } - -- Templates{' '} - - -> - -
-- Explore starter templates for Next.js. -
- + function handleSubmit(event: any) { + setTossed(true); + console.log(event.target.value); + if (event.target.value === generateToss()) { + setToss("You won the toss"); + } else { + setToss("You lost the toss"); + const computerChoice = generateComputerChoice(); + if (computerChoice == "Batting") { + setUserBatting(false); + } else { + setUserBatting(true); + } + setChoosed(true); + setPlay(true); + } + } - -- Deploy{' '} - - -> - -
-
- Instantly deploy your Next.js site to a shareable URL with Vercel.
+ function generateComputerScore() {
+ return Math.floor(Math.random() * 6) + 1;
+ }
+
+ function handleUserBatting(userShot: number, computerShot: number) {
+ if (userShot === computerShot) {
+ if (firstInning) {
+ setFirstInning(false);
+ setUserBatting(false);
+ } else {
+ setGameOver(true);
+ setMessage("You are out. Computer won the match");
+ }
+ } else {
+ setUserScore(userScore + userShot);
+ }
+ }
+
+ function handleUserBowling(userShot: number, computerShot: number) {
+ if (userShot === computerShot) {
+ if (firstInning) {
+ setFirstInning(false);
+ setUserBatting(true);
+ } else {
+ setGameOver(true);
+ setMessage("Computer is out. You won the match");
+ }
+ } else {
+ setComputerScore(computerScore + computerShot);
+ }
+ }
+
+ function handlePlay(event: any) {
+ if (balls === 6) {
+ if (firstInning) {
+ setFirstInning(false);
+ setUserBatting(!userBatting);
+ setBalls(0);
+ } else {
+ setGameOver(true);
+ if (userScore > computerScore) {
+ setMessage("You won the match");
+ } else if (computerScore > userScore) {
+ setMessage("Computer won the match");
+ } else {
+ setMessage("Match draw");
+ }
+ }
+ }
+ const userShot = parseInt(event.target.value);
+ const computerShot = generateComputerScore();
+ setBalls(balls + 1);
+
+ if (userBatting) {
+ handleUserBatting(userShot, computerShot);
+ } else {
+ handleUserBowling(userShot, computerShot);
+ }
+ }
+ if (gameOver) {
+ return
+ {userBatting + ? "Computer won the toss and choose bowling" + : "Computer won the toss and choose batting"}
- + )} + {play && ( +{message}
+ +Play
+{userBatting ? "You are batting" : "You are bowling"}
+Computer Score: {computerScore}
+Your Score: {userScore}
+Balls: {balls}
+First Inning: {firstInning ? "Yes" : "No"}
+Game Over: {gameOver ? "Yes" : "No"}
+