-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
54 lines (50 loc) · 2.08 KB
/
Client.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException {
// write your code here
String host = "127.0.0.1";
int port = 4001;
try (Socket socket = new Socket(host, port)) {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Scanner scanner = new Scanner(System.in);
String line = "";
String input = "";
while(!input.equalsIgnoreCase("quit")){
line = in.readLine();
//print response from client
if (line.trim().equals("SETPHRASE")){
System.out.print("Enter a phrase for your opponent to guess: ");
input = scanner.nextLine();
//send the secret phrase to the server
out.println(input);
out.flush();
System.out.println("Got it. Your secret phrase is: " + input);
System.out.println("Waiting for your opponent to guess.");
} else if (line.trim().equals("GUESS")) {
System.out.print("Guess a letter: ");
input = scanner.nextLine();
out.println(input);
out.flush();
} else if (line.trim().equals("GUESSAGAIN")) {
System.out.print("You already guessed that letter. Guess again: ");
input = scanner.nextLine();
out.println(input);
out.flush();
} else if (line.trim().equals("ENDGAME")) {
System.exit(0);
} else {
System.out.println(line);
}
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}