blamed

mafia_sim

May 8th, 2011
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.03 KB | None | 0 0
  1. #!/usr/bin/python -tt
  2. # coding: utf-8
  3. import random
  4. import time
  5. class Player:
  6.     def __init__(self, mafia, can_kill, blocker, defence, healer):
  7.         self.mafia = mafia
  8.         self.can_kill = can_kill
  9.         self.blocker = blocker
  10.         self.defence = defence
  11.         self.healer = healer
  12.     alive = 'yes'
  13.     healed = 'no'
  14.     blocked = 'no'
  15.     moved = 'no'
  16.     kills = 0
  17.     #def __repr__(self):
  18.     #    return "Player(%s, %s, %s)" % (self.alive, self.mafia, self.can_kill)
  19.  
  20.  
  21.  
  22. def main():
  23.     number_of_players = 20
  24.     players = [Player('no', 'no', 'no', 'no', 'no') for i in range(number_of_players)]
  25.  
  26.     #                    mafia can_kill blocker defence   healer
  27.     #mafia
  28.     players[0] = Player('yes',  'no',   'no',   'yes',  'no')
  29.     players[1] = Player('yes',  'yes',  'yes',  'no',   'no')
  30.     players[2] = Player('yes',  'yes',  'no',   'no',   'yes')
  31.     players[3] = Player('yes',  'yes',  'no',   'no',   'no')
  32.     players[4] = Player('yes',  'yes',  'no',   'no',   'no')
  33.     #maniac
  34.     players[5] = Player('no',   'yes',  'no',   'no',   'no')
  35.     players[6] = Player('no',   'yes',  'no',   'no',   'no')
  36.     #blocker
  37.     players[7] = Player('no',   'no',   'yes',  'no',   'no')
  38.     #healer
  39.     players[8] = Player('no',   'no',   'no',   'no',   'yes')
  40.     #def_towns
  41.     players[9] = Player('no',   'no',   'no',   'yes',   'no')
  42.     players[10]= Player('no',   'no',   'no',   'yes',   'no')
  43.  
  44.     game(players, number_of_players)
  45.  
  46.  
  47. def game(players, number_of_players):
  48.     days_count = 0
  49.     while True:
  50.         killed_list = []
  51.         alive_list = []
  52.         alive_count = 0
  53.         mafia_count = 0
  54.         maniac_count = 0
  55.         player_count = 0
  56.         for player in players:
  57.             if player.alive == 'yes':
  58.                 alive_count += 1
  59.                 alive_list.append(player_count)
  60.                 if player.mafia == 'yes':
  61.                     mafia_count += 1
  62.                 if player.mafia == 'no' and player.can_kill == 'yes':
  63.                     maniac_count += 1
  64.             player_count += 1
  65.         print "-------------------------------------------------------------------"
  66.         print "%s день" % days_count
  67.         print "Живых: %s" % alive_count
  68.         print "Город: %s" % (alive_count - mafia_count - maniac_count)
  69.         print "Мафов: %s" % mafia_count
  70.         print "Маньяков: %s" % maniac_count
  71.        
  72.         if alive_count - mafia_count <= mafia_count and maniac_count == 0:
  73.             print 'mafia wins'
  74.             break
  75.         if mafia_count == 0 and maniac_count == 0:
  76.             print 'town wins'
  77.             break
  78.         if mafia_count == 0 and maniac_count == 1 and alive_count <= 2:
  79.             print 'maniac wins'
  80.             break
  81.        
  82.         for player in players:
  83.             player.moved = 'no'
  84.             player.blocked = 'no'
  85.             player.healed = 'no'
  86.            
  87.         #ход блокеров
  88.         player_count = 0
  89.         for player in players:
  90.             if player.alive == 'yes' and player.blocked == 'no' and player.moved == 'no':
  91.                 if player.mafia == 'no':
  92.                     if player.blocker == 'yes':
  93.                         block = 0
  94.                         n = 0
  95.                         while block == 0 or n > 1000:
  96.                             n += 1
  97.                             blocked = random.randint(0, number_of_players-1)
  98.                             if players[blocked].alive == 'yes' and blocked != player_count:
  99.                                 block = 1
  100.                         players[blocked].blocked = 'yes'
  101.  #                       players[player_count].moved = 'yes'
  102.                         print 'городской блокер %s блокирует игрока номер %s' % (player_count, blocked)
  103.                 elif player.mafia == 'yes':
  104.                     if player.blocker == 'yes':
  105.                         if mafia_count != 2:
  106.                             block = 0
  107.                             n = 0
  108.                             while block == 0 or n > 1000:
  109.                                 n += 1
  110.                                 blocked = random.randint(0, number_of_players-1)
  111.                                 if players[blocked].alive == 'yes' and players[blocked].mafia == 'no':
  112.                                     block = 1
  113.                             players[blocked].blocked = 'yes'
  114.                             if mafia_count != 1:
  115.                                 players[player_count].moved = 'yes'
  116.                             print 'маф блокер %s блокирует игрока номер %s' % (player_count, blocked)
  117.  
  118.             player_count += 1
  119.  
  120.  
  121.         #ход лекарей
  122.         player_count = 0
  123.         for player in players:
  124.             if player.alive == 'yes' and player.blocked == 'no' and player.moved == 'no':
  125.                 if player.mafia == 'no':
  126.                     if player.healer == 'yes':
  127.                         heal = 0
  128.                         n = 0
  129.                         while heal == 0 or n > 1000:
  130.                             n += 1
  131.                             healed = random.randint(0, number_of_players-1)
  132.                             if players[healed].alive == 'yes' and healed != player_count:
  133.                                 heal = 1
  134.                         players[healed].healed = 'yes'
  135. #                        players[player_count].moved = 'yes'
  136.                         print 'городской лекарь %s лечит игрока номер %s' % (player_count, healed)
  137.                 elif player.mafia == 'yes':
  138.                     if player.healer == 'yes':
  139.                         heal = 0
  140.                         n = 0
  141.                         while heal == 0 or n > 1000:
  142.                             n += 1
  143.                             healed = random.randint(0, number_of_players-1)
  144.                             if players[healed].alive == 'yes' and players[healed].mafia == 'yes':
  145.                                 heal = 1
  146.                         players[healed].healed = 'yes'
  147.                         players[player_count].moved = 'yes'
  148.                         print 'маф лекарь %s лечит игрока номер %s' % (player_count, healed)
  149.  
  150.             player_count += 1
  151.  
  152.         #ход маньяков
  153.         player_count = 0
  154.         for player in players:
  155.             if player.alive == 'yes' and player.blocked == 'no' and player.moved == 'no' and player.mafia == 'no' and player.can_kill == 'yes':
  156.                 kill = 0
  157.                 n = 0
  158.                 while kill == 0 or n > 1000:
  159.                     n += 1
  160.                     killed = random.randint(0, number_of_players-1)
  161.                     if players[killed].alive == 'yes' and killed != player_count:
  162.                         kill = 1
  163.                 if players[killed].defence == 'no' and players[killed].healed == 'no':
  164.                     killed_list.append(killed)
  165.                 elif players[killed].defence == 'yes':
  166.                     players[killed].defence = 'no'
  167. #                players[player_count].moved = 'yes'
  168.                 print "Номер убивающего маньяка(должен быть 5-6): %s, идет убивать: %s" % (player_count, killed)
  169.             player_count += 1
  170.  
  171.  
  172.         #ход оставшихся мафов
  173.         mafia_kill = 0
  174.         player_count = 0
  175.         for player in players:
  176.             if player.alive == 'yes' and player.blocked == 'no' and player.moved == 'no' and player.mafia == 'yes' and player.can_kill == 'yes':
  177.                 if mafia_kill == 0: # убивала ли уже мафия
  178.                     kill = 0
  179.                     n = 0
  180.                     while kill == 0 or n > 1000:
  181.                         n += 1
  182.                         killed = random.randint(0, number_of_players-1)
  183.                         if players[killed].alive == 'yes' and players[killed].mafia == 'no':
  184.                             kill = 1
  185.                     if players[killed].defence == 'no' and players[killed].healed =='no':
  186.                         killed_list.append(killed)
  187.                     elif players[killed].defence == 'yes':
  188.                         players[killed].defence = 'no'
  189.                     mafia_kill = 1
  190.                     players[player_count].moved = 'yes'
  191.                     print "Номер убивающего мафа(должен быть 1-4): %s, идет убивать: %s" % (player_count, killed)
  192.  
  193.             player_count += 1
  194.          
  195.  
  196.         print killed_list
  197.         print alive_list
  198.         for kill in killed_list:
  199.             players[kill].alive = 'no'
  200.  
  201.         end_alive_count = 0
  202.         for player in players:
  203.             if player.alive == 'yes':
  204.                 end_alive_count += 1
  205.         kill_count = alive_count - end_alive_count
  206.  
  207.         print "***"
  208.         print "Убийств ночью: %s" % kill_count
  209.         days_count += 1
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216. # This is the standard boilerplate that calls the main() function.
  217. if __name__ == '__main__':
  218.   main()
Advertisement
Add Comment
Please, Sign In to add comment