Craig Box's journeys, stories and notes...


BASIC liability insurance

As those of you who know me personally will know that I am, quite vocally, not someone who enjoys programming. My main problem is that I write code so infrequently that whenever I sit down I've forgotten either the syntax of language or the parameters of the API, and find more time is taken with reading the docs or finding suitable examples than actually writing or thinking.

I visited my parents yesterday to help my 16 year old brother study for a test, and he also asked for some help with a programming assignment. Due either to a recent loss of computing equipment, or general difficulty in deciding on a language for teaching programming, they're using QBasic. Last time I used QBasic, I hadn't gone through a university degree and learnt half a dozen programming languages, so I found it all a bit easier this time. The problem was that I had no idea if half of the things I wanted to do had syntax in QB: you can't specify an array using numbers[4] = {1, 3, 6, 8}, so an example on the web (the only time I had to go outside of the help) pointed out that you use READ and a DATA statement. I remember seeing DATA statements in Commodore 64 code printouts in magazines, and not having any idea how the stuff quite how they worked. Surely it's easier to specify your data inline, rather than in a DATA statement? Isn't BASIC supposed to be, well, basic?

Anyway, below the fold is what I wrote. The goal was to teach my brother, and his brief was "write a roulette game". I insisted there would be no GOTOs, and no functions/subroutines, as he hadn't covered them at school. Generally the code had to make sense so he could read it and figure out how it all worked.

We finished it, and then, like the bastard I am, I deleted the bit that tests for odd and even and made him rewrite that. I suspect he'll find this post before actually rewriting it himself. Maybe not enjoying programming runs in the family.

The game

RANDOMIZE TIMER
' You start with $100 in your hand.  (You are not a high roller.)
cash% = 100

' build array for which numbers are red, and which are black

DIM rednumbers(18)
DIM blacknumbers(18)

FOR i = 1 TO 18
  READ rednumbers(i)
NEXT i

FOR i = 1 TO 18
  READ blacknumbers(i)
NEXT i

' red numbers
DATA 1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36
' black numbers
DATA 2,4,5,8,10,11,13,15,17,20,22,24,26,28,29,31,33,35

CLS
PRINT "This is Jared's Almost-Roulette!"
PRINT
PRINT "Rules of the game: The wheel has the numbers 1-36 and 0."
PRINT "You can bet on odd or even numbers, or any number on the wheel."
PRINT
PRINT "To quit, bet -1."
PRINT
bet% = 0

DO UNTIL bored
  DO UNTIL bet% > 0
    INPUT "How much do you want to bet?  $", bet%
    ' check that we aren't spending more money than we have
    IF bet% = -1 THEN
      PRINT "Thanks for playing."
      END
    END IF
    IF bet% > cash% THEN
      PRINT "You only have"; cash%; "."
      bet% = 0
    END IF
  LOOP

  PRINT "You are betting $"; bet%
  cash% = cash% - bet%

  num% = INT(RND * 37)
  PRINT "the number is... "; num%; "!"

  correctchoice = 0
  DO UNTIL correctchoice = 1

    INPUT "Please type in a number between 0 and 36, or one of 'odd even red black': ", choice$
    ' Check your input is valid
    ' numbers (1-36)
    ' odd or even
    ' zero, which needs to be separate, because of qbasic
    IF (UCASE$(choice$) = "ODD") OR (UCASE$(choice$) = "EVEN") OR (UCASE$(choice$) = "RED") OR (UCASE$(choice$) = "BLACK") OR (VAL(choice$) >= 1 AND VAL(choice$) <= 36) OR (choice$ = "0") THEN
      correctchoice = 1
    ELSE
      PRINT "Please type one of 'odd even red black', or a number between 0 and 36."
    END IF
  LOOP

  ' the computer spins the wheel.

' possible things that happen:
' 1. you picked odd or even - check to see if you won and pay out 2* bet
' (remember to exclude 0)
' 2. you picked red or black
' 3. you picked a number, you either win 36:1 or you don't.

IF UCASE$(choice$) = "ODD" THEN
  IF num% MOD 2 = 1 THEN
    PRINT "It was odd: you win $"; 2 * bet%
    cash% = cash% + 2 * bet%
  ELSE
    PRINT "The number is even, so you win nothing."
  END IF
END IF

IF UCASE$(choice$) = "EVEN" THEN
  IF num% MOD 2 = 0 AND num% <> 0 THEN
    PRINT "It was even: you win "; 2 * bet%
    cash% = cash% + 2 * bet%
  ELSE
    PRINT "The number is odd, so you win nothing"
  END IF
END IF

IF UCASE$(choice$) = "RED" THEN
  match = 0
  FOR i = 1 TO 18
    IF num% = rednumbers(i) THEN
      match = 1
    END IF
  NEXT
  ' if match = 1, then our number is in the red array
  IF match = 1 THEN
    PRINT "It was red!: you win "; 2 * bet%
    cash% = cash% + 2 * bet%
  ELSE
    PRINT "The number is not red, so you win nothing"
  END IF
END IF

IF UCASE$(choice$) = "BLACK" THEN
  match = 0
  FOR i = 1 TO 18
    IF num% = blacknumbers(i) THEN
      match = 1
    END IF
  NEXT
  ' if match = 1, then our number is in the black array
  IF match = 1 THEN
    PRINT "It was black!: you win "; 2 * bet%
    cash% = cash% + 2 * bet%
  ELSE
    PRINT "The number is not black, so you win nothing"
  END IF
END IF

IF num% = VAL(choice$) THEN
  PRINT "Your number came up!"
  PRINT
  PRINT "You win "; 36 * bet%
  cash% = cash% + 36 * bet%
END IF

  PRINT "You now have $"; cash%; "in your hot little hand."
  PRINT
  IF cash% = 0 THEN
    PRINT "You should stop playing now, else you'll end up betting"
    PRINT "your gold watch, then borrowing money you can't pay back"
    PRINT "then getting kneecapped in the alleyway outside."
    END
  END IF
  bet% = 0
LOOP

Tags:

2 Responses to “BASIC liability insurance”

  1. Perry Lorier says:

    Your program lets me bet negative amounts of money (-20). This means when I lose, I gain money.

  2. Craig says:

    If only casinos worked that way 🙂

Leave a Reply