-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathClient.java
More file actions
104 lines (83 loc) · 2.62 KB
/
Client.java
File metadata and controls
104 lines (83 loc) · 2.62 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
/**
* The client class creates a connection to the server at default port 1337.
* Waits for the user to input a single character with the keyboard. The
* character has to be ’R’ (rock), ’P’ (paper) or ’S’ (scissors). Sends the
* character to the server via the TCP protocol. Waits for a reply from the
* Server. Prints the message received from the Server. Closes the connection.
*
* @author Mathias Schilling <https://github.com/pinkbigmacmedia>
* @version 1.0
*
*/
package PaperScissorsStone;
import java.io.*;
import java.net.*;
class Client {
/**
* The host
*
* @var string
*
*/
private static String host = "localhost";
/**
* The port
*
* @var integer
*/
private static Integer port = 1337;
/**
* The version of the client class
*
* @var double
*/
private static Double versionNumber = 1.0;
/**
* A short welcome msg
*
* @var string
*/
private static String msgWelcome = "--- Welcome to Paper Scissors Stone V. "
+ versionNumber + " --- \n";
/**
* The help context
*
* @var string
*
*/
private static String msgRules = "\nRule set:\n - (R)ock beats (S)cissors\n - (S)cissors beats (P)aper\n - (P)aper beats (R)ock\n";
public static void main(String args[]) throws Exception {
String input = "";
String response;
System.out.println(Client.msgWelcome);
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
System.in));
Socket clientSocket = new Socket(Client.host, Client.port);
DataOutputStream outToServer = new DataOutputStream(
clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
do {
if (input.equals("-rules")) {
System.out.println(Client.msgRules);
}
// Prompt user for select rock, paper or scissors ...
System.out
.println("Start the game by selecting (R)ock (P)aper, (S)cissors");
System.out.print("or type \"-rules\" in order to see the rules: ");
input = inFromUser.readLine();
} while (!input.equals("R") && !input.equals("P") && !input.equals("S"));
// Transmit input to the server and provide some feedback for the user
outToServer.writeBytes(input + "\n");
System.out
.println("\nYour input ("
+ input
+ ") was successfully transmitted to the server. Now just be patient and wait for the result ...");
// Catch respones
response = inFromServer.readLine();
// Display respones
System.out.println("Response from server: " + response);
// Close socket
clientSocket.close();
}
}