OS X games from the ground up: challenge project 1

You can see an index of all the posts in this series: go to index.

Challenge projects are a bit more involved than the challenges you’ve seen previously, as they involve creating an entire game using the skills you’ve acquired so far. In this first challenge project you will convert the BASIC game, Acey Ducey to a Command Line Tool written in C. If you want the original source code, here it is: aceyducy.bas. It’s all explained in this post however.

Here’s the original BASIC listing in full:

Let’s break this down, bit by bit, starting with lines 10 to 80.

This section shouldn’t present any real challenge to you, it simply displays instructions to the user. You can keep your text left-aligned, so don’t worry about the TAB instructions in lines 10 and 20. Remember that a PRINT instruction on its own, e.g. line 21, simply moves the print position to the next line. Here’s how that looks:

Acey Ducey instructions

Now for lines 1oo and 110.

These lines initialise two variables. N holds the amount of the player’s bet, while Q holds the amount of money the player has available. Remember that c doesn’t have the single letter variable name restriction that BASIC does, so you can give your variables more self-explanatory names.

This short section of code simply displays the amount of money the player has remaining before moving on to the code at 260 (which is the code for displaying the cards). It looks like this:

Acey Ducey dollars remaining

This section picks two cards at random and displays them to the player. Lines 270 and 300 are used to pick the cards and store them in the variables A and B. Notice that the range for the random numbers is 2 to 14, with 2 to 10 being the number cards, 11 for a Jack, 12 for a Queen, 13 for a King and 14 for an Ace (Aces are high in this game).

The code in lines 280 to 290 and 310 to 320 ensures the random number is constrained within the range 2 to 14. You should be able to write your random number generation in such a way that this additional code is unnecessary.

Line 330 checks to see if the first card has the same value or a higher value than the second card. If it does, control is returned to line 270 so two new cards can be chosen.

Lines 350 to 390 go to an appropriate PRINT command based on the value of the card. Cards with a value below 11 are printed as the numbers 2 to 10 in line 400. Lines 420, 440, 460 and 480 display the word “JACK”, “QUEEN”, “KING” or “ACE” respectively. Once the card value has been displayed, control is passed to line 500.

Note that lines 500 to 630 are almost identical to lines 350 to 480 except they use the card in variable B rather than the card in variable A. The limitations of this early dialect of BASIC often forced programmers into using this sort of brute force approach to a problem. You should be able to think of a more elegant and efficient way of doing this. Hint: could a function help here? Here’s what this code displays:

Acey Ducey cards

Line 660 displays a prompt and then gets the amount of the player’s bet into the variable M. Hint: You can do this in a couple of ways. You can use the input technique you have already seen to get the player’s bet, but you will have to use a different conversion specifier. Alternatively, is there a function you used when interpreting command line arguments that might be useful here?

In line 670 the amount of the player’s bet is checked to see if it is anything other than zero. If it is zero, then a suitably derisory response is displayed on line 675. Control is then returned to the code that displays the cards on line 260.

Acey Ducey Chicken

If the player has entered something other than zero, control is passed to line 680, which checks that the player hasn’t tried to bet more money than they have available.

If the player has bet too much they are warned in line 690 and reminded of the amount remaining in line 700. Control is then returned to line 650, where the player is asked to enter a new bet.

Acey Ducey bet too much

Line 730 chooses the third card. This line and the lines that follow it are identical to the routines for the other two cards, except that the card is stored in the variable C. Again, you should be looking for ways you can achieve this more efficiently.

Line 910 checks if the third card is higher than the first card. If it is, it moves on to the next check in 930. If it is isn’t, control is passed to the lose routine on 970.

Line 930 checks if the third card is equal to or higher than the second card. If it is then control is passed to the lose routine on 970.

If the third card is between the first and second cards then line 950 displays a win message. Control is then passed back to line 210, which adds the player’s winnings to their total.

Line 970 is the start of the lose routine. It displays a lose message.  Line 980 then checks that the amount to be deducted is less than the player’s remaining money. If it is then control passes back to line 240, which deducts the loss. If it isn’t then a broke message is shown on line 1010.

Line 1020 prompts the player to indicate if they want to play again. The answer is checked in line 1030, and if the player answered “YES”, control is passed back to line 110, which resets the player’s cash to the starting amount.

If the player answers anything other than “YES”, a farewell message is displayed on line 1040 and the programme ends.

Reading a word rather than a single character from the player’s input is a bit trickier. By all means try and find a solution (and I’ll show you one in the next post), but it’s fine if you just check for ‘y’ instead of ‘YES’.

Acey Ducey end game

The last few lines you haven’t yet seen are these. Line 210 adds the amount of the player’s bet to their total and then control returns to line 120, which prints the remaining amount of money. Correspondingly, line 240 subtracts the amount of the player’s bet from their total before passing control back to line 120.

Here’s a flowchart for Acey Ducey:

Acey Ducey flowchart

While you are working on your solution, you might find it useful to refer to the documentation for the commands and functions you are using.  While you are in Xcode, make sure the Utilities pane (that’s the one on the right) is visible and select the second icon at the top – the one that has two wavy lines. This is the Quick Help inspector. If you then click on a command or function in the main window, you’ll see help for it here:

Xcode quick help

If you need further help, click the OS X Man Pages link. This will open the relevant manual page in a new window.

Good luck with the challenge. An example solution will be explained in the next post.


This entry was posted in Games, Programming Tutorials and tagged , , , , , . Bookmark the permalink.

Leave a reply