Arbitrarily vote for themselves

Assignment Help Business Management
Reference no: EM132286741

Now, you want to play a game, inspired by Werewolf, with a group of your friends. A Player can either be a Werewolf, or a Villager. In this game, you can have 4 or more players. The first 2 players are automatically designated to be Werewolves, while everyone else is assigned to be a Villager. One play of the game involves all of the players voting for a player who they believe to be a Werewolf; in this implementation, all players, except yourself, arbitrarily vote for themselves. At the end of each play, the player with the most votes is removed from the game. Each play of the game alternates between daytime and nighttime. If it is nighttime, only votes by werewolves count.

The game ends when there are no werewolves left, which means the villagers won, or when there are more werewolves than villagers, which means the werewolves have won.

However, there are bugs in the code for the game! Read through the code and fix all of the bugs so that the game can work properly. As hint, there are at least four bugs in the code! For this lab there is a hidden test. In other words, we will be running a test against your code that you don't have access to, so make sure to be thorough! You should not need to add any lines - only edit existing lines.

def get_most_common_element(lst):
    return max(set(lst), key=lst.count)

class Player:
    def __init__(self, name):
        self.name = name
        self.active = True

class Werewolf(Player):
    def __init__(self, name):
        Player.__init__(self, name)

    def reveal_player_type(self):
        print("You are a werewolf!")

class Villager(Player):
    def __init__(self, name):
        Villager.__init__(self, name)    

    def reveal_player_type(self):
        print("You are a villager!")

class Game:
    def __init__(self, players, your_name):
        """
        Sets the game up. players is a list of strings that are names of all 
        of the players. your_name is a string and must be one of the players.
        >>> game = Game(["a", "b", "c", "d", "e", "f"], "a")
        You are a werewolf!
        >>> game.your_name
        'a'
        >>> game.play("b")
        'Keep playing!'
        >>> len(game.werewolves)
        1
        >>> len(game.villagers)
        4
        >>> game.play("c")
        'Keep playing!'
        >>> game.play("d")
        'Keep playing!'
        >>> game.play("a")
        'Villagers win!'
        >>> game.werewolves
        []
        >>> len(game.villagers)
        2
        """
        if len(players) < 4:
            raise Exception("Not enough players!")
        names = players[0:2]
        self.your_name = your_name
        self.werewolves = [Werewolf(self, w) for w in names]
        self.villagers = [Villager(self, p) for p in players if p not in names]
        self.name_to_player = {}

        for werewolf in self.werewolves:
            self.name_to_player[werewolf.name] = werewolf

        for villager in self.villagers:
            self.name_to_player[villager.name] = villager

        player = self.name_to_player[your_name]
        player.reveal_player_type()

        self.state = "night"

    def play(self, vote):
        """
        While the game is still being played, make a move. vote is the player 
        who you vote for, because you believe they are on the opposing team. 
        You can continue playing until either the villagers or the werewolves win.
        """
        self.make_move(vote)
        if not self.check_if_end_of_game():
            return "Keep playing!"
        else:
            if len(self.werewolves) == 0:
                return "Villagers win!"
            elif len(self.werewolves) > len(self.villagers):
                return "Werewolves win!"

    def make_move(self, vote):
        """
        Every player votes (players arbitrarily vote for themselves). Then, 
        if the state of the game is day, remove the player with the most votes 
        overall, and set the state to night. If the state of the game is night, 
        remove the player with the most votes by werewolves, and set the state to day.
        """
        votes = []
        werewolf_votes = []

        if self.state == "night":
            werewolf_votes.append(vote)
        votes.append(vote)

        for player in self.name_to_player:
            if self.state == "night" and isinstance(player, Werewolf(name)):
                werewolf_votes.append(player)
            votes.append(player)

        if self.state == "day":
            majority_vote = get_most_common_element(votes)
            self.state = "night"
        elif self.state == "night":
            majority_vote = get_most_common_element(werewolf_votes)
            self.state = "day"

        if majority_vote in self.name_to_player:
            self.remove_player(majority_vote)
        else:
            raise Exception("Invalid player.")

    def remove_player(player_to_remove):
        """
        Set the player with the majority vote to inactive, and remove it from 
        its respective list of players.
        """
        player = self.name_to_player[player_to_remove]
        self.active = False

        if player in self.werewolves:
            self.werewolves.remove(player)
        elif player in self.villagers:
            self.villagers.remove(player)
        else:
            print("Player already removed!")

    def check_if_end_of_game(self):
        """
        Returns True if the game is over, and False if it is not. The game is over when 
        there are no werewolves remaining, or if there are more werewolves than villagers.
        """

        if len(Game.werewolves) == 0:
            return True
        elif len(Game.werewolves) > len(Game.villagers):
            return True
        else:
            return False

Reference no: EM132286741

Questions Cloud

Manage runlevel services : There are certain methods you can use to move from one run level to the next. Explain how /etc/inittab file and specific commands
Fixed partitioning scheme with equal-size partitions : A process table is maintained that includes a pointer to a partition for each resident process. How many bits are required for the pointer?
Develop a user training program on security : What would you do if you needed to develop a user training program on security awareness and security policy implementation?
How would you begin the planning process : Consider the following scenario: You are a program manager at a local public health department tasked with increasing physical activity of adults.
Arbitrarily vote for themselves : Now, you want to play a game, inspired by Werewolf, with a group of your friends. A Player can either be a Werewolf, or a Villager.
Research paper - Foreign adoptions and Dima Yakovlev law : Write a 5-7 page research paper on Foreign adoptions and Dima Yakovlev law. Could an American citizen today adopt a child from Russia
Define what the advocacy accomplished for the patient : Discuss the importance of advocacy as it pertains to patient care. What is the nurse's role in patient advocacy? Describe a situation in which you were involved
Rights for a student who is leaving prematurely : Need step-by-step IT security policy for handling user accounts/rights for a student who is leaving prematurely (drops, is expelled, and so on).
Name all the computation devices : Name all the computation devices you use on a regular basis. Which one you can let go without losing any productivity.

Reviews

Write a Review

Business Management Questions & Answers

  Describe the need for achievement

1. Describe the need for achievement, need for affiliation, and need for power as discussed in McClelland's Need Theory. Discuss the managerial implications of McClelland's needs.

  Assume someone argues that inflation is good for business

Assume someone argues that inflation is good for business (because businesses receive higher prices)

  Input three customers information

Create a program that will give an output as shown on figure A.The program will ask to input three customers information. Each customer have three items purchased.

  Determine what other management differences have impacted

Determine what other management differences have impacted the relative success of Kodak and Fujifilm

  Controlled in the construction industry

Using the pmbok 6th edition, how can you enure that a project schedule is followed and the costs are controlled in the construction industry?

  What role does communications play in sustaining texas

What role does communications play in sustaining Texas Nameplates entrepreneurial culture

  The critical steps in planning and implementing

What are the critical steps in planning and implementing an effective training program

  Why should countries engage in international trade rather th

Fair Trade Answer the following questions. 1. Why should countries engage in international trade rather than remaining self-sufficient and avoiding the unfair competition of low-paid foreign workers? 2. If our country can make everything better and c..

  Essential that all the partners take decisions

essential that all the partners take decisions jointlynicholas jack and madeline are planning to form a partnership to

  Compare and contrast business-to-business

Prepare a simple matrix using pizza hut in which you compare and contrast business-to-business (B2B) and business-to-consumer (B2C) e-commerce

  Elements of a business''s environment and its design

In this module, we learned about the elements of a business's environment and its design. The purpose of this discussion is to define the elements of a business's environment, explore the options available for structural design, and identify emerg..

  How is web analytics different from data analytics

How is Web analytics different from data analytics? Give three examples where Web analytics is very useful for supporting businesses

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd