Created
October 26, 2010 22:05
-
-
Save lfborjas/647932 to your computer and use it in GitHub Desktop.
Solución a la segunda parte del examen, con pruebas automatizadas agregadas.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package parte2; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
/** | |
* @author lfborjas | |
*/ | |
public class Baraja { | |
public static String[] palos={"diamantes", "tréboles", "corazones", "espadas"}; | |
private ArrayList<Carta> cartas; | |
public Baraja(){ | |
cartas = new ArrayList<Carta>(); | |
for(String palo: palos) | |
for(int i=1;i<=13;i++) | |
cartas.add(new Carta(palo, i)); | |
} | |
public void barajar(){ | |
Collections.shuffle(cartas); | |
} | |
public ArrayList<Carta> sacar(int n){ | |
ArrayList<Carta> rv = new ArrayList<Carta>(); | |
for(int i=0;i<n;i++) | |
rv.add(cartas.remove(0)); | |
return rv; | |
} | |
public Carta sacar(){ | |
return cartas.remove(0); | |
} | |
public ArrayList<Carta> getCartas(){ | |
return this.cartas; | |
} | |
public String toString(){ | |
return cartas.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package parte2; | |
import java.util.ArrayList; | |
import java.util.Random; | |
import junit.framework.TestCase; | |
/** | |
* | |
* @author lfborjas | |
*/ | |
public class BarajaTest extends TestCase { | |
public BarajaTest(String testName) { | |
super(testName); | |
} | |
@Override | |
protected void setUp() throws Exception { | |
super.setUp(); | |
} | |
@Override | |
protected void tearDown() throws Exception { | |
super.tearDown(); | |
} | |
/** | |
* Test of sacar method, of class Baraja. | |
*/ | |
public void testSacar_int() { | |
System.out.println("sacar"); | |
Random gen = new Random(); | |
int n = gen.nextInt(52); | |
Baraja instance = new Baraja(); | |
ArrayList result = instance.sacar(n); | |
assertEquals(n, result.size()); | |
System.out.println(result); | |
assertEquals(52-n, instance.getCartas().size()); | |
} | |
/** | |
* Test of sacar method, of class Baraja. | |
*/ | |
public void testSacar_0args() { | |
System.out.println("sacar"); | |
Baraja instance = new Baraja(); | |
Carta expResult = null; | |
Carta result = instance.sacar(); | |
assertEquals(52-1, instance.getCartas().size()); | |
System.out.println(result); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package parte2; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
/** | |
* @author lfborjas | |
*/ | |
public class Carta { | |
private String palo; | |
private int numero; | |
public Carta(String palo, int numero){ | |
this.palo = palo; | |
this.numero = numero; | |
} | |
@Override | |
public String toString(){ | |
return String.format("%s de %s", getRankDisplay(), palo); | |
} | |
public String getRankDisplay(){ | |
switch(this.numero){ | |
case 1: | |
return "as"; | |
case 11: | |
return "sota"; | |
case 12: | |
return "reina"; | |
case 13: | |
return "rey"; | |
default: | |
return String.valueOf(numero); | |
} | |
} | |
public boolean equals(Carta otra){ | |
return this.palo.equals(otra.palo) && this.numero == otra.numero; | |
} | |
public static String getSerie(ArrayList<Carta> mano){ | |
int mayorPalo=0; | |
int mayorNum=0; | |
HashMap<Integer, Integer> numeros = new HashMap<Integer, Integer>(); | |
HashMap<String, Integer> palos = new HashMap<String, Integer>(); | |
for(Carta c: mano){ | |
int add = palos.get(c.palo)!= null ? palos.get(c.palo)+1 : 1 ; | |
palos.put(c.palo, add); | |
add = numeros.get(c.numero) != null ? numeros.get(c.numero)+1: 1; | |
numeros.put(c.numero, add); | |
} | |
for(Integer e: palos.values()){ | |
mayorPalo = e > mayorPalo ? e : mayorPalo; | |
} | |
for(Integer e: numeros.values()){ | |
mayorNum = e > mayorNum ? e : mayorNum; | |
} | |
StringBuilder retVal = new StringBuilder(); | |
if(mayorPalo == mano.size()) | |
return "flush"; | |
if(mayorNum == mano.size() -1) | |
return "poker"; | |
return "ninguna"; | |
} | |
public static int getCuenta(ArrayList<Carta> mano){ | |
int c=0; | |
boolean hasAce=false; | |
for(Carta k: mano){ | |
switch(k.numero){ | |
case 1: | |
c += 11; | |
hasAce = true; | |
break; | |
case 11: | |
case 12: | |
case 13: | |
c += 10; | |
break; | |
default: | |
c += k.numero; | |
} | |
} | |
if(c >= 21 && hasAce) | |
return c-10; | |
else | |
return c; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package parte2; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import junit.framework.TestCase; | |
/** | |
* | |
* @author lfborjas | |
*/ | |
public class CartaTest extends TestCase { | |
public CartaTest(String testName) { | |
super(testName); | |
} | |
@Override | |
protected void setUp() throws Exception { | |
super.setUp(); | |
} | |
@Override | |
protected void tearDown() throws Exception { | |
super.tearDown(); | |
} | |
/** | |
* Test of toString method, of class Carta. | |
*/ | |
public void testToString() { | |
System.out.println("toString"); | |
Carta[] instances = {new Carta("diamantes", 11), | |
new Carta("diamantes", 12), | |
new Carta("diamantes", 13), | |
new Carta("diamantes", 1), | |
new Carta("diamantes", 5), | |
}; | |
String[] expResults = {"sota de diamantes", | |
"reina de diamantes", | |
"rey de diamantes", | |
"as de diamantes", | |
"5 de diamantes", | |
}; | |
for(int i=0; i < instances.length; i++) | |
assertEquals(instances[i].toString(), expResults[i]); | |
} | |
/** | |
* Test of equals method, of class Carta. | |
*/ | |
public void testEquals() { | |
System.out.println("equals"); | |
Carta otra = new Carta("corazones", 11); | |
Carta otra2 = new Carta("corazones", 12); | |
Carta otra3 = new Carta("espadas", 11); | |
Carta instance = new Carta("corazones", 11); | |
assertTrue(instance.equals(otra)); | |
assertFalse(instance.equals(otra2)); | |
assertFalse(instance.equals(otra3)); | |
} | |
/** | |
* Test of getSerie method, of class Carta. | |
*/ | |
public void testGetSerie() { | |
System.out.println("getSerie"); | |
ArrayList<Carta> manoFlush = new ArrayList<Carta>(); | |
ArrayList<Carta> manoPoker = new ArrayList<Carta>(); | |
ArrayList<Carta> manoNil = new ArrayList<Carta>(); | |
//llenar el flush: | |
manoFlush.add(new Carta("diamantes", 1)); | |
manoFlush.add(new Carta("diamantes", 9)); | |
manoFlush.add(new Carta("diamantes", 10)); | |
manoFlush.add(new Carta("diamantes", 12)); | |
manoFlush.add(new Carta("diamantes", 11)); | |
Collections.shuffle(manoFlush); | |
//poker: | |
manoPoker.add(new Carta("diamantes", 1)); | |
manoPoker.add(new Carta("espadas", 1)); | |
manoPoker.add(new Carta("corazones", 1)); | |
manoPoker.add(new Carta("tréboles", 1)); | |
manoPoker.add(new Carta("diamantes", 12)); | |
Collections.shuffle(manoFlush); | |
//esta mano no es ni poker ni flush | |
manoNil.add(new Carta("corazones", 1)); | |
manoNil.add(new Carta("espadas", 1)); | |
manoNil.add(new Carta("tréboles", 1)); | |
manoNil.add(new Carta("diamantes", 1)); | |
manoNil.add(new Carta("corazones", 1)); | |
Collections.shuffle(manoNil); | |
assertEquals("flush", Carta.getSerie(manoFlush)); | |
assertEquals("poker", Carta.getSerie(manoPoker)); | |
assertEquals("ninguna", Carta.getSerie(manoNil)); | |
} | |
/** | |
* Test of getCuenta method, of class Carta. | |
*/ | |
public void testGetCuenta() { | |
System.out.println("getCuenta"); | |
ArrayList<Carta> manoSinAs = new ArrayList<Carta>(); | |
ArrayList<Carta> mano = new ArrayList<Carta>(); | |
//una mano con un as que la hace pasarse de 21: | |
mano.add(new Carta("corazones", 11)); | |
mano.add(new Carta("corazones", 1)); | |
//una mano normal | |
manoSinAs.add(new Carta("corazones", 12)); | |
manoSinAs.add(new Carta("corazones", 13)); | |
manoSinAs.add(new Carta("corazones", 7)); | |
assertEquals(11, Carta.getCuenta(mano)); | |
assertEquals(27, Carta.getCuenta(manoSinAs)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package parte2; | |
import java.util.ArrayList; | |
import javax.swing.JOptionPane; | |
/** | |
* | |
* @author lfborjas | |
*/ | |
public class Main { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
boolean consent=true; | |
Baraja baraja = new Baraja(); | |
baraja.barajar(); | |
ArrayList<Carta> sacadas; | |
sacadas = baraja.sacar(5); | |
do{ | |
JOptionPane.showMessageDialog(null, | |
String.format("Cartas sacadas:\n%s",sacadas)); | |
JOptionPane.showMessageDialog(null, | |
String.format("¿Serie especial?:\n%s",Carta.getSerie(sacadas))); | |
JOptionPane.showMessageDialog(null, | |
String.format("Cuenta:\n%d",Carta.getCuenta(sacadas))); | |
consent = JOptionPane.showConfirmDialog(null, "Desea salir?", | |
"¿Desea salir?", | |
JOptionPane.YES_NO_OPTION) == 0; | |
if(baraja.getCartas().size() >= 5) | |
sacadas = baraja.sacar(5); | |
else | |
consent = true; | |
}while(!consent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment