This commit is contained in:
Guillermo Roche 2019-12-11 10:56:04 +01:00
commit b067f6034f
9 changed files with 244 additions and 0 deletions

6
.classpath Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/bin/

17
.project Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Servidor2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -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

View File

@ -0,0 +1,43 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class ComunacacionJugador extends Thread{
Socket socket;
public ComunacacionJugador(Socket socket) {
this.socket = socket;
this.start();
}
public void run() {
boolean continuar = true;
int turno = 2;
try {
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);
if(Servidor.salas.get(sala).getJugador1().equals(this.socket)) {
turno = 1;
bufferSalida.println("Primero");
Servidor.salas.get(sala).tablero.colocarFicha(Integer.parseInt(bufferEntrada.readLine()),
Integer.parseInt(bufferEntrada.readLine()),turno);
synchronized (getClass()) {
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();
}
}
}catch (Exception e) {
// TODO: handle exception
}
}
}

9
src/Main.java Normal file
View File

@ -0,0 +1,9 @@
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}

36
src/Sala.java Normal file
View File

@ -0,0 +1,36 @@
import java.net.Socket;
public class Sala {
int nJugadores;
Socket[] jugadores;
Tablero tablero;
public Sala() {
this.nJugadores = 0;
this.jugadores = new Socket[2];
this.tablero = new Tablero();
}
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);
}
}

48
src/Servidor.java Normal file
View File

@ -0,0 +1,48 @@
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Servidor {
static Servidor instancia;
static ArrayList<Sala> salas;
private Servidor() {
ServerSocket server;
Servidor.salas = new ArrayList<Sala>();
Servidor.salas.add(new Sala());
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 Servidor getInstance() {
if(Servidor.instancia == null) {
Servidor.instancia = new Servidor();
}
return Servidor.instancia;
}
public static String mostrarSalas() {
String ret = "";
int cont = 1;
for(Sala sala:Servidor.salas) {
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);
}
}

73
src/Tablero.java Normal file
View File

@ -0,0 +1,73 @@
public class Tablero {
int[][] casillas;
int[] ultimasCoordenadas;
public Tablero() {
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 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(i<j&&j<2) {
if(this.casillas[(j+1)][2]==this.casillas[i][0]) {
ret=this.casillas[i][0];
}
}else if(i==j) {
if(this.casillas[j][2]==this.casillas[i][0]) {
ret=this.casillas[i][0];
}
}else if(i>j&&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;
}
}