Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// I have seen other solutions to this question, but for my version i cannot use the java.util.Random function for REQ 2, so is there

// I have seen other solutions to this question, but for my version i cannot use the java.util.Random function for REQ 2, so is there a way to do it without it?

* 1. Solution to P3

* 2. Questions for P4. Comments starting with REQ represent the questions.

*

* Features:

* - We have from 1 to 3 players

* - We have many questions. Each player may be asked more than one question.

* - User can play many rounds of the game.

*

* Focus:

* - Arrays and Methods

*

* Aim:

* - Organize code and avoid code redundancy by the use of methods

*/

public class Main {

static Game game;

//Two arrays for questions and answers (both are global, i.e., accessible by all code in the class).

//REQ1: Replace array values with real questions/answers

static String[] questions = {"What is the Capital of India?", "What Chemical Elemant is K?", "What year did WWII End?", "How Many Provinces in Canada?", "First Prime Minister of Canada", "Name the world's largest ocean?", "What Sport is popular in Canada", "How many Plamets in our Solar System?", "Which planet is closest to the sun?"};

static String[] answers = {"New Delhi", "Potassium", "1945", "10", "John MacDonald", "Pacific", "Hockey", "8", "Mercury"};

public static void main(String[] args) {

String ans;

do{

//Reset the game

game = new Game();

//Get number of players (from 1 to 3)

int numPlayers = game.askForInt("How many players", 1, 3);

//Add up to 3 players to the game

for (int i = 0; i < numPlayers; i++) {

String name = game.askForText("What is player " + i + " name?");

game.addPlayer(name);

}

//REQ2: Call a method to shuffle questions and answers. For that, you need to create a method with the header: void shuffleQuestions();

//REQ3: - Calculate the maximum number of questions each player could be asked (equals to the number of available questions divided by numPlayers). Store this value in a variable maxQuestions

// - Ask the user how many questions should be given to each player. The value read from the user should not exceed MaxQuestion. Store this value in a variable numQuestions.

// - Ask each player the next unanswered question (e.g., player 0 gets the first question. If it is answered correctly, then player1 gets the next question in the array, otherwise player1 gets the same question again, and so on).

// Assume that an incorrectly answered question will keep popping up until it is correctly answered.

// Hint: you need to create a for loop that repeats the below code block numQuestions times.

// Hint: you need to have a variable that keeps track of the next question to be offered.

for (int i = 0; i < numPlayers; i++) {

game.setCurrentPlayer(i);//draw rectangle around player 0, and currentPlayer = player0

String answer = game.askForText(questions[i]);

if(answers[i].equals(answer))

game.correct(); //display "Correct", increment score, change frame color to green

else

game.incorrect(); //display "incorrect", change frame color of player to red

}

//Do you want to play again? make sure you get valid input

ans = game.askForText("Play again? (Y/N)");

while(ans != null && !ans.toUpperCase().equals("Y") && !ans.toUpperCase().equals("N"))

ans = game.askForText("Invalid input. Play again? (Y/N)");

}while(ans.toUpperCase().equals("Y")); //play again if the user answers "Y" or "y"

System.exit(1); //This statement terminates the program

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions