This repository was archived by the owner on Jun 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameClient.java
More file actions
227 lines (197 loc) · 6.51 KB
/
Copy pathGameClient.java
File metadata and controls
227 lines (197 loc) · 6.51 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import java.net.*;
import java.io.*;
public class GameClient{
private static int mistakeCount;
private static String oppName;
static void setCount(){
mistakeCount = 3;
}
static boolean checkCount(){
if(mistakeCount == 0){
System.out.println("Sorry, We can't bear your more Mistakes\nYou Lose");
return true;
}
else
return false;
}
static boolean compareLetter(String word, char letter){
boolean flag = false;
if(word.charAt(0) == letter)
flag = true;
if(!flag){
mistakeCount--;
System.out.println("Your word begin with wrong letter \'" + word.charAt(0) + "\', Word must start with \'" + letter + "\'");
}
return flag;
}
static boolean checkValidity(String[] signal){
if(signal[0].equals("T")){
System.out.println("Your Score: " + signal[1] + " " + oppName + ": " + signal[2] + " Lives remaining: " + mistakeCount);
return true;
}
else if(signal[0].equals("F")){
System.out.println("Invalid Word");
mistakeCount--;
System.out.println("Lives remaining: " + mistakeCount);
}
else if(signal[0].equals("R")){
System.out.println("Word is repeated, Please enter new word");
}
else if(signal[0].equals("C")){
System.out.println("the word you entered is very common, please try again");
}
else if(signal[0].equals("S")){
System.out.println("Word length should be atleast 3, please try again");
System.out.print("Re-Enter your word: ");
}
return false;
}
static void rules(){
String rule = "\nWelcome to the WAR WITH WORDS (WWW)\n\n" +
"Game Rules are:\n" +
"* 1. This is 2 Player Game.\n" +
"* 2. Each Player have only 3 Lives.\n" +
"* 3. Game Score is calculated in terms of word length, after 10 moves winner will be decided.\n" +
"* 4. Lives will be deducted for following mistakes:\n" +
"* \t- Word start with different letter from the given letter\n" +
"* \t- Word length less than 3.\n" +
"* \t- If word does\'t exists in Dictionary.\n" +
"* 5. Warning will be given for following mistakes:\n" +
"* \t- For repeated word.\n" +
"* \t- For some common words like I, we, the...\n" +
"* 6. You can give up in the middle of war by sending \'q\', but in that case opponent will be declared as winner.";
System.out.println(rule);
}
public static void main(String args[]){
if(args.length != 1){
System.out.println("Invalid Number of arguments: only one argument <IP Address> is required");
}
else{
try{
int port = 2000;
int id, toss, flag;
String name, word, signal, message, msg;
boolean val = true;
String[] str;
MyStreamSocket mySocket = new MyStreamSocket(args[0], port);
rules();
// user input
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
// ask user for name
System.out.print("\nEnter your Name: ");
name = input.readLine();
mySocket.sendMessage(name);
setCount();
// Assing player id and Opponent Name
String[] c = mySocket.receiveMessage().split(",");
id = Integer.parseInt(c[0]);
oppName = c[1];
System.out.println("Your word war is against " + oppName);
// Toss and Starting Letter
String[] tossAndLetter = mySocket.receiveMessage().split(",");
toss = Integer.parseInt(tossAndLetter[0]);
char letter = tossAndLetter[1].charAt(0);
if(id == toss){
System.out.println("You won the toss");
System.out.println("Start war with letter " + letter);
System.out.print("You: ");
do{
if(checkCount()){
mySocket.close();
return;
}
word = input.readLine().toLowerCase();
val = compareLetter(word, letter);
if(val){
mySocket.sendMessage(word);
signal = mySocket.receiveMessage();
str = signal.split(",");
val = checkValidity(str);
}
}while(!val);
}
else{
System.out.println("You lost the toss");
System.out.println(oppName + " is going to start the war with letter " + letter);
}
// Communication Between Opponents
do{
msg = mySocket.receiveMessage();
str = msg.split(",");
if(str[0].equals("W")){
System.out.println("You Win the War\nYour Score : " + str[1] + " " + oppName + " score: " + str[2]);
mySocket.close();
return;
}
else if(str[0].equals("L")){
System.out.println("You Lost the War\nYour Score : " + str[1] + " " + oppName + " score: " + str[2]);
mySocket.close();
return;
}
else if(str[0].equals("D")){
System.out.println("Game Draw, both have same score\nYour Score : " + str[1] + " " + oppName + " score: " + str[2]);
mySocket.close();
return;
}
message = str[0];
if(message.equals("q")){
System.out.println(oppName + " quit the game.\nYou WON, Huurrrrraaaaaaayyyyyyyyyyy");
break;
}
System.out.println("Your Score: " + str[1] + " " + oppName + ": " + str[2] + " Lives remaining: " + mistakeCount);
System.out.println(oppName + " : " + message);
letter = message.charAt(message.length()-1);
System.out.println("Your letter is " + letter);
// write message to output Stream
System.out.print("You: ");
do{
if(checkCount()){
mySocket.close();
return;
}
if(!val){
System.out.print("Re-Enter your word: ");
}
word = input.readLine().toLowerCase();
if(word.equals("q")){
System.out.println("You quit the game in the middle of war,\nYou Lost");
mySocket.sendMessage(word);
mySocket.close();
return;
}
val = compareLetter(word, letter);
if(val){
mySocket.sendMessage(word);
signal = mySocket.receiveMessage();
str = signal.split(",");
if(str[0].equals("W")){
System.out.println("You Win the War\nYour Score : " + str[1] + " " + oppName + " score: " + str[2]);
mySocket.close();
return;
}
else if(str[0].equals("L")){
System.out.println("You Lost the War\nYour Score : " + str[1] + " " + oppName + " score: " + str[2]);
mySocket.close();
return;
}
else if(str[0].equals("D")){
System.out.println("Game Draw, both have same score\nYour Score : " + str[1] + " " + oppName + " score: " + str[2]);
mySocket.close();
return;
}
val = checkValidity(str);
}
}while(!val);
}while(true);
// Connection Close
mySocket.close();
}
catch(SocketException e){
System.out.println(e);
}
catch(Exception e){
System.out.println(e);
}
}
}
}