Skip to content

Commit 7038cda

Browse files
committed
game works!
1 parent 9a00623 commit 7038cda

File tree

4 files changed

+114
-92
lines changed

4 files changed

+114
-92
lines changed

TriviaQuestions.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,4 @@ What is guacamole made of?
247247
Avocado
248248
Banana
249249
Yoghurt
250-
Chick Pea
251-
250+
Chick Pea

src/ReadFile.java

+7-44
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
import java.io.File;
2-
import java.util.Random;
32
import java.util.Scanner;
4-
53
import javax.swing.JOptionPane;
64

7-
import java.util.ArrayList;
8-
95
public class ReadFile {
10-
116
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;
177
private static File TriviaQuestions=new File("TriviaQuestions.txt");
8+
189
/*
1910
* 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
2011
*/
@@ -26,53 +17,25 @@ public static void openFile() {
2617
System.exit(0);
2718
}
2819
}
29-
20+
21+
/*
22+
* This method returns the next line if there is one and still the file end was not reached
23+
*/
3024
public static String nextLine() {
3125
if (input.hasNext()) {
3226
return input.nextLine();
3327
}
3428
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();
29+
return("Game Ended");
5930

6031
}
61-
return str;//returns the string
6232
}
33+
6334
/*
6435
* This method closes the file
6536
*/
6637
public static void closeInput() {
6738
input.close();
6839
}
6940

70-
/*
71-
* This method generated a random number
72-
*/
73-
private static int generateRandomNumber() {
74-
return rand.nextInt(counter);
75-
76-
}
77-
7841
}

src/TriviaController.java

+70-36
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import java.util.ArrayList;
22
import java.util.Collections;
3+
import java.util.Optional;
34
import javafx.event.ActionEvent;
45
import javafx.fxml.FXML;
6+
import javafx.scene.control.Alert;
57
import javafx.scene.control.Button;
8+
import javafx.scene.control.ButtonType;
69
import javafx.scene.control.Label;
10+
import javafx.scene.control.Alert.AlertType;
711
import javafx.scene.text.Text;
812

913
public class TriviaController {
@@ -27,17 +31,39 @@ public class TriviaController {
2731
private Text questionBox;
2832

2933
private TriviaLogic game;
30-
private int score=0;
31-
34+
35+
/*
36+
* This method initializes a game
37+
*/
3238
public void initialize() {
33-
ReadFile.openFile();
34-
game=new TriviaLogic();
35-
setScore();
36-
setGameRound();
39+
ReadFile.openFile();//opens the questions and answers file
40+
game=new TriviaLogic();//creates a game of triviaLogic
41+
setScore();//set the score to 0;
42+
setGameRound();//sets the game for the round
3743
}
38-
44+
45+
/*
46+
* This method sets a next round of the game
47+
* changes the question and the possible four answers
48+
*/
3949
private void setGameRound(){
40-
questionBox.setText(ReadFile.nextLine());
50+
String question=ReadFile.nextLine();
51+
if (question.equals("Game Ended")) {
52+
Alert alert = new Alert(AlertType.CONFIRMATION);
53+
alert.setTitle("Game Ended");
54+
alert.setHeaderText("You have reached the end of the game");
55+
alert.setContentText("You have reached the end of the game\nYou got "+ game.get_score() + " Points \nThanks for playing \nIf you wish to play again press: OK \nIf you wish to close the game press: CANCEL");
56+
Optional<ButtonType> option = alert.showAndWait();
57+
if(option.get()==ButtonType.OK) {
58+
restart();
59+
question=ReadFile.nextLine();
60+
}
61+
else if (option.get() == ButtonType.CANCEL){
62+
ReadFile.closeInput();//closes the Text file
63+
System.exit(0);//stops the game from running
64+
}
65+
}
66+
questionBox.setText(question);
4167
game.setCorrectAnswer(ReadFile.nextLine());
4268
ArrayList<String> answers = new ArrayList<String>();
4369
answers.add(game.getCorrectAnswer());
@@ -50,46 +76,54 @@ private void setGameRound(){
5076
answer3.setText(answers.get(2));
5177
answer4.setText(answers.get(3));
5278
}
53-
79+
80+
/*
81+
* This method sets the score
82+
*/
5483
private void setScore() {
55-
ScoreBox.setText("Score: "+score);
84+
ScoreBox.setText("Score: "+game.get_score());
5685
}
57-
86+
87+
/*
88+
* This method checks if the chosen answers is correct and sets the game for next round by calling setGameRound method
89+
*/
5890
@FXML
5991
void answerSelected(ActionEvent event) {
6092
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();
93+
displayMessage(game.checkAnswer(btn.getText()));
7394
setScore();
7495
setGameRound();
7596
}
76-
77-
private void changeQuestionBox(boolean correct) {
97+
98+
/*
99+
* This method displays a message if the answer was correct or incorrect and allows the player to choose to close the game or continue for the next question
100+
*/
101+
private void displayMessage(boolean correct) {
102+
Alert alert = new Alert(AlertType.CONFIRMATION);
103+
alert.setContentText("The answer was: " + game.getCorrectAnswer()+ "\nIf you wish to try another question press: OK \nIf you wish to close the game press: CANCEL ");
78104
if (correct) {
79-
score+=10;
80-
questionBox.setText("Correct Answer!");
105+
alert.setTitle("Correct Answer");
106+
alert.setHeaderText("+10 Points");
107+
game.setscore(game.get_score()+10);
81108
}
82109
else {
83-
score-=5;
84-
questionBox.setText("Incorrect Answer!");
110+
alert.setTitle("Incorrect Answer");
111+
alert.setHeaderText("-5 Points");
112+
game.setscore(game.get_score()-5);
113+
}
114+
Optional<ButtonType> option = alert.showAndWait();
115+
if (option.get() == ButtonType.CANCEL){
116+
ReadFile.closeInput();//closes the Text file
117+
System.exit(0);//stops the game from running
85118
}
86119
}
87-
88-
private void pause() {
89-
try {
90-
Thread.sleep(2000);
91-
} catch (InterruptedException e) {
92-
e.printStackTrace();
93-
}
120+
121+
/*
122+
* This method restarts the game if all questions in the TriviaQuestions file were answered
123+
*/
124+
private void restart() {
125+
game.setscore(0);
126+
ReadFile.openFile();
94127
}
128+
95129
}

src/TriviaLogic.java

+36-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
11

22
public class TriviaLogic {
3-
private String _correctAnswer;
3+
private String _correctAnswer;
4+
private int _score;
45

5-
public void setCorrectAnswer(String correctAnswer) {
6-
_correctAnswer = correctAnswer;
7-
}
8-
public String getCorrectAnswer() {
9-
return _correctAnswer;
10-
}
6+
/*
7+
* This method sets _correctAnswer variable to contain the correct answer
8+
*/
9+
public void setCorrectAnswer(String correctAnswer) {
10+
_correctAnswer = correctAnswer;
11+
}
1112

12-
public boolean checkAnswer(String answer) {
13-
return (_correctAnswer.equals(answer));
14-
}
13+
/*
14+
* This method returns the correct answer
15+
*/
16+
public String getCorrectAnswer() {
17+
return _correctAnswer;
18+
}
19+
20+
/*
21+
* This method checks if the answer given as parameter is equal to the correct answer, if it is it means that the answer provided as parameter is the correct answer for the question
22+
*/
23+
public boolean checkAnswer(String answer) {
24+
return (_correctAnswer.equals(answer));
25+
}
26+
27+
/*
28+
* This method sets _score variable
29+
*/
30+
public void setscore(int score) {
31+
_score=score;
32+
}
33+
34+
/*
35+
* This method returns _score variable
36+
*/
37+
public int get_score() {
38+
return _score;
39+
}
40+
1541
}

0 commit comments

Comments
 (0)