Problem Description
This is the graphical editor problem. It's sort of a text version of the Microsoft Paint program. This one doesn't read in a file, it takes user input of commands to change the "image."
The commands to implement are as follows:
Solution
This is another 2D array problem, but again in Python it's a list of lists.
I'm not sure how clean I got the object design aspects of this, I have a class called Interface and a class called Drawing that handle the user input processing and the drawing of the image. Trouble is I kind of have some interface aspects in the main function. It seems to work, but probably could be cleaner. The Interface class is a while loop that will allow you to continue to read commands until the exit command is given. I also implemented a commend to output a text menu of choices, although that wasn't really in the problem. Here is the menu.
Here is the Interface class, interface method. It's a loop that checks if you made a valid selection and returns your selection. (Here's the bad job by me, the X command is actually handled in the main function. So, that's where the design could be better. Maybe I'll still fix it)
def interface(self): valid_entry = {"C":"valid","I":"valid", "L":"valid", "V":"valid", "H":"valid", "K":"valid", "F":"valid", "S":"valid", "X":"valid", "M":"valid"} while True: self.selection = "" self.selection = input("Select a command or enter M to see the menu:") try: valid_entry[self.selection[0]] == "valid" except KeyError: print("Please make a valid selection, enter M to see menu") continue if self.selection == "M": print(self.command_menu) elif self.selection == "X": print("Exiting") return self.selection break elif valid_entry[self.selection[0]] == "valid": print("valid selction") return self.selection else: print("Please make a valid selection, enter M to see menu")
The main function runs a loop that calls the interface and returns the command to be processed
def execute_main(): return_command = "" image = None problem4_interface = Inter.Interface() process_drawing = Drawing() while return_command != "X": return_command = problem4_interface.interface() if return_command != "X": image = process_drawing.command_router(return_command, image) else: continue
It took a long time to write all the methods to process all the lists (maybe that's why I've posted this with some problems - haha) Here is the method to fill a region in the Drawing class. It determines what color you select as input and changes all of that color to a the new color.
def fill_region(self, image): self.image = image if self.image == None: print("No image has been created, run command I M N") else: self.target_color = self.image[int(self.command[2])-1][int(self.command[4])-1] print(f"target {self.target_color}") for index, num_row in enumerate(self.image): for index2, color_val in enumerate(num_row): if color_val == self.target_color: self.image[index][index2] = self.command[6] else: None print(self.image) return self.image
See the full code on my Github
0 Comments
Leave a Reply. |
Archives
December 2022
Categories |