-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMindRushClient.java
More file actions
180 lines (155 loc) · 6.93 KB
/
MindRushClient.java
File metadata and controls
180 lines (155 loc) · 6.93 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.HashSet;
import java.util.Set;
public class MindRushClient {
private static final int SERVER_PORT = 5190;
private BufferedReader in;
private PrintWriter out;
private final JFrame frame = new JFrame("Mind Rush");
private JTextArea display = new JTextArea(20, 50);
private JLabel questionLabel = new JLabel("Waiting for question...");
private JButton choiceA = new JButton("A");
private JButton choiceB = new JButton("B");
private JButton choiceC = new JButton("C");
private JButton choiceD = new JButton("D");
// private JLabel scoreLabel = new JLabel("Score: 0");
private final JButton startButton = new JButton("Start Quiz");
private final JButton playAgainButton = new JButton("Play Again");
private JPanel scoreChart = new JPanel();
private JPanel playAgain = new JPanel();
private Set<String> processedScores = new HashSet<>();
private void gameHomeMenu(String username) {
frame.getContentPane().removeAll();
frame.revalidate();
frame.repaint();
JPanel questionPanel = new JPanel();
questionPanel.setLayout(new BorderLayout());
JLabel userLabel = new JLabel(username);
questionPanel.add(questionLabel, BorderLayout.CENTER);
// questionPanel.add(scoreLabel, BorderLayout.EAST);
questionPanel.add(userLabel, BorderLayout.EAST);
frame.add(questionPanel, BorderLayout.NORTH);
JPanel choicesPanel = new JPanel();
choicesPanel.setLayout(new GridLayout(2, 2));
choicesPanel.add(choiceA);
choicesPanel.add(choiceB);
choicesPanel.add(choiceC);
choicesPanel.add(choiceD);
frame.add(choicesPanel, BorderLayout.CENTER);
JPanel bottom = new JPanel();
bottom.setLayout(new FlowLayout());
bottom.add(new JScrollPane(display));
bottom.add(startButton);
frame.add(bottom, BorderLayout.SOUTH);
};
// This Panel will be rendered when the player is done with their 5 questions
private void scoreBoard(){
JPanel score = new JPanel();
score.setLayout(new GridLayout(2,1));
scoreChart.setLayout(new GridLayout(0,1));
playAgain.setLayout(new GridLayout(1,1));
playAgain.add(playAgainButton);
score.add(scoreChart);
score.add(playAgain);
frame.getContentPane().removeAll();
frame.add(score);
frame.revalidate();
frame.repaint();
};
public MindRushClient(String serverAddr, String username) throws IOException {
Socket socket = new Socket(serverAddr, SERVER_PORT);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
out.println(username);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
//render game menu
gameHomeMenu(username);
startButton.addActionListener(e -> {
out.println("START_CLICKED");
});
playAgainButton.addActionListener(e -> {
scoreChart.setLayout(new GridLayout(0, 1)); // Reset the layout
scoreChart.revalidate(); // Update the layout
scoreChart.repaint();
out.println("NEW_GAME");
gameHomeMenu(username);
});
display.setEditable(false);
frame.pack();
frame.setVisible(true);
choiceA.addActionListener(e -> out.println("0"));
choiceB.addActionListener(e -> out.println("1"));
choiceC.addActionListener(e -> out.println("2"));
choiceD.addActionListener(e -> out.println("3"));
new Thread(() -> {
try {
String line;
while ((line = in.readLine()) != null) {
System.out.println("Received message: " + line);
if (line.startsWith("START GAME")){
display.append("Game start!\n");
} else if (line.startsWith("QUESTION:")) {
// Parse question and choices
String[] parts = line.substring(9).split(";");
// after splitting the parts should have 5 parts
// the question, choice 1, 2, 3, 4
if (parts.length == 5) {
final String questionText = parts[0];
final String[] choices = new String[] { parts[1], parts[2], parts[3], parts[4] };
SwingUtilities.invokeLater(() -> {
questionLabel.setText(questionText);
choiceA.setText(choices[0]);
choiceB.setText(choices[1]);
choiceC.setText(choices[2]);
choiceD.setText(choices[3]);
});
} else {
System.err.println("Invalid QUESTION format: " + line);
}
} else if (line.startsWith("SCORE:")) {
if (processedScores.contains(line)) {
continue;
}
processedScores.add(line);
// Update score
final String player_score = line;
JLabel score_label = new JLabel(player_score);
SwingUtilities.invokeLater(() -> {
scoreChart.add(score_label);
});
} else if(line.startsWith("GAME FINISHED")) {
SwingUtilities.invokeLater(() -> {
//Print score board
scoreBoard();
});
} else if (line.startsWith("START NEW GAME")) {
SwingUtilities.invokeLater(() -> {
scoreChart.removeAll();
gameHomeMenu(username);
});
}
else {
// Log other messages
final String message = line;
SwingUtilities.invokeLater(() -> {
display.append(message + "\n");
});
}
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
public static void main(String[] args) throws Exception {
String serverAddr = JOptionPane.showInputDialog(
"Enter IP Address of the Server:", "localhost");
String username = JOptionPane.showInputDialog(
"Enter your username:");
new MindRushClient(serverAddr, username);
}
}