-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer1.java
72 lines (58 loc) · 2.66 KB
/
Player1.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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 Player1 {
public static Socket socket = null;
public static void main(String[] args) {
try {
socket = new Socket("localhost", 20);
System.out.println("\n\nConnected to Server\n" + "Socket: " + socket.getInetAddress() + ":" + socket.getPort());
System.out.println("===========================================");
} catch (IOException e) {
System.out.print("Connection to network can not be established");
}
BufferedReader in;
PrintWriter out;
Scanner consoleInput = new Scanner(System.in);
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
System.out.println("Server: " + in.readLine());
System.out.print("Enter player name: ");
String you = consoleInput.nextLine();
out.println(you);
//Player getting game rules
String gameRules = in.readLine();
System.out.println("Server: " + gameRules);
System.out.println("=======================================================================");
while (true) {
System.out.print(you + ": ");
String userInput = consoleInput.nextLine();
switch (userInput) {
case "1" -> System.out.println(you + ": Rock\n");
case "2" -> System.out.println(you + ": Paper\n");
case "3" -> System.out.println(you + ": Scissors\n");
default -> System.out.println(you + ": Invalid input!\n");
}
out.println(userInput);
System.out.print("Server: ");
String serverTurn = in.readLine();
System.out.println(serverTurn);
switch (serverTurn) {
case "1" -> System.out.println("Server: Rock\n");
case "2" -> System.out.println("Server: Paper\n");
case "3" -> System.out.println("Server: Scissors\n");
default -> System.out.println("Server: Can't play unless your input is valid!\n");
}
System.out.print("Result: ");
System.out.println(in.readLine());
System.out.println("=================================================");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}