Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- note
- description : "Draws checker triangle and diamond according to user input"
- author : "Philipp Schaad"
- date : "25.10.2014"
- revision : "1.0"
- class
- APPLICATION
- inherit
- ARGUMENTS
- create
- execute
- feature {NONE} -- Initialization
- execute
- -- Run application.
- local
- dimensions: INTEGER
- do
- dimensions := get_input
- draw_triangle (dimensions)
- draw_diamond (dimensions)
- end
- feature -- Interaction / Output
- get_input: INTEGER
- -- Getting user input
- local
- validated: BOOLEAN
- user_input: STRING
- do
- from
- validated := false
- until
- validated = true
- loop
- io.put_string ("Enter the dimensions: ")
- io.read_line
- user_input := io.last_string
- if
- user_input.is_integer
- then
- Result := user_input.to_integer
- if
- Result > 0 and Result <= 20
- then
- validated := true
- elseif
- Result > 20
- then
- validated := ask_back
- else
- io.put_string ("Please input a positive integer!")
- io.new_line
- end
- else
- io.put_string ("Please input a positive integer!")
- io.new_line
- end
- end
- end
- ask_back: BOOLEAN
- -- Ask if user wants that big of a dimension
- local
- user_answer: STRING
- validated: BOOLEAN
- do
- io.put_string ("WARNING! You chose a dimension greater than 20.")
- io.new_line
- io.put_string ("Due to the size of your console, this might not show correctly.")
- io.new_line
- from
- validated := false
- until
- validated = true
- loop
- io.put_string ("Do you want to continue anyways? (y/n): ")
- io.read_line
- user_answer := io.last_string
- if
- user_answer ~ "y"
- then
- validated := true
- Result := true
- elseif
- user_answer ~ "n"
- then
- validated := true
- Result := false
- else
- io.put_string ("Please answer with 'y' or 'n'!")
- io.new_line
- end
- end
- end
- draw_triangle (d: INTEGER)
- -- Draw the checker triangle.
- local
- line_count: INTEGER
- column_count: INTEGER
- do
- io.new_line
- io.put_string ("Checker triangle:")
- io.new_line
- io.new_line
- -- Loop through the lines
- from
- line_count := 1
- until
- line_count = d + 1
- loop
- if
- (line_count + 2) \\ 2 = 1
- then
- -- Case for uneven lines (Loop column)
- from
- column_count := 1
- until
- column_count > line_count
- loop
- if
- (column_count + 2) \\ 2 = 1
- then
- io.put_string ("*")
- else
- io.put_string (" ")
- end
- column_count := column_count + 1
- end
- else
- -- Case for even lines (Loop column)
- from
- column_count := 1
- until
- column_count > line_count
- loop
- if
- (column_count + 2) \\ 2 = 1
- then
- io.put_string (" ")
- else
- io.put_string ("*")
- end
- column_count := column_count + 1
- end
- end
- line_count := line_count + 1
- io.new_line
- end
- end
- draw_diamond (d: INTEGER)
- -- Draw the checker diamond.
- local
- line_count: INTEGER
- column_count: INTEGER
- place_holder: INTEGER
- do
- io.new_line
- io.new_line
- io.put_string ("Checker diamond:")
- io.new_line
- io.new_line
- -- Upper half
- from
- line_count := 1
- until
- line_count = d
- loop
- -- Add spaces to center asterisks
- from
- place_holder := 1
- until
- place_holder = (d - line_count) + 1
- loop
- io.put_string (" ")
- place_holder := place_holder + 1
- end
- -- Drawing asterisks
- from
- column_count := 1
- until
- column_count > (2 * line_count) - 1
- loop
- if
- (column_count + 2) \\ 2 = 1
- then
- io.put_string ("*")
- else
- io.put_string (" ")
- end
- column_count := column_count + 1
- end
- io.new_line
- line_count := line_count + 1
- end
- -- Middle line
- from
- column_count := 1
- until
- column_count = (2 * d) + 1
- loop
- if
- (column_count + 2) \\ 2 = 1
- then
- io.put_string ("*")
- else
- io.put_string (" ")
- end
- column_count := column_count + 1
- end
- io.new_line
- -- Lower half
- from
- line_count := d
- until
- line_count = 1
- loop
- -- Add spaces to center asterisks
- from
- place_holder := 1
- until
- place_holder = (d - line_count) + 2
- loop
- io.put_string (" ")
- place_holder := place_holder + 1
- end
- -- Drawing asterisks
- from
- column_count := 1
- until
- column_count > (2 * line_count) - 2
- loop
- if
- (column_count + 2) \\ 2 = 1
- then
- io.put_string ("*")
- else
- io.put_string (" ")
- end
- column_count := column_count + 1
- end
- io.new_line
- line_count := line_count - 1
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement