๐Ÿง  Day 2 โ€” MindReader Game | #30Days30Projects

Rushikesh UngeRushikesh Unge
4 min read

Welcome to Day 2 of my #30Days30Projects Java series! Todayโ€™s project was all about binary logic and user interaction โ€” a console-based MindReader Game that can predict the number you're thinking of.

Let me walk you through it โ€” how it works, what I learned, and how you can build your own!


๐ŸŽฎ What is the MindReader Game?

Itโ€™s a simple yet logical Java program where the computer attempts to guess the number you're thinking between a given range (1โ€“100) within 5 attempts.

Itโ€™s based on the concept of Binary Search.


๐Ÿง  Learning Goals for Day 2

  • Understand Binary Search logic and apply it in a practical scenario.

  • Improve console-based user interaction.

  • Practice control statements (if, else if, for, and loops).

  • Build a game-like feel using plain Java.


๐Ÿ› ๏ธ Tools & Concepts Used

Tool/ConceptUsage
ScannerTaking input from the user
for loopAttempt counter
Binary Search logicTo guess userโ€™s number efficiently
System.out.printlnFormatting console for user instructions

๐Ÿ”ง How the Game Works

  1. You secretly think of a number between 1 and 100.

  2. The computer guesses a number.

  3. You reply with:

    • low โ†’ if your number is greater

    • high โ†’ if your number is less

    • correct โ†’ if the guess is right!

  4. The program adjusts its guessing range and tries again.

It has a maximum of 5 tries.

๐Ÿ—‚๏ธ Folder Structure

02_MindReaderGame/
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ MindReader.java
โ””โ”€โ”€ README.md

๐Ÿ’ป Java Code

import java.util.Scanner;

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

        int min = 1;
        int max = 100;
        int attempts = 5;

        System.out.println("==============================================");
        System.out.println("         ๐Ÿง  Welcome to MindReader Game         ");
        System.out.println("==============================================");
        System.out.println("Think of a number between " + min + " and " + max + ".");
        System.out.println("Iโ€™ll try to guess it in " + attempts + " attempts.");
        System.out.println("Respond with: low / high / correct\n");

        for (int i = 1; i <= attempts; i++) {
            int guess = min + (max - min) / 2;
            System.out.println("Attempt " + i + ": Is it " + guess + "?");
            System.out.print("Your answer (low/high/correct): ");
            String response = scanner.nextLine().toLowerCase();

            if (response.equals("correct")) {
                System.out.println("\n๐ŸŽ‰ Hurray! I guessed it in " + i + " attempt(s)!");
                return;
            } else if (response.equals("low")) {
                min = guess + 1;
            } else if (response.equals("high")) {
                max = guess - 1;
            } else {
                System.out.println("โš ๏ธ Invalid input. Please respond with 'low', 'high', or 'correct'.");
                i--; // Invalid response shouldn't count as an attempt
            }

            if (min > max) {
                System.out.println("\nโŒ Hmm... seems like thereโ€™s a mistake. Are you sure you answered correctly?");
                return;
            }
        }

        System.out.println("\n๐Ÿ˜ž I couldn't guess your number within " + attempts + " attempts. You win!");
    }
}

๐Ÿ–ฅ๏ธ Sample Output

==============================================
         ๐Ÿง  Welcome to MindReader Game         
==============================================
Think of a number between 1 and 100.
Iโ€™ll try to guess it in 5 attempts.
Respond with: low / high / correct

Attempt 1: Is it 50?
Your answer (low/high/correct): low

Attempt 2: Is it 75?
Your answer (low/high/correct): high

Attempt 3: Is it 62?
Your answer (low/high/correct): correct

๐ŸŽ‰ Hurray! I guessed it in 3 attempt(s)!

๐Ÿ“˜ What I Learned Today

โœ… Binary Search in Real Life

This game is actually a real-world application of binary search โ€” where we halve the search space on every user response. Instead of checking every number from 1 to 100, it quickly narrows it down.


โœ… The Importance of User Input Validation

At first, if the user gave a wrong input ("maybe"), it would still count as an attempt โ€” which wasnโ€™t ideal. So I fixed that using a simple i-- to retry on invalid input. Small UX things matter a lot!


โœ… Thinking Like a User

I realized that clear instructions and formatted outputs make the game feel more polished and enjoyable. Even a console game can feel good if the messaging is human-friendly.


๐Ÿงช Takeaways

  • Building small games is a great way to learn core programming logic.

  • Donโ€™t underestimate the power of simple projects.

  • Make your console apps user-friendly. Your users are your future testers!

  • Understand not just what to write, but why.


๐Ÿ”— GitHub Repository

๐Ÿ‘‰ MindReader Game on GitHub - https://github.com/Rushi-Unge/30Days30Projects


๐Ÿ“… Whatโ€™s Next?

For Day 3, Iโ€™ll continue exploring Java logic-based projects โ€” something more visual next time!

Follow the journey:
๐Ÿ”— @rushikesh_unge on Twitter/X
๐Ÿ”— LinkedIn Profile


๐Ÿ”– Tags

#Java #BinarySearch #30Days30Projects #100DaysOfCode #JavaBeginners #CLIgame #MindReader #RushikeshJavaLab #ConsoleApps

0
Subscribe to my newsletter

Read articles from Rushikesh Unge directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rushikesh Unge
Rushikesh Unge