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

Code Golf challenge that plays Mafia

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

Problem

I am working on writing a Code Golf King of the Hill challenge, the details of which can be read here. You can read the full code here.

Briefly, the game proceeds in cycles of night then day. There are two mafia, who collectively kill one person at night, and five town sided players (some of whom have roles during the night, not important here.) during the day, in human version all people living discuss to try to figure out who is who. The mafia want to kill all the towns people but the townspeople want to eliminate the mafia. Therefore, after discussing the occurrences of the night, they vote to lynch a person (or not to), the objective being to eliminate a mafia. By making false claims, the mafia can pretend to be town, but if they mess up the town should notice. The fun of the game is that not everyone is telling the truth. The link above has a more in depth explanation.

Notice that each person has to claim a role publicly, and they can claim to be one role while actually bring a different one. Therefore I have two identities, one actual and one public. They can be contradictory or in agreement, depending on the players strategy.

Basically, the users will program Java bots that will play the game, given only the information provided by other players and sometimes from the controller program. This information is provided by a static ArrayList of the Identity objects of all players, specifically the public Identity object, since each player has two Identities.

I specifically want to ask about how my class structure and object inheritance structure is set up, whether it should be changed, whether users can easily use it and understand what is going on, possible information leaks, etc. I also want some advice about initializing the game in my controller class, and especially how I am going to load all the users responses into the program and randomly select some of their strategies to be put to the test.

I have put ... in place of some methods which aren't

Solution

I specifically want to ask about how my class structure and object inheritance structure is set up, whether it should be changed

A generic Player class with methods like dayCop, dayDoctor, dayMafia sounds like it would be better to have Cop, Doctor, Mafia sub-classes and a day method defined in the parent Player and overridden in the sub-classes.

Except that "day" is not a good name, as it doesn't suggest the purpose (just like "dayCop", "dayDoctor", etc, don't either).


[...] whether users can easily use it and understand what is going on

I cannot. The class names and method names don't really mean anything, it all seems very abstract.


[...] and especially how I am going to load all the users responses into the program and randomly select some of their strategies to be put to the test.

It's interesting that you mention the term "strategies".
The Strategy design pattern looks like a very good fit for your situation.
There can be a general behavior in your program that works with Player instances,
and specialized behavior that is implemented in the sub-classes.
For example it seems you can have a DayStrategy and a NightStrategy.

Bad practices

Several bad practices jump in the eyes:

  • Refer to types by interfaces instead of implementation. For example, declare variables as List instead of ArrayList



  • Make member variables private final or private if possible



  • Avoid repeated hash table lookups like roles.get(i). Cache in a local variable and reuse



  • Use else if between if statements that cannot be true at the same time



  • Variables should be named camelCase (violated by "Identities" and "Ids")

Context

StackExchange Code Review Q#94571, answer score: 8

Revisions (0)

No revisions yet.