For each of the following programs, provide the following:
(a) a control flow graph
(b) the set of unique paths through the program
(c) for each feasible path, exact values of program variables that would cause the path to be executed.
Programs are written in a Pascal-like pseudo-code.
The following routine handles the side-to-side movement of the user-controlled base in a Space-Invaders-like game. When the user presses the "<" key, the base moves left, ">" moves right. The base has a width, and the left side can't be at a position less than zero, and the right side can't be at a position greater than GAME_WIDTH.
procedure move(input keypress: char, input/output base_left: integer, input/output base_right: integer)
begin
if (keypress = "<") then
if (base_left > 1) then
base_left := base_left - 1;
base_right := base_right - 1;
end if;
end if;
if (keypress = ">") then
if (base_right < GAME_WIDTH) then
base_right := base_right + 1;
base_left := base_left + 1;
end if;
end if;
end;
In the same Space-Invaders-like game, the following routine is used to check and see whether any of the active shots coming down from the aliens has hit the user-controlled base. The height of the base is a constant, BASE_HEIGHT. A shot hits the base if it falls within the bounding box defined by base_left, base_right, and BASE_HEIGHT.
Italics denote comments, not source code.
procedure check_base_for_impact(input base_left : integer,
input base_right : integer,
input active_shot_x[] : array of integer,
input active_shot_y[] : array of integer,
input num_active_shots : integer,
output alive : boolean)
begin
integer i;
alive := true;
for i = 1 to num_active_shots begin
if (active_shot_y[i] <= BASE_HEIGHT) then
if ((active_shot_x[i] >= base_left) AND
(active_shot_x[i] <= base_right)) then
alive = false;
break; break out of the loop, go to statement
immediately after end for
end if;
end if;
end for;
end;