-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCrossword.java
121 lines (97 loc) · 4.3 KB
/
Crossword.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*Crossword.java
*Copyright 2021 mysteryLab
*/
/**
*The class Crossword extends the superclass Game. The class implements the crossword puzzle game.
*The Class displays 4 questions to which the user must give the correct answer in order to move on to the next
*level of the game.
*
*
*@version _____
*@author PATRA ROXANI,ELENI NTOUSI
*/
package mysteryLab;
import javax.swing.ImageIcon;
public class Crossword extends Game {
//Class fields
private int[] success = new int[4]; //number of right answers
private int roomNumber; //the number of the chosen room
private String roomName; //the name of the chosen room
boolean continueGame = true;
UI ui;
//Constructor
public Crossword (int room, String name, UI userInterface) {
roomNumber = room;
roomName = name;
ui = userInterface;
}
//Method getRoomName: Getter method for the field roomName
public String getRoomName() {
return roomName;
}
//Method printInstructions: prints the game's instructions on the screen
public void printInstructions() {
ui.mainTextArea.setText("Καλώς ήρθατε στο Escape room!\n"
+"Επιλέξατε να αποδράσετε από το δωμάτιο του/της " + getRoomName() + "\n"
+"Η πρώτη δοκιμάσια απαιτεί να αξιοποιήσετε το \nγνωστικό σας υπόβαθρο και να λύσετε το παρακάτω σταυρόλεξο.\n"
+"Οι κανόνες του παιχνιδιού είναι η εξής: \n"
+"Πρέπει να ανακαλύψετε και τις 4 κρυμμένες λέξεις \nπροκειμένου να προχωρήσετε στο επόμενο στάδιο.\n"
+"Μπορείτε να προσπαθήσετε όσες φορές χρειαστεί!\n"
+"Καλή Επιτυχία!\n" + printSelectionMenu());
}
//Method playGame: the main implementation of the crossword game
/**
* @param answer
*/
public void playGame(String answer) {
if (continueGame) {
ui.mainTextArea.setText(printSelectionMenu() + "\n"); //Print the game's instructions
checkAnswers(answer); //Check if the user answered correctly
continueGame = false;
for (int i = 0; i < success.length; i++) { //Check if the user has answered all the questions correctly
if (success[i] == 0) { //If there is at least one question that has not been answered
continueGame = true; //Continue playing
}
}
}
if (!continueGame) { //If all the questions have been answered correctly
ui.mainTextArea.setText("ΜΠΡΑΒΟ! Αποδράσατε από το πρώτο στάδιο του δωματίου! "
+ "Είστε έτοιμοι να προχωρήσετε!"); //Print success message
ui.showCrossword(roomNumber); //Show the finished crossword picture
ui.CWPanel.setVisible(true);
EscapeRoom.miniGame = 2; //Proceed to the next minigame
ui.cb.setVisible(true); //Set continue button visible
}
}
//Method printSelectionMenu: Creates and returns a string with the crossword questions of the chosen room
public String printSelectionMenu() {
String quest = "";
for (int i=0;i<4;i++) {
quest += (i+1) + ") " + questions[roomNumber][i] + "\n";
}
quest += "Δώστε την απάντηση σας!\n";
return quest;
}
//Method checkAnswers: checks if the answer that the user gives is the correct one
//and returns boolean value true or false for each case
public boolean checkAnswers(String answer) {
String change = answer.toUpperCase(); //Convert every answer in Upper letters in order to match the correct answer
boolean result = false;
for (int i = 0; i < 4; i++) {
if (change.equals(answers[roomNumber][(i)])) { //If the given answer is correct
result = true;
if (success[i] == 0) {
success[i]++;
ui.mainTextArea.append("Συγχαρητήρια! Βρήκατε την λέξη!\n"); //Print success message
} else {
ui.mainTextArea.append("Έχετε απαντήσει ήδη αυτή την ερώτηση. "
+ "Απάντηστε μία άλλη ερώτηση!"); //Print already answered question message
}
}
}
if (!result) { //If the given answer is wrong
ui.mainTextArea.append("Λάθος! Προσπαθήστε ξανά!"); //Print error message
}
return result;
}
}