GOSUB and Repetitious Messages

GOSUB to me still has something of the mystique that cursive handwriting has for young schoolkids.  It is emblematic of maturation and advancing skill.  Using GOSUB is a message to the world saying, “No longer does this wee programmer engage in spaghetti code.  I function on a higher plane!”  The truth is a little less exciting.  GOSUBs do take you away from the main logic of your program and it can take some effort to keep track of a program that branches out a lot with numerous subroutines.  Furthermore, if you choose to do so, you can go into just as deep a rabbit hole with GOSUB as you ever could with GOTO; subroutines never “have” to end (well, within GW-BASIC’s memory limits at least) and can themselves contain ever more subroutines and GOTOs.  However, you can also use GOSUB in such a way that each subroutine you write terminates and returns to the main program (or just ends the program).  Theoretically, you could do the exact same thing with GOTO, but it is easier to get in the habit of using RETURN statements to automatically return to execute the next line after a GOSUB statement since this does not require keeping track of line numbers.

One of my current projects that I’m working on is a multiple choice trivia game.  As a program, it does not really need to do a lot of branching out — I could write it without using any GOTO or GOSUB statements whatsoever.  It can proceed totally linearly, from one question to the next.  As a matter of fact, this is exactly what I have done in my program.  Each question displays a success or failure message; some are unique and some are repeated.  Going forward, though, I think using GOSUB to display repetitious success and failure messages would be the wiser choice if I ever decide to do a similar program in the future.  Let me give you an example of a world capitals game (not actually what I’m working on, but I might do one of those as well):

10 PRINT “What is the capital of Australia?”

15 PRINT “1. Duluth  2. Canberra  3. Sydney  4. Melbourne”

20 INPUT “Enter your choice:”; Q1%

25 IF Q1% = 2 THEN GOSUB 1000 ELSE GOSUB 2000

30 END

1000 PRINT “You are correct.”

1005 RETURN

2000 PRINT “That is incorrect.”

2005 RETURN

The practical value of using GOSUB here is you only need to enter the success and failure messages once into your program rather than entering them in after each question.  This can lead to significant space savings as you add more and more questions.  One of the downsides to using GW-BASIC is that you do need to keep your programs small to stay within the memory limit.  Using subroutines and functions intelligently can really help you keep your programs neat and trim.

Posted in Uncategorized | 1 Comment

PRESIDEN.BAS Development Notes

My latest GW-BASIC program is more a proof of concept than a useful program.  My idea of GW-BASIC being a platform rather than “just” a programming language lead me reflect that a GW-BASIC program could be used simply to relay information much like a web page.  Today’s Web is not very friendly to older browsers so very arguably a BASIC program could actually offer a more convenient way to access information than a web page if you’re using a really old computer.  OK, so GW-BASIC won’t really be competing with the World Wide Web any time soon, but I thought I’d take a break from my latest game project (which is unfortunately taking me far longer to complete than I had anticipated) and do something informational and very easy.

The fruit of my work is PRESIDEN.BAS, a simple list of US presidents.  What I found most interesting about this little side project was how it forced me to think quite a bit about how my data would be presented.  In most programs I’ve written, I haven’t had any need to display a whole lot of text before prompting the user to do something.  PRESIDEN.BAS, though, only exists in order to display text.  Making sure that the text was presented in a legible way was pretty much my only job.

PRESIDEN.BAS in action!

These are the American presidents, presented in GW-BASIC!

I had about as much fun as I possibly could creating a program that is really just a series of print statements.  OK, I did use dummy INPUT statements to allow the user to move on to the next page and CLS to clear the screen after each page.  Apart from that, it’s just a bunch of a print statements.   Keen observers of the above screenshot will note I was actually running the program in PC-BASIC 3.23.  In fact, I actually wrote the program in the PC-BASIC environment, a first for me.  The experience was as smooth as can be, and I’ve subsequently run the program in vanilla GW-BASIC successfully as well.  There’s another possible first: I suspect this MIGHT be the first GW-BASIC program that makes mention of Barack Obama, though I wouldn’t be willing to bet on it.

If you’re going to be displaying a lot of text in GW-BASIC, it’s important to keep track of how many lines you are displaying per page so none get “lost.”  You might think you should have 25 lines to play with in text mode, but GW-BASIC uses one line to display the function key shortcuts, reducing you effectively to 24 lines.  Furthermore, you’ll probably need a line to prompt the user to continue to the next page of text or do something else, reducing you to 23 lines of other text.  If a program displays all the text at once and then terminates, one additional line will be taken by the “OK” message GW-BASIC displays after a program has finished running so you’ll effectively have only 22 lines at your disposal!

Image of GW-BASIC program that can only display 22 lines of text.

It’s not only actors who need to know their lines — GW-BASIC programers must as well!

Posted in Uncategorized | Leave a comment

A Note About RND and Randomize

I’ve recently talked about the magic of RND, GW-Basic’s random number generator.   Pig 2014 relies on RND.  However, if you’re actually going to be using RND in the wild, it’s important to realize it may not be as random as it appears in its basic form.  If you just PRINT RND a few times in GW-BASIC, the output certainly would LOOK pretty random:

gwrnd2

However, when you start incorporating RND statements into your programs you may be surprised to notice that the same old numbers are being generated over and over again.  Don’t believe me?  Check this little program out:

10 FOR X = 1 TO 3

20 PRINT RND

30 NEXT

gwnotrnd

Instead of getting three random numbers each time the program is run, you’re getting the same not so random numbers every time.  In practice, this means that if you use “vanilla” RND statements to make decisions in your program it will make the same decisions every time because the numbers will be the same every time.  RND is only useful as a random number generator if you use it in tandem with its partner in crime, RANDOMIZE, which “seeds” the generator with new numbers.  There are a few ways you can use RANDOMIZE: you can directly choose a seed with the statement RANDOMIZE seednumber where seednumber is a number between -32768 and 32768 (eg RANDOMIZE 140), RANDOMIZE by itself (which prompts for a seed number), or, perhaps the most common choice, RANDOMIZE TIMER (which generates random seeds based on the time).   If your program requires random numbers, your easiest option is to include the statement RANDOMIZE TIMER somewhere in your code before you use any RND statements.  The combination of RANDOMIZE and RND makes a big, big difference in our little program:

gwyesrnd

In short, if you want random, then RANDOMIZE!

Posted in Uncategorized | Leave a comment

Be Careful With That Save Command!

I’ve always appreciated how the function keys in the GW-BASIC environment give you access to common commands at a keystroke.  There are four that I use all the time: F1 for LIST (displays the source code of the currently loaded program), F2 for RUN (runs the currently loaded program), F3 for LOAD (loads an existing program into memory), and F4 for SAVE (saves the current program under the specified file name).  In all the time I’ve been BASICing, I can’t remember EVER making a mistake with the function keys…until yesterday, that is.

I didn’t just screw up — I figuratively PULVERIZED my first major comeback program into little bits of nothing.  Just how did I perpetrate this blunder?  I hit F4 instead of F3 and didn’t notice what I did, ultimately causing me to save a whole heap of nothing as PIG.BAS and thus in an instant sending my completed PIG 2014 game that I recently wrote about into the dustbin of history.  Some would call this an improvement, all things considered, but to me it was among the most epic of epic fails.  Now, there were not actually any real world consequences to my blunder…I keep backups, man.  Plus, the game is online now — it’ll probably still be floating around in the cloud somewhere long after I’m dead.  What were you expecting, some sort of sob story?  Nah, that’s not what this post is about.  It’s just a warning: it’s a little too easy to save instead of load something if you use the function key shortcuts regularly.  Watch out, and backup liberally.  At this point in my computing career, I’ve pretty much resigned to even backing up stuff I’m 99% sure I’m never going to want to access again…just in case.

While I have your attention, I think this is a good time to discuss the other function keys.  They can actually be reassigned to whatever you want, but let’s start by talking about the default settings.  I already discussed LIST, RUN, LOAD, and SAVE.  F5 is CONT (short for continue) and is useful for debugging purposes.  It causes a program to resume at the line at which it halted due to a break or where a STOP statement is encountered.  If you’re trying to pinpoint where an error is occurring in your code, insert STOP statements at strategic points in your code and then use CONT to execute your program in bits and pieces.  Of course, the most useful application of CONT is probably to resume a “Hello World” infinite loop — it lets you act like you’ve fixed the “problem” to your amazed computer-illiterate audience for a moment before you hit F5 and submerge them once again into utter hopelessness.  OK, maybe only I use the command that way…  F6 is a shortcut for ,”LPT1 and would be used to direct output to a printer connected to the LPT1 port.  I must confess that I’ve never actually used this — I’ve never had occasion to need to print from GW-BASIC thus far.  One day, one day…  F7 is TRON (trace on), another useful debugging command.  When you invoke TRON and then RUN a program, your program will be executed, but you’ll see what lines of the program the computer reads to invoke each line of output (they’re enclosed in brackets before the output).  TRON allows you to follow the logic of the program essentially.  I doubt what I just said is comprehensible so an image might be welcome at this point:

gwbtron

TRON’s partner in crime is F8, TROFF, which turns trace off and enables you to run your programs as normal once again.  F9 is a shortcut for KEY.  You use KEY statements to create shortcuts like we’ve been discussing.  F1 does not need to be a shortcut for LIST, F2 does not need to be a shortcut for RUN, etc — you can redefine the function keys to stand for any string of up to 15 characters you choose.  Suppose you hardly ever use the KEY statement, for instance, and so you don’t need F9 to be a shortcut for KEY.  You could enter the statement KEY 9, “PRINT ” and have a handy PRINT shortcut at your fingertips instead.  PRINT can now be invoked just by hitting F9.  The list of GW-BASIC commands at the bottom of the screen will even update automatically in response to your KEY statements!  Note that you don’t change the development environment permanently just by using a KEY statement — GW-BASIC will open up with the same old function key assignments as always.  However, you can customize the function keys from program to program or from session to session as needed.  Finally, F10 is a shortcut for SCREEN 0, 0, 0, the default display mode (text only).  It’s a useful means to get “back to normal” if you’ve been working with graphics.

I can’t believe I originally planned this to be a SHORT post!

Posted in Uncategorized | Leave a comment

Pig 2014 Development Notes

Picking a comeback programming project is tough.  I knew I wanted to do something new, but I also wanted it to be relatively simple since I am just getting back into the flow of things.  I thought a dice game would meet both criteria; it’s something I’d never done before, but I didn’t think it would be difficult to program because most dice games are fairly simple.  I was familiar with the dice game Pig because I’d played the Elwynor Technologies’ door game version on Quicksilver BBS [telnet://quicksilvermbbs.dyndns.org] many times before so it seemed like a logical game to start with.  I modeled my version of the game after Elwynor’s, but my research revealed that there are actually many versions of the basic dice game.  The goal is always to roll more points than your opponent while avoiding the “bad” roll which wipes your unbanked points away.  In Elwynor’s and my version of the game, each player rolls two dice and tries to avoid rolls that sum to 7.  The winner is the first to reach 100 points.   The major strategic decision the player must make is when to bank his or her points.  The disadvantage of banking your points is your turn ends and your opponent gets to roll.  The advantage of banking is you get to save your points; without banking, all points accumulated in a round are a bad roll away from being wiped away.

To implement any dice game, you need to simulate the more or less random roll of dice.  GW-Basic’s RND function generates a random number between 0 and 1.  That might not seem so useful from the start since we’re only interested in the integers 1, 2, 3, 4, 5, and 6.  However, we can do some math to the number RND generates to turn it into one of those integers.  Here’s some relevant code from Pig 2014:

55 DICEA = INT (RND*6 + 1)

What are we doing here?  We’re letting RND generate a random number between 0 and 1, multiplying it times 6 (thus, we get an intermediate result that lies between 0 and 6),  adding 1 to the intermediate result to ensure that we don’t roll any 0s when RND generates a number very close to 0, and using the INT function to convert this result into an integer.  Offhand, I’m not sure if RND ever generates .000000 or 1.000000; if it does, those would be exceedingly unlikely occurrences, but if RND generates a 1.000000 it would make DICEA equal 7 in that case which would not be good at all.  Incidentally, I didn’t protect against this occurrence in my program — I think I assumed that RND never generates one of the end points, but now I’ve realized I might not know this for sure.

GW-Basic's RND Function

RND generates a random number between 0 and 1.

An important aspect of any program is keeping track of your variables.  In my case, I had a number of numbers that I needed to be stored.  First, I needed a pair of dice; I called them DICEA and DICEB (shouldn’t that be DIEA and DIEB?).  I also needed to record the sum of each roll for each player; I called these variables PROLL and CROLL.  I also needed the round points for each player to be recorded.  The round total increases when a player rolls something other than a 7, and gets zeroed when a 7 is rolled or a player banks his points.  I called the player’s round total PROLLCALL and the computer’s round total CROLLCALL.  Line 63 is an important line of code: PROLLCALL = PROLLCALL + PROLL.  This means PROLLCALL is a running total — GW-BASIC does not forget what was rolled previously (PROLLCALL) after a new roll (PROLL).  Remember, though, that PROLLCALL is zeroed whenever a 7 is rolled or a player banks his points.  An early bug in my program failed to properly zero PROLLCALL after a 7 was rolled, meaning that the only consequence for rolling a 7 was losing your turn and banking was a pointless activity.  I also needed to store the number of points each player had banked, necessitating the variables PBANK and CBANK.  Finally, I also needed to track the sum of the current round total and the number of previously banked points because when a player reaches 100 he immediately wins the game — the player does not have to bank his final round points to win.  I used the variables PTOTAL and CTOTAL to track this.

The major “decision” that must be made in a game of Pig is whether to keep rolling the dice or to bank one’s points.  The player can simply be asked what he wants to do:

80 INPUT “Do you wish to roll again (Enter Y or N)”;QUEST$

If the player doesn’t say Y or N or END, he still has made a choice: he loses his turn AND his round total.  That’s probably unnecessarily brutal, come to think of it.

The computer also has the same decision to make.  Another implementation of the RND function takes care of this.  I had a tough time deciding whether to give the computer a true strategy; for instance, the computer could choose to keep rolling if PROLLCALL is small and choose to bank if PROLLCALL is large.  However, I found that my own strategy to playing PIG shifted a lot depending on the circumstances of a particular game and my own mood so I felt random decisionmaking was good enough, and, indeed, might make for a more interesting opponent than a computer playing a predictable strategy would provide.  I might revisit this topic in a future version of the game.  As it is, the computer is ever so slightly more likely to choose to continue rolling as opposed to banking its points:

125 TOSSUP = RND

130 IF TOSSUP >= .5 THEN GOTO 100 ‘ Continues rolling (ed.)

135 IF TOSSUP < .5 THEN CBANK = CBANK + CROLLCALL

One thing you won’t find in my code are comments.  I’ve taken a programming class before and I made a point to comment practically every line of code obsessively, even though my teacher had said we wouldn’t be graded on our use of comments.  Leave me to my own devices, though, and proper programming practices seem to just go out the window.  I always feel more creative when I just “wing it,” but I was feeling especially naughty when writing PIG 2014 because I knew I wanted to include lots of GOTO statements in my comeback program.  I’ll probably comment future projects a bit more.  Well, I hope so…

Has this post whetted your interest in my little game?  If so, you can download it from the official Pig 2014 project page!

Posted in Uncategorized | Leave a comment

GW-Basic As a Platform

There was a time when I wondered if anyone would be able to run any GW-BASIC program I might happen to write.  After all, GW-BASIC is a 1980s programming language with an integrated development environment that works only in DOS.  The decline of DOS meant there were fewer potential GW-BASIC users and programmers out there.  The sands of time are merciless; might the language just be open to those who remembered using it back in the day?  After the old-timers programmed their last lines of code, was it all over?  I no longer have the same concerns.  You see, a funny thing happened on the way to obscurity…DOS has become more accessible than ever thanks to DOSBox, DOS virtual machines, and FreeDOS.  Your “primary” operating system no longer limits you as it once did.  Well, what if you don’t want to deal with DOS at all?  Younger GW-BASIC neophytes can’t be blamed for not knowing their way around a DOS command line and might not be inclined to learn a new OS.  No matter: we now have access to a wonderful project called PC-BASIC which is a GW-BASIC interpreter that works on modern versions of Windows, OSX, and Linux.  In essentially no time at all, anyone on any popular operating system can execute a GW-BASIC program or start writing their own, all in an environment almost exactly like that of the original.

The power of 80s programming at your fingertips!

PC-BASIC 3.23 is a multi-platform GW-BASIC interpreter.

Don’t get me wrong — there’s still a lot to be said for running GW-BASIC the old-fashioned way.  It will probably always feel the most authentic for anyone who used to program in DOS and, as a bit of a classic computer collector, I love being able to put my old machines to work again.  There may even be tangible reasons why you might not want to use one of the more “modern” options.  For instance, Ctrl + Break does not halt the execution of a GW-BASIC program in DOSBox as it should.  It simply doesn’t work.  You can use Ctrl + Scroll Lock instead, though (see this interesting discussion for more details on this issue).  I have yet to use PC-BASIC enough to discover if it has any similar bugs or issues.

On the whole, however, I am absolutely thrilled that GW-BASIC is so accessible to modern computer users.  GW-BASIC itself can be thought of as a platform for developing software that can run without modification on millions of computers from the 1980s to the present.  There’s power in that!  Of course, GW-Basic is not headed for worldwide domination any time soon — its limitations mean that for many software projects it is not a good fit and the fact is only a tiny fraction of people who could be using the “platform” will actually discover (or rediscover) and begin to use it.  Nonetheless, I think it’s the best time in more than 20 years to be a GW-BASIC programmer and user.  We live in interesting and exciting times!

Posted in Uncategorized | 1 Comment

To Goto or Not to Goto

gwshutup

I’ve never met anyone who knows anything about programming who thinks BASIC GOTO statements are a good thing.  Nonetheless, what is the single most recognizable snippet of code in the world?  For me, this is what tends to come to mind:

10 PRINT “Hello World”

20 GOTO 10

There’s something extremely accessible and intuitive about GOTO statements.  That doesn’t mean that unreadable spaghetti code isn’t bad.  That doesn’t mean that eager GOTOing might not ruin any chance you have of ever becoming a disciplined programmer.  It just means that GOTO statements are easy to understand and easy to implement, especially for a coder who is just starting out.

At this point, it’s probably best that I confess that this isn’t the first time I’ve tried to take up GW-BASIC again.  In fact, it’s something I tend to do every couple of years or so.  I know this habit of mine doesn’t bode well for my present effort, but I like to think the fact that I’ve gone ahead and started this blog suggests I have a deeper commitment to my learning this time around.  At any rate, one thing all my returns to GW-BASIC have in common is that the first comeback program I write pretty much always involves the prodigious use of GOTO statements.  Alas, I have to admit that I remained true to form when I created my latest effort, PIG 2014, which I’ll be discussing shortly on this blog.  Bear in mind, I’m not an utter noob here — I’ve dabbled in the dark arts of subroutines and functions before — but somehow I always seem to go back to GOTO.  It’s accessible, it’s intuitive, it’s…well, easy.  I’m sincerely going to try to be a better programmer in the future, but I suspect there will never come a time when I swear off the use of GOTO entirely.  GOTO 4 LIFE!

Posted in Uncategorized | Leave a comment

Back to (GW)-Basic

GW-Basic is a programming language that is close to my heart. To think of it brings back a surge of memories: beloved old computers, my father teaching the basics of programming to me, my first clumsy attempts at writing programs. Although I’ve dabbled in various other programming languages since, I’ve always regretted that I never learned the ins and outs of GW-Basic since my first early experiences with it were so rewarding. It’s a regret I’ve carried with me for two decades now. At the tail end of 2014, I’ve decided to finally DO something about it.

Of course, there’s really only one way I can amend for lost time: it’s to finally learn all the nuts and bolts of the language that I should have learned long ago. I intend for this blog to chronicle all my awkward efforts. Hopefully, I’m not the only 21st century soul still trying to learn this classic language — who knows, perhaps the blog will even encourage some more people to take on GW-Basic or some other challenge they’ve put off for too long.

Posted in Uncategorized | Leave a comment