Advertisement
PhilDrummer

Assignment_6 01

Oct 25th, 2014
2,734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 4.87 KB | None | 0 0
  1. note
  2.     description : "Draws checker triangle and diamond according to user input"
  3.     author      : "Philipp Schaad"
  4.     date        : "25.10.2014"
  5.     revision    : "1.0"
  6.  
  7. class
  8.     APPLICATION
  9.  
  10. inherit
  11.     ARGUMENTS
  12.  
  13. create
  14.     execute
  15.  
  16. feature {NONE} -- Initialization
  17.  
  18.     execute
  19.             -- Run application.
  20.         local
  21.             dimensions: INTEGER
  22.         do
  23.             dimensions := get_input
  24.             draw_triangle (dimensions)
  25.             draw_diamond (dimensions)
  26.         end
  27.  
  28. feature -- Interaction / Output
  29.  
  30.     get_input: INTEGER
  31.             -- Getting user input
  32.         local
  33.             validated: BOOLEAN
  34.             user_input: STRING
  35.         do
  36.             from
  37.                 validated := false
  38.             until
  39.                 validated = true
  40.             loop
  41.                 io.put_string ("Enter the dimensions: ")
  42.                 io.read_line
  43.                 user_input := io.last_string
  44.                 if
  45.                     user_input.is_integer
  46.                 then
  47.                     Result := user_input.to_integer
  48.                     if
  49.                         Result > 0 and Result <= 20
  50.                     then
  51.                         validated := true
  52.                     elseif
  53.                         Result > 20
  54.                     then
  55.                         validated := ask_back
  56.                     else
  57.                         io.put_string ("Please input a positive integer!")
  58.                         io.new_line
  59.                     end
  60.                 else
  61.                     io.put_string ("Please input a positive integer!")
  62.                     io.new_line
  63.                 end
  64.             end
  65.         end
  66.  
  67.     ask_back: BOOLEAN
  68.             -- Ask if user wants that big of a dimension
  69.         local
  70.             user_answer: STRING
  71.             validated: BOOLEAN
  72.         do
  73.             io.put_string ("WARNING! You chose a dimension greater than 20.")
  74.             io.new_line
  75.             io.put_string ("Due to the size of your console, this might not show correctly.")
  76.             io.new_line
  77.  
  78.             from
  79.                 validated := false
  80.             until
  81.                 validated = true
  82.             loop
  83.                 io.put_string ("Do you want to continue anyways? (y/n): ")
  84.                 io.read_line
  85.                 user_answer := io.last_string
  86.                 if
  87.                     user_answer ~ "y"
  88.                 then
  89.                     validated := true
  90.                     Result := true
  91.                 elseif
  92.                     user_answer ~ "n"
  93.                 then
  94.                     validated := true
  95.                     Result := false
  96.                 else
  97.                     io.put_string ("Please answer with 'y' or 'n'!")
  98.                     io.new_line
  99.                 end
  100.             end
  101.         end
  102.  
  103.     draw_triangle (d: INTEGER)
  104.             -- Draw the checker triangle.
  105.         local
  106.             line_count: INTEGER
  107.             column_count: INTEGER
  108.         do
  109.             io.new_line
  110.             io.put_string ("Checker triangle:")
  111.             io.new_line
  112.             io.new_line
  113.  
  114.                 -- Loop through the lines
  115.             from
  116.                 line_count := 1
  117.             until
  118.                 line_count = d + 1
  119.             loop
  120.                 if
  121.                     (line_count + 2) \\ 2 = 1
  122.                 then
  123.                     -- Case for uneven lines (Loop column)
  124.                     from
  125.                         column_count := 1
  126.                     until
  127.                         column_count > line_count
  128.                     loop
  129.                         if
  130.                             (column_count + 2) \\ 2 = 1
  131.                         then
  132.                             io.put_string ("*")
  133.                         else
  134.                             io.put_string (" ")
  135.                         end
  136.                         column_count := column_count + 1
  137.                     end
  138.                 else
  139.                     -- Case for even lines (Loop column)
  140.                     from
  141.                         column_count := 1
  142.                     until
  143.                         column_count > line_count
  144.                     loop
  145.                         if
  146.                             (column_count + 2) \\ 2 = 1
  147.                         then
  148.                             io.put_string (" ")
  149.                         else
  150.                             io.put_string ("*")
  151.                         end
  152.                         column_count := column_count + 1
  153.                     end
  154.                 end
  155.                 line_count := line_count + 1
  156.                 io.new_line
  157.             end
  158.         end
  159.  
  160.     draw_diamond (d: INTEGER)
  161.             -- Draw the checker diamond.
  162.         local
  163.             line_count: INTEGER
  164.             column_count: INTEGER
  165.             place_holder: INTEGER
  166.         do
  167.             io.new_line
  168.             io.new_line
  169.             io.put_string ("Checker diamond:")
  170.             io.new_line
  171.             io.new_line
  172.  
  173.                 -- Upper half
  174.             from
  175.                 line_count := 1
  176.             until
  177.                 line_count = d
  178.             loop
  179.                     -- Add spaces to center asterisks
  180.                 from
  181.                     place_holder := 1
  182.                 until
  183.                     place_holder = (d - line_count) + 1
  184.                 loop
  185.                     io.put_string (" ")
  186.                     place_holder := place_holder + 1
  187.                 end
  188.  
  189.                     -- Drawing asterisks
  190.                 from
  191.                     column_count := 1
  192.                 until
  193.                     column_count > (2 * line_count) - 1
  194.                 loop
  195.                     if
  196.                         (column_count + 2) \\ 2 = 1
  197.                     then
  198.                         io.put_string ("*")
  199.                     else
  200.                         io.put_string (" ")
  201.                     end
  202.                     column_count := column_count + 1
  203.                 end
  204.                 io.new_line
  205.                 line_count := line_count + 1
  206.             end
  207.  
  208.                 -- Middle line
  209.             from
  210.                 column_count := 1
  211.             until
  212.                 column_count = (2 * d) + 1
  213.             loop
  214.                 if
  215.                     (column_count + 2) \\ 2 = 1
  216.                 then
  217.                     io.put_string ("*")
  218.                 else
  219.                     io.put_string (" ")
  220.                 end
  221.                 column_count := column_count + 1
  222.             end
  223.             io.new_line
  224.  
  225.                 -- Lower half
  226.             from
  227.                 line_count := d
  228.             until
  229.                 line_count = 1
  230.             loop
  231.                     -- Add spaces to center asterisks
  232.                 from
  233.                     place_holder := 1
  234.                 until
  235.                     place_holder = (d - line_count) + 2
  236.                 loop
  237.                     io.put_string (" ")
  238.                     place_holder := place_holder + 1
  239.                 end
  240.  
  241.                     -- Drawing asterisks
  242.                 from
  243.                     column_count := 1
  244.                 until
  245.                     column_count > (2 * line_count) - 2
  246.                 loop
  247.                     if
  248.                         (column_count + 2) \\ 2 = 1
  249.                     then
  250.                         io.put_string ("*")
  251.                     else
  252.                         io.put_string (" ")
  253.                     end
  254.                     column_count := column_count + 1
  255.                 end
  256.                 io.new_line
  257.                 line_count := line_count - 1
  258.             end
  259.         end
  260.  
  261. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement