Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import javax.swing. * ; import java.awt. * ; import java.awt.event. * ; import java.io . BufferedReader; import java.io . FileReader; import java.io . IOException; import

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
class EarnDiamondGame {
private static final int EASY =1;
private static final int MODERATE =2;
private static final int DIFFICULT =3;
private JFrame frame;
private JPanel panel;
private JTextArea questionArea;
private ButtonGroup optionsGroup;
private JRadioButton[] options;
private JButton askQuestionButton, playGameButton;
private JLabel scoreLabel;
private int score =0;
private ArrayList questions;
EarnDiamondGame(){
frame = new JFrame("Earn Diamond Game");
panel = new JPanel();
questionArea = new JTextArea(5,30);
optionsGroup = new ButtonGroup();
options = new JRadioButton[4];
for (int i =0; i <4; i++){
options[i]= new JRadioButton();
optionsGroup.add(options[i]);
}
askQuestionButton = new JButton("Ask & Answer");
playGameButton = new JButton("Play Game");
scoreLabel = new JLabel("Score: "+ score);
panel.setLayout(new GridLayout(0,1));
panel.add(questionArea);
for (int i =0; i <4; i++){
panel.add(options[i]);
}
panel.add(askQuestionButton);
panel.add(playGameButton);
panel.add(scoreLabel);
loadQuestions();
askQuestionButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
askQuestion();
}
});
playGameButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
playGame();
}
});
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private void loadQuestions(){
questions = new ArrayList<>();
String csvFile = "questions.csv";
String line;
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))){
while ((line = br.readLine())!= null){
String[] data = line.split(",");
questions.add(data);
}
} catch (IOException e){
e.printStackTrace();
}
}
private void askQuestion(){
Random rand = new Random();
int category = getCategory();
ArrayList filteredQuestions = new ArrayList<>();
for (String[] q : questions){
if (Integer.parseInt(q[1])== category){
filteredQuestions.add(q);
}
}
if (!filteredQuestions.isEmpty()){
int randomIndex = rand.nextInt(filteredQuestions.size());
String[] selectedQuestion = filteredQuestions.get(randomIndex);
questionArea.setText(selectedQuestion[2]);
for (int i =0; i <4; i++){
options[i].setText(selectedQuestion[i +3]);
}
int selectedOption =-1;
for (int i =0; i <4; i++){
if (options[i].isSelected()){
selectedOption = i;
break;
}
}
if (selectedOption !=-1 && options[selectedOption].getText().equals(selectedQuestion[7])){
if (category == EASY){
score +=10;
} else if (category == MODERATE){
score +=30;
} else {
score +=50;
}
scoreLabel.setText("Score: "+ score);
}
} else {
JOptionPane.showMessageDialog(frame,"No questions available for selected category.");
}
}
private void playGame(){
int totalTime = score *3;
// Simulated game logic for the matrix and diamond interaction
JOptionPane.showMessageDialog(frame, "You have "+ totalTime +" seconds for the game.");
// Simulate game completion after the determined time
simulateGameCompletion(totalTime);
}
private void simulateGameCompletion(int totalTime){
// Simulate game completion after the specified time
try {
Thread.sleep(totalTime *1000L);
} catch (InterruptedException e){
e.printStackTrace();
}
JOptionPane.showMessageDialog(frame, "Game Over! Your time is up.");
}
private int getCategory(){
int category =-1;
// Add logic to retrieve selected category (e.g., from a GUI element)
// For demonstration purposes, return a random category between 1 to 3
Random ran

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions

Question

Alcohol and drug use among student athletes

Answered: 1 week ago