bridge hand generator

new topic     » topic index » view thread      » older message » newer message

Hi,

Below is my early Xmas present to all budding card game programmers. I
hope it's short enough not to bother the rest of the audience. I think
it illustrates several points recently discussed on the list, and it
also gives me an opportunity to show one of the proudest achievements
of my programming life, here completely hidden in a couple of innocent
looking lines blink.

Some twenty-five years ago I watched bridge programers to shuffle
their cards, then to take the first quarter of the pack and give it to
North, the second quarter to East, and so on. The trouble was, each
hand was jumbled, it had to be sorted before it could be displayed -
slow and laborious process, especially back in those early days. So I
decided, it would be saner to shuffle not the cards - they remain in
their spots - but their recipients! No sorting! - I hope one or two
clever people on list will appreciate the trick.

For simplicity sake, the program is in the text mode only. The board
header shows the correct conventional bridge rotations of the dealer
and all vulnerabilities (what a word!). The yellow numbers indicate
high card counts of each hand (Ace=4, K=3, Q=2 and J=1).

Enjoy. jiri


--  file    : deal.ex : random bridge hand generator
--  author  : jiri babor
--  date    : 99-12-08

include graphics.e

constant ROW    = {5,11,17,11},
         COL    = {29,45,29,15},
         SUIT   = {6,3,4,5},
         RANK   = "AKQJT98765432",
         PLAYER = {"North","East ","South","West "},
         VUL    = {"Nil", "N-S", "E-W", "All"}
sequence pack, hand, hcp
integer board, key

procedure init_pack()
    pack = {}
    for i = 1 to 4 do
        for j = 1 to 13 do
            pack = append(pack,i)
        end for
    end for
end procedure

procedure show_header()
    integer dealer, vul
    dealer = remainder(board-1,4)+1
    vul = remainder(floor(remainder(board-1,16)/4) + dealer-1,4)+1
    text_color(BRIGHT_BLUE)
    position(2,1)
    puts(1, "   Random Bridge Hand Generator\n   Jiri Babor\n   Dec 1999\n\n")
    text_color(RED)
    printf(1, "   Deal   %d\n", board)
    puts(1, "   Dealer " & PLAYER[dealer]&'\n')
    puts(1, "   Vulner " & VUL[vul])
end procedure

procedure shuffle()
    integer j, temp
    for i = 52 to 2 by -1 do
        j = rand(52)
        temp = pack[j]
        pack[j] = pack[i]
        pack[i] = temp
    end for
end procedure

procedure deal()
    integer player, suit, rank
    hand = repeat(repeat({},4),4)
    hcp = repeat(0,4)
    for i = 1 to 52 do
        player = pack[i]
        suit = floor((i-1)/13)+1
        rank = remainder(i-1,13)+1
        hand[player][suit] = append(hand[player][suit], rank)
        if rank <= 4 then
            hcp[player] = hcp[player]+5-rank
        end if
    end for
end procedure

procedure show_deal()
    clear_screen()
    show_header()
    shuffle()
    deal()
    for i = 1 to 4 do
        position(ROW[i], COL[i])
        text_color(BLUE)
        puts(1,PLAYER[i])
        text_color(YELLOW)
        printf(1,"%3d",hcp[i])
        for j = 1 to 4 do
            position(ROW[i]+j, COL[i])
            if j=1 or j=4 then
                text_color(BLACK)
            else
                text_color(BRIGHT_RED)
            end if
            puts(1,SUIT[j])
            text_color(BRIGHT_WHITE)
            if length(hand[i][j]) then
                for k = 1 to length(hand[i][j]) do
                    puts(1,' ' & RANK[hand[i][j][k]])
                end for
            end if
        end for
    end for
    position(23,4)
    text_color(BLACK)
    puts(1, "Press ")
    text_color(BRIGHT_BLUE)
    puts(1, "Esc ")
    text_color(BLACK)
    puts(1, "to exit, any other key for next hand...\n")
end procedure -- show_deal

board = 1
init_pack()
bk_color(GREEN)
while 1 do
    show_deal()
    key = get_key()
    while key=-1 do key = get_key() end while
    if key = 27 then exit end if
    board += 1
end while

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu