This lab involves:
Create a separate directory (lab03 is a good name) using mkdir for this week's lab exercises.
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.
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.
/*
* Put your header comment here
*/
#include <stdio.h>
#include <stdlib.h>
int
main(void) {
/*
* Place your program here
*/
return 0;
}
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.
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