Proyecto Final (Boliche)
Partida(interface)
package boliche;
public interface Partida {
int TURNOS = 10;
void jugarTurno(int turno, int ronda);
Jugador buscarGanador();
void Juego();
}
package boliche;
public class Jugador {
short puntajeTotal = 0;
short[] puntos = new short[10];
boolean chuza = false;
boolean spare = false;
String nombre;
public Jugador(String nombre) {
this.nombre = nombre;
}
public byte tirar(byte desicion, byte fuerza, Pino[] pinos) {
byte pinosDerribados = 0;
for (Pino pino : pinos) {
if (!pino.dePie) {
continue;
}
if ((int)((int)(100 / pino.probCaida(tipoTiro(desicion, fuerza)))*Math.random()) == 0) {
pino.dePie = false;
pinosDerribados++;
}
}
return pinosDerribados;
}
public byte tipoTiro(byte desicion, byte fuerza) {
if (desicion == 1) {
if (fuerza >= 1 && fuerza <= 5) {
return 1;
} else if (fuerza >= 6 && fuerza <= 9) {
return 3;
} else {
return 5;
}
} else if (desicion == 2) {
if (fuerza >= 1 && fuerza <= 5) {
return 2;
} else if (fuerza >= 6 && fuerza <= 9) {
return 4;
} else {
return 6;
}
} else {
if (fuerza >= 1 && fuerza <= 5) {
return 7;
} else if (fuerza >= 6 && fuerza <= 9) {
return 8;
} else {
return 9;
}
}
}
}
package boliche;
public class Pino {
boolean dePie = true;
byte posicion;
Pino(int pos) {
posicion = (byte) pos;
dePie = true;
}
public byte probCaida(byte caso) {
if (caso == 1) {
if (posicion == 4) {
return 20;
} else {
return 1;
}
} else if (caso == 2) {
if (posicion == 6) {
return 20;
} else {
return 1;
}
} else if (caso == 3) {
if (posicion == 4) {
return 100;
} else if (posicion == 5 || posicion == 2 || posicion == 7 || posicion == 8) {
return 80;
} else if (posicion == 1 || posicion == 3 || posicion == 6 || posicion == 9) {
return 40;
} else {
return 1;
}
} else if (caso == 4) {
if (posicion == 6) {
return 100;
} else if (posicion == 3 || posicion == 5 || posicion == 9 || posicion == 10) {
return 80;
} else if (posicion == 1 || posicion == 2 || posicion == 8 || posicion == 4) {
return 40;
} else {
return 1;
}
} else if (caso == 5) {
if (posicion == 4) {
return 100;
} else if (posicion == 5 || posicion == 2 || posicion == 7 || posicion == 8) {
return 90;
} else if (posicion == 1 || posicion == 3 || posicion == 6 || posicion == 9) {
return 50;
} else {
return 10;
}
} else if (caso == 6) {
if (posicion == 6) {
return 100;
} else if (posicion == 3 || posicion == 5 || posicion == 9 || posicion == 10) {
return 90;
} else if (posicion == 1 || posicion == 2 || posicion == 8 || posicion == 4) {
return 50;
} else {
return 10;
}
} else if (caso == 7) {
if (posicion == 1) {
return 80;
} else if (posicion == 2 || posicion == 3) {
return 60;
} else if (posicion == 5) {
return 20;
} else if (posicion == 4 || posicion == 6) {
return 2;
}
return 1;
} else if (caso == 8) {
if (posicion == 1 || posicion == 2 || posicion == 3) {
return 90;
} else if (posicion == 5 || posicion == 4 || posicion == 6) {
return 60;
} else if (posicion == 8 || posicion == 9) {
return 20;
}
return 2;
} else {
if (posicion == 1 || posicion == 2 || posicion == 3) {
return 100;
} else if (posicion == 5 || posicion == 4 || posicion == 6) {
return 80;
} else if (posicion == 8 || posicion == 9) {
return 70;
}
return 50;
}
}
}
clase JuegoBoliche
package boliche;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
enum tipoTiro{SPARE,NORMAL,CHUZA}
public class JuegoBoliche implements Partida{
Jugador[] participantes=new Jugador[4];
short[][] marcador=new short[4][10];
Jugador ganador;
Pino[] pinos={new Pino(1),new Pino(2),new Pino(3),new Pino(4),new Pino(5),new Pino(6),new Pino(7),new Pino(8),new Pino(9),new Pino(10)};
tipoTiro[][] tiros=new tipoTiro[4][10];
boolean batallaTerminada=false;
ServerSocket servidor;
Socket[] datos=new Socket[4];
ObjectOutputStream[] flujosSalida=new ObjectOutputStream[4];
ObjectInputStream[] flujosEntrada=new ObjectInputStream[4];
@Override
public void jugarTurno(int i, int j) {
short pinosDerribados=0;
try {
pinosDerribados+=participantes[i].tirar(flujosEntrada[i].readByte(), flujosEntrada[i].readByte(), pinos);
if(pinosDerribados!=10){
for (int k = 0; k < 4; k++) {
flujosSalida[k].writeChar('d');
flujosSalida[k].writeByte(i);
flujosSalida[k].flush();
}
System.out.println("enviando d");
pinosDerribados+=participantes[i].tirar(flujosEntrada[i].readByte(), flujosEntrada[i].readByte(), pinos);
if(pinosDerribados==10){
tiros[i][j]=tipoTiro.SPARE;
marcador[i][j]=15;
participantes[i].puntajeTotal+=15;
for (int k = 0; k < 4; k++) {
flujosSalida[k].writeChar('e');
System.out.println("enviando e");
flujosSalida[k].flush();
}
enviarResultados(tiros[i][j],i,pinosDerribados);
}
else{
tiros[i][j]=tipoTiro.NORMAL;
marcador[i][j]=pinosDerribados;
participantes[i].puntajeTotal+=pinosDerribados;
for (int k = 0; k < 4; k++) {
flujosSalida[k].writeChar('e');
System.out.println("enviando e");
flujosSalida[k].flush();
}
enviarResultados(tiros[i][j],i,pinosDerribados);
}
}
else{
tiros[i][j]=tipoTiro.CHUZA;
marcador[i][j]=30;
participantes[i].puntajeTotal+=30;
for (int k = 0; k < 4; k++) {
flujosSalida[k].writeChar('e');
System.out.println("enviando e");
flujosSalida[k].flush();
}
enviarResultados(tiros[i][j],i,pinosDerribados);
}
} catch (IOException ex) {
Logger.getLogger(JuegoBoliche.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void enviarResultados(tipoTiro t, int turno, short pinosDerribados) throws IOException{
for (int i = 0; i < 4; i++) {
if(t==tipoTiro.CHUZA){
flujosSalida[i].writeObject("<html>"+participantes[turno].nombre+"<br>hizo una Chuza!<br>+30 puntos<html>");
}
else if(t==tipoTiro.SPARE){
flujosSalida[i].writeObject("<html>"+participantes[turno].nombre+"<br>hizo un Spare!<br>+15 puntos<html>");
}
else{
flujosSalida[i].writeObject("<html>"+participantes[turno].nombre+"<br>derribo "+pinosDerribados+" pinos<br>+"+pinosDerribados+" puntos<html>");
}
flujosSalida[i].writeByte(turno);
flujosSalida[i].writeShort(participantes[turno].puntajeTotal);
if(turno!=3)
flujosSalida[i].writeByte(turno+1);
else
flujosSalida[i].writeByte(0);
flujosSalida[i].flush();
}
}
@Override
public Jugador buscarGanador() {
if(participantes[0].puntajeTotal>participantes[1].puntajeTotal&&participantes[0].puntajeTotal>participantes[2].puntajeTotal)
return participantes[0];
else if(participantes[1].puntajeTotal>participantes[0].puntajeTotal&&participantes[1].puntajeTotal>participantes[2].puntajeTotal)
return participantes[1];
else
return participantes[2];
}
@Override
@SuppressWarnings("empty-statement")
public void Juego() {
byte i=0;
while (i<4) {
try {
servidor=new ServerSocket(5000+(i*100));
System.out.println("Esperando conexiones...");
datos[i]=servidor.accept();
System.out.println("Conexion aceptada");
flujosSalida[i]=new ObjectOutputStream(datos[i].getOutputStream());
InputStream oi=datos[i].getInputStream();
flujosEntrada[i]=new ObjectInputStream(oi);
flujosSalida[i].writeByte(i);
flujosSalida[i].flush();
System.out.println("Enviando Turno");
participantes[i]=new Jugador((String)(flujosEntrada[i].readObject()));
System.out.println("Objeto creado");
} catch (Exception ex) {
Logger.getLogger(JuegoBoliche.class.getName()).log(Level.SEVERE, null, ex);
}
i++;
}
try{
for (int j = 0; j < participantes.length; j++) {
for (int k = 0; k < participantes.length; k++) {
if(!participantes[j].equals(participantes[k])){
flujosSalida[j].writeByte(k);
flujosSalida[j].writeObject(participantes[k].nombre);
flujosSalida[j].flush();
}
}
}
}catch(Exception e){}
byte pinosDerribados;
for (byte j = 0; j < TURNOS; j++) {
for (i = 0; i < 4; i++) {
for (int k = 0; k < 10; k++) {
pinos[k].dePie=true;
}
jugarTurno(i,j);
for (int k = 0; k < 4; k++) {
if((j+1)==TURNOS){
try {
ganador=buscarGanador();
flujosSalida[k].writeBoolean(true);
flujosSalida[k].writeObject(ganador.nombre);
flujosSalida[k].flush();
} catch (IOException ex) {
Logger.getLogger(JuegoBoliche.class.getName()).log(Level.SEVERE, null, ex);
}
}
else{
try {
flujosSalida[k].writeBoolean(false);
flujosSalida[k].flush();
} catch (IOException ex) {
Logger.getLogger(JuegoBoliche.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
for (int j = 0; j < 4; j++) {
try {
flujosEntrada[j].close();
flujosSalida[j].close();
datos[j].close();
} catch (IOException ex) {
Logger.getLogger(JuegoBoliche.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
servidor.close();
} catch (IOException ex) {
Logger.getLogger(JuegoBoliche.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
clase Boliche
package boliche;
public class Boliche {
public static void main(String[] args) {
JuegoBoliche juego=new JuegoBoliche();
juego.Juego();
}
}
package boliche;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.Timer;
public class Bolerama extends javax.swing.JFrame {
Timer contador;
byte velocidadContador;
byte desicion;
byte turno;
String ipserv="192.168.1.65";
int numPuerto=5200;
String nombre;
Socket conexion;
String[] nombresJugadores=new String[3];
short[] puntajesJugadores=new short[3];
byte[] turnosJugadores=new byte[3];
ObjectInputStream entrada;
ObjectOutputStream salida;
Jugador yo;
public Bolerama() throws UnknownHostException, IOException, ClassNotFoundException {
initComponents();
setSize(700,400);
this.contador = new Timer(velocidadContador,fuerza);
jButton2.setEnabled(false);
nombre=JOptionPane.showInputDialog("Escribe tu nombre");
ipserv=JOptionPane.showInputDialog("Escribe la ip del Servidor");
numPuerto=Integer.parseInt(JOptionPane.showInputDialog("Escribe el puerto que te corresponde: Jugador1(5000), jugador 2(5100),jugador3(5200)"));
conexion=new Socket(InetAddress.getByName(ipserv),numPuerto);
entrada=new ObjectInputStream(conexion.getInputStream());
salida=new ObjectOutputStream(conexion.getOutputStream());
turno=entrada.readByte();
salida.writeObject(nombre);
salida.flush();
this.setResizable(false);
this.setLocationRelativeTo(null);
for (int i = 0; i < 3; i++) {
turnosJugadores[i]=this.entrada.readByte();
nombresJugadores[i]=(String)this.entrada.readObject();
}
yo=new Jugador(nombre);
jLabel16.setText("<html>Tu:<br>"+yo.puntajeTotal+"</html>");
jLabel17.setText("<html>"+nombresJugadores[0]+":<br>"+puntajesJugadores[0]+"</html>");
jLabel18.setText("<html>"+nombresJugadores[1]+":<br>"+puntajesJugadores[1]+"</html>");
jLabel21.setText("<html>"+nombresJugadores[2]+":<br>"+puntajesJugadores[2]+"</html>");
jLabel14.setVisible(false);
jLabel15.setVisible(false);
jLabel13.setVisible(false);
if(turno!=0){
recibirResultados();
}
}
ActionListener fuerza=new ActionListener(){
int i=0;
boolean regreso=false;
public void actionPerformed(ActionEvent e){
if(jSlider1.getValue()<10&&!regreso){
jSlider1.setValue(i+1);
i++;
if (jSlider1.getValue()==10) {
regreso=true;
}
}
else if(jSlider1.getValue()>1&®reso){
jSlider1.setValue(i-1);
i--;
if (jSlider1.getValue()==1) {
regreso=false;
}
}
}
};
@SuppressWarnings("unchecked")
private void initComponents() {
jLabel21 = new javax.swing.JLabel();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jLabel15 = new javax.swing.JLabel();
jLabel18 = new javax.swing.JLabel();
jLabel17 = new javax.swing.JLabel();
jLabel16 = new javax.swing.JLabel();
jLabel14 = new javax.swing.JLabel();
jLabel13 = new javax.swing.JLabel();
jLabel11 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jSlider1 = new javax.swing.JSlider();
jButton2 = new javax.swing.JButton();
jLabel19 = new javax.swing.JLabel();
jLabel20 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(102, 0, 255));
getContentPane().setLayout(null);
getContentPane().add(jLabel21);
jLabel21.setBounds(260, 230, 170, 30);
jButton3.setText("^");
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jButton3MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jButton3MouseExited(evt);
}
});
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3);
jButton3.setBounds(160, 300, 40, 23);
jButton4.setText("^");
jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jButton4MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jButton4MouseExited(evt);
}
});
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
getContentPane().add(jButton4);
jButton4.setBounds(20, 300, 40, 23);
jButton5.setText("^");
jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jButton5MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jButton5MouseExited(evt);
}
});
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
getContentPane().add(jButton5);
jButton5.setBounds(90, 300, 40, 23);
jLabel15.setBackground(new Color(79,249,201,60));
jLabel15.setOpaque(true);
getContentPane().add(jLabel15);
jLabel15.setBounds(140, 10, 80, 280);
getContentPane().add(jLabel18);
jLabel18.setBounds(260, 180, 170, 30);
getContentPane().add(jLabel17);
jLabel17.setBounds(260, 140, 170, 30);
getContentPane().add(jLabel16);
jLabel16.setBounds(260, 100, 170, 30);
jLabel14.setBackground(new Color(79,249,201,60));
jLabel14.setOpaque(true);
jLabel14.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jLabel14MouseEntered(evt);
}
});
getContentPane().add(jLabel14);
jLabel14.setBounds(80, 10, 80, 280);
jLabel13.setBackground(new Color(79,249,201,60));
jLabel13.setOpaque(true);
getContentPane().add(jLabel13);
jLabel13.setBounds(20, 10, 80, 280);
jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel11.setText("jLabel2");
getContentPane().add(jLabel11);
jLabel11.setBounds(190, 10, 20, 40);
jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel10.setText("jLabel2");
getContentPane().add(jLabel10);
jLabel10.setBounds(140, 10, 20, 40);
jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel9.setText("jLabel2");
getContentPane().add(jLabel9);
jLabel9.setBounds(80, 10, 20, 40);
jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel8.setText("jLabel2");
getContentPane().add(jLabel8);
jLabel8.setBounds(40, 10, 20, 40);
jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel7.setText("jLabel2");
getContentPane().add(jLabel7);
jLabel7.setBounds(160, 50, 20, 40);
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel2.setText("jLabel2");
getContentPane().add(jLabel2);
jLabel2.setBounds(110, 120, 20, 40);
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel3.setText("jLabel2");
getContentPane().add(jLabel3);
jLabel3.setBounds(80, 90, 20, 40);
jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel4.setText("jLabel2");
getContentPane().add(jLabel4);
jLabel4.setBounds(140, 90, 20, 40);
jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel5.setText("jLabel2");
getContentPane().add(jLabel5);
jLabel5.setBounds(60, 50, 20, 40);
jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel6.setText("jLabel2");
getContentPane().add(jLabel6);
jLabel6.setBounds(110, 50, 20, 40);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/lane.png")));
jLabel1.setText("jLabel1");
getContentPane().add(jLabel1);
jLabel1.setBounds(0, 0, 260, 400);
jSlider1.setMaximum(10);
jSlider1.setMinimum(1);
jSlider1.setValue(1);
getContentPane().add(jSlider1);
jSlider1.setBounds(260, 30, 170, 23);
jButton2.setText("Detener");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
jButton2.setBounds(270, 60, 71, 23);
getContentPane().add(jLabel19);
jLabel19.setBounds(270, 270, 160, 120);
jLabel20.setText("Fuerza:");
getContentPane().add(jLabel20);
jLabel20.setBounds(260, 0, 140, 14);
pack();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
contador.stop();
jButton2.setEnabled(false);
try {
salida.writeByte(desicion);
salida.writeByte(jSlider1.getValue());
salida.flush();
recibirResultados();
} catch (Exception ex) {
Logger.getLogger(Bolerama.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jLabel14MouseEntered(java.awt.event.MouseEvent evt) {
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
jButton4.setVisible(false);
jButton5.setVisible(false);
jButton2.setEnabled(true);
contador.setDelay(50);
contador.start();
desicion=1;
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
jButton3.setVisible(false);
jButton5.setVisible(false);
jButton2.setEnabled(true);
desicion=2;
contador.setDelay(50);
contador.start();
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
jButton4.setVisible(false);
jButton3.setVisible(false);
jButton2.setEnabled(true);
contador.setDelay(35);
contador.start();
desicion=3;
}
private void jButton3MouseEntered(java.awt.event.MouseEvent evt) {
jLabel15.setVisible(true);
}
private void jButton3MouseExited(java.awt.event.MouseEvent evt) {
jLabel15.setVisible(false);
}
private void jButton5MouseEntered(java.awt.event.MouseEvent evt) {
jLabel14.setVisible(true);
}
private void jButton5MouseExited(java.awt.event.MouseEvent evt) {
jLabel14.setVisible(false);
}
private void jButton4MouseExited(java.awt.event.MouseEvent evt) {
jLabel13.setVisible(false);
}
private void jButton4MouseEntered(java.awt.event.MouseEvent evt) {
jLabel13.setVisible(true);
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Bolerama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Bolerama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Bolerama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Bolerama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new Bolerama().setVisible(true);
} catch (Exception ex) {
Logger.getLogger(Bolerama.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
public void recibirResultados() throws IOException, ClassNotFoundException{
byte turnoActual;
boolean l=false;
do{
if(l){
jButton2.setEnabled(false);
jButton3.setEnabled(false);
jButton4.setEnabled(false);
jButton5.setEnabled(false);
}
jLabel19.setText("...");
char accion=entrada.readChar();
System.out.println("char recibido");
if(accion=='d'){
if(turno==entrada.readByte()){
jButton3.setEnabled(true);
jButton4.setEnabled(true);
jButton5.setEnabled(true);
jButton3.setVisible(true);
jButton4.setVisible(true);
jButton5.setVisible(true);
break;
}
turnoActual=(byte)(turno+1);
}
else{
jLabel19.setText((String)(entrada.readObject()));
byte enem=entrada.readByte();
short puntos=entrada.readShort();
if(enem==turno){
yo.puntajeTotal=puntos;
}
else{
for (int i = 0; i < turnosJugadores.length; i++) {
if(enem==turnosJugadores[i]){
if(i==0){
puntajesJugadores[0]=puntos;
System.out.println("nuevo p 0:"+puntajesJugadores[0]);
}
else if(i==1){
puntajesJugadores[1]=puntos;
System.out.println("nuevo p 1:"+puntajesJugadores[1]);
}
else{
puntajesJugadores[2]=puntos;
System.out.println("nuevo p 1:"+puntajesJugadores[1]);
}
}
}
}
jLabel16.setText("<html>Tu<br>Puntaje: "+yo.puntajeTotal+"</html>");
jLabel17.setText("<html>"+nombresJugadores[0]+"<br>Puntaje: "+puntajesJugadores[0]+"</html>");
jLabel18.setText("<html>"+nombresJugadores[1]+"<br>Puntaje: "+puntajesJugadores[1]+"</html>");
jLabel21.setText("<html>"+nombresJugadores[2]+"<br>Puntaje: "+puntajesJugadores[2]+"</html>");
turnoActual=entrada.readByte();
boolean fin=entrada.readBoolean();
if(fin){
JOptionPane.showMessageDialog(null, "El juego ha terminado. Ganador: "+(String)entrada.readObject());
dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
System.out.println("Turno siguiente");
if(turnoActual==turno){
jButton3.setEnabled(true);
jButton4.setEnabled(true);
jButton5.setEnabled(true);
jButton3.setVisible(true);
jButton4.setVisible(true);
jButton5.setVisible(true);
}
}
}while(turnoActual!=turno);
}
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel15;
private javax.swing.JLabel jLabel16;
private javax.swing.JLabel jLabel17;
private javax.swing.JLabel jLabel18;
private javax.swing.JLabel jLabel19;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel20;
private javax.swing.JLabel jLabel21;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JSlider jSlider1;
}
package boliche;
public interface Partida {
int TURNOS = 10;
void jugarTurno(int turno, int ronda);
Jugador buscarGanador();
void Juego();
}
clase Jugador
package boliche;
public class Jugador {
short puntajeTotal = 0;
short[] puntos = new short[10];
boolean chuza = false;
boolean spare = false;
String nombre;
public Jugador(String nombre) {
this.nombre = nombre;
}
public byte tirar(byte desicion, byte fuerza, Pino[] pinos) {
byte pinosDerribados = 0;
for (Pino pino : pinos) {
if (!pino.dePie) {
continue;
}
if ((int)((int)(100 / pino.probCaida(tipoTiro(desicion, fuerza)))*Math.random()) == 0) {
pino.dePie = false;
pinosDerribados++;
}
}
return pinosDerribados;
}
public byte tipoTiro(byte desicion, byte fuerza) {
if (desicion == 1) {
if (fuerza >= 1 && fuerza <= 5) {
return 1;
} else if (fuerza >= 6 && fuerza <= 9) {
return 3;
} else {
return 5;
}
} else if (desicion == 2) {
if (fuerza >= 1 && fuerza <= 5) {
return 2;
} else if (fuerza >= 6 && fuerza <= 9) {
return 4;
} else {
return 6;
}
} else {
if (fuerza >= 1 && fuerza <= 5) {
return 7;
} else if (fuerza >= 6 && fuerza <= 9) {
return 8;
} else {
return 9;
}
}
}
}
clase Pino
package boliche;
public class Pino {
boolean dePie = true;
byte posicion;
Pino(int pos) {
posicion = (byte) pos;
dePie = true;
}
public byte probCaida(byte caso) {
if (caso == 1) {
if (posicion == 4) {
return 20;
} else {
return 1;
}
} else if (caso == 2) {
if (posicion == 6) {
return 20;
} else {
return 1;
}
} else if (caso == 3) {
if (posicion == 4) {
return 100;
} else if (posicion == 5 || posicion == 2 || posicion == 7 || posicion == 8) {
return 80;
} else if (posicion == 1 || posicion == 3 || posicion == 6 || posicion == 9) {
return 40;
} else {
return 1;
}
} else if (caso == 4) {
if (posicion == 6) {
return 100;
} else if (posicion == 3 || posicion == 5 || posicion == 9 || posicion == 10) {
return 80;
} else if (posicion == 1 || posicion == 2 || posicion == 8 || posicion == 4) {
return 40;
} else {
return 1;
}
} else if (caso == 5) {
if (posicion == 4) {
return 100;
} else if (posicion == 5 || posicion == 2 || posicion == 7 || posicion == 8) {
return 90;
} else if (posicion == 1 || posicion == 3 || posicion == 6 || posicion == 9) {
return 50;
} else {
return 10;
}
} else if (caso == 6) {
if (posicion == 6) {
return 100;
} else if (posicion == 3 || posicion == 5 || posicion == 9 || posicion == 10) {
return 90;
} else if (posicion == 1 || posicion == 2 || posicion == 8 || posicion == 4) {
return 50;
} else {
return 10;
}
} else if (caso == 7) {
if (posicion == 1) {
return 80;
} else if (posicion == 2 || posicion == 3) {
return 60;
} else if (posicion == 5) {
return 20;
} else if (posicion == 4 || posicion == 6) {
return 2;
}
return 1;
} else if (caso == 8) {
if (posicion == 1 || posicion == 2 || posicion == 3) {
return 90;
} else if (posicion == 5 || posicion == 4 || posicion == 6) {
return 60;
} else if (posicion == 8 || posicion == 9) {
return 20;
}
return 2;
} else {
if (posicion == 1 || posicion == 2 || posicion == 3) {
return 100;
} else if (posicion == 5 || posicion == 4 || posicion == 6) {
return 80;
} else if (posicion == 8 || posicion == 9) {
return 70;
}
return 50;
}
}
}
clase JuegoBoliche
package boliche;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
enum tipoTiro{SPARE,NORMAL,CHUZA}
public class JuegoBoliche implements Partida{
Jugador[] participantes=new Jugador[4];
short[][] marcador=new short[4][10];
Jugador ganador;
Pino[] pinos={new Pino(1),new Pino(2),new Pino(3),new Pino(4),new Pino(5),new Pino(6),new Pino(7),new Pino(8),new Pino(9),new Pino(10)};
tipoTiro[][] tiros=new tipoTiro[4][10];
boolean batallaTerminada=false;
ServerSocket servidor;
Socket[] datos=new Socket[4];
ObjectOutputStream[] flujosSalida=new ObjectOutputStream[4];
ObjectInputStream[] flujosEntrada=new ObjectInputStream[4];
@Override
public void jugarTurno(int i, int j) {
short pinosDerribados=0;
try {
pinosDerribados+=participantes[i].tirar(flujosEntrada[i].readByte(), flujosEntrada[i].readByte(), pinos);
if(pinosDerribados!=10){
for (int k = 0; k < 4; k++) {
flujosSalida[k].writeChar('d');
flujosSalida[k].writeByte(i);
flujosSalida[k].flush();
}
System.out.println("enviando d");
pinosDerribados+=participantes[i].tirar(flujosEntrada[i].readByte(), flujosEntrada[i].readByte(), pinos);
if(pinosDerribados==10){
tiros[i][j]=tipoTiro.SPARE;
marcador[i][j]=15;
participantes[i].puntajeTotal+=15;
for (int k = 0; k < 4; k++) {
flujosSalida[k].writeChar('e');
System.out.println("enviando e");
flujosSalida[k].flush();
}
enviarResultados(tiros[i][j],i,pinosDerribados);
}
else{
tiros[i][j]=tipoTiro.NORMAL;
marcador[i][j]=pinosDerribados;
participantes[i].puntajeTotal+=pinosDerribados;
for (int k = 0; k < 4; k++) {
flujosSalida[k].writeChar('e');
System.out.println("enviando e");
flujosSalida[k].flush();
}
enviarResultados(tiros[i][j],i,pinosDerribados);
}
}
else{
tiros[i][j]=tipoTiro.CHUZA;
marcador[i][j]=30;
participantes[i].puntajeTotal+=30;
for (int k = 0; k < 4; k++) {
flujosSalida[k].writeChar('e');
System.out.println("enviando e");
flujosSalida[k].flush();
}
enviarResultados(tiros[i][j],i,pinosDerribados);
}
} catch (IOException ex) {
Logger.getLogger(JuegoBoliche.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void enviarResultados(tipoTiro t, int turno, short pinosDerribados) throws IOException{
for (int i = 0; i < 4; i++) {
if(t==tipoTiro.CHUZA){
flujosSalida[i].writeObject("<html>"+participantes[turno].nombre+"<br>hizo una Chuza!<br>+30 puntos<html>");
}
else if(t==tipoTiro.SPARE){
flujosSalida[i].writeObject("<html>"+participantes[turno].nombre+"<br>hizo un Spare!<br>+15 puntos<html>");
}
else{
flujosSalida[i].writeObject("<html>"+participantes[turno].nombre+"<br>derribo "+pinosDerribados+" pinos<br>+"+pinosDerribados+" puntos<html>");
}
flujosSalida[i].writeByte(turno);
flujosSalida[i].writeShort(participantes[turno].puntajeTotal);
if(turno!=3)
flujosSalida[i].writeByte(turno+1);
else
flujosSalida[i].writeByte(0);
flujosSalida[i].flush();
}
}
@Override
public Jugador buscarGanador() {
if(participantes[0].puntajeTotal>participantes[1].puntajeTotal&&participantes[0].puntajeTotal>participantes[2].puntajeTotal)
return participantes[0];
else if(participantes[1].puntajeTotal>participantes[0].puntajeTotal&&participantes[1].puntajeTotal>participantes[2].puntajeTotal)
return participantes[1];
else
return participantes[2];
}
@Override
@SuppressWarnings("empty-statement")
public void Juego() {
byte i=0;
while (i<4) {
try {
servidor=new ServerSocket(5000+(i*100));
System.out.println("Esperando conexiones...");
datos[i]=servidor.accept();
System.out.println("Conexion aceptada");
flujosSalida[i]=new ObjectOutputStream(datos[i].getOutputStream());
InputStream oi=datos[i].getInputStream();
flujosEntrada[i]=new ObjectInputStream(oi);
flujosSalida[i].writeByte(i);
flujosSalida[i].flush();
System.out.println("Enviando Turno");
participantes[i]=new Jugador((String)(flujosEntrada[i].readObject()));
System.out.println("Objeto creado");
} catch (Exception ex) {
Logger.getLogger(JuegoBoliche.class.getName()).log(Level.SEVERE, null, ex);
}
i++;
}
try{
for (int j = 0; j < participantes.length; j++) {
for (int k = 0; k < participantes.length; k++) {
if(!participantes[j].equals(participantes[k])){
flujosSalida[j].writeByte(k);
flujosSalida[j].writeObject(participantes[k].nombre);
flujosSalida[j].flush();
}
}
}
}catch(Exception e){}
byte pinosDerribados;
for (byte j = 0; j < TURNOS; j++) {
for (i = 0; i < 4; i++) {
for (int k = 0; k < 10; k++) {
pinos[k].dePie=true;
}
jugarTurno(i,j);
for (int k = 0; k < 4; k++) {
if((j+1)==TURNOS){
try {
ganador=buscarGanador();
flujosSalida[k].writeBoolean(true);
flujosSalida[k].writeObject(ganador.nombre);
flujosSalida[k].flush();
} catch (IOException ex) {
Logger.getLogger(JuegoBoliche.class.getName()).log(Level.SEVERE, null, ex);
}
}
else{
try {
flujosSalida[k].writeBoolean(false);
flujosSalida[k].flush();
} catch (IOException ex) {
Logger.getLogger(JuegoBoliche.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
for (int j = 0; j < 4; j++) {
try {
flujosEntrada[j].close();
flujosSalida[j].close();
datos[j].close();
} catch (IOException ex) {
Logger.getLogger(JuegoBoliche.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
servidor.close();
} catch (IOException ex) {
Logger.getLogger(JuegoBoliche.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
clase Boliche
package boliche;
public class Boliche {
public static void main(String[] args) {
JuegoBoliche juego=new JuegoBoliche();
juego.Juego();
}
}
clase Bolerama
package boliche;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.Timer;
public class Bolerama extends javax.swing.JFrame {
Timer contador;
byte velocidadContador;
byte desicion;
byte turno;
String ipserv="192.168.1.65";
int numPuerto=5200;
String nombre;
Socket conexion;
String[] nombresJugadores=new String[3];
short[] puntajesJugadores=new short[3];
byte[] turnosJugadores=new byte[3];
ObjectInputStream entrada;
ObjectOutputStream salida;
Jugador yo;
public Bolerama() throws UnknownHostException, IOException, ClassNotFoundException {
initComponents();
setSize(700,400);
this.contador = new Timer(velocidadContador,fuerza);
jButton2.setEnabled(false);
nombre=JOptionPane.showInputDialog("Escribe tu nombre");
ipserv=JOptionPane.showInputDialog("Escribe la ip del Servidor");
numPuerto=Integer.parseInt(JOptionPane.showInputDialog("Escribe el puerto que te corresponde: Jugador1(5000), jugador 2(5100),jugador3(5200)"));
conexion=new Socket(InetAddress.getByName(ipserv),numPuerto);
entrada=new ObjectInputStream(conexion.getInputStream());
salida=new ObjectOutputStream(conexion.getOutputStream());
turno=entrada.readByte();
salida.writeObject(nombre);
salida.flush();
this.setResizable(false);
this.setLocationRelativeTo(null);
for (int i = 0; i < 3; i++) {
turnosJugadores[i]=this.entrada.readByte();
nombresJugadores[i]=(String)this.entrada.readObject();
}
yo=new Jugador(nombre);
jLabel16.setText("<html>Tu:<br>"+yo.puntajeTotal+"</html>");
jLabel17.setText("<html>"+nombresJugadores[0]+":<br>"+puntajesJugadores[0]+"</html>");
jLabel18.setText("<html>"+nombresJugadores[1]+":<br>"+puntajesJugadores[1]+"</html>");
jLabel21.setText("<html>"+nombresJugadores[2]+":<br>"+puntajesJugadores[2]+"</html>");
jLabel14.setVisible(false);
jLabel15.setVisible(false);
jLabel13.setVisible(false);
if(turno!=0){
recibirResultados();
}
}
ActionListener fuerza=new ActionListener(){
int i=0;
boolean regreso=false;
public void actionPerformed(ActionEvent e){
if(jSlider1.getValue()<10&&!regreso){
jSlider1.setValue(i+1);
i++;
if (jSlider1.getValue()==10) {
regreso=true;
}
}
else if(jSlider1.getValue()>1&®reso){
jSlider1.setValue(i-1);
i--;
if (jSlider1.getValue()==1) {
regreso=false;
}
}
}
};
@SuppressWarnings("unchecked")
private void initComponents() {
jLabel21 = new javax.swing.JLabel();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jLabel15 = new javax.swing.JLabel();
jLabel18 = new javax.swing.JLabel();
jLabel17 = new javax.swing.JLabel();
jLabel16 = new javax.swing.JLabel();
jLabel14 = new javax.swing.JLabel();
jLabel13 = new javax.swing.JLabel();
jLabel11 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jSlider1 = new javax.swing.JSlider();
jButton2 = new javax.swing.JButton();
jLabel19 = new javax.swing.JLabel();
jLabel20 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(102, 0, 255));
getContentPane().setLayout(null);
getContentPane().add(jLabel21);
jLabel21.setBounds(260, 230, 170, 30);
jButton3.setText("^");
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jButton3MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jButton3MouseExited(evt);
}
});
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3);
jButton3.setBounds(160, 300, 40, 23);
jButton4.setText("^");
jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jButton4MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jButton4MouseExited(evt);
}
});
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
getContentPane().add(jButton4);
jButton4.setBounds(20, 300, 40, 23);
jButton5.setText("^");
jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jButton5MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jButton5MouseExited(evt);
}
});
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
getContentPane().add(jButton5);
jButton5.setBounds(90, 300, 40, 23);
jLabel15.setBackground(new Color(79,249,201,60));
jLabel15.setOpaque(true);
getContentPane().add(jLabel15);
jLabel15.setBounds(140, 10, 80, 280);
getContentPane().add(jLabel18);
jLabel18.setBounds(260, 180, 170, 30);
getContentPane().add(jLabel17);
jLabel17.setBounds(260, 140, 170, 30);
getContentPane().add(jLabel16);
jLabel16.setBounds(260, 100, 170, 30);
jLabel14.setBackground(new Color(79,249,201,60));
jLabel14.setOpaque(true);
jLabel14.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jLabel14MouseEntered(evt);
}
});
getContentPane().add(jLabel14);
jLabel14.setBounds(80, 10, 80, 280);
jLabel13.setBackground(new Color(79,249,201,60));
jLabel13.setOpaque(true);
getContentPane().add(jLabel13);
jLabel13.setBounds(20, 10, 80, 280);
jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel11.setText("jLabel2");
getContentPane().add(jLabel11);
jLabel11.setBounds(190, 10, 20, 40);
jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel10.setText("jLabel2");
getContentPane().add(jLabel10);
jLabel10.setBounds(140, 10, 20, 40);
jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel9.setText("jLabel2");
getContentPane().add(jLabel9);
jLabel9.setBounds(80, 10, 20, 40);
jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel8.setText("jLabel2");
getContentPane().add(jLabel8);
jLabel8.setBounds(40, 10, 20, 40);
jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel7.setText("jLabel2");
getContentPane().add(jLabel7);
jLabel7.setBounds(160, 50, 20, 40);
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel2.setText("jLabel2");
getContentPane().add(jLabel2);
jLabel2.setBounds(110, 120, 20, 40);
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel3.setText("jLabel2");
getContentPane().add(jLabel3);
jLabel3.setBounds(80, 90, 20, 40);
jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel4.setText("jLabel2");
getContentPane().add(jLabel4);
jLabel4.setBounds(140, 90, 20, 40);
jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel5.setText("jLabel2");
getContentPane().add(jLabel5);
jLabel5.setBounds(60, 50, 20, 40);
jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/bowlingpin.png")));
jLabel6.setText("jLabel2");
getContentPane().add(jLabel6);
jLabel6.setBounds(110, 50, 20, 40);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/boliche/lane.png")));
jLabel1.setText("jLabel1");
getContentPane().add(jLabel1);
jLabel1.setBounds(0, 0, 260, 400);
jSlider1.setMaximum(10);
jSlider1.setMinimum(1);
jSlider1.setValue(1);
getContentPane().add(jSlider1);
jSlider1.setBounds(260, 30, 170, 23);
jButton2.setText("Detener");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
jButton2.setBounds(270, 60, 71, 23);
getContentPane().add(jLabel19);
jLabel19.setBounds(270, 270, 160, 120);
jLabel20.setText("Fuerza:");
getContentPane().add(jLabel20);
jLabel20.setBounds(260, 0, 140, 14);
pack();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
contador.stop();
jButton2.setEnabled(false);
try {
salida.writeByte(desicion);
salida.writeByte(jSlider1.getValue());
salida.flush();
recibirResultados();
} catch (Exception ex) {
Logger.getLogger(Bolerama.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jLabel14MouseEntered(java.awt.event.MouseEvent evt) {
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
jButton4.setVisible(false);
jButton5.setVisible(false);
jButton2.setEnabled(true);
contador.setDelay(50);
contador.start();
desicion=1;
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
jButton3.setVisible(false);
jButton5.setVisible(false);
jButton2.setEnabled(true);
desicion=2;
contador.setDelay(50);
contador.start();
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
jButton4.setVisible(false);
jButton3.setVisible(false);
jButton2.setEnabled(true);
contador.setDelay(35);
contador.start();
desicion=3;
}
private void jButton3MouseEntered(java.awt.event.MouseEvent evt) {
jLabel15.setVisible(true);
}
private void jButton3MouseExited(java.awt.event.MouseEvent evt) {
jLabel15.setVisible(false);
}
private void jButton5MouseEntered(java.awt.event.MouseEvent evt) {
jLabel14.setVisible(true);
}
private void jButton5MouseExited(java.awt.event.MouseEvent evt) {
jLabel14.setVisible(false);
}
private void jButton4MouseExited(java.awt.event.MouseEvent evt) {
jLabel13.setVisible(false);
}
private void jButton4MouseEntered(java.awt.event.MouseEvent evt) {
jLabel13.setVisible(true);
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Bolerama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Bolerama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Bolerama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Bolerama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new Bolerama().setVisible(true);
} catch (Exception ex) {
Logger.getLogger(Bolerama.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
public void recibirResultados() throws IOException, ClassNotFoundException{
byte turnoActual;
boolean l=false;
do{
if(l){
jButton2.setEnabled(false);
jButton3.setEnabled(false);
jButton4.setEnabled(false);
jButton5.setEnabled(false);
}
jLabel19.setText("...");
char accion=entrada.readChar();
System.out.println("char recibido");
if(accion=='d'){
if(turno==entrada.readByte()){
jButton3.setEnabled(true);
jButton4.setEnabled(true);
jButton5.setEnabled(true);
jButton3.setVisible(true);
jButton4.setVisible(true);
jButton5.setVisible(true);
break;
}
turnoActual=(byte)(turno+1);
}
else{
jLabel19.setText((String)(entrada.readObject()));
byte enem=entrada.readByte();
short puntos=entrada.readShort();
if(enem==turno){
yo.puntajeTotal=puntos;
}
else{
for (int i = 0; i < turnosJugadores.length; i++) {
if(enem==turnosJugadores[i]){
if(i==0){
puntajesJugadores[0]=puntos;
System.out.println("nuevo p 0:"+puntajesJugadores[0]);
}
else if(i==1){
puntajesJugadores[1]=puntos;
System.out.println("nuevo p 1:"+puntajesJugadores[1]);
}
else{
puntajesJugadores[2]=puntos;
System.out.println("nuevo p 1:"+puntajesJugadores[1]);
}
}
}
}
jLabel16.setText("<html>Tu<br>Puntaje: "+yo.puntajeTotal+"</html>");
jLabel17.setText("<html>"+nombresJugadores[0]+"<br>Puntaje: "+puntajesJugadores[0]+"</html>");
jLabel18.setText("<html>"+nombresJugadores[1]+"<br>Puntaje: "+puntajesJugadores[1]+"</html>");
jLabel21.setText("<html>"+nombresJugadores[2]+"<br>Puntaje: "+puntajesJugadores[2]+"</html>");
turnoActual=entrada.readByte();
boolean fin=entrada.readBoolean();
if(fin){
JOptionPane.showMessageDialog(null, "El juego ha terminado. Ganador: "+(String)entrada.readObject());
dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
System.out.println("Turno siguiente");
if(turnoActual==turno){
jButton3.setEnabled(true);
jButton4.setEnabled(true);
jButton5.setEnabled(true);
jButton3.setVisible(true);
jButton4.setVisible(true);
jButton5.setVisible(true);
}
}
}while(turnoActual!=turno);
}
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel15;
private javax.swing.JLabel jLabel16;
private javax.swing.JLabel jLabel17;
private javax.swing.JLabel jLabel18;
private javax.swing.JLabel jLabel19;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel20;
private javax.swing.JLabel jLabel21;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JSlider jSlider1;
}
Comentarios
Publicar un comentario