dados
This commit is contained in:
1 parent
be0890250f
commit
5170e7b2e7
3 files changed
+156
-4
No files matched your search
+7
-3
@@ -4,7 +4,7 @@ public class Metodos1{
|
|||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
int numero=0;
|
int numero=0;
|
||||||
System.out.println("Introduce un entero");
|
System.out.println("Introduce un entero");
|
||||||
numero = leerEntero();
|
numero = leerEnteroPositivo();
|
||||||
int resultado = potencias2(numero);
|
int resultado = potencias2(numero);
|
||||||
System.out.printf("El resultado es %d\n",resultado);
|
System.out.printf("El resultado es %d\n",resultado);
|
||||||
}
|
}
|
||||||
@@ -13,19 +13,23 @@ public class Metodos1{
|
|||||||
while(1 << result <= n) result++;
|
while(1 << result <= n) result++;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
public static int leerEntero() {
|
public static int leerEnteroPositivo() {
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
boolean parar = false;
|
boolean parar = false;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
while(!parar) {
|
while(!parar) {
|
||||||
try {
|
try {
|
||||||
n = sc.nextInt();
|
n = sc.nextInt();
|
||||||
parar = true;
|
|
||||||
} catch(Exception e){
|
} catch(Exception e){
|
||||||
System.out.println("Número invalido, intentalo otra vez");
|
System.out.println("Número invalido, intentalo otra vez");
|
||||||
} finally {
|
} finally {
|
||||||
sc.next();
|
sc.next();
|
||||||
}
|
}
|
||||||
|
if(n > 0)
|
||||||
|
parar = true;
|
||||||
|
else {
|
||||||
|
System.out.println("Tiene que ser positivo");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
sc.close();
|
sc.close();
|
||||||
return n;
|
return n;
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ public class Metodos3 {
|
|||||||
parar = true;
|
parar = true;
|
||||||
} catch(Exception e){
|
} catch(Exception e){
|
||||||
System.out.println("Número invalido, intentalo otra vez");
|
System.out.println("Número invalido, intentalo otra vez");
|
||||||
} finally {
|
|
||||||
sc.next();
|
sc.next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,149 @@
|
|||||||
|
import java.util.Random;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Metodos7 {
|
||||||
|
private class Jugador {
|
||||||
|
String nombre = "Anonimo";
|
||||||
|
int puntos = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("GUERRA DE DADOS");
|
||||||
|
System.out.println("===============");
|
||||||
|
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
Random r = new Random();
|
||||||
|
int puntosParaGanar = 1000;
|
||||||
|
|
||||||
|
Metodos7 juego = new Metodos7();
|
||||||
|
Jugador j1 = juego.new Jugador();
|
||||||
|
Jugador j2 = juego.new Jugador();
|
||||||
|
|
||||||
|
switch (menuPrincipal(sc)) {
|
||||||
|
case 1: // Configuración
|
||||||
|
System.out.println("Cuantos puntos hacen falta para ganar?");
|
||||||
|
puntosParaGanar = leerEnteroPositivo(sc);
|
||||||
|
case 2: // Jugar
|
||||||
|
System.out.println("Introduce el nombre del jugador 1");
|
||||||
|
j1.nombre = sc.nextLine();
|
||||||
|
System.out.println("Introduce el nombre del jugador 2");
|
||||||
|
j2.nombre = sc.nextLine();
|
||||||
|
|
||||||
|
while (j1.puntos < puntosParaGanar && j2.puntos < puntosParaGanar) {
|
||||||
|
int puntos1 = tirarDados(j1, sc, r);
|
||||||
|
int puntos2 = tirarDados(j2, sc, r);
|
||||||
|
if(puntos1 == puntos2){
|
||||||
|
System.out.println("Ha habido un empate!!");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Jugador ganador, perdedor;
|
||||||
|
if (puntos1 > puntos2){
|
||||||
|
ganador = j1;
|
||||||
|
perdedor = j2;
|
||||||
|
} else {
|
||||||
|
ganador = j2;
|
||||||
|
perdedor = j1;
|
||||||
|
}
|
||||||
|
int puntosGanador = puntos1 + puntos2;
|
||||||
|
int puntosPerdedor = (puntos1 < puntos2) ? puntos1 : puntos2;
|
||||||
|
System.out.printf("%s gana la ronda: +%dpts\n", ganador.nombre, puntosGanador);
|
||||||
|
ganador.puntos += puntosGanador;
|
||||||
|
System.out.printf("%s pierde la ronda: -%dpts\n", perdedor.nombre, puntosPerdedor);
|
||||||
|
perdedor.puntos -= puntosPerdedor;
|
||||||
|
mostrarPuntos(j1, j2);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (j1.puntos >= puntosParaGanar && j2.puntos >= puntosParaGanar) {
|
||||||
|
System.out.println("Ha habido un empate");
|
||||||
|
} else if (j1.puntos > j2.puntos) {
|
||||||
|
System.out.printf("El ganador es %s\n", j1.nombre);
|
||||||
|
} else {
|
||||||
|
System.out.printf("El ganador es %s\n", j2.nombre);
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void mostrarPuntos(Jugador j1, Jugador j2) {
|
||||||
|
System.out.printf("%s tiene %d puntos\n", j1.nombre, j1.puntos);
|
||||||
|
System.out.printf("%s tiene %d puntos\n", j2.nombre, j2.puntos);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int tirarDados(Jugador j, Scanner sc, Random r) {
|
||||||
|
System.out.printf("Turno de %s\n", j.nombre);
|
||||||
|
System.out.println("Pulsa ENTER para tirar los dados");
|
||||||
|
sc.nextLine();
|
||||||
|
int d1 = r.nextInt(1, 7);
|
||||||
|
int d2 = r.nextInt(1, 7);
|
||||||
|
int resultado = d1;
|
||||||
|
System.out.printf("Has tirado un %d con modificador %d\n", d1, d2);
|
||||||
|
switch (d2) {
|
||||||
|
case 1:
|
||||||
|
resultado = 1;
|
||||||
|
for (int i = 2; i <= d2; i++) resultado *= i;
|
||||||
|
System.out.println("MODIFICADOR: Factorial");
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
resultado = (d1 * (d1 + 1)) / 2;
|
||||||
|
System.out.println("MODIFICADOR: Sumatorio");
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
resultado = d1 * d1;
|
||||||
|
System.out.println("MODIFICADOR: Cuadrado");
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
resultado = 1;
|
||||||
|
System.out.println("MODIFICADOR: Elevar a 0");
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
System.out.println("Sin modificador :-(");
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
resultado = 0;
|
||||||
|
System.out.println("MODIFICADOR: Sin puntos :-(");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
System.out.printf("Ahora tu puntuación es %d\n", resultado);
|
||||||
|
System.out.println();
|
||||||
|
return resultado;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int menuPrincipal(Scanner sc) {
|
||||||
|
boolean parar = false;
|
||||||
|
while (!parar) {
|
||||||
|
System.out.print("Menu Principal:\n" +
|
||||||
|
"\t1-Configurar\n" +
|
||||||
|
"\t2-Jugar\n" +
|
||||||
|
"\t3-Salir\n");
|
||||||
|
int n = leerEnteroPositivo(sc);
|
||||||
|
if (n > 0 && n <= 3) {
|
||||||
|
return n;
|
||||||
|
} else System.out.println("Numero invalido, elige uno entre las opciones");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int leerEnteroPositivo(Scanner sc) {
|
||||||
|
boolean parar = false;
|
||||||
|
int n = -1;
|
||||||
|
while (!parar) {
|
||||||
|
try {
|
||||||
|
n = sc.nextInt();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Numero invalido vuelve a insertar uno");
|
||||||
|
sc.nextLine();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (n > 0) parar = true;
|
||||||
|
else System.out.println("Tiene que ser un numero positivo!");
|
||||||
|
}
|
||||||
|
sc.nextLine();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user