Skip to content

Commit 9a00623

Browse files
committed
added readFile and triviaLogic and lots of changes to the other Files in the project
1 parent d0a22b8 commit 9a00623

File tree

5 files changed

+226
-19
lines changed

5 files changed

+226
-19
lines changed

bin/Trivia.fxml

+22-10
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,35 @@
44
<?import javafx.scene.control.Label?>
55
<?import javafx.scene.layout.AnchorPane?>
66
<?import javafx.scene.text.Font?>
7+
<?import javafx.scene.text.Text?>
78

8-
9-
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="778.0" prefWidth="966.0" style="-fx-background-image: url('https://media.istockphoto.com/id/1154425711/vector/quiz-banner-with-yellow-neon-question-mark-in-realistic-style-on-dark-brick-wall-background.jpg');" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="TriviaController">
9+
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="776.0" prefWidth="1380.0" style="-fx-background-image: url('https://img.freepik.com/premium-vector/multiple-choice-quiz-template_515038-14332.jpg'); -fx-background-repeat: no-repeat; -fx-background-size: 1380px 776px;" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="TriviaController">
1010
<children>
11-
<Label fx:id="questionBox" alignment="CENTER" layoutX="259.0" layoutY="148.0" prefHeight="162.0" prefWidth="449.0" text="Question">
11+
<Label fx:id="ScoreBox" alignment="TOP_LEFT" layoutX="25.0" layoutY="31.0" prefHeight="130.0" prefWidth="306.0" text="Score:">
1212
<font>
1313
<Font name="Arial Black" size="24.0" />
1414
</font>
1515
</Label>
16-
<Label fx:id="ScoreBox" alignment="TOP_LEFT" layoutX="25.0" layoutY="31.0" prefHeight="130.0" prefWidth="306.0" text="SCORE: ">
16+
<Button fx:id="answer1" layoutX="209.0" layoutY="349.0" mnemonicParsing="false" onAction="#answerSelected" prefHeight="152.0" prefWidth="407.0" text="answer1">
1717
<font>
18-
<Font name="Arial Black" size="24.0" />
18+
<Font name="Tahoma" size="24.0" />
19+
</font></Button>
20+
<Button fx:id="answer3" layoutX="209.0" layoutY="528.0" mnemonicParsing="false" onAction="#answerSelected" prefHeight="152.0" prefWidth="407.0" text="answer3">
21+
<font>
22+
<Font name="Tahoma" size="24.0" />
23+
</font></Button>
24+
<Button fx:id="answer2" layoutX="872.0" layoutY="349.0" mnemonicParsing="false" onAction="#answerSelected" prefHeight="152.0" prefWidth="407.0" text="answer2">
25+
<font>
26+
<Font name="Tahoma" size="24.0" />
27+
</font></Button>
28+
<Button fx:id="answer4" layoutX="872.0" layoutY="528.0" mnemonicParsing="false" onAction="#answerSelected" prefHeight="152.0" prefWidth="407.0" text="answer4">
29+
<font>
30+
<Font name="Tahoma" size="24.0" />
31+
</font></Button>
32+
<Text fx:id="questionBox" layoutX="489.0" layoutY="152.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Question" wrappingWidth="567.13671875">
33+
<font>
34+
<Font size="24.0" />
1935
</font>
20-
</Label>
21-
<Button fx:id="answer1" layoutX="111.0" layoutY="377.0" mnemonicParsing="false" onAction="#answerSelected" prefHeight="190.0" prefWidth="356.0" text="answer1" />
22-
<Button fx:id="answer3" layoutX="111.0" layoutY="578.0" mnemonicParsing="false" onAction="#answerSelected" prefHeight="183.0" prefWidth="356.0" text="answer3" />
23-
<Button fx:id="answer2" layoutX="493.0" layoutY="377.0" mnemonicParsing="false" onAction="#answerSelected" prefHeight="190.0" prefWidth="356.0" text="answer2" />
24-
<Button fx:id="answer4" layoutX="493.0" layoutY="574.0" mnemonicParsing="false" onAction="#answerSelected" prefHeight="190.0" prefWidth="356.0" text="answer4" />
36+
</Text>
2537
</children>
2638
</AnchorPane>

src/ReadFile.java

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import java.io.File;
2+
import java.util.Random;
3+
import java.util.Scanner;
4+
5+
import javax.swing.JOptionPane;
6+
7+
import java.util.ArrayList;
8+
9+
public class ReadFile {
10+
11+
private static Scanner input;
12+
private static int counter=0;
13+
private static Random rand = new Random();
14+
private static int randomValue;
15+
private static ArrayList<Integer> wordsChosenAlready = new ArrayList<Integer>();
16+
private static String str;
17+
private static File TriviaQuestions=new File("TriviaQuestions.txt");
18+
/*
19+
* This Method opens the file and counts how many words are in the file to know what is the limit for the randomly generated number
20+
*/
21+
public static void openFile() {
22+
try {
23+
input = new Scanner(TriviaQuestions);
24+
} catch (Exception e) {
25+
JOptionPane.showMessageDialog(null, "Error! \nWas unable to open the file");
26+
System.exit(0);
27+
}
28+
}
29+
30+
public static String nextLine() {
31+
if (input.hasNext()) {
32+
return input.nextLine();
33+
}
34+
else {
35+
return ("no more questions");
36+
}
37+
}
38+
39+
/*
40+
* This method returns a random word from the text file and makes sure the word wasnt the chosen word in a previous game
41+
*/
42+
public static String getWord() {
43+
try {
44+
input = new Scanner(TriviaQuestions);
45+
} catch (Exception e) {
46+
JOptionPane.showMessageDialog(null, "Error! \nWas unable to open the file");
47+
System.exit(0);
48+
}
49+
randomValue = generateRandomNumber();
50+
for (Integer wordNumber : wordsChosenAlready) {//if the word was already chosen in the previous games, will choose another word
51+
if (wordNumber==randomValue)
52+
getWord();
53+
54+
}
55+
wordsChosenAlready.add(randomValue);//adds the number of the random word to the arraylist to make sure it is not chosen again
56+
57+
for (int i = 0; i < randomValue && input.hasNext() ; i++) {
58+
str = input.next();
59+
60+
}
61+
return str;//returns the string
62+
}
63+
/*
64+
* This method closes the file
65+
*/
66+
public static void closeInput() {
67+
input.close();
68+
}
69+
70+
/*
71+
* This method generated a random number
72+
*/
73+
private static int generateRandomNumber() {
74+
return rand.nextInt(counter);
75+
76+
}
77+
78+
}

src/Trivia.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1+
import javafx.application.Application;
2+
import javafx.fxml.FXMLLoader;
3+
import javafx.scene.Parent;
4+
import javafx.scene.Scene;
5+
import javafx.stage.Stage;
16

2-
public class Trivia {
7+
public class Trivia extends Application{
8+
public void start(Stage stage) throws Exception{
9+
Parent root = (Parent)
310

11+
FXMLLoader.load(getClass().getResource("Trivia.fxml"));
12+
13+
Scene scene = new Scene(root);
14+
stage.setTitle("Trivia");
15+
stage.setScene(scene);
16+
stage.show();
17+
}
18+
public static void main(String[] args) {
19+
launch(args);
20+
System.out.println();
421
}
22+
}

src/TriviaController.java

+92-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,95 @@
1-
/**
2-
*
3-
*/
4-
5-
/**
6-
* @author BenA
7-
*
8-
*/
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import javafx.event.ActionEvent;
4+
import javafx.fxml.FXML;
5+
import javafx.scene.control.Button;
6+
import javafx.scene.control.Label;
7+
import javafx.scene.text.Text;
8+
99
public class TriviaController {
1010

11+
@FXML
12+
private Label ScoreBox;
13+
14+
@FXML
15+
private Button answer1;
16+
17+
@FXML
18+
private Button answer2;
19+
20+
@FXML
21+
private Button answer3;
22+
23+
@FXML
24+
private Button answer4;
25+
26+
@FXML
27+
private Text questionBox;
28+
29+
private TriviaLogic game;
30+
private int score=0;
31+
32+
public void initialize() {
33+
ReadFile.openFile();
34+
game=new TriviaLogic();
35+
setScore();
36+
setGameRound();
37+
}
38+
39+
private void setGameRound(){
40+
questionBox.setText(ReadFile.nextLine());
41+
game.setCorrectAnswer(ReadFile.nextLine());
42+
ArrayList<String> answers = new ArrayList<String>();
43+
answers.add(game.getCorrectAnswer());
44+
answers.add(ReadFile.nextLine());
45+
answers.add(ReadFile.nextLine());
46+
answers.add(ReadFile.nextLine());
47+
Collections.shuffle(answers);//shuffles the answers
48+
answer1.setText(answers.get(0));
49+
answer2.setText(answers.get(1));
50+
answer3.setText(answers.get(2));
51+
answer4.setText(answers.get(3));
52+
}
53+
54+
private void setScore() {
55+
ScoreBox.setText("Score: "+score);
56+
}
57+
58+
@FXML
59+
void answerSelected(ActionEvent event) {
60+
Button btn=(Button) event.getSource();//gets the button the was pressed
61+
/*
62+
if(game.checkAnswer(btn.getText())) {
63+
score+=10;
64+
questionBox.setText("Correct Answer!");
65+
}
66+
else {
67+
score-=5;
68+
questionBox.setText("Incorrect Answer!");
69+
}
70+
*/
71+
changeQuestionBox(game.checkAnswer(btn.getText()));
72+
pause();
73+
setScore();
74+
setGameRound();
75+
}
76+
77+
private void changeQuestionBox(boolean correct) {
78+
if (correct) {
79+
score+=10;
80+
questionBox.setText("Correct Answer!");
81+
}
82+
else {
83+
score-=5;
84+
questionBox.setText("Incorrect Answer!");
85+
}
86+
}
87+
88+
private void pause() {
89+
try {
90+
Thread.sleep(2000);
91+
} catch (InterruptedException e) {
92+
e.printStackTrace();
93+
}
94+
}
1195
}

src/TriviaLogic.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
public class TriviaLogic {
3+
private String _correctAnswer;
4+
5+
public void setCorrectAnswer(String correctAnswer) {
6+
_correctAnswer = correctAnswer;
7+
}
8+
public String getCorrectAnswer() {
9+
return _correctAnswer;
10+
}
11+
12+
public boolean checkAnswer(String answer) {
13+
return (_correctAnswer.equals(answer));
14+
}
15+
}

0 commit comments

Comments
 (0)