commit a1e1e6f2faba3dd64c0317bc9f6274c54e4c0129 Author: groche97 Date: Tue Feb 11 11:59:39 2020 +0100 first commit diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..aa96ce4 --- /dev/null +++ b/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..2f96fb7 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Servidor2Json + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..3a21537 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/gson-2.8.6-javadoc.jar b/gson-2.8.6-javadoc.jar new file mode 100644 index 0000000..e3cce52 Binary files /dev/null and b/gson-2.8.6-javadoc.jar differ diff --git a/gson-2.8.6-sources.jar b/gson-2.8.6-sources.jar new file mode 100644 index 0000000..1e07df4 Binary files /dev/null and b/gson-2.8.6-sources.jar differ diff --git a/gson-2.8.6.jar b/gson-2.8.6.jar new file mode 100644 index 0000000..4765c4a Binary files /dev/null and b/gson-2.8.6.jar differ diff --git a/src/ComunacacionJugador.java b/src/ComunacacionJugador.java new file mode 100644 index 0000000..69f2811 --- /dev/null +++ b/src/ComunacacionJugador.java @@ -0,0 +1,172 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.Socket; +import com.google.gson.Gson; + +public class ComunacacionJugador extends Thread{ + Socket socket; + public ComunacacionJugador(Socket socket) { + this.socket = socket; + this.start(); + } + + public void run() { + try { + Gson gson = new Gson(); + BufferedReader bufferEntrada = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); + PrintWriter bufferSalida = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream()),true); + bufferSalida.println(gson.toJson(Servidor.mostrarSalas()+"fin")); + int sala = Integer.parseInt(gson.fromJson(bufferEntrada.readLine(), String.class))-1; + if(Servidor.addJugador(sala, this.socket)) { + bufferSalida.println(gson.toJson("Has entrado en la sala")); + switch(Servidor.determinarJuego(sala)){ + case Servidor.juego4: + sala -= Servidor.salas3.size(); + bufferSalida.println(gson.toJson("4")); + partida4(bufferEntrada, bufferSalida, sala); + break; + case Servidor.juego3: + bufferSalida.println(gson.toJson("3")); + partida3(bufferEntrada, bufferSalida, sala); + break; + } + }else { + bufferSalida.println(gson.toJson("Error al entrar en la sala")); + socket.close(); + return; + } + + + }catch (Exception e) { + e.printStackTrace(); + } + } + + private void partida3(BufferedReader bufferEntrada, PrintWriter bufferSalida, int sala) throws InterruptedException, NumberFormatException, IOException { + Gson gson = new Gson(); + boolean continuar = true; + int jugador = 2; + if(Servidor.salas3.get(sala).getJugador1().equals(this.socket)) { + bufferSalida.println(gson.toJson("Esperando al segundo jugador")); + synchronized (this.socket) { + this.socket.wait(); + } + jugador = 1; + bufferSalida.println(gson.toJson("Primero")); + bufferSalida.println(gson.toJson(Servidor.salas3.get(sala).tablero.getTablero())); + Servidor.salas3.get(sala).tablero.colocarFicha(Integer.parseInt(gson.fromJson(bufferEntrada.readLine(), String.class)), + Integer.parseInt(gson.fromJson(bufferEntrada.readLine(), String.class)),jugador); + /*Calculamos el modulo 2 de jugador para que cuando este sea 2 se acceda al socket del primero, + y viceversa, ya que usamos los sockets como mutex*/ + synchronized (Servidor.salas3.get(sala).jugadores[jugador%2]) { + Servidor.salas3.get(sala).jugadores[jugador%2].notify(); + } + synchronized (this.socket) { + this.socket.wait(); + } + }else { + bufferSalida.println(gson.toJson("Iniciando partida")); + bufferSalida.println(gson.toJson("Segundo")); + synchronized (Servidor.salas3.get(sala).jugadores[jugador%2]) { + Servidor.salas3.get(sala).jugadores[jugador%2].notify(); + } + synchronized (this.socket) { + this.socket.wait(); + } + } + while(continuar) { + int finalJuego = Servidor.salas3.get(sala).tablero.finalJuego(); + /*funalJuego determinara quien ha ganado, mientras sea 0 nadie habra ganado, + y cuando alguien gane contendra el numero del jugador que ha ganado*/ + if(finalJuego != 0) { + continuar = false; + if(finalJuego == jugador) { + bufferSalida.println(gson.toJson("v")); + Servidor.salas3.remove(sala); + }else { + bufferSalida.println(gson.toJson("f")); + synchronized (Servidor.salas3.get(sala).jugadores[jugador%2]) { + Servidor.salas3.get(sala).jugadores[jugador%2].notify(); + } + } + + }else { + bufferSalida.println(gson.toJson(Servidor.salas3.get(sala).tablero.getTablero())); + Servidor.salas3.get(sala).tablero.colocarFicha(Integer.parseInt(gson.fromJson(bufferEntrada.readLine(), String.class)), + Integer.parseInt(bufferEntrada.readLine()),jugador); + synchronized (Servidor.salas3.get(sala).jugadores[jugador%2]) { + Servidor.salas3.get(sala).jugadores[jugador%2].notify(); + } + synchronized (this.socket) { + this.socket.wait(); + } + } + + } + + } + + private void partida4(BufferedReader bufferEntrada, PrintWriter bufferSalida, int sala) throws InterruptedException, NumberFormatException, IOException { + Gson gson = new Gson(); + boolean continuar = true; + int jugador = 2; + if(Servidor.salas4.get(sala).getJugador1().equals(this.socket)) { + bufferSalida.println(gson.toJson("Esperando al segundo jugador")); + synchronized (this.socket) { + this.socket.wait(); + } + jugador = 1; + bufferSalida.println(gson.toJson("Primero")); + bufferSalida.println(gson.toJson(Servidor.salas4.get(sala).tablero.getTablero())); + Servidor.salas4.get(sala).tablero.colocarFicha(Integer.parseInt(gson.fromJson(bufferEntrada.readLine(), String.class)),jugador); + /*Calculamos el modulo 2 de jugador para que cuando este sea 2 se acceda al socket del primero, + y viceversa, ya que usamos los sockets como mutex*/ + synchronized (Servidor.salas4.get(sala).jugadores[jugador%2]) { + Servidor.salas4.get(sala).jugadores[jugador%2].notify(); + } + synchronized (this.socket) { + this.socket.wait(); + } + }else { + bufferSalida.println(gson.toJson("Iniciando partida")); + bufferSalida.println(gson.toJson("Segundo")); + synchronized (Servidor.salas4.get(sala).jugadores[jugador%2]) { + Servidor.salas4.get(sala).jugadores[jugador%2].notify(); + } + synchronized (this.socket) { + this.socket.wait(); + } + } + while(continuar) { + int finalJuego = Servidor.salas4.get(sala).tablero.finalJuego(); + /*funalJuego determinara quien ha ganado, mientras sea 0 nadie habra ganado, + y cuando alguien gane contendra el numero del jugador que ha ganado*/ + if(finalJuego != 0) { + continuar = false; + if(finalJuego == jugador) { + bufferSalida.println(gson.toJson("v")); + Servidor.salas4.remove(sala); + }else { + bufferSalida.println(gson.toJson("f")); + synchronized (Servidor.salas4.get(sala).jugadores[jugador%2]) { + Servidor.salas4.get(sala).jugadores[jugador%2].notify(); + } + } + }else { + bufferSalida.println(gson.toJson(Servidor.salas4.get(sala).tablero.getTablero())); + Servidor.salas4.get(sala).tablero.colocarFicha(Integer.parseInt(gson.fromJson(bufferEntrada.readLine(), String.class)),jugador); + synchronized (Servidor.salas4.get(sala).jugadores[jugador%2]) { + Servidor.salas4.get(sala).jugadores[jugador%2].notify(); + } + synchronized (this.socket) { + this.socket.wait(); + } + } + + } + + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..39088ee --- /dev/null +++ b/src/Main.java @@ -0,0 +1,9 @@ + +public class Main { + + public static void main(String[] args) { + Servidor servidor = Servidor.getInstance(); + + } + +} diff --git a/src/Sala3.java b/src/Sala3.java new file mode 100644 index 0000000..bde494f --- /dev/null +++ b/src/Sala3.java @@ -0,0 +1,36 @@ +import java.net.Socket; + +public class Sala3 { + int nJugadores; + Socket[] jugadores; + Tablero3 tablero; + + public Sala3() { + this.nJugadores = 0; + this.jugadores = new Socket[2]; + this.tablero = new Tablero3(); + } + + public boolean aniadirJugador(Socket jugador) { + if(this.nJugadores <= 1 ) { + this.jugadores[this.nJugadores] = jugador; + this.nJugadores++; + return true; + }else { + return false; + } + } + + public Socket getJugador1() { + return this.jugadores[0]; + } + + public Socket getJugador2() { + return this.jugadores[1]; + } + + public String toString() { + return String.valueOf(this.nJugadores) + " Tres en raya"; + } + +} diff --git a/src/Sala4.java b/src/Sala4.java new file mode 100644 index 0000000..832c409 --- /dev/null +++ b/src/Sala4.java @@ -0,0 +1,36 @@ +import java.net.Socket; + +public class Sala4 { + int nJugadores; + Socket[] jugadores; + Tablero4 tablero; + + public Sala4() { + this.nJugadores = 0; + this.jugadores = new Socket[2]; + this.tablero = new Tablero4(); + } + + public boolean aniadirJugador(Socket jugador) { + if(this.nJugadores <= 1 ) { + this.jugadores[this.nJugadores] = jugador; + this.nJugadores++; + return true; + }else { + return false; + } + } + + public Socket getJugador1() { + return this.jugadores[0]; + } + + public Socket getJugador2() { + return this.jugadores[1]; + } + + public String toString() { + return String.valueOf(this.nJugadores) + " Cuatro en raya"; + } + +} diff --git a/src/Servidor.java b/src/Servidor.java new file mode 100644 index 0000000..307bb3b --- /dev/null +++ b/src/Servidor.java @@ -0,0 +1,78 @@ +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.ArrayList; + +public class Servidor { + static Servidor instancia; + static ArrayList salas3; + static ArrayList salas4; + static final int juego3 = 0; + static final int juego4 = 1; + + private Servidor() { + ServerSocket server; + Servidor.salas3 = new ArrayList(); + Servidor.salas3.add(new Sala3()); + Servidor.salas4 = new ArrayList(); + Servidor.salas4.add(new Sala4()); + try { + server = new ServerSocket(8080); + while(true) { + Socket socket = server.accept(); + ComunacacionJugador cliente = new ComunacacionJugador(socket); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public static Servidor getInstance() { + if(Servidor.instancia == null) { + Servidor.instancia = new Servidor(); + } + + return Servidor.instancia; + } + + public static String mostrarSalas() { + String ret = ""; + int cont = 1; + for(Sala3 sala:Servidor.salas3) { + ret += cont + " - Jugadores: " + sala.toString() + "\n"; + cont++; + } + for(Sala4 sala:Servidor.salas4) { + ret += cont + " - Jugadores: " + sala.toString() + "\n"; + cont++; + } + return ret; + } + + public static boolean addJugador(int sala, Socket socket) { + boolean ret = false; + if(sala < Servidor.salas3.size()) { + ret = Servidor.salas3.get(sala).aniadirJugador(socket); + if(Servidor.salas3.get(sala).nJugadores == 1) { + Servidor.salas3.add(new Sala3()); + } + }else if(sala < (Servidor.salas4.size() + Servidor.salas3.size())) { + ret = Servidor.salas4.get(sala - Servidor.salas3.size()).aniadirJugador(socket); + if(Servidor.salas4.get(sala - Servidor.salas3.size()).nJugadores == 1) { + Servidor.salas4.add(new Sala4()); + } + } + + return ret; + } + + + public static int determinarJuego(int sala) { + if(Servidor.salas3.size() > sala) { + return Servidor.juego3; + }else { + return Servidor.juego4; + } + } +} diff --git a/src/Tablero3.java b/src/Tablero3.java new file mode 100644 index 0000000..dfd8e5d --- /dev/null +++ b/src/Tablero3.java @@ -0,0 +1,87 @@ + +public class Tablero3 { + int[][] casillas; + int[] ultimasCoordenadas; + public Tablero3() { + this.casillas = new int[3][3]; + this.ultimasCoordenadas = new int[2]; + } + + public boolean colocarFicha(int x, int y, int jugador) { + if(this.casillas[x][y] == 0) { + this.casillas[x][y]=jugador; + this.ultimasCoordenadas[0]=x; + this.ultimasCoordenadas[1]=y; + return true; + } + return false; + + } + + public int getLastX() { + return this.ultimasCoordenadas[0]; + } + + public int getLastY() { + 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; + } + + /*Lo hice hace un anio y ni idea de como va pero va, es muy feo + el de el 4 en raya es mas bonito y deberia ir en un tablero 3x3 + pero no me apetecia probarlo*/ + public int finalJuego() { + int ret=0; + for(int i=0;i<3;i++) { + if(this.casillas[i][0]>0) { + for(int j=0;j<3;j++) { + if(this.casillas[j][1]==this.casillas[i][0]) { + if(Math.abs(i-j)<2) { + if(ij&&i<2) { + if(this.casillas[i+1][2]==this.casillas[i][0]) { + ret=this.casillas[i][0]; + } + } + + } + } + } + } + } + if(ret==0) { + for(int i=0;i<3;i++) { + ret=this.casillas[0][i]; + for(int j=1;j<3;j++) { + if(ret!=this.casillas[j][i]) { + ret=0; + break; + } + } + if(ret>0) { + break; + } + } + } + return ret; + } +} diff --git a/src/Tablero4.java b/src/Tablero4.java new file mode 100644 index 0000000..35df7f0 --- /dev/null +++ b/src/Tablero4.java @@ -0,0 +1,120 @@ + +public class Tablero4 { + int[][] casillas; + int[] ultimasCoordenadas; + public Tablero4() { + this.casillas = new int[7][6]; + this.ultimasCoordenadas = new int[2]; + } + + public boolean colocarFicha(int x, int jugador) { + if(this.casillas[x][5] == 0) { + for(int i = 0;i < this.casillas[x].length; i++) { + if(this.casillas[x][i] == 0) { + this.casillas[x][i] = jugador; + this.ultimasCoordenadas[0]=x; + this.ultimasCoordenadas[1]=i; + break; + } + } + return true; + } + return false; + + } + + public int getLastX() { + return this.ultimasCoordenadas[0]; + } + + public int getLastY() { + return this.ultimasCoordenadas[1]; + } + + public String getTablero() { + String ret = ""; + for(int i = this.casillas[0].length-1; i >= 0; i--) { + for(int j = 0; j < this.casillas.length; j++) { + ret += this.casillas[j][i] + " "; + } + if(i != this.casillas.length-1) { + ret += "\n"; + } + + } + return ret; + } + + public int finalJuego() { + int ret=0; + for(int i = 0;i < this.casillas.length; i++) { + for(int j = 0;j < this.casillas[i].length; j++) { + if(this.casillas[i][j] != 0) { + if(comprobarHorizontal(i, j, this.casillas[i][j]) >= 3) { + return this.casillas[i][j]; + } + if(comprobarVertical(i, j, this.casillas[i][j]) >= 3) { + return this.casillas[i][j]; + } + if(comprobarDiagonalArriba(i, j, this.casillas[i][j]) >= 3) { + return this.casillas[i][j]; + } + if(comprobarDiagonalAbajo(i, j, this.casillas[i][j]) >= 3) { + return this.casillas[i][j]; + } + } + } + } + return ret; + } + + private int comprobarHorizontal(int x, int y, int jugador) { + if(x == this.casillas.length-1) { + return 0; + } + if(this.casillas[x+1][y] == jugador) { + return comprobarHorizontal(x+1, y, jugador)+1; + }else { + return 0; + } + } + + private int comprobarVertical(int x, int y, int jugador) { + if(y == this.casillas[x].length-1) { + return 0; + } + if(this.casillas[x][y+1] == jugador) { + return comprobarVertical(x, y+1, jugador)+1; + }else { + return 0; + } + } + + private int comprobarDiagonalArriba(int x, int y, int jugador) { + if(y == this.casillas[x].length-1) { + return 0; + } + if(x == this.casillas.length-1) { + return 0; + } + if(this.casillas[x+1][y+1] == jugador) { + return comprobarDiagonalArriba(x+1, y+1, jugador)+1; + }else { + return 0; + } + } + + private int comprobarDiagonalAbajo(int x, int y, int jugador) { + if(y == 0) { + return 0; + } + if(x == this.casillas.length-1) { + return 0; + } + if(this.casillas[x+1][y-1] == jugador) { + return comprobarDiagonalAbajo(x+1, y-1, jugador)+1; + }else { + return 0; + } + } +}