DEV Community

Cover image for NumberGuessingGame with springBoot and java
Neelakandan R
Neelakandan R

Posted on

NumberGuessingGame with springBoot and java

NumberGuessingGame

package pratice;

import java.util.Scanner;
import java.util.Random;

public class NumberGuessingGame {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        int lowerBound = 1;
        int upperBound = 100;
        int numberToGuess = random.nextInt(101);
        int numberOfTries = 0;
        int userGuess = 0;

        System.out.println("🎮 Welcome to the Number Guessing Game!");
        System.out.println("I have selected a number between " + lowerBound + " and " + upperBound + ".");
        System.out.println("Can you guess it?");

        while (userGuess != numberToGuess) {
            System.out.print("Enter your guess: ");
            userGuess = scanner.nextInt();
            numberOfTries++;

            if (userGuess < numberToGuess) {
                System.out.println("Too low! Try again.");
            } else if (userGuess > numberToGuess) {
                System.out.println("Too high! Try again.");
            } else {
                System.out.println("🎉 Congratulations! You guessed the number in " + numberOfTries + " attempts.");
            }
        }

        scanner.close();
    }
}


Enter fullscreen mode Exit fullscreen mode

Output:

🎮 Welcome to the Number Guessing Game!
I have selected a number between 1 and 100.
Can you guess it?
Enter your guess: 56
Too low! Try again.
Enter your guess: 78
Too high! Try again.
Enter your guess: 65
Too low! Try again.
Enter your guess: 70
Too low! Try again.
Enter your guess: 72
Too low! Try again.
Enter your guess: 74
Too low! Try again.
Enter your guess: 76
🎉 Congratulations! You guessed the number in 7 attempts.

SpringBoot NumberGuessingGame

NumberGuessingGameApplication.java

package com.example.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class NumberGuessingGameApplication {
    public static void main(String[] args) {
        SpringApplication.run(NumberGuessingGameApplication.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

Controller

package com.example.demo.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

@Controller
public class GameController {

    @GetMapping("/")
    public String home(HttpSession session, Model model) {
        if (session.getAttribute("numberToGuess") == null) {
            session.setAttribute("numberToGuess", new Random().nextInt(100) + 1);
            session.setAttribute("attempts", 0);
        }
        model.addAttribute("message", "Guess a number between 1 and 100");
        return "index";
    }

    @PostMapping("/guess")
    public String guess(@RequestParam int userGuess, HttpSession session, Model model) {
        int numberToGuess = (int) session.getAttribute("numberToGuess");
        int attempts = (int) session.getAttribute("attempts");

        attempts++;
        session.setAttribute("attempts", attempts);

        if (userGuess < numberToGuess) {
            model.addAttribute("message", "🔽 Too low! Try again.");
        } else if (userGuess > numberToGuess) {
            model.addAttribute("message", "🔼 Too high! Try again.");
        } else {
            model.addAttribute("message", "🎉 Correct! You guessed it in " + attempts + " attempts.");
            return "result";
        }

        return "index";
    }

    @PostMapping("/restart")
    public String restart(HttpSession session) {
        session.invalidate();
        return "redirect:/";
    }
}


Enter fullscreen mode Exit fullscreen mode

In template Create index.html,result.html

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Number Guessing Game</title>
</head>
<body>
    <h1>🎮 Number Guessing Game</h1>
    <p th:text="${message}"></p>
    <form method="post" action="/guess">
        <input type="number" name="userGuess" required min="1" max="100"/>
        <button type="submit">Guess</button>
    </form>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

result.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Result</title>
</head>
<body>
    <h2 th:text="${message}"></h2>
    <form method="post" action="/restart">
        <button type="submit">Play Again</button>
    </form>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Result Page:

Image description

Output Page:

Image description

Top comments (0)