Skip to content

Instantly share code, notes, and snippets.

@jmbruel
Created December 17, 2018 10:24
Show Gist options
  • Save jmbruel/846115f49df9714985fdb47b098cde3d to your computer and use it in GitHub Desktop.
Save jmbruel/846115f49df9714985fdb47b098cde3d to your computer and use it in GitHub Desktop.
CPOA Design pattern course IUT Blagnac
package cpoa2018;
// Initial code from https://www.usna.edu/Users/cs/wcbrown/courses/S15IC211/lec/l03/lec.html
import java.util.*;
public class Card
{
public static int makeCard(int face, int suit) { return 20*suit + face; }
public static int cardToSuit(int card) { return card/20; }
public static int cardToFace(int card) { return card % 20; }
public static String cardToString(int card)
{
String[] suits = {"\u2663","\u2666","\u2665","\u2660"};
String[] faces = {"","","2","3","4","5","6","7","8","9","10","J","Q","K","A"};
return faces[cardToFace(card)] + suits[cardToSuit(card)];
}
public static int[] makeDeck()
{
int[] D = new int[52];
int i = 0;
for(int s = 0; s < 4; s++)
for(int f = 2; f <= 14; f++)
D[i++] = makeCard(f,s);
return D;
}
public static void shuffle(int[] D, Random rand)
{
for(int k = 0; k < 200; k++)
{
int i = rand.nextInt(52), j = rand.nextInt(52);
int t = D[i];
D[i] = D[j];
D[j] = t;
}
}
public static void main(String[] args)
{
Random rand = new Random(System.currentTimeMillis());
int[] D = makeDeck();
shuffle(D,rand);
for(int i = 0; i < 52; i++)
System.out.println(cardToString(D[i]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment