If you wish you can take a look at a very old deck of cards implementation I did a long time ago:
It’s split into 2 sketches:
1 creates & saves the “.csv” file, while the other loads it and makes a deck of cards out of it.
For convenience I’ve merged them now into 1 sketch w/ 4 “.pde” tabs:
“Table_CSV_Card_Pack.pde”:
/**
* Table CSV Card Pack (v1.0.1)
* by GoToLoop (2022/Sep/13)
*
* https://discourse.Processing.org/t/
* the-operator-is-undefined-for-the-argument-type-s-int-
* how-to-resolve-error/38705/6
*
* https://forum.Processing.org/two/discussion/2801/
* picking-cards-at-random-then-excluding-those-from-further-picking-#Item_2
*/
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
static final String ARCHIVE = "Cardpack.csv", EXT = ".jpg";
static final int NUM = 5, GAP = 040, FPS = 60, FILL = 0200;
final List<Card> myHand = new ArrayList<Card>(NUM);
Card[] cards;
void setup() {
size(600, 900);
frameRate(FPS);
noLoop();
fill(FILL);
textSize(GAP);
textAlign(CENTER);
cards = loadCards(ARCHIVE, EXT, true, false);
//cards = loadCards(ARCHIVE, EXT, true, true);
showDeckContent(true, cards);
println(ENTER);
mousePressed();
}
void draw() {
clear();
for (int i = myHand.size(); i-- != 0;
myHand.get(i).displayAt(width >> 1, i*GAP + GAP));
}
void mousePressed() {
final int btn = mouseButton;
final int len = myHand.size();
if (btn == RIGHT & len > 0) myHand.remove(len - 1);
else if (btn == LEFT) pickCard();
else PickNewRandomHand(NUM);
println(myHand);
println();
redraw();
}
“Card_Class.pde”
class Card implements Comparable<Card> {
short x, y;
final byte w, h;
final byte value, group;
final int hash;
final String rank, suit, info;
final PImage img;
Card(final int val, final String r, final String s, final PImage pic) {
value = (byte) abs(val);
rank = r;
suit = s;
img = pic;
group = (byte) suit.charAt(0);
hash = group<<020 | value;
w = (byte) (img == null? 0 : img.width);
h = (byte) (img == null? 0 : img.height);
info = rank + " of " + suit;
}
Card setPos(final int xx, final int yy) {
x = (short) xx;
y = (short) yy;
return this;
}
Card display() {
return displayAt(x, y);
}
Card displayAt(final int x, final int y) {
if (img != null) set(x, y, img);
else text(info, x, y);
return this;
}
String toString() {
return info;
}
int hashCode() {
return hash;
}
boolean equals(final Object o) {
return o instanceof Card? ((Card) o).hash == hash : false;
}
int compareTo(final Card o) {
return value != o.value?
Integer.signum(value - o.value) :
Integer.signum(group - o.group);
}
}
“Card_Functions.pde”:
Card[] loadCards(
final String name, final String ext,
final boolean gotHeader, final boolean toLoadImages)
{
final Table table;
if (!dataFile(name).isFile()) table = createAndSaveTableFile(name);
else {
table = loadTable(name, gotHeader? "header" : "");
table.setColumnType(0, Table.INT);
}
final Card[] deck = new Card[table.getRowCount()];
for (int i = 0; i != deck.length; ++i) {
final TableRow row = table.getRow(i);
final int num = row.getInt(0);
final String rank = row.getString(1), suit = row.getString(2);
final PImage img = toLoadImages? loadImage(row.getString(3) + ext) : null;
deck[i] = new Card(num, rank, suit, img);
}
return deck;
}
static final void showDeckContent(final boolean toSort, final Card... cards) {
println();
println(cards);
println();
if (toSort) {
Arrays.sort(cards);
println(cards);
println();
}
for (final Card c : cards) {
final int rank = c.hash & 0xFF;
final char suit = (char) (c.hash >> 020);
print(" - " + rank + suit);
}
}
void pickCard() {
if (myHand.size() >= cards.length) return;
Card c;
do c = cards[(int) random(cards.length)];
while (myHand.contains(c));
myHand.add(c);
Collections.sort(myHand);
}
void PickNewRandomHand(int qty) {
if ((qty = min(abs(qty), cards.length))
== (cards.length | myHand.size())) return;
myHand.clear();
if (qty == cards.length) {
myHand.addAll(Arrays.asList(cards));
return;
}
while (qty != 0) {
final Card c = cards[(int) random(cards.length)];
if (!myHand.contains(c)) {
myHand.add(c);
--qty;
}
}
Collections.sort(myHand);
}
“Table_CSV_File_Creation.pde”:
static final String[] RANKS = {
"Ace", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King"
};
static final String[] SUITS = {
"Clubs", "Diamonds", "Hearts", "Spades"
};
Table createAndSaveTableFile(final String filename) {
final Table table = new Table();
table.addColumn("Number", Table.INT);
table.addColumn("Rank");
table.addColumn("Suit");
table.addColumn("Image");
for (int r = 0, s = 0; s != SUITS.length; ++s, r = 0)
while (r != RANKS.length) {
final TableRow newRow = table.addRow();
newRow.setString("Rank", RANKS[r]);
newRow.setString("Suit", SUITS[s]);
newRow.setInt("Number", ++r);
newRow.setString("Image", str(r) +
Character.toLowerCase(SUITS[s].charAt(0)));
}
saveTable(table, dataPath(filename));
return table;
}