-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServidorTresEnRaya.java
More file actions
102 lines (91 loc) · 3.63 KB
/
ServidorTresEnRaya.java
File metadata and controls
102 lines (91 loc) · 3.63 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
/*
* 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 TresEnRayaB;
import com.google.gson.Gson;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Vicky
*/
public class ServidorTresEnRaya implements Constantes {
public static void main(String[] args) {
new ServidorTresEnRaya().listen();
}
private void listen() {
final int PUERTO = 6000;
ServerSocket servidor = null;
try {
servidor = new ServerSocket(PUERTO);
System.out.println("Conectado al servidor: " + servidor);
} catch (IOException ex) {
System.out.println("IMPOSIBLE CONECTAR CON EL SERVIDOR " + servidor);
System.exit(-1);
}
Socket jugadorX = null, jugadorO = null;
DataOutputStream dataOutX = null;
DataOutputStream dataOutO = null;
Datos datos;
Gson gson;
Reglas reglas;
int contPartida = 1;
System.out.println("Servidor Tres en Raya prestando servicio...");
reglas = new Reglas();
try {
while (true) {
System.out.println("Esperando jugadores para la partida " + contPartida);
jugadorX = servidor.accept();
System.out.println("Jugador " + JUGADORX + " conectado a la partida " + contPartida);
dataOutX = new DataOutputStream(jugadorX.getOutputStream());
datos = new Datos();
datos.setTurno(1);
gson = new Gson();
String json = gson.toJson(datos);
dataOutX.writeUTF(json);
dataOutX.flush();
HiloServidorTresEnRaya hiloX = new HiloServidorTresEnRaya(1, jugadorX, dataOutX, reglas);
jugadorO = servidor.accept();
System.out.println("Jugador " + JUGADORO + " conectado a la partida " + contPartida);
dataOutO = new DataOutputStream(jugadorO.getOutputStream());
datos.setTurno(2);
json = gson.toJson(datos);
dataOutO.writeUTF(json);
dataOutO.flush();
HiloServidorTresEnRaya hiloO = new HiloServidorTresEnRaya(2, jugadorO, dataOutO, reglas);
datos.setEstado(EN_PARTIDA);
datos.setTurno(1);
json = gson.toJson(datos);
dataOutX.writeUTF(json);
dataOutX.flush();
dataOutO.writeUTF(json);
dataOutO.flush();
hiloX.start();
hiloO.start();
/*if(reglas.getEstado() == FINALIZADO){
System.out.println("Terminando el juego");
break;
}*/
contPartida++;
}
} catch (IOException ex) {
Logger.getLogger(ServidorTresEnRaya.class.getName()).log(Level.SEVERE, null, ex);
}finally{
try {
servidor.close();
jugadorX.close();
jugadorO.close();
dataOutX.close();
dataOutO.close();
} catch (IOException ex) {
Logger.getLogger(ServidorTresEnRaya.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}