Skip to content

Commit 8074055

Browse files
authored
Add files via upload
1 parent 5827ea2 commit 8074055

3 files changed

Lines changed: 247 additions & 0 deletions

File tree

Main.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Main {
2+
private static String[] rateWoerter;
3+
private static String[] hilfsWoerter;
4+
public static String rateWort;
5+
public static int wordIndex = 0;
6+
7+
public Main() {
8+
PythonBackground.createList();
9+
rateWoerter = PythonBackground.rateWoerter();
10+
rateWort = rateWoerter[0];
11+
new UserInterface();
12+
13+
while (true) {
14+
UserInterface.switchTimerState();
15+
UserInterface.timer();
16+
}
17+
}
18+
19+
public static String[] getRateWoerter() {
20+
return rateWoerter;
21+
}
22+
23+
public static String[] getHilfsWoerter() {
24+
hilfsWoerter = PythonBackground.hilfsWoerter(rateWort);
25+
return hilfsWoerter;
26+
}
27+
28+
public static void main(String args[]) {
29+
new Main();
30+
}
31+
}

PythonBackground.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import org.python.util.PythonInterpreter;
2+
3+
public class PythonBackground {
4+
private static PythonInterpreter pyInterp = new PythonInterpreter();
5+
private static boolean statusReady = false;
6+
private static int listLength = 0;
7+
8+
public static void createList() {
9+
pyInterp.exec("import os");
10+
pyInterp.exec("from random import shuffle");
11+
12+
pyInterp.exec("list = []");
13+
pyInterp.exec(
14+
"def createList(): " + "\n" +
15+
" global list " + "\n" +
16+
" try: " + "\n" +
17+
" file = open(os.getcwd() + '/WordList.txt', 'r') " + "\n" +
18+
" lines = file.readlines() " + "\n" +
19+
" for i in range(lines.__len__()): " + "\n" +
20+
" list.append(lines[i].replace('\\n', '').split(','))" + "\n" +
21+
" file.close() " + "\n" +
22+
" shuffle(list) " + "\n" +
23+
" list.append(['Kein weiteres Wort!']) " + "\n" +
24+
" success = True " + "\n" +
25+
" except: " + "\n" +
26+
" success = False " + "\n" +
27+
" return success "
28+
);
29+
30+
pyInterp.exec("success = createList()");
31+
pyInterp.exec("createList = None");
32+
33+
statusReady = (Boolean) pyInterp.get("success").__tojava__(Boolean.class);
34+
pyInterp.exec("success = None");
35+
}
36+
37+
public static String[] rateWoerter() {
38+
if (statusReady) {
39+
int len = getListLength();
40+
String[] woerter = new String[len];
41+
42+
for (int i = 0; i < len; i++) {
43+
pyInterp.exec(String.format("currElement = list[%d][0]", i));
44+
woerter[i] = pyInterp.get("currElement").toString();
45+
}
46+
pyInterp.exec("currElement = None");
47+
return woerter;
48+
}
49+
50+
return new String[]{"Keine Wortliste vorhanden!"};
51+
}
52+
53+
public static String[] hilfsWoerter(String rateWort) {
54+
if (statusReady) {
55+
pyInterp.exec(String.format("rateWort = list[%d][0]", Main.wordIndex));
56+
if (pyInterp.get("rateWort").toString().equals(rateWort)) {
57+
pyInterp.exec(String.format("helpCount = list[%d].__len__()", Main.wordIndex));
58+
int helpCount = (int) pyInterp.get("helpCount").__tojava__(int.class);
59+
String[] woerter = new String[helpCount - 1];
60+
for (int i = 1; i < helpCount; i++) {
61+
pyInterp.exec(String.format("currElement = list[%d][%o]", Main.wordIndex, i));
62+
woerter[i - 1] = pyInterp.get("currElement").toString();
63+
}
64+
pyInterp.exec("currElement = None");
65+
pyInterp.exec("helpCount = None");
66+
pyInterp.exec("rateWort = None");
67+
return woerter;
68+
}
69+
return new String[]{"Keine Hilfe vorhanden!"};
70+
}
71+
72+
return new String[]{"Keine Wortliste vorhanden!"};
73+
}
74+
75+
public static int getListLength() {
76+
if (statusReady && listLength == 0) {
77+
pyInterp.exec("len = list.__len__()");
78+
listLength = (int) pyInterp.get("len").__tojava__(int.class);
79+
pyInterp.exec("len = None");
80+
return listLength;
81+
}
82+
83+
return listLength;
84+
}
85+
}

UserInterface.java

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import java.awt.event.ActionEvent;
2+
import java.awt.event.ActionListener;
3+
import javax.swing.JButton;
4+
import javax.swing.JCheckBox;
5+
import javax.swing.JFrame;
6+
import javax.swing.JLabel;
7+
8+
public class UserInterface extends JFrame {
9+
private class NextWordListener implements ActionListener {
10+
@Override
11+
public void actionPerformed(ActionEvent e) {
12+
String[] rateWoerter = Main.getRateWoerter();
13+
if (Main.wordIndex < rateWoerter.length - 1) {
14+
Main.wordIndex += 1;
15+
Main.rateWort = rateWoerter[Main.wordIndex];
16+
output.setText("Bitte erkl\u00e4re: " + Main.rateWort);
17+
helpOutput.setText("");
18+
helpIndex = 0;
19+
help.setEnabled(true);
20+
startTime = System.nanoTime();
21+
if (Main.rateWort == rateWoerter[rateWoerter.length - 1]) {
22+
helpOutput.setVisible(false);
23+
timerOnOff.setEnabled(false);
24+
timerOnOff.setSelected(false);
25+
time.setVisible(false);
26+
nextWord.setEnabled(false);
27+
help.setEnabled(false);
28+
output.setText(Main.rateWort);
29+
}
30+
}
31+
}
32+
}
33+
34+
private class HelpListener implements ActionListener {
35+
@Override
36+
public void actionPerformed(ActionEvent e) {
37+
String[] helpWords = PythonBackground.hilfsWoerter(Main.rateWort);
38+
if (helpIndex < helpWords.length) {
39+
String helpWord = helpWords[helpIndex];
40+
helpOutput.setText("Tipp: " + helpWord);
41+
helpIndex++;
42+
} else {
43+
help.setEnabled(false);
44+
helpOutput.setText("Kein Tipp vorhanden!");
45+
}
46+
}
47+
}
48+
49+
private static final long serialVersionUID = 541391875104762428L;
50+
private static JButton nextWord;
51+
private static JButton help;
52+
private static JLabel output;
53+
private static JLabel helpOutput;
54+
private static JLabel time;
55+
private static JCheckBox timerOnOff;
56+
private static int helpIndex = 0;
57+
private static long startTime;
58+
59+
public UserInterface() {
60+
getContentPane().setLayout(null);
61+
setupGUI();
62+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
63+
startTime = System.nanoTime();
64+
}
65+
66+
private void setupGUI() {
67+
nextWord = new JButton();
68+
nextWord.setLocation(50, 300);
69+
nextWord.setSize(150, 50);
70+
nextWord.setText("N\u00e4chstes Wort");
71+
nextWord.setToolTipText("Zeigt das n\u00e4chste Wort an!");
72+
nextWord.addActionListener(new NextWordListener());
73+
getContentPane().add(nextWord);
74+
75+
help = new JButton();
76+
help.setLocation(245, 300);
77+
help.setSize(100, 50);
78+
help.setText("Hilfe");
79+
help.setToolTipText("Zeigt einen Tipp an!");
80+
help.addActionListener(new HelpListener());
81+
getContentPane().add(help);
82+
83+
output = new JLabel();
84+
output.setLocation(90, 150);
85+
output.setSize(200, 50);
86+
output.setText("Bitte erkl\u00e4re: " + Main.rateWort);
87+
getContentPane().add(output);
88+
89+
helpOutput = new JLabel();
90+
helpOutput.setLocation(90, 205);
91+
helpOutput.setSize(200, 50);
92+
helpOutput.setText("");
93+
getContentPane().add(helpOutput);
94+
95+
time = new JLabel();
96+
time.setLocation(90, 80);
97+
time.setSize(200, 50);
98+
time.setVisible(false);
99+
time.setText("Ben\u00f6tigte Zeit: ");
100+
getContentPane().add(time);
101+
102+
timerOnOff = new JCheckBox();
103+
timerOnOff.setLocation(90, 25);
104+
timerOnOff.setSize(200, 50);
105+
timerOnOff.setText("Ben\u00f6tigte Zeit anzeigen?");
106+
timerOnOff.setSelected(false);
107+
getContentPane().add(timerOnOff);
108+
109+
setTitle("Informatrix Random Word Generator");
110+
setSize(400, 400);
111+
setVisible(true);
112+
setResizable(false);
113+
}
114+
115+
public static void switchTimerState() {
116+
try {
117+
Thread.sleep(1L);
118+
} catch (Exception exception) {}
119+
120+
if (timerOnOff.isSelected()) {
121+
time.setVisible(true);
122+
} else {
123+
time.setVisible(false);
124+
}
125+
}
126+
127+
public static void timer() {
128+
String timeStr = String.valueOf((System.nanoTime() - startTime) * 0.000000001);
129+
time.setText(timeStr.substring(0, timeStr.indexOf(".", 0)) + " Sekunden");
130+
}
131+
}

0 commit comments

Comments
 (0)