HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Mafia game simulation engine

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
enginesimulationgamemafia

Problem

I've written a program to simulate a game of Mafia among several bots. Here's the directory structure the program lives in (to make understanding parts of the program easier):

/
start
README.md # Readme file
wtfpl.txt # License
controller/
main.py
messages.py # Contains a list of strings. I only put it into
# a separate file because it's long.
players/
log
player_folder/ # Every player has one of these
to_server
from_server
players
run


main.py is the actual program that I'm looking for feedback with, and it is run by running ./start in the root folder (which just executes controller/main.py, nothing fancy).

I have a few concerns, most of which come down to "good style". I tried to follow PEP8 where appropriate (with the exception of tabs vs. spaces, because TABS FOREVER) but I'm sure there are places I screwed that up. Largely, though, I'm just concerned because I'm not an incredibly experienced programmer and a not-insignificant portion of that already limited experience is code golf, so I have almost no idea of how to make an actual real program not suck. I'm just hoping people can take a look at this and suggest ways to make it "better", for whatever definition of better they choose.

I think I've commented heavily enough/chosen good enough variable and function names to explain what the program actually does, but if you have any questions I'd be happy to answer. Additionally, I know I'm not supposed to edit this question with updates as I make them, but if you want to know what my code looks like right now I'll be updating the git repo here with any updates as I make them.

Finally, one last thing: I use some try: ... except: blocks that blindly catch any exception and ignore it. I'm aware of how terrible this is, however, these bots are going to be written by other people, and I want to be catching any error at all and defaulting to "ignore the input", which is why I've

Solution

Overall, it looks pretty good to me. I agree that you have good commenting and variable and function naming. I see just a few things to comment on.

-
You have assign_roles() nested inside get_players(). Is there a reason for doing this? Unless there's a good one, I'd recommend putting assign_roles() at the top level along with everything else.

-
Could you put the log file someplace like '/tmp/log' so it doesn't mess up your list of players? Besides, what if you have a player named 'log'? Also, you might consider looking into python's logging module. It can be a bit complex, but provides a lot of nice features for managing log files.

-
I see an opportunity for further modularizing your main() routine -- maybe routines like day_action(), maybe_lynch_somebody(), mafia_night_action(), cop_night_action(), and doctor_night_action(). It might involve passing your lists around some, but you're already doing that up above, so it shouldn't be an issue.

Looks like an interesting game.

Context

StackExchange Code Review Q#47836, answer score: 8

Revisions (0)

No revisions yet.