-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
198 lines (135 loc) · 4 KB
/
Copy pathClient.java
File metadata and controls
198 lines (135 loc) · 4 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
import java.net.*;
import java.util.*;
import java.io.*;
public class Client {
// Server parameters
private final static String SERVER_NAME = "localhost";
private final static int PORT = 1200;
// Conexion variables
private static Socket socket;
private static PrintWriter out;
private static BufferedReader in;
// Client actions
private static final String[] actions = {"MOVE", "MOVE_RESPONSE", "WIN", "LOSE"};
private static boolean end = false;
private static int totalTurns = 15;
private static int turn = 0;
private static String[] log;
public static void main(String[] args) {
System.setProperty("line.separador","\r\n");
log = new String[totalTurns + 1];
initSocket();
try {
OutputStream outputStream = socket.getOutputStream();
InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream());
out = new PrintWriter(outputStream, true);
in = new BufferedReader(inputStreamReader);
joinGame();
System.out.println("");
String[] serverMessage;
while (turn < totalTurns && !end) {
serverMessage = lookForServerResponse();
chooseAction(serverMessage);
}
if (!end) {
System.out.println("EMPATE!!, se han acabado los turnos de todos los jugadores.");
}
} catch(IOException e) {
System.out.println("No es posible realizar la conexión\n" + e);
}
}
private static void initSocket() {
try {
socket = new Socket();
InetAddress IPDir = InetAddress.getByName(SERVER_NAME);
SocketAddress address = new InetSocketAddress (IPDir, PORT);
socket.connect(address);
System.out.println("Conexión establecida");
System.out.println("Servidor: " + SERVER_NAME + " " + PORT);
} catch(UnknownHostException e) {
System.out.println("Nombre del servidor desconocido\n" + e);
} catch (IOException e) {
System.out.println("No es posible realizar la conexión\n" + e);
}
}
private static void joinGame() {
out.println("JOIN_GAME\n");
}
private static String[] lookForServerResponse() {
String action = null;
String args = "null";
try {
while (action == null || action.length() < 1) {
action = in.readLine();
}
args = in.readLine();
} catch (IOException e) {
System.out.print("Error de lectura");
}
return new String[] {action, args};
}
private static void move() {
System.out.print("Te toca, introduce tu jugada: ");
String move;
do {
move = System.console().readLine();
System.out.print("INTRODUCE 4 LETRAS:\n");
} while (move.length() != 4);
log[turn] = move;
out.println("C_MOVES" + "\n" + move + "\n");
}
private static void showMoveResponse(String response) {
log[turn++] += " --> " + response;
clearScreen();
for (int i = 0; i < turn; i++) {
System.out.println(log[i]);
}
System.out.println("\nResultado: " + log[turn - 1]);
System.out.println("Esperando a los oponentes...\n");
}
private static void showWinMessage(String winMessage) {
end = true;
clearScreen();
System.out.println(winMessage);
}
private static void chooseAction(String[] serverMessage) {
String action = serverMessage[0];
String args = serverMessage[1];
for (int i = 0; i < actions.length; i++) {
if (actions[i].equals(action)) {
switch (i) {
case 0: // MOVE
move();
break;
case 1: // MOVE RESPONSE
showMoveResponse(args);
break;
case 2: // WIN
showWinMessage(args);
break;
case 3: // LOSE
gameOver(args);
break;
default:
System.out.println("Invalid action key " + action);
break;
}
return;
}
}
}
private static void gameOver(String id) {
int fakeID = Integer.parseInt(id) + 1;
clearScreen();
System.out.println("Ha ganado el jugador " + fakeID + "\nGAME OVER");
end = true;
}
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
try {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
} catch(IOException ex) {}
catch (InterruptedException ex) {}
}
}