-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClienteAhorcado.java
More file actions
178 lines (159 loc) · 6.64 KB
/
ClienteAhorcado.java
File metadata and controls
178 lines (159 loc) · 6.64 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ahorcadoBC;
import com.google.gson.Gson;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Vicky
*/
public class ClienteAhorcado implements Constantes {
private Socket cliente;
private DataInputStream dataIn;
private DataOutputStream dataOut;
private Datos datos;
private int id;
public ClienteAhorcado() {
}
public static void main(String[] args) {
new ClienteAhorcado().listen();
}
private void listen() {
final int PUERTO = 6000;
final String HOST = "localhost";
Gson gson;
Scanner scanner;
try {
cliente = new Socket(HOST, PUERTO);
dataIn = new DataInputStream(cliente.getInputStream());
dataOut = new DataOutputStream(cliente.getOutputStream());
datos = new Datos();
gson = new Gson();
scanner = new Scanner(System.in);
String json = dataIn.readUTF();
datos = gson.fromJson(json, Datos.class);
System.out.println("Eres el jugador " + datos.getTurno());
id = datos.getTurno();
json = dataIn.readUTF();
datos = gson.fromJson(json, Datos.class);//lee estado
System.out.println("Comenzando partida");
char letra;
while (datos.getEstado() == EN_PARTIDA) {
System.out.println("Id: " + id);
System.out.println("Turno: " + datos.getTurno());
if (datos.getTurno() == id) {
System.out.println("\nElige una letra");
letra = scanner.nextLine().charAt(0);
datos.setLetra(letra);
json = gson.toJson(datos);
dataOut.writeUTF(json);
dataOut.flush();//manda letra
} else {
System.out.println("Jugando jugador " + datos.getTurno());
}
json = dataIn.readUTF();//lee aciertos, fallos y turno
datos = gson.fromJson(json, Datos.class);
dibujaAhorcado(datos.getFallos());
mostrarTablero();
if (datos.isGana()) {
System.out.println("Felicidades, los jugadores han ganado");
}
if (datos.isPierde()) {
System.out.println("Lo sentimos, los jugadores han perdido");
}
}
} catch (IOException ex) {
Logger.getLogger(ClienteAhorcado.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
cliente.close();
dataIn.close();
dataOut.close();
} catch (IOException ex) {
Logger.getLogger(ClienteAhorcado.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void mostrarTablero() {
for (int i = 0; i < datos.getTablero().length; i++) {
System.out.print(datos.getTablero()[i] + " ");
//System.out.print("_ ");
}
}
private void dibujaAhorcado(int fallos) {
switch (fallos) {
case 0:
System.out.println(".___________ ");
System.out.println("| | ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
break;
case 1:
System.out.println(".___________ ");
System.out.println("| | ");
System.out.println("| () ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
break;
case 2:
System.out.println(".___________ ");
System.out.println("| | ");
System.out.println("| () ");
System.out.println("| || ");
System.out.println("| || ");
System.out.println("| ");
System.out.println("| ");
break;
case 3:
System.out.println(".___________ ");
System.out.println("| | ");
System.out.println("| () ");
System.out.println("| ||\\ ");
System.out.println("| || ");
System.out.println("| ");
System.out.println("| ");
break;
case 4:
System.out.println(".___________ ");
System.out.println("| | ");
System.out.println("| () ");
System.out.println("| /||\\ ");
System.out.println("| || ");
System.out.println("| ");
System.out.println("| ");
break;
case 5:
System.out.println(".___________ ");
System.out.println("| | ");
System.out.println("| () ");
System.out.println("| /||\\ ");
System.out.println("| || ");
System.out.println("| \\ ");
System.out.println("| ");
break;
case 6:
System.out.println(".___________ ");
System.out.println("| | ");
System.out.println("| () ");
System.out.println("| /||\\ ");
System.out.println("| || ");
System.out.println("| / \\ ");
System.out.println("| ");
break;
}
}
}