Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I have to create a trivia game in Processing https://processing.org (Java) that reads questions from a CSV and accepts answers as keystrokes (A, B,

Hi, I have to create a trivia game in Processing https://processing.org (Java) that reads questions from a CSV and accepts answers as keystrokes (A, B, C, and D). On a good answer, the program should call the correctAnswerSubmitted function and wrong answers should trigger wrongAnswerSubmitted. The program should spend 10 seconds on each question before simply going to the next question, unless a correct answer is given, then it should go right away to the next question.

So far my code will import the CSV and display individual questions. I need assistance accepting answer input, triggering a correct/wrong response, and going to the next question as described above. Thank you! Code So Far:

import java.util.ArrayList; import java.io.*; import java.util.List; import java.util.Scanner; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit;

Table questions; //This table is the initial imported CSV String csvFile = "questions.csv"; int rowCount; //How many questions there are in the import int id; //Individual question IDs int questionCount = 100; //Maximum size I expect the array of questions could be. String[][] question = new String[questionCount][9]; //two dimensional array of questions, their answers, and correct/wrong responses. PFont f; //EPB's Antenna font (must be installed, included)

void settings() { size(1920, 1080); }

void setup() { colorMode(RGB, 255); questionCount = (questions = loadTable(csvFile, "header")).getRowCount(); //Load up the questions sheet convertTable(); // Convert the questions sheet to a two dimensional array background(0); //Make the background black f = createFont("Antenna-Regular", 90, true); // Set the font }

//Draws the background and question void draw() {

}

//Function for converting CSV table into two dimensional array String[][] convertTable() { Table questions = loadTable(csvFile, "header");

for (TableRow row: questions.rows()) { id = row.getInt("id"); question[id][0] = row.getString("id"); question[id][1] = row.getString("Question"); question[id][2] = row.getString("A"); question[id][3] = row.getString("B"); question[id][4] = row.getString("C"); question[id][5] = row.getString("D"); question[id][6] = row.getString("Correct").toLowerCase(); question[id][7] = row.getString("Correct Response"); question[id][8] = row.getString("Wrong Response"); } return question; }

//Draws the question and answer on screen when passed the array of questions and the question ID public void displayNewQuestion(String[][] question, int questionID) {

textFont(f, 90); fill(255); text(question[questionID][1], width / 16, (height / 2) - 400, width / 1.15, 600); // STEP 5 Display Text

fill(220, 20, 60); ellipse((width / 16) + 65, (height / 2) + 50, 95, 95); fill(34, 139, 34); ellipse((width / 16) + 65, (height / 2) + 150, 95, 95); fill(70, 130, 180); ellipse((width / 16) + 65, (height / 2) + 250, 95, 95); fill(255, 215, 0); ellipse((width / 16) + 65, (height / 2) + 350, 95, 95);

fill(255); textFont(f, 70); text("A: ", (width / 16) + 30, (height / 2) + 20, width / 1.15, 200); text("B: ", (width / 16) + 30, (height / 2) + 120, width / 1.15, 200); text("C: ", (width / 16) + 30, (height / 2) + 220, width / 1.15, 200); text("D: ", (width / 16) + 30, (height / 2) + 320, width / 1.15, 200);

text(question[questionID][2], (width / 16) + 150, (height / 2) + 20, width / 1.15, 200); text(question[questionID][3], (width / 16) + 150, (height / 2) + 120, width / 1.15, 200); text(question[questionID][4], (width / 16) + 150, (height / 2) + 220, width / 1.15, 200); text(question[questionID][5], (width / 16) + 150, (height / 2) + 320, width / 1.15, 200);

char correctAnswer = question[questionID][6].toLowerCase().charAt(0); if (key == correctAnswer){ correctAnswerSubmitted(question[questionID][7]); } else if (keyPressed == true){ wrongAnswerSubmitted(question[questionID][8]); } else { print("waiting "); }

}

//When called, this will generate a random number between the range of 1 and questionCount, and put that question on screen. public void looper (String[][] question){ int questionSelected = (int) random(1, questionCount); //print(questionSelected); //print(question[questionSelected][1]); displayNewQuestion(question, questionSelected); }

void correctAnswerSubmitted(String correctResponse) { print(correctResponse);

}

void wrongAnswerSubmitted(String wrongResponse) { print(wrongResponse); }

Format of questions.csv (You can copy this block directly into a blank .csv file in a text editor): id,Question,A,B,C,D,Correct,Correct Response,Wrong Response 1,Which uses more electricity?,All the lights in an average home,An 5 story elevator ride,10 loads of laundry,10 Dishwasher cycles,B,Correct! Use the stairs whenever possible to save energy!,Hold the door! 2,What type of bag is more eco-friendly?,Plastic,Paper,Both,Neither,D,That's right! Paper and plastic both harm the enviornment. Bring your own reusable bags!," here, nor there." 3,Lorem ipsum dolor set amet. Ipsum lrem. Lorem ipsum dolor set amet. Lorem ipsum dolor set amet.,Plastic,Paper test of long answer and longer answer,Both,Neither,D,That's right! Paper and plastic both harm the enviornment. Bring your own reusable bags!," here, nor there."

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

More Books

Students also viewed these Databases questions