Thursday, September 17, 2009

Build Castle using Oracle PL/SQL








DECLARE

/*****************************************************************************************************************/
/*-------------------------------------- BUILD CASTLE -----------------------------------------------------------*/

PROCEDURE build_castle(infront_space NUMBER, len_of_castle NUMBER)
IS
blank_space_before_castle NUMBER := infront_space;
blank_space_above_castle NUMBER := FLOOR(blank_space_before_castle/3);
go_new_line VARCHAR2(2) := 'T';





----------- ^^^^^^^^^^ DECLARATIONS for FLAG ^^^^^^^^^ ---------------

space_infront_of_flag NUMBER := infront_space; -- space infront of flag
breadth_of_flag NUMBER := FLOOR(len_of_castle/5);
mid_len_flag_no_verti_lines NUMBER := (breadth_of_flag - 4);
space_btn_flag_hori_lines NUMBER := (breadth_of_flag + (breadth_of_flag - 1))-2;
-- (breadth_of_flag - 1) coz, count the star with space, except the last star whose blank space is outside the rite verti line
-- substract 2 from the total coz, we shouldnt consider the two verti lines in the flag
rod_len_in_flag NUMBER := (mid_len_flag_no_verti_lines + 2); -- keeping the len of flag same as the len of the rod of the flag
-- add 2 here coz,two verti lines in flag are missing in the mid_len_of_flag

next_line VARCHAR2(2) := 'F';

---------- vvvvvvvvvvv END OF DECLARATIONS for FLAG vvvvvvvv ------------






----------- ^^^^^^^^^^ DECLARATIONS for Triangle ^^^^^^^^^ ---------------
-- keeping the first triangle the same length as the breath of the flag

mid_len_of_1st_middle_triangle NUMBER := (breadth_of_flag - 2); -- substracting the hori line in the triangel and the tip of the triangle
first_center_space_in_triangel NUMBER := 1;
space_infront_tip_of_triangle NUMBER := infront_space;



---------- vvvvvvvvvvv END OF DECLARATIONS foR Triangle vvvvvvvv ------------






------- ^^^^^^^^^^ Declaration for tall building ^^^^^^^ --------------------
top_castle_len NUMBER := (mid_len_flag_no_verti_lines +2) + rod_len_in_flag + (mid_len_of_1st_middle_triangle +2);
-- calculates the length of the top part of the castle which includes
-- the length of the flag with pole/rod and the length of the triangle
tot_len_of_tall_building NUMBER := (len_of_castle - top_castle_len)-1; -- gives the len of the tallest building
-- substract 1 coz, we are taking out the last base line in the castle
individual_top_len_each_castle NUMBER := FLOOR(tot_len_of_tall_building/3);

front_space_in_middle_castle NUMBER;
middle_space_in_middle_castle NUMBER;


-------- vvvvvvvvv END OF Declaration for tall building vvvvv ---------------





------------^^^^^^^^^^^^^^^^^ Declaration for the 2nd middle building ^^^^^^^^^ --------

middle_space_2nd_building NUMBER;
front_space_in_2nd_building NUMBER;

middle_hori_line NUMBER;
middle_space_in_small_tri NUMBER :=4;
tot_middle_hori_space NUMBER;
space_infront_smal_tri NUMBER;
----------- vvvvvvvvvvvvvvvv END OF Declaration for the 2nd middle building vvvvv --------






------------ ^^^^^^^^^^^ Declaration for the last building ^^^^^^^^^^ --------------
no_of_stars_last_building NUMBER;
middle_space_in_last_building NUMBER;
spce_btn_hori_line_last_build NUMBER;
spce_before_last_building NUMBER;
door_space_bottom_middle NUMBER;


--------------- vvvvvvvvvvvv END OF Declaration for the last building vvvvvvvvvvv --------




------ ^^^^^^^^^^ Remainig Declarations ^^^^^^^^ -----------------
top_of_the_door NUMBER;
after_top_door VARCHAR2(2):= 'F';
upper_midle_verti_line NUMBER;
lower_verti_line NUMBER;
base_of_the_castle NUMBER;
no_of_base_stars NUMBER;
--------vvvvvvvv END OF Remaining Declarations vvvvvv ------------






---- #### Procedure to print space in castle #### ----
PROCEDURE print_spaces(para_len_of_space NUMBER)
IS
BEGIN
FOR for_len_of_space IN 1 .. para_len_of_space
LOOP
dbms_output.put(' ');
END LOOP;
END;
---- #### END OF Procedure to print space in castle #### ----





---- #### Procedure to print a space and a star #### ----
PROCEDURE print_space_n_star(para_space_infront_of_star NUMBER)
IS
BEGIN
print_spaces(para_space_infront_of_star );
dbms_output.put('*');
END;

---- #### END OF Procedure to print a space and a star #### ----






---- #### Procedure to print stars in a line #### ----
PROCEDURE print_hori_stars_no_space(para_no_of_stars NUMBER, para_space_before_star NUMBER)
IS
BEGIN
print_spaces(para_space_before_star);
FOR for_no_of_stars IN 1 .. para_no_of_stars
LOOP
dbms_output.put('*');
END LOOP;
END;
---- #### END OF Procedure to print stars in a line #### ----




---- #### Procedure to print a single vertical line #### ----
PROCEDURE print_single_verti_line(para_no_of_verti_stars NUMBER, para_space_infront_of_rod NUMBER)
IS
BEGIN
FOR for_print_single_vertei IN 1 .. para_no_of_verti_stars
LOOP
print_spaces(para_space_infront_of_rod);
dbms_output.put('*');
IF next_line = 'T'
THEN
dbms_output.new_line;
END IF;
END LOOP;

END;

---- #### Procedure to print a single vertical line #### ----






---- #### Procedure to print stars in horizontal line along with spaces #### ----
PROCEDURE print_hori_stars_wit_space(para_no_of_verti_stars NUMBER, para_space_infront NUMBER)
IS
BEGIN
print_spaces(para_space_infront); -- prints the space infront of the hori line

FOR for_len_of_hori_line IN 1 .. para_no_of_verti_stars
LOOP
dbms_output.put('*');
dbms_output.put(' ');
END LOOP;

END;

---- #### END OF Procedure to print stars in horizontal line along with spaces #### ----







---- #### Procedure to print two vertical line the Castle #### ----
PROCEDURE print_two_verti_line_spaces(para_no_of_hori_stars NUMBER, para_space_btn_lines NUMBER, space_infront_verti_line NUMBER)
IS
BEGIN


FOR for_2_verti_line IN 1 .. para_no_of_hori_stars
LOOP
print_spaces(space_infront_verti_line);
dbms_output.put('*'); -- prints the first star in the left verti side of the two lines
print_spaces(para_space_btn_lines); -- prints the space btn the two lines
dbms_output.put('*'); -- prints the last star in the right verti side of the two lines

IF go_new_line = 'T'
THEN
dbms_output.new_line;
END IF;


END LOOP;
END;
---- #### END OF Procedure to print two vertical line the Castle #### ----





---- #### Procedure to print a triangle on the castle #### ----
PROCEDURE print_triangle(para_mid_len_of_triangle NUMBER, para_space_infront_triangel NUMBER )
IS
changing_tri_infront_space NUMBER := para_space_infront_triangel;
base_stars_in_triangle NUMBER;

BEGIN
print_spaces(para_space_infront_triangel);
dbms_output.put_line('*');

FOR for_no_of_middle_part IN 1 .. para_mid_len_of_triangle
LOOP
changing_tri_infront_space := changing_tri_infront_space - 1; -- dec the space infront of the triangle
print_spaces(changing_tri_infront_space);
dbms_output.put('*');
print_spaces(first_center_space_in_triangel); -- prints the center space in the triangle
dbms_output.put('*');
dbms_output.new_line;
first_center_space_in_triangel := first_center_space_in_triangel + 2; -- inc the center space in the triangle
END LOOP;
base_stars_in_triangle := CEIL(first_center_space_in_triangel/2);
print_hori_stars_wit_space((base_stars_in_triangle+1),changing_tri_infront_space - 1);
dbms_output.new_line;

front_space_in_middle_castle := changing_tri_infront_space - 1;
middle_space_in_middle_castle := ((base_stars_in_triangle + 1) + (base_stars_in_triangle))- 2;
-- here wer are adding the stars along with the spaces so add base_stars + one less space than the tot stars, as the last space will be printed outside the range
-- substract 2, coz removing the two vertical lines in building, we need only the spaces without the vertical lines


END;

---- #### END OF Procedure to print a triangle on the castle #### ----






------ #### Procedure to print the tallesst middle building in the castle #### ----
PROCEDURE print_tallest_middle_building(para_top_len_of_middle NUMBER)
IS
BEGIN

print_two_verti_line_spaces(para_top_len_of_middle,middle_space_in_middle_castle,front_space_in_middle_castle);

middle_space_2nd_building := middle_space_in_middle_castle;


END;


------ #### END OF Procedure to print the tallesst middle building in the castle #### ----






---- #### Procedure to print the small triangles on the small buildings #### ----
PROCEDURE print_small_triangle(para_middle_2nd_building NUMBER, para_front_middle_castle NUMBER)
IS

small_tri_middle_space NUMBER := para_middle_2nd_building;
right_of_small_triangle NUMBER := CEIL(small_tri_middle_space/2);

small_tri_front_space NUMBER := (para_front_middle_castle - right_of_small_triangle)-1 ;
-- subtract 1,coz we shouldnt consider the space where the top star of the 2nd triangle occupies


BEGIN


print_spaces(small_tri_front_space);
dbms_output.put('*');
go_new_line := 'F';
print_two_verti_line_spaces(1,middle_space_in_middle_castle,right_of_small_triangle);
print_spaces(right_of_small_triangle);
dbms_output.put('*');
dbms_output.new_line;

front_space_in_2nd_building := small_tri_front_space;

END;

---- #### END OF Procedure to print the small triangles on the small buildings #### ----






---- #### Procedure to print the second line in the small triangle #### ----
PROCEDURE secnd_line_triangle(para_times NUMBER)
IS
BEGIN

FOR secnd_line_triange IN 1 .. para_times
LOOP
go_new_line := 'F';
print_two_verti_line_spaces(1,middle_space_in_small_tri,(front_space_in_2nd_building -2));
print_two_verti_line_spaces(1,middle_space_in_middle_castle,1);
print_two_verti_line_spaces(1,middle_space_in_small_tri,1);
dbms_output.new_line;
END LOOP;
END;
---- #### END OF Procedure to print the second line in the small triangle #### ----





---- #### Procedure to print huge spaces infront of a single star #### ----
PROCEDURE print_huge_space_n_star(para_huge_space NUMBER)
IS
BEGIN
print_spaces(para_huge_space);
dbms_output.put('*');
END;



---- #### END OF Procedure to print huge spaces infront of a single star #### ----








BEGIN

---- @@@@ Prints the flag on the top of the castle @@@@ ----

print_hori_stars_wit_space( breadth_of_flag, space_infront_of_flag); -- this is to print the first hori line in the flag with stars and spaces
dbms_output.new_line; -- to get the control to the next line after drawing the first hori line in the flag
print_two_verti_line_spaces(mid_len_flag_no_verti_lines, space_btn_flag_hori_lines, space_infront_of_flag); -- prints the middle part of the flag, which has two hori lines and spaces in btn
print_hori_stars_wit_space( breadth_of_flag, space_infront_of_flag);
dbms_output.new_line; -- to get the control to the next line after drawing the first hori line in the flag
next_line := 'T';
print_single_verti_line(rod_len_in_flag,space_infront_of_flag);

---- @@@@ END OF Prints the flag on the top of the castle @@@@ ----
next_line := 'F';





---- @@@@ Print the triangle in the castle @@@@ ----
print_triangle(mid_len_of_1st_middle_triangle, space_infront_tip_of_triangle);

---- @@@@ END OF Print the triangle in the castle @@@@ ----




---- @@@@ Print the top part of tallest middle castle building @@@@ ----
print_tallest_middle_building(individual_top_len_each_castle-1); -- subtract 1, coz remvoing the base line in the upper part of the castle

---- @@@@ END OF Print the top part of tallest middle castle building @@@@ ----


---- @@@@ Print small triangle @@@@ ----
print_small_triangle( middle_space_2nd_building,front_space_in_middle_castle) ;
secnd_line_triangle(1) ;
---- @@@@ Print small triangle @@@@ ----



tot_middle_hori_space := (middle_space_in_small_tri + middle_space_in_middle_castle+ middle_space_in_small_tri)+ 6 + 2;
-- this add the middle spaces in the two side 2nd buildings and the middle builiding
-- along with the 6 vertical lines and 2 spaces btn the side building and the center building

middle_hori_line := CEIL(tot_middle_hori_space/2)+1;
space_infront_smal_tri := front_space_in_middle_castle - (middle_space_in_small_tri+3);

print_hori_stars_wit_space(middle_hori_line,space_infront_smal_tri);
-- subtracting the space occupied by the 2nd building from
-- the total left space from the center of the middle building

dbms_output.new_line;




---- @@@@ Print the upper middle veretical lines

upper_midle_verti_line := FLOOR(tot_len_of_tall_building/3)-2;
FOR for_upper_middle_veret IN 1 .. upper_midle_verti_line
LOOP
print_two_verti_line_spaces(1, middle_space_2nd_building ,space_infront_smal_tri);
print_two_verti_line_spaces(1, middle_space_2nd_building ,middle_space_in_middle_castle);
dbms_output.new_line;
END LOOP;




---- @@@@ Print the two hori lines for the bottom building of the castle
middle_space_in_last_building := middle_space_2nd_building;


spce_before_last_building := (space_infront_smal_tri - middle_space_in_last_building)-1;
-- calc front space of the last building, by subtracting the front space of the
-- middle space from last building from the front space of 2nd building
-- and then 1 ie the vertical line of the 2nd building

no_of_stars_last_building := FLOOR(middle_space_in_last_building/2)+2; -- half of the center space and add 2
-- for the corner stars on the two verti lines


print_hori_stars_wit_space(no_of_stars_last_building,spce_before_last_building );
print_hori_stars_wit_space(1,(middle_space_2nd_building-1));
print_hori_stars_wit_space(1,(middle_space_in_middle_castle-1));
print_hori_stars_wit_space(no_of_stars_last_building,(middle_space_2nd_building-1));
dbms_output.new_line;



---- @@@@ Print the lower vertical lines
lower_verti_line := ((tot_len_of_tall_building-2) - upper_midle_verti_line);

FOR for_lower_part_castle IN 1 .. lower_verti_line
LOOP

print_two_verti_line_spaces(1, middle_space_in_last_building ,spce_before_last_building);
-- prints the the vertical line of the last building in the lower part of castle
IF(for_lower_part_castle = 6)
THEN
door_space_bottom_middle := FLOOR(middle_space_in_middle_castle/3);
top_of_the_door := (middle_space_in_middle_castle - (door_space_bottom_middle *2));


print_single_verti_line(1,middle_space_2nd_building);
-- prints the right side wall of the 2nd building
print_hori_stars_no_space(top_of_the_door,door_space_bottom_middle);
-- prints the top of hte door
print_two_verti_line_spaces(1,middle_space_2nd_building,door_space_bottom_middle);
-- prints the left and right side walls of the right 2nd building
print_single_verti_line(1,middle_space_in_last_building);
-- prints the right side wall of the last building
after_top_door := 'T';

ELSE
-- this has to be executed after the top of the door is printed in the bottom half of the castle
IF after_top_door = 'T'
THEN
print_two_verti_line_spaces(1,door_space_bottom_middle,middle_space_2nd_building);
print_two_verti_line_spaces(1,door_space_bottom_middle,(top_of_the_door-2));
print_two_verti_line_spaces(1,middle_space_in_last_building,middle_space_2nd_building);

-- this is executed before the the top of the door is printed in the top half of the castle
ELSIF after_top_door = 'F'
THEN
print_two_verti_line_spaces(1,middle_space_in_middle_castle,middle_space_2nd_building);
print_two_verti_line_spaces(1,middle_space_in_last_building,middle_space_2nd_building);
END IF;
END IF;
dbms_output.new_line;
END LOOP;

--- base of the castle
base_of_the_castle := (middle_space_in_last_building *2) + (middle_space_2nd_building *2) + middle_space_in_middle_castle +4;
-- total + 4 coz, counting the vertical lines with the spaces in between
no_of_base_stars := FLOOR(base_of_the_castle/2) +2;
-- + 2 for the vertical lines in the begining and the end of the base line


print_hori_stars_wit_space(no_of_base_stars,spce_before_last_building);
dbms_output.new_line;
END;





/*-------------------------------------- BUILD CASTLE -----------------------------------------------------------*/
/*****************************************************************************************************************/



BEGIN
build_castle(30,25);
END;

1 comment:

  1. Wow.. What a practice test. I needed some of these for my team. thanks buddy.

    sap upgrades

    ReplyDelete