Unquote Game(Java), Finished but playing the actual game in the end using the steps provided

Link to where I am at
Disclaimer: I am essentially posting the solution (or atleast my version of it) to the Unquote Java Game Cumulative Project 4. But because I need help for the last part. This is just my solution, try to learn from it if you get stuck in the previous steps, the “solutions” to them are essentially here.

Ok so I finished the Unquote game Cumulative Project 4 and it works, to what it seems. Though I don’t get how I am actually supposed to “play” the game in the end. Here is the final task (21/21) and there steps:

  1. Try it out!

If you’ve completed every step, Unquote is now… partially functional!

Tab over to Main.java to “play” the game:

  1. Start the game by invoking MainActivity.startNewGame();
  2. Retrieve the current Question by calling MainActivity.getCurrentQuestion();
  3. Print the Question and answers using the provided printQuestion() method
  4. Set an answer directly on the Question by modifying the playerAnswer member variable
  5. Then submit an answer by invoking MainActivity.onAnswerSubmission();
  6. Go nuts!

Im going to add the whole code base just so you guys know what exactly is going on. So here it is.
Question.java:

public class Question {
    int imageId;
    String questionText;
    String answer0;
    String answer1;
    String answer2;
    String answer3;
    int correctAnswer;
    int playerAnswer;

    public Question(int imageIdentifier, String questionString, String answerZero,String answerOne, String answerTwo, String answerThree, int correctAnswerIndex) {
        imageId = imageIdentifier;
        questionText = questionString;
        answer0 = answerZero;
        answer1 = answerOne;
        answer2 = answerTwo;
        answer3 = answerThree;
        correctAnswer = correctAnswerIndex;
        playerAnswer = -1;
    }

    boolean isCorrect() {
      return correctAnswer == playerAnswer;
    }
}

Here is MainActivity.java:

import java.util.ArrayList;

public class MainActivity {

    // TODO #1: add integer member variables here:
    int currentQuestionIndex;
    int totalCorrect;
    int totalQuestions;

    // TODO #2: add ArrayList member variable here:
    ArrayList<Question> questions = new ArrayList<Question>();

    // TODO #3 add startNewGame() here:
    void startNewGame() {
      Question question1 = new Question(921238, "How tall is the Eiffel tower?", "1024 ft", "1063 ft", "1124 ft", "1163 ft", 1);
      Question question2 = new Question(107343, "Who invented the computer algorithm?", "Charles Babbage", "John Carmack", "Alan Turing", "Ada Lovelace", 3);
      Question question3 = new Question(748294, "What is the name for the patch of skin found on your elbow?", "Elbow Skin", "Fascia Elbora", "Wenis", "Todd", 2);
      questions = new ArrayList<>();
      questions.add(question1);
      questions.add(question2);
      questions.add(question3);
      totalCorrect = 0;
      totalQuestions = questions.size();
      Question firstQuestion = chooseNewQuestion();
      //displayQuestion(firstQuestion);
      //displayQuestionsRemaining(questions.size());
    }
    // TODO #4 add chooseNewQuestion() here:
    Question chooseNewQuestion() {
      int randomNum = generateRandomNumber(2);
      currentQuestionIndex = randomNum;
      return questions.get(currentQuestionIndex);

    }
    // TODO #5 add getCurrentQuestion() here:
    Question getCurrentQuestion() {
     return questions.get(currentQuestionIndex); 
    }
    // TODO #6 add onAnswerSubmission() here:
    void onAnswerSubmission() {
      Question currentQues = getCurrentQuestion();
      if (currentQues.isCorrect()) {
        totalCorrect += 1;
        questions.remove(currentQues);
      }
      // displayQuestionsRemaining(questions.size());
      if (questions.size() == 0) {
        System.out.println("Game Over");
        startNewGame();
        chooseNewQuestion();
        // TODO: uncomment after implementing displayQuestion()
        // displayQuestion(getCurrentQuestion());
      }
    }

    int generateRandomNumber(int max) {
      double randomNumber = Math.random();
      double result = max * randomNumber;
      return (int) result;
    }

    String getGameOverMessage(int totalCorrect, int totalQuestions) {
        if (totalCorrect == totalQuestions) {
            return "You got all " + totalQuestions + " right! You won!";
        } else {
            return "You got " + totalCorrect + " right out of " + totalQuestions + ". Better luck next time!";
        }
    }
}

Then last but not least is Main.java:

public class Main {
    public static void main(String[] args) {
        MainActivity mainActivity = new MainActivity();
        mainActivity.startNewGame();
        System.out.println("Questions remaining: " + mainActivity.totalQuestions);
        Question currentQuestion = mainActivity.getCurrentQuestion();
        printQuestion(currentQuestion);
        // Play the game!
        
        
    }

    static void printQuestion(Question question) {
        System.out.println("Question: " + question.questionText);
        System.out.println("Option 1: " + question.answer0);
        System.out.println("Option 2: " + question.answer1);
        System.out.println("Option 3: " + question.answer2);
        System.out.println("Option 4: " + question.answer3);
    }
}

to my understanding the first three steps in the task 21 are already done in the main method. But what about the rest of them 4 - onward. I dont know exactly what to do to “play” the game?

I dont know exactly what to do to “play” the game?

Have you tried running it in the console?

Yes, forgot to add that:
I got this

Questions remaining: 3
Question: Who invented the computer algorithm?
Option 1: Charles Babbage
Option 2: John Carmack
Option 3: Alan Turing
Option 4: Ada Lovelace

But if im not mistaken. It is also saying I can answer the question?

Hm, I don’t know how this exercise works (I don’t have access to pro).

However, normally in java you instantiate a scanner object to get some input stream (which I don’t see here).

I see that:

  • Then submit an answer by invoking MainActivity.onAnswerSubmission();

But to me that type of code isn’t going to ask for input in the terminal (unless I’m missing something very vital here).

@miguel_4526 This type of idea

import java .util.*;

//
//
//
//
public static void main(String[] args)  
{
    Scanner sc= new Scanner(System.in); 
    System.out.print("Enter some string: ");  
    String str= sc.nextLine();          // this takes your input
    System.out.print("Your input was: "+str);
}
1 Like