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())); | ||||
| 			PrintWriter bufferSalida = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream()),true); | ||||
| 			bufferSalida.println(Servidor.mostrarSalas()+"fin"); | ||||
| 			int sala = Integer.getInteger(bufferEntrada.readLine()); | ||||
| 			Servidor.addJugador(sala, this.socket); | ||||
| 			int sala = Integer.parseInt(bufferEntrada.readLine())-1; | ||||
| 			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)) { | ||||
| 				bufferSalida.println("Esperando al segundo jugador"); | ||||
| 				synchronized (getClass()) { | ||||
| 					this.getClass().wait(); | ||||
| 				} | ||||
| 				turno = 1; | ||||
| 				bufferSalida.println("Primero"); | ||||
| 				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(); | ||||
| 				} | ||||
| 			}else { | ||||
| 				bufferSalida.println("Iniciando partida"); | ||||
| 				bufferSalida.println("Segundo"); | ||||
| 				synchronized (getClass()) { | ||||
| 					this.getClass().notifyAll(); | ||||
| 					this.getClass().wait(); | ||||
| 				} | ||||
| 			} | ||||
| 			while(continuar) { | ||||
| 				Servidor.salas.get(sala).tablero.colocarFicha(Integer.parseInt(bufferEntrada.readLine()),  | ||||
| 						Integer.parseInt(bufferEntrada.readLine()),turno); | ||||
| 				synchronized (getClass()) { | ||||
| 					this.getClass().wait(); | ||||
| 				int finalJuego = Servidor.salas.get(sala).tablero.finalJuego(); | ||||
| 				if(finalJuego != 0) { | ||||
| 					continuar = false; | ||||
| 					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) { | ||||
| 			// TODO: handle exception | ||||
| 			e.printStackTrace(); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -2,7 +2,7 @@ | ||||
| public class Main { | ||||
| 
 | ||||
| 	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) { | ||||
| 		if(this.nJugadores >=1 ) { | ||||
| 		if(this.nJugadores <= 1 ) { | ||||
| 			this.jugadores[this.nJugadores] = jugador; | ||||
| 			this.nJugadores++; | ||||
| 			return true; | ||||
|  | ||||
| @ -23,7 +23,7 @@ public class Servidor { | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
| 	public Servidor getInstance() { | ||||
| 	public static Servidor getInstance() { | ||||
| 		if(Servidor.instancia == null) { | ||||
| 			Servidor.instancia = new Servidor(); | ||||
| 		} | ||||
| @ -35,14 +35,17 @@ public class Servidor { | ||||
| 		String ret = ""; | ||||
| 		int cont = 1; | ||||
| 		for(Sala sala:Servidor.salas) { | ||||
| 			ret += cont + "Jugadores: " + sala.toString() + "\n"; | ||||
| 			ret += cont + " - Jugadores: " + sala.toString() + "\n"; | ||||
| 			cont++; | ||||
| 		} | ||||
| 		return ret; | ||||
| 	} | ||||
| 	 | ||||
| 	public static Sala addJugador(int sala, Socket socket) { | ||||
| 		Servidor.salas.get(sala).aniadirJugador(socket); | ||||
| 		return Servidor.salas.get(sala); | ||||
| 	public static boolean addJugador(int sala, Socket socket) { | ||||
| 		boolean ret = Servidor.salas.get(sala).aniadirJugador(socket); | ||||
| 		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) { | ||||
| 		if(this.casillas[x][y] != 0) { | ||||
| 		if(this.casillas[x][y] == 0) { | ||||
| 			this.casillas[x][y]=jugador; | ||||
| 			this.ultimasCoordenadas[0]=x; | ||||
| 			this.ultimasCoordenadas[1]=y; | ||||
| @ -26,7 +26,18 @@ public class Tablero { | ||||
| 		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() { | ||||
|         int ret=0; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user