/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ahorcado;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JOptionPane;
/**
*
* @author Alumnos
*/
public class ClienteJuego extends Frame implements ActionListener{
private TextField letra = new TextField(1);
private Label palabra = new Label("");
private Label letras_ocupadas = new Label("");
private Label ganador = new Label("");
private Button ok = new Button("Ingresar");
private Graphics g;
private int comparador[];
private String palabra_actual = "";
private int errores = 0;
Label turno;
Label turno2;
int jugador;
BufferedReader entrada;
PrintWriter salida;
public ClienteJuego(){
super("Juego del Ahorcado");
Socket socket = new Socket();
try {
socket = new Socket("localhost",2250);
entrada = new BufferedReader(new InputStreamReader(socket.getInputStream()));
salida = new PrintWriter(socket.getOutputStream(), true);
Thread lectura = new Thread( new Lector());
lectura.start();
System.out.println("Conexion Establecida con Servidor");
} catch (IOException e) {
System.err.println("No se pudo establecer la conexión");
System.exit(1);
}
setLayout(new BorderLayout());
setBackground(Color.yellow);
setResizable(false);
Canvas canvas = new Canvas();
canvas.setSize(200,300);
Panel p = new Panel(new GridLayout(5,1));
Panel p1 = new Panel();
turno = new Label("Es Turno del jugador 1 : ");
turno2 = new Label("Es Turno del jugador 1 : ");
p1.add(turno);
p1.add(letra);
p1.add(ok);
Panel p2 = new Panel();
p2.add(new Label("Palabra : "));
p2.add(palabra);
Panel p3 = new Panel();
p3.add(new Label("Ha ingresado las siguientes Letras : "));
p.add(p1);p.add(p2);p.add(p3);p.add(letras_ocupadas);p.add(ganador);
add("West",canvas);
add("Center",p);
ok.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent w){
System.exit(0);
}});
setSize(500,330);
show();
g = canvas.getGraphics();
}
@Override
public void actionPerformed(ActionEvent e) {
String l = String.valueOf( letra.getText().charAt(0) );
System.out.println("La letra a mandar es: " + l);
salida.println(l.toUpperCase());
System.out.println("Mandando Letra a salida");
}
public static void main( String args[] ){
new ClienteJuego();
}
private class Lector implements Runnable{
String cadena;
@Override
public void run() {
try {
while(true){
cadena = entrada.readLine();
if( cadena != null ){
System.out.println("Recibiendo distinto de nulo en cliente");
String caracter = String.valueOf(cadena.charAt(2));
System.out.println("Caracter es: " + caracter );
if( cadena.charAt(0) == 'N' ){
jugador = cadena.charAt(1) - 47;
setTitle("JUGADOR " + jugador);
palabra_actual = cadena.substring(2).trim();
String aux = "";
System.out.println(palabra_actual);
comparador = new int[ palabra_actual.length() ];
System.out.println("llega");
for(int i = 0; i < palabra_actual.length(); i++){
aux = aux + "_ ";
comparador[i] = 0;
}
System.out.println("el auxiliar es: " + aux);
palabra.setText(aux);
}else if( cadena.charAt(0) == 'C' ){
if( (cadena.charAt(4)-47) == jugador ){
letra.enable(true);
turno.setText("Ingresa una letra:");
}else{
letra.enable(false);
turno.setText("Espera tu turno:");
}
String aux="";
for(int i = 0; i < comparador.length; i++){
if(caracter.equals(""+palabra_actual.charAt(i)) ){
comparador[i] = 1;
}
if(comparador[i]==1)
aux += palabra_actual.charAt(i);
else
aux += "_ ";
}
palabra.setText(aux);
}else if( cadena.charAt(0) == 'I' ){
System.out.println("el jugador es: " + jugador );
System.out.println("Y el caracter es: " + (cadena.charAt(4)-47) );
if( (cadena.charAt(4)-47) == jugador ){
letra.enable(true);
turno2.setText("Ingresa una letra: ");
}else{
turno2.setText("Espera tu turno:");
letra.enable(false);
}
turno.setText("");
errores++;
String aux = letras_ocupadas.getText();
if( aux.indexOf(caracter) < 0 )
aux += " " + caracter;
letras_ocupadas.setText(aux);
switch(errores){
case 1:
g.setColor(Color.red);
g.fillRect(110,220,30,10);break;
case 2:
g.fillRect(120,20,10,200);break;
case 3:
g.fillRect(70,20,50,10);break;
case 4:
g.fillRect(68,20,5,20);break;
case 5:
g.setColor(Color.black);
g.drawOval(50,40,40,40);break;
case 6:// Cuerpo
g.drawLine(70,80,70,150);break;
case 7:// Brazos
g.drawLine(70,100,40,100);break;
case 8:
g.drawLine(70,100,100,100);break;
case 9:// Piernas
g.drawLine(70,150,40,190);break;
case 10:
g.drawLine(70,150,100,190);
ganador.setText("Perdiste!!!");
palabra.setText(palabra_actual);
}
}else if(cadena.charAt(0) == 'P' ){
String puntos = cadena.substring(1);
JOptionPane.showMessageDialog(null, "Has Perdido con " + puntos);
}else{
String puntos = cadena.substring(1);
JOptionPane.showMessageDialog(null, "Has Ganado con " + puntos);
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("CONEXIÓN FINALIZADA");
System.exit(0);
}
}
}
}