-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEchothread.java.bak
152 lines (124 loc) · 5.31 KB
/
Echothread.java.bak
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
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 Echothread implements Runnable{
private ServerSocket server;
private int port ;
private ObjectOutputStream SocketOutput;
private ObjectInputStream SocketInput;
private int LP = 5;
public Echothread(int port){
this.port = port;
try{
server = new ServerSocket(port);
}catch(Exception e){
}
}
@Override
public void run(){
String [] Word = { "Avatar","Titanic","Jurassic World",
"The Avengers","Black Panther","Frozen,Beauty and the Beast","Incredibles",
"Iron Man","Minions","Aquaman",
"Captain Marvel","Skyfall","The Dark Knight Rises","Toy Story",
"Pirates of the Caribbean","Despicable Me","Jurassic Park","Finding Dory",
"Star Wars","Alice in Wonderland","Zootopia","The Hobbit","The Dark Knight",
"The Lion King","The Jungle Book","Finding Nemo"};
String RandomWord;
char[] SecretWord;
String User = "";
int MissCount = 0;
char[] Miss = new char[5];
boolean LetterF = false, solved = false;
RandomWord = Word[ (int)(Math.random() * Word.length) ].toLowerCase();
SecretWord = new char[RandomWord.length()];
int IsWin = 0;
int IsLose = 0;
for (int i = 0; i < RandomWord.length(); i++) {
if (RandomWord.charAt(i) == ' ') {
SecretWord[i] = ' ';
} else {
SecretWord[i] = '_';
}
}
StringBuilder RespondSecretWord = new StringBuilder();
int RespondMissCount = MissCount;
StringBuilder RespondMiss = new StringBuilder();
//// SecretWord|miss_count|Miss|IsWin|IsLose
System.out.println("Start Games : " + RandomWord);
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("\nHidden Word: ");
RespondSecretWord.delete(0, RespondSecretWord.length());
for (int i = 0; i < RandomWord.length(); i++) {
System.out.print(SecretWord[i] + " ");
RespondSecretWord.append(SecretWord[i]).append(" ");
}
System.out.print("\nMisses: ");
RespondMiss.delete(0, RespondMiss.length());
for (int i = 0; i < Miss.length; i++) {
System.out.print(Miss[i]);
RespondMiss.append(Miss[i]).append(" ");
}
/// check action which Clint do
String action = (String) SocketInput.readObject();
System.out.println(action);
if (action.equals("Start") || action.equals("GetStatus")){
/// response games status
RespondMissCount = MissCount;
SocketOutput.writeObject(RespondSecretWord + "@" + RespondMissCount + "@" + RespondMiss + "@" + IsWin + "@" + IsLose );
}else if (action.equals("GetAnswer") && IsLose == 1) {
SocketOutput.writeObject(RandomWord);
}else if (action.equals("Exit")) {
SocketOutput.close();
SocketInput.close();
socket.close();
}else if (action.substring(0,5).equals("send:")){
/// get Client input
User = action.substring(5,6);
System.out.print("\nGuess: " + User);
/// Game Logical
LetterF = false;
for (int i = 0; i < RandomWord.length(); i++) {
if (User.toLowerCase().charAt(0) == RandomWord.toLowerCase().charAt(i)) {
SecretWord[i] = RandomWord.charAt(i);
LetterF = true;
}
}
if (!LetterF) {
Miss[MissCount] = User.charAt(0);
MissCount++;
}
int hidden_count = 0;
for (int i = 0; i < RandomWord.length(); i++) {
if ('_' == SecretWord[i])
hidden_count++;
}
if (hidden_count > 0) {
solved = false;
} else{
solved = true;
}
}
/// check win or lose
if (MissCount >= LP){
IsLose = 1;
}
if (solved){
IsWin = 1;
}
}
}catch(Exception e){
System.out.println(e);
}
}
}
}