Mafia is a party game for a group of friends to get together and yell and lie at each other. It is incredibly fun, and I play all of the time with my friends and family. During the game of mafia, there are townspeople and mafia members, and each group seeks to kill off the other. The mafia are the informed minority: there are few of them (generally two), and the townspeople are the majority. The mafia know who is in the mafia, but the townspeople do not, so the goal of the game is for the townspeople to discover who is in the mafia. Because the game is played this way, there must be one person acting as the narrator, who tells the townspeople what the mafia do at night. This player does not participate in the game, and generally, every player in the game makes it more fun. Also, the mafia members and the townspeople must be chosen randomly, so playing cards or some other randomizer is required for the game.

UMafia comes to the rescue.

UMafia is a companion for the party game: it is meant to assist the game, rather than something like "Town of Salem", which is just a way to play the game online. UMafia is meant for people to sit around a living room or a campfire, and pull out their phones to vote and kill other players. UMafia takes the place of the narrator, meaning that an extra person can play, and it also randomizes the roles. It makes friendly mafia games much easier.

How I built it

UMafia is a website built with html, css, javascript and bootstrap. It should be compatible with mobile devices. I modeled UMafia after spyfall,which is a different party game to be played in person. It is nearly 500 lines of code, made mostly from scratch, except for some bootstrap effects which I added at the last minute. The javascript creates an array pL (short for playerList), which holds playerdata, and role data. It randomizes roles, and keeps track of how many players remain. If the condition is met that all of the mafia players are dead, then the townspeople win, and if the condition is met that the amount of mafia players exceeds the amount of townspeople, then the mafia wins.

The Interface

During the day cycle of the game, all of the players vote one player to be killed, and during the night cycle the players choose which other players to be the target of their abilities. Therefore, I chose the interface to be a list of buttons, with each player on one of the buttons. Every a player makes a decision (ie, when a player votes for another player, or when the mafia decides to kill a player), UMafia puts the decision in an array called "decisions", and checks whether there are enough decisions to proceed. During the daytime, as soon as a majority of the decisions are made for one player, that player is killed and the game shifts to night. During the nighttime, UMafia waits for every player to make a decision, even players who have no role. These players must select another player, even if it does not do anything, otherwise people would be able to deduce who is in the mafia based on who is on their phones. After every player makes a decision during the nighttime, the game kills whichever player was chosen by the Godfather; the leader of the mafia, then the game transitions back to daytime. In this way, the game goes back and forth from day and night until any win conditions are met.


Several issues came up during the development of this game. During the production, everything broke multiple times, and most of the code was rewritten. In particular, using buttons in html was difficult, because I needed to create a function that created n buttons (where n is the number of players), and each button had to have an event listener for a different function. Initially this did not work, every event listener was the same. Google eventually led me to a solution in which I used the button object within the parameters of the event listener to convey information. Another issue was that of interaction between users. However, it would be hard to test if I constantly needed multiple users. Therefore, I made a temporary solution, which is a drop-down list of users, which would simulate different users. I have not yet created a way for users to interact, but it will be coming soon.

Takeaways from YHack

After YHack, I feel optimistic about the future of UMafia. I have learned many things, and just as I have improved my code, my code has improved me. I stepped outside my comfort zone this time and searched through APIs for help in my code. Previously, I was doing all of my code from scratch, and challenging myself to use API code took a lot of time and energy. In the end, I did not end up using Google Cloud APIs as I had planned, but instead, I tried Bootstrap, which was much easier to access compared to Google's APIs. Using that allowed my entire interface to evolve. YHack made a boost in knowledge which will propel this project into the future.

What's next?

The next few things for UMafia are integration of new roles, user interaction, and scalability. Adding new roles like doctor, detective and vigilante will improve the experience of the game. Adding user interaction is the final step before the application is functional. After that, UMafia should be expanded to include any number of players and many more new roles.

Share this project: