๐ง Day 2 โ MindReader Game | #30Days30Projects


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/Concept | Usage |
Scanner | Taking input from the user |
for loop | Attempt counter |
Binary Search logic | To guess userโs number efficiently |
System.out.println | Formatting console for user instructions |
๐ง How the Game Works
You secretly think of a number between 1 and 100.
The computer guesses a number.
You reply with:
low
โ if your number is greaterhigh
โ if your number is lesscorrect
โ if the guess is right!
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
Subscribe to my newsletter
Read articles from Rushikesh Unge directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
