-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMultiThreadRespond.java
More file actions
114 lines (106 loc) · 4.47 KB
/
MultiThreadRespond.java
File metadata and controls
114 lines (106 loc) · 4.47 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
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class MultiThreadRespond implements Runnable{
private ServerSocket server;
private int port ;
private ObjectOutputStream socketOutput;
private ObjectInputStream socketInput;
private int left = 7;
public MultiThreadRespond(int port){
this.port = port;
try{
server = new ServerSocket(port);
}catch(Exception e){
}
}
@Override
public void run(){
String [] Word = { "predator", "kingsman", "alien", "aquaman", "avengers", "cinderella","annabelle", "chucky", "incredibles", "conjuring", "frozen", "juon" };
String rand_word;
char[] hidden_word;
String user_guess = "";
int wrong_guess = 0;
char[] missed = new char[7];
boolean letter_found = false, solved = false;
rand_word = Word[ (int)(Math.random() * Word.length) ].toLowerCase();
hidden_word = new char[rand_word.length()];
int win = 0;
int lose = 0;
for (int i = 0; i < rand_word.length(); i++) {
if (rand_word.charAt(i) == ' ') {
hidden_word[i] = ' ';
} else {
hidden_word[i] = '_';
}
}
StringBuilder res_hidden_word = new StringBuilder();
int res_miss_count = wrong_guess;
StringBuilder res_missed = new StringBuilder();
System.out.println("Start Games : " + rand_word);
while(true){
try{
Socket socket = server.accept();
socketInput = new ObjectInputStream(socket.getInputStream());
socketOutput = new ObjectOutputStream(socket.getOutputStream());
boolean running = true;
while (running){
System.out.print("\nWord: ");
res_hidden_word.delete(0, res_hidden_word.length());
for (int i = 0; i < rand_word.length(); i++) {
System.out.print(hidden_word[i] + " ");
res_hidden_word.append(hidden_word[i]).append(" ");
}
String action = (String) socketInput.readObject();
System.out.println(action);
if (action.equals("start") || action.equals(" ")){
res_miss_count = wrong_guess;
socketOutput.writeObject(res_hidden_word + "@" + res_miss_count + "@" + res_missed + "@" + win + "@" + lose );
}else if (action.equals("End Game") && lose == 1) {
socketOutput.writeObject(rand_word);
}else if (action.equals("exit")) {
socketOutput.close();
socketInput.close();
socket.close();
}else if (action.substring(0,5).equals("send:")){
user_guess = action.substring(5,6);
System.out.print("\nGuess: " + user_guess);
letter_found = false;
for (int i = 0; i < rand_word.length(); i++) {
if (user_guess.toLowerCase().charAt(0) == rand_word.toLowerCase().charAt(i)) {
hidden_word[i] = rand_word.charAt(i);
letter_found = true;
}
}
if (!letter_found) {
missed[wrong_guess] = user_guess.charAt(0);
wrong_guess++;
}
int hidden_count = 0;
for (int i = 0; i < rand_word.length(); i++) {
if ('_' == hidden_word[i])
hidden_count++;
}
if (hidden_count > 0) {
solved = false;
} else{
solved = true;
}
}
if (wrong_guess >= left){
lose = 1;
}
if (solved){
win = 1;
}
}
}catch(Exception e){
System.out.println(e);
}
}
}
}