Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #python 2.7.8
- from random import randint, shuffle
- class MafiaSim:
- def __init__(self, mafiaCount, citizenCount, startLynchRound):
- self._mafiaCount = mafiaCount
- self._citizenCount = citizenCount
- self._playerCount = mafiaCount + citizenCount
- self._startLynchRound = startLynchRound;
- self._wins, self._loses, self._draws = 0, 0, 0
- def printResults(self):
- print "Wins: {0} \nLoses: {1} \nDraws: {2} \nLynching Started At Round: {3}\n".format(self._wins, self._loses, self._draws, self._startLynchRound)
- def playTimes(self, times):
- self._wins, self._loses, self._draws = 0, 0, 0
- for i in range(times):
- result = self.playAGame()
- if result == 'mafia win':
- self._loses += 1
- elif result == 'citizens win':
- self._wins += 1
- else:
- self._draws += 1
- def playAGame(self):
- votes = [0]*self._playerCount
- rMafiaCount, rCitizenCount, rPlayerCount = self._mafiaCount, self._citizenCount, self._playerCount
- phase = 'night' #game starts night phase
- turn = 0
- while 1:
- if phase == 'night':
- rCitizenCount -= 1
- phase = 'day'
- turn += 1 # 1 turn counted every night
- elif phase == 'day' and turn >= self._startLynchRound:
- for i in range(rMafiaCount):
- votes[randint(0, rCitizenCount)] += 1
- for i in range(rPlayerCount):
- temp = range(0,i)+range(i+1, rPlayerCount)
- shuffle(temp)
- votes[temp[0]] += 1
- if votes.index(max(votes)) < rCitizenCount:
- rCitizenCount -= 1
- else:
- rMafiaCount -= 1
- elif phase == 'day':
- phase = 'night'
- if rMafiaCount > rCitizenCount:
- return 'mafia win'
- if rMafiaCount == 0:
- return 'citizens win'
- votes = [0]*self._playerCount
- rPlayerCount = rMafiaCount + rCitizenCount
- return 'draw' # never reaches
- if __name__ == "__main__":
- for startLynchRound in range(1, 10): #from 1, to 10
- sim = MafiaSim(3, 10, startLynchRound) #3 mafia, 10 citizens/thugs..etc
- sim.playTimes(10000)
- sim.printResults()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement