Hasta JSON

This commit is contained in:
Guillermo Roche 2020-02-11 11:23:38 +01:00
parent 324a4db7fe
commit bd2d28cc04
6 changed files with 32 additions and 46 deletions

View File

@ -2,5 +2,8 @@
<classpath> <classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> <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="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"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<projectDescription> <projectDescription>
<name>Cliente2</name> <name>Cliente2Json</name>
<comment></comment> <comment></comment>
<projects> <projects>
</projects> </projects>

BIN
gson-2.8.6-javadoc.jar Normal file

Binary file not shown.

BIN
gson-2.8.6-sources.jar Normal file

Binary file not shown.

BIN
gson-2.8.6.jar Normal file

Binary file not shown.

View File

@ -6,11 +6,14 @@ import java.io.PrintWriter;
import java.net.Socket; import java.net.Socket;
import java.util.Scanner; import java.util.Scanner;
import com.google.gson.Gson;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Entra cliente"); System.out.println("Entra cliente");
Socket socket; Socket socket;
Gson gson = new Gson();
try { try {
if(args.length>0) { if(args.length>0) {
socket = new Socket(args[0], 8080); socket = new Socket(args[0], 8080);
@ -21,15 +24,13 @@ public class Main {
Scanner es = new Scanner(System.in); Scanner es = new Scanner(System.in);
BufferedReader bufferEntrada = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedReader bufferEntrada = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter bufferSalida = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true); PrintWriter bufferSalida = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
String entrada = bufferEntrada.readLine(); String entrada = gson.fromJson(bufferEntrada.readLine(), String.class);
while(!entrada.equals("fin")) {
System.out.println(entrada); System.out.println(entrada);
entrada = bufferEntrada.readLine();
}
System.out.println("Introduce el numero de partida en el que quieres entrar"); System.out.println("Introduce el numero de partida en el que quieres entrar");
bufferSalida.println(es.nextLine()); bufferSalida.println(es.nextLine());
System.out.println(bufferEntrada.readLine()); System.out.println(gson.fromJson(bufferEntrada.readLine(), String.class));
String tipoJuego = bufferEntrada.readLine(); String tipoJuego = gson.fromJson(bufferEntrada.readLine(), String.class);
switch(tipoJuego) { switch(tipoJuego) {
case "3": case "3":
partida3(bufferEntrada, bufferSalida, es); partida3(bufferEntrada, bufferSalida, es);
@ -45,9 +46,10 @@ public class Main {
} }
static void partida3(BufferedReader bufferEntrada, PrintWriter bufferSalida, Scanner es) throws IOException { static void partida3(BufferedReader bufferEntrada, PrintWriter bufferSalida, Scanner es) throws IOException {
System.out.println(bufferEntrada.readLine()); Gson gson = new Gson();
System.out.println(bufferEntrada.readLine()); System.out.println(gson.fromJson(bufferEntrada.readLine(), String.class));
while(dibujarTablero3(bufferEntrada)) { System.out.println(gson.fromJson(bufferEntrada.readLine(), String.class));
while(dibujarTablero(bufferEntrada)) {
System.out.println("introduce la x de la nueva marca"); System.out.println("introduce la x de la nueva marca");
bufferSalida.println(es.nextLine()); bufferSalida.println(es.nextLine());
System.out.println("introduce la y de la nueva marca"); System.out.println("introduce la y de la nueva marca");
@ -56,46 +58,27 @@ public class Main {
} }
static void partida4(BufferedReader bufferEntrada, PrintWriter bufferSalida, Scanner es) throws IOException { static void partida4(BufferedReader bufferEntrada, PrintWriter bufferSalida, Scanner es) throws IOException {
System.out.println(bufferEntrada.readLine()); Gson gson = new Gson();
System.out.println(bufferEntrada.readLine()); System.out.println(gson.fromJson(bufferEntrada.readLine(), String.class));
while(dibujarTablero4(bufferEntrada)) { System.out.println(gson.fromJson(bufferEntrada.readLine(), String.class));
while(dibujarTablero(bufferEntrada)) {
System.out.println("introduce la columna de la nueva ficha"); System.out.println("introduce la columna de la nueva ficha");
bufferSalida.println(es.nextLine()); bufferSalida.println(gson.fromJson(es.nextLine(), String.class));
} }
} }
static boolean dibujarTablero3(BufferedReader entrada) throws IOException { static boolean dibujarTablero(BufferedReader entrada) throws IOException {
for(int i = 0; i < 3; i++) { Gson gson = new Gson();
String linea = entrada.readLine(); String linea = gson.fromJson(entrada.readLine(), String.class);
if(linea.length() == 1) { if(linea.contains("v")) {
if(linea.equals("v")) {
System.out.println("Has ganado!"); System.out.println("Has ganado!");
}else { }else if (linea.contains("f")){
System.out.println("Has perdido..."); System.out.println("Has perdido...");
} }else {
return false;
} else {
System.out.println(linea); System.out.println(linea);
}
}
return true; return true;
} }
static boolean dibujarTablero4(BufferedReader entrada) throws IOException {
for(int i = 0; i < 7; i++) {
String linea = entrada.readLine();
if(linea.length() == 1) {
if(linea.equals("v")) {
System.out.println("Has ganado!");
}else {
System.out.println("Has perdido...");
}
return false; return false;
} else {
System.out.println(linea);
}
}
return true;
} }
} }