Skip to content

Instantly share code, notes, and snippets.

@jbedo
Created March 6, 2011 09:30
Show Gist options
  • Save jbedo/857165 to your computer and use it in GitHub Desktop.
Save jbedo/857165 to your computer and use it in GitHub Desktop.
Deals bridge hands and outputs in either a pretty format or PBN
#include<u.h>
#include<libc.h>
#define CHILD(x) ((x) * 2 + 1)
enum{
Nsuits = 4,
Ncards = 13,
Ntot = Nsuits * Ncards,
Nplayers = 4
};
enum {
CLUBS,
DIAMONDS,
HEARTS,
SPADES
} suit;
enum {
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING,
ACE
} value;
char *valstr[Ncards] = {
[ACE] "A",
[TWO] "2",
[THREE] "3",
[FOUR] "4",
[FIVE] "5",
[SIX] "6",
[SEVEN] "7",
[EIGHT] "8",
[NINE] "9",
[TEN] "T",
[JACK] "J",
[QUEEN] "Q",
[KING] "K"
};
char *suitstr[Nsuits] = {
[CLUBS] = "♣",
[DIAMONDS] = "♢",
[HEARTS] = "♡",
[SPADES] = "♠"
};
typedef struct card card;
struct card{
enum suit s;
enum value v;
};
card deck[Ntot] = {
{ CLUBS, ACE },
{ CLUBS, TWO },
{ CLUBS, THREE },
{ CLUBS, FOUR },
{ CLUBS, FIVE },
{ CLUBS, SIX },
{ CLUBS, SEVEN },
{ CLUBS, EIGHT },
{ CLUBS, NINE },
{ CLUBS, TEN },
{ CLUBS, JACK },
{ CLUBS, QUEEN },
{ CLUBS, KING },
{ DIAMONDS, ACE },
{ DIAMONDS, TWO },
{ DIAMONDS, THREE },
{ DIAMONDS, FOUR },
{ DIAMONDS, FIVE },
{ DIAMONDS, SIX },
{ DIAMONDS, SEVEN },
{ DIAMONDS, EIGHT },
{ DIAMONDS, NINE },
{ DIAMONDS, TEN },
{ DIAMONDS, JACK },
{ DIAMONDS, QUEEN },
{ DIAMONDS, KING },
{ HEARTS, ACE },
{ HEARTS, TWO },
{ HEARTS, THREE },
{ HEARTS, FOUR },
{ HEARTS, FIVE },
{ HEARTS, SIX },
{ HEARTS, SEVEN },
{ HEARTS, EIGHT },
{ HEARTS, NINE },
{ HEARTS, TEN },
{ HEARTS, JACK },
{ HEARTS, QUEEN },
{ HEARTS, KING },
{ SPADES, ACE },
{ SPADES, TWO },
{ SPADES, THREE },
{ SPADES, FOUR },
{ SPADES, FIVE },
{ SPADES, SIX },
{ SPADES, SEVEN },
{ SPADES, EIGHT },
{ SPADES, NINE },
{ SPADES, TEN },
{ SPADES, JACK },
{ SPADES, QUEEN },
{ SPADES, KING }
};
void
sdown(card *cards, int n, int i)
{
int c, v;
card tmp;
if(i >= n)
return;
c = CHILD(i);
v = i;
if(c < n && cards[c].s < cards[v].s)
v = c;
else if(c < n && cards[c].s == cards[v].s && cards[c].v < cards[v].v)
v = c;
c++;
if(c < n && cards[c].s < cards[v].s)
v = c;
else if(c < n && cards[c].s == cards[v].s && cards[c].v < cards[v].v)
v = c;
if(v != i){
tmp = cards[i];
cards[i] = cards[v];
cards[v] = tmp;
sdown(cards, n, v);
}
}
void
sort(card *cards, int n)
{
int i;
card tmp;
/* Heapify */
for(i = n - 1; i >= 0; i--)
sdown(cards, n, i);
/* Sort */
while(n > 0){
tmp = cards[0];
cards[0] = cards[--n];
cards[n] = tmp;
sdown(cards, n, 0);
}
}
void
usage(void)
{
fprint(2, "usage: %s [-p]\n", argv0);
fprint(2, "\t-p: output in PBN\n");
exits("usage");
}
void
main(int argc, char **argv)
{
int i, j, pbn;
card c;
enum suit cur;
pbn = 0;
ARGBEGIN{
case 'p':
pbn = 1;
break;
default:
usage();
break;
}ARGEND;
/* Shuffle */
for(i = Ntot - 1; i >= 0; i--){
j = ntruerand(i + 1);
c = deck[i];
deck[i] = deck[j];
deck[j] = c;
}
for(i = 0; i < Nplayers; i++)
sort(deck + i * Ntot / Nplayers, Ntot / Nplayers);
cur = SPADES;
if(pbn) {
for(i = 0; i < Ntot; i++){
if(cur != deck[i].s){
cur = deck[i].s;
if(cur == SPADES)
print(" ");
else
print(".");
}
print("%s", valstr[deck[i].v]);
}
} else {
print("%s", suitstr[cur]);
for(i = 0; i < Ntot; i++){
if(cur != deck[i].s){
cur = deck[i].s;
if(cur == SPADES)
print("\n");
print("\n%s", suitstr[cur]);
}
print("%s", valstr[deck[i].v]);
}
}
print("\n");
exits(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment