In this tutorial, we will build a simple riddle game in Java. The game will ask the user a series of riddles, and the user will have a limited number of attempts to guess the answer correctly.
If the user answers correctly, they score a point. If they run out of attempts, they will lose the round and the correct answer will be displayed.
Features of the Game:
The game will have a set of riddles stored in an array or list.
The user will be prompted to answer each riddle.
The user has a limited number of attempts (e.g., 3 attempts) to answer each riddle.
If the user guesses correctly, they earn points.
After all riddles have been answered, the game will display the final score.
Complete code
import java.util.Scanner; public class RiddleGame { // Array of riddles private static String[] riddles = { "I speak without a mouth and hear without ears. I have no body, but I come alive with wind. What am I?", "You measure my life in hours, and I serve you by expiring. I'm quick when I'm thin and slow when I'm fat. What am I?", "The more of this there is, the less you see. What is it?" }; // Corresponding array of answers private static String[] answers = { "echo", "candle", "darkness" }; // Number of attempts allowed for each riddle private static final int MAX_ATTEMPTS = 3; // Method to start the game public static void startGame() { Scanner scanner = new Scanner(System.in); int score = 0; // Initialize the player's score // Loop through all riddles for (int i = 0; i < riddles.length; i++) { System.out.println("Riddle " + (i + 1) + ": " + riddles[i]); boolean answeredCorrectly = false; // Give the user multiple attempts to answer for (int attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) { System.out.print("Attempt " + attempt + " of " + MAX_ATTEMPTS + ": "); String userAnswer = scanner.nextLine().trim().toLowerCase(); // Check if the user's answer is correct if (userAnswer.equals(answers[i])) { System.out.println("Correct! You've earned 1 point.\n"); score++; // Increment score for a correct answer answeredCorrectly = true; break; // Exit the loop if the answer is correct } else { System.out.println("Wrong answer."); } } // If the user fails to answer correctly after all attempts if (!answeredCorrectly) { System.out.println("You've used all attempts. The correct answer was: " + answers[i] + "\n"); } } // Display the user's final score after all riddles System.out.println("Game Over! Your final score is: " + score + "/" + riddles.length); scanner.close(); } // Main method public static void main(String[] args) { System.out.println("Welcome to the Riddle Game!"); startGame(); } }
Explanation of the Code:
1. Array of Riddles and Answers
private static String[] riddles = { "I speak without a mouth and hear without ears. I have no body, but I come alive with wind. What am I?", "You measure my life in hours, and I serve you by expiring. I'm quick when I'm thin and slow when I'm fat. What am I?", "The more of this there is, the less you see. What is it?" }; private static String[] answers = { "echo", "candle", "darkness" };
We define two arrays: one for the riddles and one for their corresponding answers. The indexes of the riddle and its answer match, so riddles[0] has the answer answers[0].
2. MAX_ATTEMPTS Constant
private static final int MAX_ATTEMPTS = 3; This constant defines the maximum number of attempts the user will have for each riddle. In this case, the user gets 3 attempts to guess the correct answer.
3. The startGame Method
public static void startGame() { Scanner scanner = new Scanner(System.in); int score = 0; // Initialize the player's score // Loop through all riddles for (int i = 0; i < riddles.length; i++) { System.out.println("Riddle " + (i + 1) + ": " + riddles[i]); boolean answeredCorrectly = false; // Give the user multiple attempts to answer for (int attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) { System.out.print("Attempt " + attempt + " of " + MAX_ATTEMPTS + ": "); String userAnswer = scanner.nextLine().trim().toLowerCase(); // Check if the user's answer is correct if (userAnswer.equals(answers[i])) { System.out.println("Correct! You've earned 1 point.\n"); score++; // Increment score for a correct answer answeredCorrectly = true; break; // Exit the loop if the answer is correct } else { System.out.println("Wrong answer."); } } // If the user fails to answer correctly after all attempts if (!answeredCorrectly) { System.out.println("You've used all attempts. The correct answer was: " + answers[i] + "\n"); } } // Display the user's final score after all riddles System.out.println("Game Over! Your final score is: " + score + "/" + riddles.length); scanner.close(); }
Score Initialization: A variable score is initialized to keep track of the userโs correct answers.
Looping through Riddles: The game loops through the riddles array, displaying one riddle at a time.
Multiple Attempts: For each riddle, a nested loop allows the user to attempt answering the riddle up to MAX_ATTEMPTS times.
If the userโs answer is correct (matches the corresponding answers[i]), the score is incremented, and the loop breaks.
If the user fails to answer within the given attempts, the correct answer is revealed.
Final Score: After all the riddles have been asked, the game displays the user's final score.
4. Main Method
public static void main(String[] args) { System.out.println("Welcome to the Riddle Game!"); startGame(); }
The main() method is where the game begins. It prints a welcome message and calls the startGame() method to run the game logic.
Game Flow
The game starts with a welcome message.
The game presents the first riddle and gives the user three attempts to answer.
If the user answers correctly within three attempts, they earn a point.
If they fail to answer correctly, the correct answer is shown.
This process repeats for all riddles.
After all the riddles are answered, the game ends and displays the user's score.
Example Run:
Welcome to the Riddle Game! Riddle 1: I speak without a mouth and hear without ears. I have no body, but I come alive with wind. What am I? Attempt 1 of 3: wind Wrong answer. Attempt 2 of 3: sound Wrong answer. Attempt 3 of 3: echo Correct! You've earned 1 point. Riddle 2: You measure my life in hours, and I serve you by expiring. I'm quick when I'm thin and slow when I'm fat. What am I? Attempt 1 of 3: candle Correct! You've earned 1 point. Riddle 3: The more of this there is, the less you see. What is it? Attempt 1 of 3: light Wrong answer. Attempt 2 of 3: darkness Correct! You've earned 1 point. Game Over! Your final score is: 3/3
Modifying the Game
Adding more riddles: You can easily add more riddles by adding more entries to the riddles and answers arrays.
Changing the number of attempts: The number of attempts can be adjusted by changing the value of MAX_ATTEMPTS.
Enhancing the game: You could extend the game by adding features like difficulty levels, a timer, or randomizing the order of riddles.
Conclusion
This simple riddle game demonstrates how to use Java loops, conditionals, and input handling to create an interactive console-based game.
The game structure can be extended and modified to suit more advanced needs, but this version covers the basics of interactivity and scoring in a fun and engaging way.