COMP 104 : Programming
Lab
9: Falling Money
In lab 9, you will write a
program where the player controls a basket for collecting falling money from the sky. But, rubbish
will fall from the sky as well. The goal of this game is to collect the
money and avoid the rubbish.
Requirements:
- Except for constants,
your program should not use global variables.
- You should use a linked
list to store each line of the sky.
- Your program must
not have any memory leaks or dangling pointers.
- Use the following structure (note that it is a user-defined data
type, not a variable):
struct
Node{
string
data;
Node*
next;
};
typedef
Node* NodePtr;
The sky:
- You will create a sky
of 40 characters in width and 20 lines in height. Outside the sky,
there is a title, two barriers and a line showing the score. Therefore,
there are totally 42 characters x 22 lines in this game.
- ‘U’ will be used to
represent the basket. Put it in the middle bottom of the sky when the
game begins. The player can use arrow keys to move teh basket from left
to right.
- Use ‘$’ to represent
money and ‘.’ (a fullstop) to represent rubbish.
- Put two items on each line. The
items can be either ‘$’ or ‘.’.
Here is the sample layout: sample.txt
Game play:
- The
sky itself is a linked list of strings. Each line is a Node in the
linked list. You can add a line to the top and remove a line
from the bottom to make the money look like it is falling from the sky.
Here is the explanation: explain.ppt
- You will have 50 marks
at the beginning. Collecting each ‘$’ will add 10 marks, and each ‘.’
will cost you 15 marks. If you get 100 marks or more, you win. If you
get 0 marks or less, you lose.
Hints:
Use the following code
to get the input when user presses a key:
if
(kbhit()) input = getch();
// kbhit(): if someone hits the
keyboard, it returns true.
// getch(): gets a char
immediately from the input
(no need to press
enter)
You should include <conio.h> in order to use these two
functions.
- Then you
can check
which key the user pressed (left or right arrow):
const char CPPKEYLEFT = 75;
//left arrow
const char CPPKEYRIGHT = 77; //right arrow
if (input == CPPKEYLEFT) {…}
else if (input == CPPKEYRIGHT) {…}
- You can use the
following code to generate a string with 40 characters:
string
temp(40, 'x');
“temp” is the variable
name, the first parameter is the number of characters, the second
parameter is the character to be filled in.
That is the same as
string
temp = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
- Then you can use
strings like an array:
temp[9]
= 'y';
This will change the
character at index 9 to ‘y’.
- You can use
this
command to control the speed of the game:
Sleep(10);
It will stop the program
for 10 milliseconds.
You should include <Windows.h> in order to use this function.
This is a sample of first 10 steps: first_10_lines.txt
Demo your
working program for your TA. If for some
reason you cannot finish before the end of the lab period, email your
program
to your TA by Sunday 20 November 5PM (please
include your name and lab section as a comment on line 1 of your
program).