patternpythonMinor
Huge terminal game (in development)
Viewed 0 times
hugegamedevelopmentterminal
Problem
I'm developing a big (for me, as a beginner) terminal program "Brainfuck - The Game", in which you have to complete Brainfuck challenges. I would like your review on existing code (and project structuring) before I continue to expand it even more.
main.py
m_shared.py
```
""" A module that contains Shared class. """
import m_statscontainer
class Shared (object):
""" Holds everything that's shared among command functions. """
# Subclasses that help to divide this huge class into some smaller chunks
stats_container = m_statscontainer.Stats_Container()
# Constants
COMMANDS = ["help", "exit", "run", "open", "save", "setcell", "def",
"code", "limit", "del", "edit", "reset", "debug", "memo",
"brainfuck", "challenge", "random", "stats", "credits"]
DEFAULT_STEP_LIMIT = 10000 # Protects program from infinite loops
# Variables
is_running = True
last_input = []
# Variables for the brainfuck program
limit = DEFAULT_STEP_LIMIT
memory_world = {}
definitions = {}
code = []
compiled_code = []
# Methods
def __init__(self):
""" Initializes the Shared class. """
pass
def prompt_command(self, valid_commands):
""" Repeatedly prompts input unitl valid command can be returned. """
user_input = []
while user_input == [] or user_input[0] not in valid_commands:
main.py
""" Brainfuck - the game. """
def main ():
""" The main function. """
print ("Type 'help' for a list of supported commands.")
# Every command is a seperate method & has full access to shared class
shared = m_shared.Shared()
commands = { "help" : m_help.help,
"exit" : m_exit.exit }
# The loop
while shared.is_running:
commands[shared.prompt_command(shared.COMMANDS)](shared)
if __name__ == "__main__":
# Here and further on "m_" stands for "module"
import m_shared
import m_help
import m_exit
main()m_shared.py
```
""" A module that contains Shared class. """
import m_statscontainer
class Shared (object):
""" Holds everything that's shared among command functions. """
# Subclasses that help to divide this huge class into some smaller chunks
stats_container = m_statscontainer.Stats_Container()
# Constants
COMMANDS = ["help", "exit", "run", "open", "save", "setcell", "def",
"code", "limit", "del", "edit", "reset", "debug", "memo",
"brainfuck", "challenge", "random", "stats", "credits"]
DEFAULT_STEP_LIMIT = 10000 # Protects program from infinite loops
# Variables
is_running = True
last_input = []
# Variables for the brainfuck program
limit = DEFAULT_STEP_LIMIT
memory_world = {}
definitions = {}
code = []
compiled_code = []
# Methods
def __init__(self):
""" Initializes the Shared class. """
pass
def prompt_command(self, valid_commands):
""" Repeatedly prompts input unitl valid command can be returned. """
user_input = []
while user_input == [] or user_input[0] not in valid_commands:
Solution
I have a few minor comments, like "move the import statements to the top", but my biggest concern is the confusing structure. Why do you need a class? A warning sign that something isn't a class, or shouldn't be a class, is a class with two methods, one of which is
Why do you need so many modules? The most worrying example of this is, I think,
__init__. Your init is even empty. Okay, your class has three methods, but I'd still count that.Why do you need so many modules? The most worrying example of this is, I think,
m_exit.py. If you start writing modules for a single two-line function, there's something wrong.Context
StackExchange Code Review Q#86595, answer score: 3
Revisions (0)
No revisions yet.