first commit
This commit is contained in:
		
						commit
						a1e1e6f2fa
					
				
							
								
								
									
										9
									
								
								.classpath
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								.classpath
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,9 @@ | |||||||
|  | <?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="lib" path="gson-2.8.6-javadoc.jar"/> | ||||||
|  | 	<classpathentry kind="lib" path="gson-2.8.6-sources.jar"/> | ||||||
|  | 	<classpathentry kind="lib" path="gson-2.8.6.jar"/> | ||||||
|  | 	<classpathentry kind="output" path="bin"/> | ||||||
|  | </classpath> | ||||||
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | |||||||
|  | /bin/ | ||||||
							
								
								
									
										17
									
								
								.project
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								.project
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,17 @@ | |||||||
|  | <?xml version="1.0" encoding="UTF-8"?> | ||||||
|  | <projectDescription> | ||||||
|  | 	<name>Servidor2Json</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> | ||||||
							
								
								
									
										11
									
								
								.settings/org.eclipse.jdt.core.prefs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								.settings/org.eclipse.jdt.core.prefs
									
									
									
									
									
										Normal 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 | ||||||
							
								
								
									
										
											BIN
										
									
								
								gson-2.8.6-javadoc.jar
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								gson-2.8.6-javadoc.jar
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								gson-2.8.6-sources.jar
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								gson-2.8.6-sources.jar
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								gson-2.8.6.jar
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								gson-2.8.6.jar
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										172
									
								
								src/ComunacacionJugador.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										172
									
								
								src/ComunacacionJugador.java
									
									
									
									
									
										Normal file
									
								
							| @ -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(); | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 			 | ||||||
|  | 		} | ||||||
|  | 		 | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										9
									
								
								src/Main.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/Main.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,9 @@ | |||||||
|  | 
 | ||||||
|  | public class Main { | ||||||
|  | 
 | ||||||
|  | 	public static void main(String[] args) { | ||||||
|  | 		Servidor servidor = Servidor.getInstance(); | ||||||
|  | 
 | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | } | ||||||
							
								
								
									
										36
									
								
								src/Sala3.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/Sala3.java
									
									
									
									
									
										Normal file
									
								
							| @ -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"; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | } | ||||||
							
								
								
									
										36
									
								
								src/Sala4.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/Sala4.java
									
									
									
									
									
										Normal file
									
								
							| @ -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"; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | } | ||||||
							
								
								
									
										78
									
								
								src/Servidor.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								src/Servidor.java
									
									
									
									
									
										Normal file
									
								
							| @ -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<Sala3> salas3; | ||||||
|  | 	static ArrayList<Sala4> salas4; | ||||||
|  | 	static final int juego3 = 0; | ||||||
|  | 	static final int juego4 = 1; | ||||||
|  | 	 | ||||||
|  | 	private Servidor() { | ||||||
|  | 		ServerSocket server; | ||||||
|  | 		Servidor.salas3 = new ArrayList<Sala3>(); | ||||||
|  | 		Servidor.salas3.add(new Sala3()); | ||||||
|  | 		Servidor.salas4 = new ArrayList<Sala4>(); | ||||||
|  | 		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; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										87
									
								
								src/Tablero3.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								src/Tablero3.java
									
									
									
									
									
										Normal file
									
								
							| @ -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(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; | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										120
									
								
								src/Tablero4.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										120
									
								
								src/Tablero4.java
									
									
									
									
									
										Normal file
									
								
							| @ -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; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user