BASIC Code

Craps.BAS

Click on the game window below in order to type.

Craps is the Nevada rules dice game as presented in David Ahl's 1973 book BASIC Computer Games. I've converted and refactored it, creating the above build with the BASIC Anywhere Machine.

The Listing

PRINT TAB(11); "**********************************************************"
PRINT TAB(11); "**********************************************************"
PRINT TAB(11); "**                                                      **"
PRINT TAB(11); "**                        CRAPS                         **"
PRINT TAB(11); "**                                                      **"
PRINT TAB(11); "**     As seen in David Ahl's BASIC Computer Games      **"
PRINT TAB(11); "**     Converted and refactored by Michael Coorlim      **"
PRINT TAB(11); "**                                                      **"
PRINT TAB(11); "**********************************************************"
PRINT TAB(11); "**********************************************************"
PRINT:PRINT:PRINT

REM ********************
REM ** INITIALIZATION **
REM ********************
BALANCE = 0 : REM ** HOW MUCH WE'VE WON OR LOST **

Eschewing line numbers to make better use of the BASIC Anywhere Machine's labels this time around. Once again, the code was uncommented, so I'm adding REM statements liberally.

The book's code listing started with prompting the player to enter an integer which effectively acts as a random seed. This is unnecessary for us, so we omit it. We also rename the unhelpful variable R to the more understandable 'Balance.'

Interestingly this game doesn't give the player a set dollar amount to wager; instead it lets you bet however much you want, and only tracks how much you've lost or won.

?"INSTRUCTIONS:"
?"IF YOU ROLL A 2, 3, OR 12 ON THE FIRST ROLL YOU LOSE. ROLL A 7 OR 11 AND YOU WIN."
?"ROLL ANY OTHER NUMBER AND THAT BECOMES THE POINT."
?"ON SECOND OR SUBSEQUENT ROLLS HITTING THE POINT AGAIN WINS, BUT A 7 LOSES."
?

I've rewritten the instructions for clarity, and switched from PRINT statements to the shorthand question mark.

place_bet:
INPUT "HOW MUCH TO WAGER"; BET
?"ROLLING THE BONES:"
gosub roll_dice

Start of our loop. I've added the place_bet label; I'm keeping them lower-case as a convention.

I've changed the amount bet variable from the ambiguous F to the more readable BET.

roll_dice:
D1 = INT(6*RND(1))+1
D2 = INT(6*RND(1))+1
DICE = D1 + D2 : REM ** SUM OF THE DICE **
RETURN

The initial listing repeated the random roll code multiple times. I make it a subroutine, and rename the variables to D1 and D2 for the dice, and DICE for their sum. For some reason the individual die variables were different each time, as were the sum variables. This was highly unnecessary.

REM ** FIRST ROLL RESULTS
SELECT CASE DICE
	CASE 7, 11
  	GOTO natural_win:
  CASE 2, 3, 12
  	GOTO rolled_craps:
  CASE ELSE
   	GOTO set_point:
END SELECT

We return from our first dice roll to a SELECT CASE that sorts us into our three possible results. This was originally a series of IF... THEN statements.

Unlike the die rolling itself, we couldn't create a single subroutine for the dice sorting, because we need different results with our second and subsequent rolls.

natural_win:
PRINT DICE; "- A NATURAL WIN!"
PRINT "YOU WIN $"; BET
GOTO adjust_balance

rolled_craps:
IF X = 2 THEN 
	? DICE; "- SNAKE EYES."
ELSE 
  ? DICE; "- CRAPS."
END IF
BET = 0 - BET

adjust_balance:
BALANCE = BALANCE + BET
GOTO roll_or_quit

set_point:
? DICE; " IS THE POINT. PRESS A KEY TO ROLL AGAIN."
TPOINT = DICE : REM ** POINT is a reserved keypoint so we go with TPOINT for The Point.

Our possible results. Heavily refactored. If we roll a win or loss condition, we alter our BALANCE to reflect this. If not, we assign our DICE as the TPOINT variable to set our new target.

The rules for the game now change; we want to match that roll to win, and if we roll a 7 we lose.

roll_again:
SLEEP : KEYCLEAR
GOSUB roll_dice

In the original listing the game automatically rolls until a 7 or the Point is rolled. I've added the SLEEP statement to pause the game after each roll, to give the player a more immersive experience. The KEYCLEAR statement erases the key buffer so our key presses don't become part of our next INPUT.

IF DICE = 7 THEN
	? "7 - CRAPS. YOU LOSE."
  BET = 0 - BET
  GOTO adjust_balance
END IF
IF DICE = TPOINT THEN
	? DICE; " - PASS! YOU WIN."
  ? "AT 2:1 ODSS THIS PAYS OUT $"; 2 * BET
  BET = BET * 2
  GOTO adjust_balance
END IF
? DICE; " - MISS. NO POINT."
GOTO roll_again

We filter for our three results: Win, lose, or roll again.

roll_or_quit:
?:?
IF BALANCE < 0 THEN 
	PRINT "YOU ARE CURRENTLY UNDER $"; -BALANCE
ELSEIF BALANCE > 0 THEN	
	PRINT "YOU ARE AHEAD $"; BALANCE
ELSEIF BALANCE = 0 THEN	
	PRINT "YOU ARE DEAD EVEN"
END IF
INPUT "ROLL AGAIN? (YES OR NO)" A$
?
A$=LEFT$(A$,1)
IF A$ = "y" or A$ = "Y" THEN GOTO place_bet
IF BALANCE < 0 THEN 
	?"UNFORTUNATELY YOU ARE IN THE HOLE. BETTER LUCK NEXT TIME."
ELSEIF BALANCE > 0 THEN 
	?"CONGRATULATIONS ON YOUR WINNINGS!"
ELSEIF BALANCE = 0 THEN 
	?"YOU CAME OUT EVEN, NOT BAD FOR AN AMATEUR."
END IF
END

After we've won or loss, we have the choice to keep playing, and get some feedback based on our cumulative loss or profit.

That's it. Craps as a BASIC program.

#David Ahl