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.

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!