Commit 0589aa52 by Josebaezx

Mejora en la creacion de numeros aleatorios

parent 57e00f26
...@@ -39,7 +39,7 @@ public class Carta { ...@@ -39,7 +39,7 @@ public class Carta {
} }
public void valorAleatorio(){ public void valorAleatorio(){
int max = 14; int max = 13;
int min = 1; int min = 1;
int range = max - min + 1; int range = max - min + 1;
int rand = (int)(Math.random() * range) + min; int rand = (int)(Math.random() * range) + min;
...@@ -56,9 +56,6 @@ public class Carta { ...@@ -56,9 +56,6 @@ public class Carta {
else if(rand==13){ else if(rand==13){
this.valor="K"; this.valor="K";
} }
else if(rand==14){
this.valor="A";
}
else if (rand==1) { else if (rand==1) {
this.valor="A"; this.valor="A";
} else { } else {
......
import org.w3c.dom.ls.LSOutput;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Juego { public class Juego {
List<Integer> valores = null; List<Integer> valores = null;
final int MAX=3; final int MAX = 3;
final int MIN=1; final int MIN = 1;
public Juego() { public Juego() {
} }
...@@ -337,74 +337,29 @@ public class Juego { ...@@ -337,74 +337,29 @@ public class Juego {
return value; return value;
} }
public int isRepetidos(Carta[] m1, Carta[] m2) { public boolean isRepetidos(Carta[] m1, Carta[] m2) {
Carta[] m3 = new Carta[10]; Carta[] m3 = new Carta[10];
Set<Carta> cartas = new HashSet<>();
int c = 5; int c = 5;
for (int i = 0; i < m3.length; i++) { for (int i = 0; i < m3.length; i++) {
if (i < 5) { if (i < 5) {
cartas.add(m1[i]); m3[i] = (m1[i]);
} else if (i > 4) { } else if (i > 4) {
cartas.add(m2[i - c]); m3[i] = (m2[i - c]);
} }
} }
return cartas.size(); List<Carta> cartas = new ArrayList(Arrays.asList(m3));
List<Carta> distinctElementList = cartas.stream()
.distinct()
.collect(Collectors.toList());
} if (distinctElementList.size() < 10) {
return false;
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")};
} }
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")};
}
}
} }
...@@ -26,23 +26,23 @@ public class MyClass { ...@@ -26,23 +26,23 @@ public class MyClass {
MyClass mc = new MyClass(); MyClass mc = new MyClass();
List<Carta[]> jugadas = new ArrayList<Carta[]>(); List<Carta[]> jugadas = new ArrayList<Carta[]>();
Carta[] m1 = new Carta[5]; 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]; 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)); boolean respuesta=false;
if(j.isRepetidos(m1,m2) < 10){ do {
m1=j.cambiarMano1(m1); m1[0] = new Carta();
m2=j.cambiarMano2(m2); 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(m1);
jugadas.add(m2); jugadas.add(m2);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment