Solucion Solid

package com.trifulcas.juegoPPTLSID;

import java.util.Arrays;

public class JuegoColores implements ILogica{
	private String[] jugadas = { "verde","azul","amarillo","naranja","rojo" };

	public JuegoColores() {

	}

	@Override
	public int comprobar(String jugada1, String jugada2) {
		int pos1 = Arrays.asList(jugadas).indexOf(jugada1.toLowerCase());
		int pos2 = Arrays.asList(jugadas).indexOf(jugada2.toLowerCase());
		if ((pos1 + 1) % jugadas.length == pos2)
			return 1;
		if ((pos2 + 1) % jugadas.length == pos1)
			return 2;
		return 0;
	}

	@Override
	public String[] validas() {

		return this.jugadas;
	}
}


package com.trifulcas.juegoPPTLSID;

public interface IMostrar {
	 void mostrar(String res);
}


package com.trifulcas.juegoPPTLSID;

public class Juego {
	private Jugador jugador1;
	private Jugador jugador2;
	private ILogica _iLogica;
	private IMostrar _iMostrar;

	private String[] jugadas;

	public String[] getJugadas() {
		return jugadas;
	}

	public void setJugadas(String[] jugadas) {
		this.jugadas = jugadas;
	}

	public Jugador getJugador1() {
		return jugador1;
	}

	public void setJugador1(Jugador jugador1) {
		this.jugador1 = jugador1;
	}

	public Jugador getJugador2() {
		return jugador2;
	}

	public void setJugador2(Jugador jugador2) {
		this.jugador2 = jugador2;
	}

	public Juego(Jugador jugador1, Jugador jugador2, ILogica ilogica, IMostrar imostrar) {
		this.jugador1 = jugador1;
		this.jugador2 = jugador2;
		_iLogica = ilogica;
		jugadas = _iLogica.validas();
		_iMostrar = imostrar;
	}

	public Juego(Jugador jugador1, Jugador jugador2, ILogica ilogica) {
		this(jugador1, jugador2, ilogica, new Consola());
	}

	public String jugar() {
		jugador1.pedirJugada(jugadas);
		jugador2.pedirJugada(jugadas);
		_iMostrar.mostrar(jugador1.getNombre() + " elige " + jugador1.getJugada());
		_iMostrar.mostrar(jugador2.getNombre() + " elige " + jugador2.getJugada());
		int res = _iLogica.comprobar(jugador1.getJugada(), jugador2.getJugada());
		String resultado="Empate";
		if (res == 1) {
			resultado= jugador1.getNombre();
		}
		if (res == 2) {
			resultado= jugador2.getNombre();
		}
		_iMostrar.mostrar(resultado);
		return resultado;
	}
}

package com.trifulcas.juegoPPTLSID;

public class Consola implements IMostrar {

	@Override
	public void mostrar(String res) {
		System.out.println(res);
	}

}


package com.trifulcas.juegoPPTLSID;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class Fichero implements IMostrar {

	@Override
	public void mostrar(String res) {
		Path path = Paths.get("resultado.txt");
		try {
			if (!Files.exists(path))
				Files.createFile(path);
			Files.writeString(path, res+"\r\n", StandardOpenOption.APPEND);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

Publicado por

Avatar del usuario

Juan Pablo Fuentes

Formador de programación y bases de datos