Python Forum
conway's game of life / Single Responsibility Principle
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
conway's game of life / Single Responsibility Principle
#3
So first I want to make clear, the following is not intended to run.  It is just the kind of thing I want to see stylistically.  You will need to deal with the details of making real code out of this:
def bind_keys(canvas):
    """Bind all appropriate keys to the correct function."""
    bindings = {"<keypress-up>" : movesquare, "<keypress-down>": movesquare,
                "<keypress-left>": movesquare, "<keypress-right>": movesquare,
                "x": otherkey, "f": otherkey, "r": otherkey}
    for binding in bindings:
        canvas.bind_all(binding, bindings[binding])    


def setup_phase(window, canvas):
    done = False
    while not done:
        window.update()
        window.update_idletasks()
        if keypressed == "f": #finished
            cells = on_finish_setup(canvas, sqident)
            done = True
        elif keypressed == "r": #create 15 random squares
            cells = on_random_setup(canvas, sqident)
            done = True
        return cells


def on_finish_setup(canvas, sqident):
    # Your func here.


def on_random_setup(canvas, sqident):
    # Your func here.


def main_phase(cells, window, canvas):
    while 1:
        window.update();window.update_idletasks()
        livelist = canvas.find_all()
        createlist, killlist = [],[] 
        update_create_kill(createlist, kill_list)
        render(createlist, killlist)


def main():
    window = tk(); window.title("conway's game of life")
    canvas = canvas(window, width=1200, height=900, bd=0, highlightthickness=0)
    canvas.pack() 
    cells = setup_phase(window, canvas)
    main_phase(cells, window, canvas)


if __name__ == "__main__":
    main()
Note how every function is very short and does one specific thing.  If any further complicated logic is needed it is then put in its own function as well.  One function, one job.  

Other than that you have bad habits that need to be gotten rid of now.
Most of your issues in turning your program into good modular code will be in removing the globals.
I would honestly use classes but this might be too much for the moment.


Any names defined in the global space that are intended to be constant should be capitalized like:  
CELL_SIZE = 50
MOVES = {"up": (0, -CELL_SIZE), "down": (0, CELL_SIZE),
         "left" : (-CELL_SIZE, 0), "right" : (CELL_SIZE, 0)} 
Global variables are not acceptable.  If it is defined globally it better not change.  Consider the global keyword completely off limits.
No more * imports.  Stop that now.  
Also, stop the semicolons to squeeze multiple statements on one line.
Learn proper comment style.  # comments are for short, either inline or mid-function comments.
For real documentation use docstrings.
A doc string looks like this:
def some_function():
    """
    I am a doc string.
    """
    pass
As the previous user suggested you should really adhere to at least the majority of pep8.
You can even use specific IDEs that give you warnings if you are in violation of standards (though honestly that is a bridge too far for me).
Reply


Messages In This Thread
RE: conway's game of life / Single Responsibility Principle - by Mekire - Dec-13-2016, 02:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  While loop/half-life Miraclefruit 6 8,598 Mar-06-2017, 05:24 PM
Last Post: nilamo
  conway's game of life hanscvanleeuwen 17 18,158 Dec-09-2016, 04:05 PM
Last Post: hanscvanleeuwen
  Game of life using IDLE Informatics109 4 5,212 Oct-29-2016, 01:39 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020