Académique Documents
Professionnel Documents
Culture Documents
Asignatura
Comunicacin de Datos y Redes
Fecha
28-04-2013
Introduccin
En el presente informe se mostraran los diferentes aspectos relacionados con el desarrollo del juego el ahorcado utilizando la arquitectura de cliente servidor, mediante las clases Socket y ServerSocketde la API de Java. Los aspectos que se explicaran sern, la definicin de la unidad de datos diseada para la transmisin de informacin cliente - servidor y el tipo de servicio de transporte utilizado.
int intentosRestantes: Este campo especifica el nmero de intentos que tiene disponible el usuario.Se inicializa siempre en seis intentos. char letra: Indicara la letra ingresada desde teclado por el jugador la cual ser enviada por el cliente al servidor para que este evale si pertenece a la palabra que el jugador est tratando de adivinar. String palabraEnProgreso: Representa el String que indica las letras de la palabra en un momento dado han sido adivinadas y las letras que no lo han sido mediante el carcter de guion bajo. int nroMensaje: nos indicar el nmero de mensajes que se han enviado desde el servidor al cliente.
De esta manera todos los campos anteriormente descritos se concatenan utilizando como separador el carcter # para formar un String de la siguiente manera:
estadoJuego+"#"+intentosRestantes+"#"+letra+"#"+palabraEnProgreso+"#"+nroMensaje
Por otra parte en el Servidor se maneja una variable llamada palabra la cual es la que representa la palabra en juego. palabra: Indicala palabra que el jugador debe tratar de adivinar. este atributo solo se encuentra en la clase ServidorTCP.
ClienteTCP.java
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.util.StringTokenizer; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Sebastian */ public class ClienteTCP { //DECLARACIONES private static final int INACTIVO = 0; private static final int JUGANDO = 1; private static final int GANO = 2; private static final int PERDIO = 3; private static final int SOLICITANDO_JUEGO_NUEVO = 4; private static final int JUEGO_TERMINADO = 5; //ATRIBUTOS DE LA CONEXION private static final int puerto = 6112; private static BufferedReader in = null; private static DataOutputStream out = null; private static Socket conexion; //ATRIBUTOS DEL JUEGO private static int estadoJuego = INACTIVO; private static int intentosRestantes = 6; private static char letra = '*'; private static String palabraEnProgreso = "*"; private static Integer nroMensaje = 0; public static void main(String[] args) { try { conexion = new Socket("186.78.24.232", 10733); BufferedReader entradaUsuario = new BufferedReader(new InputStreamReader(System.in)); System.out.println("iniciando Conexion con el servidor..."); in = new BufferedReader(new InputStreamReader(conexion.getInputStream())); out = new DataOutputStream(conexion.getOutputStream()); System.out.println("conexion realizada..."); boolean jugando = true; while (jugando == true) { if (estadoJuego == INACTIVO) { setEstadoJuego(SOLICITANDO_JUEGO_NUEVO); enviarMensaje(); imprimirSalida(); } else { //LEER MENSAJE DESDE SERVIDOR leerMensaje(in.readLine()); imprimirEntrada(); if (estadoJuego == JUGANDO) { //IMPRIMIR MENSAJE EN PANTALLA Y LEER UNA LETRA DEL CLIENTE imprimirMensajeEnPantalla(); System.out.println("ingrese una letra: "); //SETEAR LA LETRA LEIDA POR EL CLIENTE setLetra(entradaUsuario.readLine().charAt(0)); enviarMensaje(); } else { if (estadoJuego == GANO) { setEstadoJuego(JUEGO_TERMINADO);
//
//
imprimirMensajeEnPantalla(); jugando = false; System.out.println("FELICITACIONES HAS GANADO EL JUEGO EL AHORCADO"); } if (estadoJuego == PERDIO) { setEstadoJuego(JUEGO_TERMINADO); imprimirMensajeEnPantalla(); jugando = false; System.out.println("QUE LASTIMA , NO TE QUEDAN MAS INTENTOS.\nHAS PERDIDO EL JUEGO EL AHORCADO"); } System.out.println("programa terminado..."); } } } } catch (IOException ex) { System.out.println("ERROR DE ENRADA/SALIDA"); System.out.println("No fue posible realizar la conexion, posiblemente el servidor este inactivo."); } } private static void enviarMensaje() { try { if (estadoJuego == SOLICITANDO_JUEGO_NUEVO) { out.writeBytes(crearMensajeRespuesta()); } else { out.writeBytes(crearMensajeRespuesta()); } } catch (IOException iOException) { System.out.println("ERROR AL ENVIAR EL MENSAJE"); } } public static String crearMensajeRespuesta() { return estadoJuego + "#" + intentosRestantes + "#" + letra + "#" + palabraEnProgreso + "#" + nroMensaje + "\n"; } public static int getEstadoJuego() { return estadoJuego; } public static char getLetra() { return letra; } public static int getNroIntentos() { return intentosRestantes; } public static String getPalabraEnProgreso() { return palabraEnProgreso; } public static void setEstadoJuego(int estadoJuego) { ClienteTCP.estadoJuego = estadoJuego; } public static void setLetra(char letra) { ClienteTCP.letra = letra; } public static void leerMensaje(String mensaje) { StringTokenizer stk = new StringTokenizer(mensaje, "#"); while (stk.hasMoreTokens()) { estadoJuego = Integer.valueOf(stk.nextToken()); intentosRestantes = Integer.valueOf(stk.nextToken());
letra = stk.nextToken().charAt(0); palabraEnProgreso = stk.nextToken(); nroMensaje = Integer.valueOf(stk.nextToken()); } } private static void imprimirMensajeEnPantalla() { //IMPRIMIENDO MENSAJE EN PANTALLA System.out.println("########### JUEGO EL AHORCADO ###########"); imprimirAhorcado(intentosRestantes); System.out.println("\nPALABRA ACTUAL: " + getPalabraActualGuionBajo()); System.out.println("INTENTOS RESTANTES: " + intentosRestantes); } private static String getPalabraActualGuionBajo() { String[] a = palabraEnProgreso.split(""); String impr = ""; for (int i = 0; i < a.length; i++) { impr += a[i] + " "; } return impr; } private static String mostrarEstado() { if (estadoJuego == 0) { return "INACTIVO"; } else { if (estadoJuego == 1) { return "JUGANDO"; } else { if (estadoJuego == 2) { return "GANO"; } else { if (estadoJuego == 3) { return "PERDIO"; } else { if (estadoJuego == 4) { return "SOLICITANDO_JUEGO_NUEVO"; } else { return "JUEGO_TERMINADO"; } } } } } } private static void imprimirEntrada() { String a = estadoJuego + "#" + intentosRestantes + "#" + letra + "#" + palabraEnProgreso + "#" + nroMensaje; System.out.println("LEIDO POR CLIENTE: " + a + "\n" + mostrarEstado()); } private static void imprimirSalida() { String a = estadoJuego + "#" + intentosRestantes + "#" + letra + "#" + palabraEnProgreso + "#" + nroMensaje; System.out.println("ENVIADO POR CLIENTE: " + a + "\n" + mostrarEstado()); } private static void imprimirAhorcado(int intentosRestantes) { System.out.println(" -----------\n | if (intentosRestantes < 1) { System.out.println(" |\n } else { if (intentosRestantes < 2) { System.out.println(" o |\n } else { |"); |\n |\n |"); |");
o_
|\n
|\n
_o_
|\n
l|
|\n
_o_
|\n
l|l
|\n
_o_
|\n
l|l
|\n
_o_
|\n
l|l
|\n
|\n____________"
ServidorTCP.java
import import import import import import import import import java.io.BufferedReader; java.io.DataOutputStream; java.io.IOException; java.io.InputStreamReader; java.net.ServerSocket; java.net.Socket; java.util.StringTokenizer; java.util.logging.Level; java.util.logging.Logger;
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Sebastian */ public class ServidorTCP { //DECLARACIONES private static final int INACTIVO = 0; private static final int JUGANDO = 1; private static final int GANO = 2; private static final int PERDIO = 3; private static final int SOLICITANDO_JUEGO_NUEVO = 4; //ATRIBUTOS DEL SERVIDOR private static final int nroPuerto = 6112; private static boolean SocketDisponible = true; private static int conexionesRealizadas = 0; private static Socket con; //ATRIBUTOS DEL JUEGO private static int estadoJuego = INACTIVO; private static char letra = '?';
private static String palabra = "?"; private static String palabraEnProgreso = "?"; private static int nroIntentos = 6; //PALABRAS EN EL SERVIDOR static private final String[] palabrasAhorcado = {"reingenieria", "cubeta", "tunelizacion", "protocolo", "puertos", "conexion", "broadcasting", "direccion", "internet", "router", "switch", "wifi", "estandar", "socket", "transporte", "enlace", "capas", "arquitectura", "cliente", "servidor", "proxy", "firewall", "redes", "LAN", "WAN", "MAN", "hub", "concentrador", "datagrama", "puente", "fibra", "TCP", "UDP", "mascara", "gateway", "servidor", "DNS", "cliente", "conmutacion", "circuito", "satelite", "coaxial", "microondas", "seal", "ingrarrojos", "token", "anillo", "bus", "control", "flujo", "congestion", "enrutamiento", "aplicacion", "correo", "peertopeer", "reingenieria", "cubeta", "tunelizacion", "protocolo", "puertos", "conexion", "broadcasting", "direccion", "internet", "router", "switch", "wifi", "estandar", "socket", "transporte", "enlace", "capas", "arquitectura", "cliente", "servidor", "proxy", "firewall", "redes", "LAN", "WAN", "MAN", "hub", "concentrador", "datagrama", "puente", "fibra", "TCP", "UDP", "mascara", "gateway", "servidor", "DNS", "cliente", "conmutacion", "circuito", "satelite", "coaxial", "microondas", "seal", "ingrarrojos", "token", "anillo", "bus", "control", "flujo", "congestion", "enrutamiento", "aplicacion", "correo", "peertopeer"}; private static int nroMensaje = 0; public static void main(String[] args) throws IOException { ServerSocket socketDeServicio = null; try { socketDeServicio = new ServerSocket(nroPuerto); BufferedReader entrada; DataOutputStream salida; while (true) { try { if (SocketDisponible) { //EL SOCKET ESTA DISPONIBLE, POR LO TANTO ACA NO SE ESTA EN JUEGO if (estadoJuego == INACTIVO) { System.out.println("\nesperando cliente..."); con = socketDeServicio.accept(); System.out.println("conexion aceptada...\n"); salida = new DataOutputStream(con.getOutputStream()); entrada = new BufferedReader(new InputStreamReader(con.getInputStream())); conexionesRealizadas++; System.out.println("SERVIDOR : conexion aceptada a cliente " + conexionesRealizadas); /*leer primer mensaje, peticion de inicio de juego desde el *cliente.*/ leerMensaje(entrada.readLine()); // imprimirEntrada(); procesarMensaje(); salida.writeBytes(responderMensaje()); // imprimirSalida(); } } else { entrada = new BufferedReader(new InputStreamReader(con.getInputStream())); salida = new DataOutputStream(con.getOutputStream()); //EL SOCKET ESTA OCUPADO, POR LO TANTO ACA SE ESTA EN JUEGO if (estadoJuego == JUGANDO) { leerMensaje(entrada.readLine()); // imprimirEntrada(); procesarMensaje(); salida.writeBytes(responderMensaje()); // imprimirSalida(); if (estadoJuego == GANO || estadoJuego == PERDIO) { estadoJuego = INACTIVO; SocketDisponible = true; System.out.println("JUEGO NRO " + conexionesRealizadas + " TERMINADO...");
} } } } catch (java.net.SocketException e) { System.out.println("TERMINO ABRUPTO DE LA CONEXION CON EL CLIENTE."); estadoJuego = INACTIVO; SocketDisponible = true; System.out.println("JUEGO NRO " + conexionesRealizadas + " TERMINADO..."); } } } catch (IOException BindException) { System.out.println("La maquina virtual de java ya esta ocupando el socket" + "en ese puerto, intente iniciar el servicio con otro puerto"); } } private static void leerMensaje(String mensaje) { StringTokenizer stk = new StringTokenizer(mensaje, "#"); while (stk.hasMoreTokens()) { estadoJuego = Integer.valueOf(stk.nextToken()); nroIntentos = Integer.valueOf(stk.nextToken()); letra = stk.nextToken().toUpperCase().charAt(0); palabraEnProgreso = stk.nextToken().toUpperCase(); nroMensaje = Integer.valueOf(stk.nextToken()); } nroMensaje++; } private static void procesarMensaje() { if (estadoJuego == SOLICITANDO_JUEGO_NUEVO) { setSocketDisponible(false); setEstadoJuego(JUGANDO); setNroIntentos(6); setLetra('?'); setPalabra(escojerPalabraJuegoNuevo()); setPalabraEnProgreso(); } else { if (estadoJuego == JUGANDO) { if (huboAcierto()) { reemplazarLetra(); if (ganoJuego()) { estadoJuego = GANO; System.out.println("SERVIDOR : CLIENTE HA GANADO JUEGO"); } else { System.out.println("SERVIDOR : CLIENTE HA ACERTADO PALABRA"); } } else { nroIntentos--; System.out.println("SERVIDOR : SE LE HA DISMINUIDO UN INTENTO AL CLIENTE POR NO HABER ACERTADO"); if (nroIntentos == 0) { estadoJuego = PERDIO; System.out.println("SERVIDOR : CLIENTE HA PERDIDO JUEGO"); } } } else { try { System.out.println("SERVIDOR : cerrando conexion..."); con.shutdownOutput(); SocketDisponible = true; System.out.println("SERVIDOR : conexion finalizada."); } catch (IOException ex) { Logger.getLogger(ServidorTCP.class.getName()).log(Level.SEVERE, null, ex); } } }
} private static String responderMensaje() { String a = estadoJuego + "#" + nroIntentos + "#" + letra + "#" + palabraEnProgreso + "#" + nroMensaje + "\n"; return a; } public static void setSocketDisponible(boolean SocketDisponible) { ServidorTCP.SocketDisponible = SocketDisponible; } public static void setConexionesRealizadas(int conexionesRealizadas) { ServidorTCP.conexionesRealizadas = conexionesRealizadas; } public static void setEstadoJuego(int estadoJuego) { ServidorTCP.estadoJuego = estadoJuego; } public static void setLetra(char letra) { ServidorTCP.letra = letra; } public static void setNroIntentos(int nroIntentos) { ServidorTCP.nroIntentos = nroIntentos; } public static void setPalabra(String palabra) { ServidorTCP.palabra = palabra; } public static void setPalabraEnProgreso(String palabraEnProgreso) { ServidorTCP.palabraEnProgreso = palabraEnProgreso; } private static String escojerPalabraJuegoNuevo() { return palabrasAhorcado[(int) (Math.random() * palabrasAhorcado.length)]; } private static void setPalabraEnProgreso() { String p = ""; for (int i = 0; i < palabra.length(); i++) { p += "_"; } palabraEnProgreso = p; } private static boolean huboAcierto() { boolean tuvoAcierto = true; //PRIMERO DEBEMOS COMPROBAR QUE LA LETRA NO SE PREPITA CON LO QUE //YA TENEMOS COMPLETADO ACTUALMENTE tuvoAcierto = !seRepite(letra, palabraEnProgreso) && esParteDeLaPalabra(letra, palabra); return tuvoAcierto; } private static boolean seRepite(char l, String enProgreso) { boolean repite = false; char[] prog = enProgreso.toCharArray(); for (int i = 0; i < prog.length; i++) { if (l == prog[i]) { repite = true; } } return repite; } private static boolean esParteDeLaPalabra(char letra, String palabra) {
boolean esParte = false; char[] pa = palabra.toUpperCase().toCharArray(); for (int i = 0; i < pa.length; i++) { if (letra == pa[i]) { esParte = true; } } return esParte; } private static void reemplazarLetra() { String[] enProg = palabraEnProgreso.split(""); String[] pal = palabra.split(""); String reemplazada = ""; for (int i = 0; i < pal.length; i++) { if (String.valueOf(letra).equalsIgnoreCase(pal[i])) { enProg[i] = String.valueOf(letra); } reemplazada += enProg[i]; } palabraEnProgreso = reemplazada; } private static boolean ganoJuego() { if (palabraEnProgreso.equalsIgnoreCase(palabra)) { return true; } else { return false; } } private static String mostrarEstado() { if (estadoJuego == 0) { return "INACTIVO"; } else { if (estadoJuego == 1) { return "JUGANDO"; } else { if (estadoJuego == 2) { return "GANO"; } else { if (estadoJuego == 3) { return "PERDIO"; } else { if (estadoJuego == 4) { return "SOLICITANDO_JUEGO_NUEVO"; } else { return "JUEGO_TERMINADO"; } } } } } } private static void imprimirEntrada() { String a = estadoJuego + "#" + nroIntentos + "#" + letra + "#" + palabraEnProgreso + "#" + nroMensaje; System.out.println("\nLEIDO POR SERVIDOR: " + a + "\n" + mostrarEstado()); } private static void imprimirSalida() { String a = estadoJuego + "#" + nroIntentos + "#" + letra + "#" + palabraEnProgreso + "#" + nroMensaje; System.out.println("\nENVIADO POR SERVIDOR: " + a + "\n" + mostrarEstado()); } }