COMP1721 - Higher Computing 1B

Higher Computing 1B - Week 3 Laboratory Exercises

Welcome to your second Computing 1B laboratory.

This lab involves:

Create a separate directory (lab03 is a good name) using mkdir for this week's lab exercises.

Exercise 1: The game of Forty-Two

Your task is to write a C program which allows a user to play the game of Forty-two against the computer. Fortunately Forty-two is a very simple game.

In the game of Forty-two2, you are given numbers until you choose to stop. Each number you are given is chosen at random from the integers between 1 and 21 inclusive. Your score is the sum of these numbers. The player with the highest score wins. However, if your score is larger than 42 you are said to bust which means you lose immediately.

The user of your program should play first. If the user busts, they lose and the game stops there. Otherwise the computer plays next. When the computer finishes playing your program should indicate who won.

The computer player should follow a simple strategy. It should stop taking numbers when and only when it has a score of at least 32.

Here are some sample games:


% a.out
Your score is: 0
Do you want to take another number [y or n]? y
You get the number: 9
Your score is: 9
Do you want to take another number [y or n]? y
You get the number: 16
Your score is: 25
Do you want to take another number [y or n]? y
You get the number: 16
Your score is: 41
Do you want to take another number [y or n]? n
Now the computer plays
The computer gets the number: 1
The computer gets the number: 17
The computer gets the number: 15
The computer's score is: 33
You win.

% a.out Your score is: 0 Do you want to take another number [y or n]? y You get the number: 13 Your score is: 13 Do you want to take another number [y or n]? y You get the number: 18 Your score is: 31 Do you want to take another number [y or n]? y You get the number: 17 Your score is: 48 You bust.
% a.out Your score is: 0 Do you want to take another number [y or n]? y You get the number: 1 Your score is: 1 Do you want to take another number [y or n]? y You get the number: 21 Your score is: 22 Do you want to take another number [y or n]? y You get the number: 13 Your score is: 35 Do you want to take another number [y or n]? n Now the computer plays The computer gets the number: 14 The computer gets the number: 21 The computer's score is: 35 Its a draw.
% a.out Your score is: 0 Do you want to take another number [y or n]? y You get the number: 20 Your score is: 20 Do you want to take another number [y or n]? n Now the computer plays The computer gets the number: 3 The computer gets the number: 1 The computer gets the number: 13 The computer gets the number: 13 The computer gets the number: 15 The computer's score is: 45 The computer busts.


Design

The first stage is the construction of your program should be to prepare an the overall design. This lays out the structure of your program. This should be done in English (well any natural language - just not in C). For a program this size, a sketch on a single piece of paper is a suitable approach to design.

The flow of control is the most important part of this particular program. Make sure your design includes the loops and choices needed in your program.

You should decide what variables your prrogram will need and choose names which clearly indicate their purpose.

Starting Point

You should put your program in a file named forty_two.c. Here is a starting point to paste into forty_two.c.



/*
 * Put your header comment here
 */
#include <stdio.h>
#include <stdlib.h>

int
main(void) {
	/*
	 *  Place your program here
	 */
    return 0;
}


Implementation

I suggest you implement your program in stages. This allows you test each part of the program as it is implemented. This can much simplify debugging. There is a very important maxim in software construction:

The earlier a bug is discovered, the easier it is to fix.

This applies equals as much to to your 20-40 line C program as it does to complex million-line software systems.

Here are the stages you might use when developing your progam.

First, implement the statements to produce a random integer between, 1 and 21 inclusive. You will need to use the function rand.

Each time rand() is called it returns an int between 0 and RAND_MAX (2147483647 on CSE systems). A different int is (almost always) returned by each call to rand(). The int returned is chosen in an (almost) random fashion.

To get different behaviour with every invocation of your program you can initialize the random number generator like this:

	srand(time(NULL));
It actually seeds the random number generator with the time in seconds since Jan 1 1970. You'll also need to add this include directive to the top of your program:
#include 


% a.out
You get the number: 18
% a.out
You get the number: 19
% a.out
You get the number: 13


Next, implement the statements to ask the user to choose another number. You will need to use the method getchar() which you've seen in lectures.


% a.out
Do you want to take another number [y or n]? y
You get the number: 7
Do you want to take another number [y or n]? y
You get the number: 8
Do you want to take another number [y or n]? y
You get the number: 15
Do you want to take another number [y or n]? n


Next, implement the statements to calculate the user's score. You must also handle the user busting.


% a.out
Your score is: 0
Do you want to take another number [y or n]? y
You get the number: 13
Your score is: 13
Do you want to take another number [y or n]? y
You get the number: 16
Your score is: 29
Do you want to take another number [y or n]? n

% a.out Your score is: 0 Do you want to take another number [y or n]? y You get the number: 9 Your score is: 9 Do you want to take another number [y or n]? y You get the number: 12 Your score is: 21 Do you want to take another number [y or n]? y You get the number: 11 Your score is: 32 Do you want to take another number [y or n]? y You get the number: 15 Your score is: 47 You bust.


Next implement the statements for the computer player.


% a.out
Your score is: 0
Do you want to take another number [y or n]? y
You get the number: 9
Your score is: 9
Do you want to take another number [y or n]? n
Now the computer plays
The computer gets the number: 13
The computer gets the number: 11
The computer gets the number: 2
The computer gets the number: 13
The computer's score is: 39


Finally implement the statements indicating the winner.


% a.out
Your score is: 0
Do you want to take another number [y or n]? y
You get the number: 3
Your score is: 3
Do you want to take another number [y or n]? n
Now the computer plays
The computer gets the number: 2
The computer gets the number: 5
The computer gets the number: 9
The computer gets the number: 10
The computer gets the number: 1
The computer gets the number: 12
The computer's score is: 39
The computer wins.


Finished?

Make sure you test your program thoroughly. When you are sure its working show it to your tutor to receive the lab mark.

After your tutor has checked that you answers are correct and given you the lab mark, you must also submit your answers using give. Here is the command to use:

% /home/cs1721/bin/classrun -give lab03 forty_two.c



Andrew Taylor (andrewt@cse.unsw.edu.au)
Higher Computing 1B, Computer Science & Engineering, UNSW