C/C++ 编程代写
当前位置:以往案例 > >CS案例game案例Introduction to Computer and Programming
2017-08-05



• Include simple comments in the code


• If applicable, split the code over several functions


• Extensively test your code and impove it


• Write a single README file per project


• Zip all the files and upload the archive on Canvas



1 Project setup
At a cross road cars are either moving if the light is green or waiting if it is red. A road is composed of two lanes, for cars going in opposite directions while the light is located on the right hand side of the cars, before the cross. Moving cars are all going at a same constant speed of one unit every second.

A snapshot of the scenario at a time t is shown on Figure 1. On the picture three cars are going from top to bottom but are waiting as the traffic light is red. In the meantime five cars going from right to left are crossing, while two other cars are coming from the opposite direction. The two last cars can be seen coming from the bottom but have not yet arrived at the cross road.

Requirement for the above scenario:

• The user inputs n, the total number of cars, w the width of the road, and p the proba- bility that a cars does not stop at the red light;

• The number of cars on each line is randomly decided with respect to n;

• The color cycle of a traffic light is green for g s, orange for o s, and red for r s; The user sets the value of the variables g , o, and r ;

• Lights in the diagonal of the cross road have a same color;

• Each time a light changes from green to orange the cars before the light stop and the ones which have already passed it finish crossing; e.g. on Fig. 1 if the green light be-

comes orange then the two cars in the middle of the cross should finish crossing the road;

image.png

• Cars should appear at the beginning of the road with respect to where they come from; e.g. a car coming from the right should start from the right most edge of the figure;

• Every second all the non-waiting cars move by one unit;


The goal is to animate the displacement of the cars with respect to the traffic lights. The choice of the user will impact the risk for a car crash to happen. The animation stops as soon a crash occurs and the player loses. If all the cars have gone through without any problem the player wins.



2 Tasks
Ex. 1 — Cross road traffic game

1. Write a simple function that initialises all the parameters, i.e. prompts the user for the number of cars, the time for each light color, randomly generates how many cars go in each direction, etc..

2. Write a function that draws the cross road.

3. Write a clear and precise algorithm describing the motion

在线提交订单