From 0589aa52bdc8ba4aa75c61d462f67aae58c6c06d Mon Sep 17 00:00:00 2001 From: Josebaezx Date: Sun, 1 May 2022 17:30:00 -0400 Subject: [PATCH] Mejora en la creacion de numeros aleatorios --- out/production/java-e005-poker-02/Carta.class | Bin 3017 -> 0 bytes out/production/java-e005-poker-02/Juego.class | Bin 8771 -> 0 bytes out/production/java-e005-poker-02/MyClass.class | Bin 3239 -> 0 bytes src/Carta.java | 5 +---- src/Juego.java | 77 ++++++++++++++++------------------------------------------------------------- src/MyClass.java | 30 +++++++++++++++--------------- 6 files changed, 32 insertions(+), 80 deletions(-) diff --git a/out/production/java-e005-poker-02/Carta.class b/out/production/java-e005-poker-02/Carta.class index d01ffdc..7cdf54c 100644 Binary files a/out/production/java-e005-poker-02/Carta.class and b/out/production/java-e005-poker-02/Carta.class differ diff --git a/out/production/java-e005-poker-02/Juego.class b/out/production/java-e005-poker-02/Juego.class index a526f33..7b72631 100644 Binary files a/out/production/java-e005-poker-02/Juego.class and b/out/production/java-e005-poker-02/Juego.class differ diff --git a/out/production/java-e005-poker-02/MyClass.class b/out/production/java-e005-poker-02/MyClass.class index bf0aed7..4ec5bd6 100644 Binary files a/out/production/java-e005-poker-02/MyClass.class and b/out/production/java-e005-poker-02/MyClass.class differ diff --git a/src/Carta.java b/src/Carta.java index d5f9199..ef4219e 100644 --- a/src/Carta.java +++ b/src/Carta.java @@ -39,7 +39,7 @@ public class Carta { } public void valorAleatorio(){ - int max = 14; + int max = 13; int min = 1; int range = max - min + 1; int rand = (int)(Math.random() * range) + min; @@ -56,9 +56,6 @@ public class Carta { else if(rand==13){ this.valor="K"; } - else if(rand==14){ - this.valor="A"; - } else if (rand==1) { this.valor="A"; } else { diff --git a/src/Juego.java b/src/Juego.java index 156b634..f18f7f1 100644 --- a/src/Juego.java +++ b/src/Juego.java @@ -1,11 +1,11 @@ -import org.w3c.dom.ls.LSOutput; - import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class Juego { List valores = null; - final int MAX=3; - final int MIN=1; + final int MAX = 3; + final int MIN = 1; public Juego() { } @@ -337,74 +337,29 @@ public class Juego { return value; } - public int isRepetidos(Carta[] m1, Carta[] m2) { + public boolean isRepetidos(Carta[] m1, Carta[] m2) { Carta[] m3 = new Carta[10]; - Set cartas = new HashSet<>(); + int c = 5; for (int i = 0; i < m3.length; i++) { if (i < 5) { - cartas.add(m1[i]); + m3[i] = (m1[i]); } else if (i > 4) { - cartas.add(m2[i - c]); + m3[i] = (m2[i - c]); } } - return cartas.size(); + List cartas = new ArrayList(Arrays.asList(m3)); + List distinctElementList = cartas.stream() + .distinct() + .collect(Collectors.toList()); - } - - public Carta[] cambiarMano1(Carta[] m1) { - int range = MAX - MIN + 1; - int rand = (int) (Math.random() * range) + MIN; - if (rand == 1) { - return m1 = new Carta[]{ - new Carta("AS"), - new Carta("2S"), - new Carta("4S"), - new Carta("6S"), - new Carta("8S")}; - } else if (rand == 2) { - return m1 = new Carta[]{ - new Carta("KH"), - new Carta("2H"), - new Carta("5C"), - new Carta("TS"), - new Carta("JS")}; - }else { - return m1 = new Carta[]{ - new Carta("JC"), - new Carta("2H"), - new Carta("5S"), - new Carta("TC"), - new Carta("JS")}; + if (distinctElementList.size() < 10) { + return false; } + return true; } - public Carta[] cambiarMano2(Carta[] m2) { - int range = MAX - MIN + 1; - int rand = (int) (Math.random() * range) + MIN; - if (rand == 1) { - return m2 = new Carta[]{ - new Carta("AK"), - new Carta("TC"), - new Carta("5H"), - new Carta("TH"), - new Carta("JH")}; - }else if(rand == 2){ - return m2 = new Carta[]{ - new Carta("AC"), - new Carta("6C"), - new Carta("7H"), - new Carta("8H"), - new Carta("9H")}; - }else { - return m2 = new Carta[]{ - new Carta("TH"), - new Carta("9C"), - new Carta("5D"), - new Carta("TH"), - new Carta("JH")}; - } - } + } diff --git a/src/MyClass.java b/src/MyClass.java index 4bc5eee..2e5e252 100644 --- a/src/MyClass.java +++ b/src/MyClass.java @@ -26,23 +26,23 @@ public class MyClass { MyClass mc = new MyClass(); List jugadas = new ArrayList(); Carta[] m1 = new Carta[5]; - m1[0] = new Carta(); - m1[1] = new Carta(); - m1[2] = new Carta(); - m1[3] = new Carta(); - m1[4] = new Carta(); Carta[] m2 = new Carta[5]; - m2[0] = new Carta(); - m2[1] = new Carta(); - m2[2] = new Carta(); - m2[3] = new Carta(); - m2[4] = new Carta(); - //System.out.println(j.isRepetidos(m1,m2)); - if(j.isRepetidos(m1,m2) < 10){ - m1=j.cambiarMano1(m1); - m2=j.cambiarMano2(m2); - } + boolean respuesta=false; + do { + m1[0] = new Carta(); + m1[1] = new Carta(); + m1[2] = new Carta(); + m1[3] = new Carta(); + m1[4] = new Carta(); + m2[0] = new Carta(); + m2[1] = new Carta(); + m2[2] = new Carta(); + m2[3] = new Carta(); + m2[4] = new Carta(); + respuesta = j.isRepetidos(m1,m2); + }while (respuesta==false); + jugadas.add(m1); jugadas.add(m2); -- libgit2 0.26.0