Poker

You can bet any amount up to the amount that you have.

You are then dealt 5 cards.

You may discard any number of these cards (i.e. 0, 1, 2, 3, 4 or all 5) in an attempt to obtain the best poker hand.

Enter the number of each card that you want to to discard (1 to 5). Enter 0 or hit return when you are finished discarding cards.

You are then dealt new cards to replace the discarded ones.

It shows you a code for your poker hand (not enough space in the program for the full descriptions)

If you have a winning hand then your winnings are paid.

The game ends when you have run out of money.

poker hands (in order, best to worst)

Royal Flush - A, K, Q, J, 10 all with the same suit

Straight Flush - 5 cards with consecutive values (see straight) all with the same suit

Four of a kind - 4 cards of the same value

Full House - 3 cards of the same value and the other 2 cards of the same value. For example 3 Fives and 2 Kings.

Flush - 5 cards all with the same suit

Straight - 5 cards with consecutive values. Ace can be counted as high or low, but not both at the same time. So both A, 2, 3, 4, 5 and 10, J, Q, K, A are straights, but Q, K, A, 2, 3 is not.

Three of a kind - 3 cards of the same value

Two Pair - 2 cards of the same value and another 2 cards of the same value

Jacks or Better - a pair of Jacks, Queens, Kings or Aces

The emulator that I use is an online one - JSBeeb. https://bbc.godbolt.org/

Simply copy and paste the program into it and type RUN.

The program assumes that PAGE (the start of the program) is set to &1900 which is the default in JSBeeb.

program structure (referring to the expanded version)

lines 100-105 - initialisation

lines 106-504 - main game loop

line 505 - end

lines 600-1000 - the h function that displays and evaluates the hand

there is also data stored on lines 400 (the poker hand codes), lines 902 (the card suits) and line 1000 (the card values and the pay table).

the data is stored in inaccessible parts of the program, so it is not executed as code.

variables used:

c(51) - the array of cards

x(12) - a count of the card values in the hand

y(4) - a count of the counts (used when checking for pairs, three of a kind and four of a kind)

d - the amount of money the player has

r - set to -48

l - the start address of the 1st letter of codes

m - the start address of the 2nd letter of codes

i,j,k - general purpose variables

b - the amount bet

w - the amount won

f - whether the hand is a flush (this includes royal flush and straight flush)

o - the suit of the 1st card in the hand (used when checking for a flush)

t - whether the hand is a straight (this includes royal flush and straight flush)

p - whether the hand contains a pair of Jacks, Queens, Kings or Aces

v - value of card (0=2, 1=3, 2=4, 3=5, 4=6, 5=7, 6=8, 7=9, 8=10, 9=J, 10=Q, 11=K, 12=A)

s - suit of card (0=Clubs, 1=Diamonds, 2=Hearts, 3=Spades)

n - number of cards with consecutive values (if this reaches 5 then t is set to -1 (true))

example hand evaluated to illustrate the use of the x and y arrays

5H, KC, 5D, KS, 5C

x(0) = 0

x(1) = 0

x(2) = 0

x(3) = 3

x(4) = 0

x(5) = 0

x(6) = 0

x(7) = 0

x(8) = 0

x(9) = 0

x(10) = 0

x(11) = 2

x(12) = 0

y(0) = 11

y(1) = 0

y(2) = 1

y(3) = 1

y(4) = 0

y(3) is 1 therefore hand is either a full house or three of a kind.

y(2) is 1 therefore hand is a full house (h function returns 3).

pay table (h function return value, name, code, win based on a \$1 bet)

0 Royal Flush RF 800

1 Straight Flush SF 50

2 Four of a kind 4K 25

3 Full House FH 9

4 Flush F 6

5 Straight S 4

6 Three of a kind 3K 3

7 Two Pair 2P 2

8 Jacks or Better JB 1

9 Nothing 0 0

full listing (expanded version) with comments

numeric arrays are initialised to 0

100DIMc(51),x(12),y(4)

101d=100

seed the pseudo random number generator

102r=RND(-TIME)

103r=-48

104l=&19DB

105m=l+10

106REPEAT

set the 52 cards in the c array

107FORi=0TO51

200c(i)=i

201NEXT

shuffle the cards (only need 1st 10 cards (5+5) so no need to shuffle all 52)

202FORi=0TO9

203j=52-RND(52-i)

204k=c(j)

205c(j)=c(i)

206c(i)=k

207NEXT

prompt for the bet

208REPEAT

300PRINT"\$";d;". Bet";

301INPUTb

302UNTILb>0ANDb<=d

display/evaluate the hand (although the evaluation is ignored)

303j=FNh

304REPEAT

once all cards have been discarded/replaced display/evaluate the hand again

400IFj<1ORj>5THENUNTILj=0:j=FNh ELSEc(j-1)=c(j+4):UNTIL0:RS4FFS32J0FFKH  KPB

calculate the amount won (800 times the bet for a royal flush, this value is too big to fit in the pay table)

500w=b*(j?&1B71+r-800*(j=0))

update the money

501d=d-b+w

display the code for the hand

502VDUl?j,m?j

503PRINT'"You win \$";w

504UNTILd=0

505END

function h returns a value 0 to 9: 0 being for a royal flush and 9 for nothing.

600DEFFNh

assume a flush initially

601f=-1

set o to suit of 1st card to check this

602o=c(0)DIV13

603t=0

604p=0

update x array with a count of the card values

also set f to 0 (false) if hand is not a flush

605FORi=0TO4

set v to value of card

606v=c(i)MOD13

set s to suit of card

607s=c(i)DIV13

608f=f ANDs=o

display each card in hand (10's require an extra '0' character).

700VDUi+49,58,v?&1B64,(v=8)*r,s?&1B3E

701PRINT

702x(v)=x(v)+1

set y array to 0

703y(i)=0

704NEXT

allow for A, 2, 3, 4, 5 straights (set n to 1 rather than 0 if the hand contains an Ace)

705n=-(x(12)=1)

update y array with a count of the counts

also set t if hand is a straight (5 consecutive values) and set p if hand contains a pair of Jacks, Queens, Kings or Aces

800FORi=0TO12

zero/increment number of consecutive values

801n=(n+1)*-(x(i)=1)

set t to -1 (true) if n is 5

802t=t ORn=5

803y(x(i))=y(x(i))+1

804p=p OR(i>8ANDx(i)=2)

set x array to 0 for next time

900x(i)=0

901NEXT

we now have all that we need to evaluate the hand

return the value as applicable (the checks are mostly done in order, the exception being three of a kind which is checked for before flush and straight)

902IFf ANDt THEN=1+(n=5)ELSEIFy(4)THEN=2ELSEIFy(3)THEN=6-y(2)*3:CDHS

1000IFf THEN=4ELSEIFt THEN=5ELSEIFy(2)=2THEN=7ELSE=p+9:234567891JQKA0bI9643210

 Status Released Author BASIC 10Liner Genre Card Game Tags 8-Bit, basic, basic10liner, bbc