Advertisement
Mr-A

Mafia Sim

Jan 7th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. #python 2.7.8
  4. from random import randint, shuffle
  5.  
  6. class MafiaSim:
  7.     def __init__(self, mafiaCount, citizenCount, startLynchRound):
  8.         self._mafiaCount = mafiaCount
  9.         self._citizenCount = citizenCount
  10.         self._playerCount = mafiaCount + citizenCount
  11.         self._startLynchRound = startLynchRound;
  12.         self._wins, self._loses, self._draws = 0, 0, 0
  13.        
  14.     def printResults(self):
  15.         print "Wins: {0} \nLoses: {1} \nDraws: {2} \nLynching Started At Round: {3}\n".format(self._wins, self._loses, self._draws, self._startLynchRound)
  16.    
  17.     def playTimes(self, times):
  18.         self._wins, self._loses, self._draws = 0, 0, 0
  19.         for i in range(times):
  20.             result = self.playAGame()
  21.             if result == 'mafia win':
  22.                 self._loses += 1
  23.             elif result == 'citizens win':
  24.                 self._wins += 1
  25.             else:
  26.                 self._draws += 1
  27.            
  28.        
  29.     def playAGame(self):
  30.         votes = [0]*self._playerCount
  31.         rMafiaCount, rCitizenCount, rPlayerCount = self._mafiaCount, self._citizenCount, self._playerCount
  32.         phase = 'night' #game starts night phase
  33.         turn = 0
  34.         while 1:
  35.             if phase == 'night':
  36.                 rCitizenCount -= 1
  37.                 phase = 'day'
  38.                 turn += 1 # 1 turn counted every night
  39.                
  40.             elif phase == 'day' and turn >= self._startLynchRound:
  41.                 for i in range(rMafiaCount):
  42.                     votes[randint(0, rCitizenCount)] += 1
  43.                 for i in range(rPlayerCount):
  44.                     temp = range(0,i)+range(i+1, rPlayerCount)
  45.                     shuffle(temp)
  46.                     votes[temp[0]] += 1
  47.                 if votes.index(max(votes)) < rCitizenCount:
  48.                     rCitizenCount -= 1
  49.                 else:
  50.                     rMafiaCount -= 1
  51.             elif phase == 'day':
  52.                 phase = 'night'
  53.                
  54.             if rMafiaCount > rCitizenCount:
  55.                 return 'mafia win'
  56.             if rMafiaCount == 0:
  57.                 return 'citizens win'
  58.             votes = [0]*self._playerCount
  59.             rPlayerCount = rMafiaCount + rCitizenCount
  60.         return 'draw' # never reaches
  61.  
  62. if __name__ == "__main__":
  63.     for startLynchRound in range(1, 10): #from 1, to 10
  64.         sim = MafiaSim(3, 10, startLynchRound) #3 mafia, 10 citizens/thugs..etc
  65.         sim.playTimes(10000)
  66.         sim.printResults()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement