Funciona una sala
This commit is contained in:
		
							parent
							
								
									b067f6034f
								
							
						
					
					
						commit
						b0db99d83a
					
				| @ -18,26 +18,62 @@ public class ComunacacionJugador extends Thread{ | |||||||
| 			BufferedReader bufferEntrada = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); | 			BufferedReader bufferEntrada = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); | ||||||
| 			PrintWriter bufferSalida = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream()),true); | 			PrintWriter bufferSalida = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream()),true); | ||||||
| 			bufferSalida.println(Servidor.mostrarSalas()+"fin"); | 			bufferSalida.println(Servidor.mostrarSalas()+"fin"); | ||||||
| 			int sala = Integer.getInteger(bufferEntrada.readLine()); | 			int sala = Integer.parseInt(bufferEntrada.readLine())-1; | ||||||
| 			Servidor.addJugador(sala, this.socket); | 			if(Servidor.addJugador(sala, this.socket)) { | ||||||
|  | 				bufferSalida.println("Has entrado en la sala"); | ||||||
|  | 			}else { | ||||||
|  | 				bufferSalida.println("Error al entrar en la sala"); | ||||||
|  | 				socket.close(); | ||||||
|  | 				return; | ||||||
|  | 			} | ||||||
|  | 			 | ||||||
| 			if(Servidor.salas.get(sala).getJugador1().equals(this.socket)) { | 			if(Servidor.salas.get(sala).getJugador1().equals(this.socket)) { | ||||||
|  | 				bufferSalida.println("Esperando al segundo jugador"); | ||||||
|  | 				synchronized (getClass()) { | ||||||
|  | 					this.getClass().wait(); | ||||||
|  | 				} | ||||||
| 				turno = 1; | 				turno = 1; | ||||||
| 				bufferSalida.println("Primero"); | 				bufferSalida.println("Primero"); | ||||||
|  | 				bufferSalida.println(Servidor.salas.get(sala).tablero.getTablero()); | ||||||
| 				Servidor.salas.get(sala).tablero.colocarFicha(Integer.parseInt(bufferEntrada.readLine()),  | 				Servidor.salas.get(sala).tablero.colocarFicha(Integer.parseInt(bufferEntrada.readLine()),  | ||||||
| 						Integer.parseInt(bufferEntrada.readLine()),turno); | 						Integer.parseInt(bufferEntrada.readLine()),turno); | ||||||
| 				synchronized (getClass()) { | 				synchronized (getClass()) { | ||||||
|  | 					this.getClass().notifyAll(); | ||||||
|  | 					this.getClass().wait(); | ||||||
|  | 				} | ||||||
|  | 			}else { | ||||||
|  | 				bufferSalida.println("Iniciando partida"); | ||||||
|  | 				bufferSalida.println("Segundo"); | ||||||
|  | 				synchronized (getClass()) { | ||||||
|  | 					this.getClass().notifyAll(); | ||||||
| 					this.getClass().wait(); | 					this.getClass().wait(); | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 			while(continuar) { | 			while(continuar) { | ||||||
| 				Servidor.salas.get(sala).tablero.colocarFicha(Integer.parseInt(bufferEntrada.readLine()),  | 				int finalJuego = Servidor.salas.get(sala).tablero.finalJuego(); | ||||||
| 						Integer.parseInt(bufferEntrada.readLine()),turno); | 				if(finalJuego != 0) { | ||||||
| 				synchronized (getClass()) { | 					continuar = false; | ||||||
| 					this.getClass().wait(); | 					if(finalJuego == turno) { | ||||||
|  | 						bufferSalida.println("v"); | ||||||
|  | 					}else { | ||||||
|  | 						bufferSalida.println("f"); | ||||||
|  | 					} | ||||||
|  | 					synchronized (getClass()) { | ||||||
|  | 						this.getClass().notifyAll(); | ||||||
|  | 					} | ||||||
|  | 				}else { | ||||||
|  | 					bufferSalida.println(Servidor.salas.get(sala).tablero.getTablero()); | ||||||
|  | 					Servidor.salas.get(sala).tablero.colocarFicha(Integer.parseInt(bufferEntrada.readLine()),  | ||||||
|  | 							Integer.parseInt(bufferEntrada.readLine()),turno); | ||||||
|  | 					synchronized (getClass()) { | ||||||
|  | 						this.getClass().notifyAll(); | ||||||
|  | 						this.getClass().wait(); | ||||||
|  | 					} | ||||||
| 				} | 				} | ||||||
|  | 				 | ||||||
| 			} | 			} | ||||||
| 		}catch (Exception e) { | 		}catch (Exception e) { | ||||||
| 			// TODO: handle exception | 			e.printStackTrace(); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
| public class Main { | public class Main { | ||||||
| 
 | 
 | ||||||
| 	public static void main(String[] args) { | 	public static void main(String[] args) { | ||||||
| 		// TODO Auto-generated method stub | 		Servidor servidor = Servidor.getInstance(); | ||||||
| 
 | 
 | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -12,7 +12,7 @@ public class Sala { | |||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	public boolean aniadirJugador(Socket jugador) { | 	public boolean aniadirJugador(Socket jugador) { | ||||||
| 		if(this.nJugadores >=1 ) { | 		if(this.nJugadores <= 1 ) { | ||||||
| 			this.jugadores[this.nJugadores] = jugador; | 			this.jugadores[this.nJugadores] = jugador; | ||||||
| 			this.nJugadores++; | 			this.nJugadores++; | ||||||
| 			return true; | 			return true; | ||||||
|  | |||||||
| @ -23,7 +23,7 @@ public class Servidor { | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	public Servidor getInstance() { | 	public static Servidor getInstance() { | ||||||
| 		if(Servidor.instancia == null) { | 		if(Servidor.instancia == null) { | ||||||
| 			Servidor.instancia = new Servidor(); | 			Servidor.instancia = new Servidor(); | ||||||
| 		} | 		} | ||||||
| @ -35,14 +35,17 @@ public class Servidor { | |||||||
| 		String ret = ""; | 		String ret = ""; | ||||||
| 		int cont = 1; | 		int cont = 1; | ||||||
| 		for(Sala sala:Servidor.salas) { | 		for(Sala sala:Servidor.salas) { | ||||||
| 			ret += cont + "Jugadores: " + sala.toString() + "\n"; | 			ret += cont + " - Jugadores: " + sala.toString() + "\n"; | ||||||
| 			cont++; | 			cont++; | ||||||
| 		} | 		} | ||||||
| 		return ret; | 		return ret; | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	public static Sala addJugador(int sala, Socket socket) { | 	public static boolean addJugador(int sala, Socket socket) { | ||||||
| 		Servidor.salas.get(sala).aniadirJugador(socket); | 		boolean ret = Servidor.salas.get(sala).aniadirJugador(socket); | ||||||
| 		return Servidor.salas.get(sala); | 		if(Servidor.salas.get(sala).nJugadores==1) { | ||||||
|  | 			Servidor.salas.add(new Sala()); | ||||||
|  | 		} | ||||||
|  | 		return ret; | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | |||||||
| @ -8,7 +8,7 @@ public class Tablero { | |||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	public boolean colocarFicha(int x, int y, int jugador) { | 	public boolean colocarFicha(int x, int y, int jugador) { | ||||||
| 		if(this.casillas[x][y] != 0) { | 		if(this.casillas[x][y] == 0) { | ||||||
| 			this.casillas[x][y]=jugador; | 			this.casillas[x][y]=jugador; | ||||||
| 			this.ultimasCoordenadas[0]=x; | 			this.ultimasCoordenadas[0]=x; | ||||||
| 			this.ultimasCoordenadas[1]=y; | 			this.ultimasCoordenadas[1]=y; | ||||||
| @ -26,7 +26,18 @@ public class Tablero { | |||||||
| 		return this.ultimasCoordenadas[1]; | 		return this.ultimasCoordenadas[1]; | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	 | 	public String getTablero() { | ||||||
|  | 		String ret = ""; | ||||||
|  | 		for(int i = 0; i< 3; i++) { | ||||||
|  | 			if(i != 2) { | ||||||
|  | 				ret += this.casillas[0][i] + " " + this.casillas[1][i] + " " + this.casillas[2][i] + "\n"; | ||||||
|  | 			}else { | ||||||
|  | 				ret += this.casillas[0][i] + " " + this.casillas[1][i] + " " + this.casillas[2][i]; | ||||||
|  | 			} | ||||||
|  | 			 | ||||||
|  | 		} | ||||||
|  | 		return ret; | ||||||
|  | 	} | ||||||
| 	 | 	 | ||||||
| 	public int finalJuego() { | 	public int finalJuego() { | ||||||
|         int ret=0; |         int ret=0; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user