-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathImposter.py
More file actions
237 lines (205 loc) · 7.5 KB
/
Imposter.py
File metadata and controls
237 lines (205 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from os import system, listdir
from platform import system as os
from random import sample, choice, randint
class Player:
playerCount = 0
currentWord = ''
playerList = []
def __init__(self, playerName: str):
self.name = playerName
self.isImposter = False
self.score = 0
self.playerId = Player.playerCount
Player.playerCount += 1
Player.playerList.append(self)
def __del__(self):
print(f"{self.name} has been deleted.")
def setImposter(self) -> None:
self.isImposter = True
return
def clearImposter(self) -> None:
self.isImposter = False
return
def reset(self) -> None:
self.isImposter = False
return
def getId(self) -> int:
return self.playerId
def getWord(self) -> str:
if self.isImposter:
return "Imposter"
else:
return Player.currentWord
def getName(self) -> str:
return self.name
@classmethod
def setWord(cls, newWord: str) -> None:
cls.currentWord = newWord
@classmethod
def getNameList(cls) -> list[str]:
returnList = []
for x in cls.playerList:
returnList.append(x.getName())
return returnList
@classmethod
def clearImposters(cls) -> None:
for player in cls.playerList:
player.clearImposter()
return
@classmethod
def clearPlayers(cls) -> None:
cls.playerList.clear()
def clearScreen() -> None:
if os() == 'Windows':
system('cls')
else:
system('clear')
def conjunction(nameList: list[str]) -> str:
if len(nameList) == 0:
return ''
elif len(nameList) == 1:
return nameList[0]
elif len(nameList) == 2:
return ' and '.join(nameList)
else:
return ', '.join(nameList[:-1]) + ', and ' + nameList[len(nameList) - 1]
def allImposter() -> bool:
for lines in open('settings.txt', 'r').read().split('\n'):
if lines == 'ALLIMPOSTERS = TRUE':
if randint(1, 100) == 100:
return True
return False
def main() -> None:
# Variables
inputPlayers = ''
imposterCount = 0
inputCount = ''
dictList = [x[:-4] for x in listdir('dictionaries') if x[-4:] == '.txt']
index = 0
indexSelector = ''
userIndex = 0
currentFile = ''
wordCount = 0
imposters = []
wordList = []
votedImposter = []
continueSame = True
continueNew = True
# Continue with new players loop
while continueNew:
# Get the player list
clearScreen()
inputPlayers = ''
print('Enter "!" when finished (minimum 2 players required).')
while True:
inputPlayers = input('Enter player name: ')
if inputPlayers == '!':
if len(Player.playerList) >= 2:
break
else:
print(f'You need at least 2 players! Currently have {len(Player.playerList)}.')
elif inputPlayers in Player.getNameList():
print('You cannot have repeat names')
elif inputPlayers != '':
Player(inputPlayers.strip())
# Continue with same players loop
continueSame = True
while continueSame:
# Get the imposter count
clearScreen()
imposterCount = -1
while imposterCount <= 0 or imposterCount > len(Player.playerList) / 2:
try:
imposterCount = int(input(f'How many imposters do you want (player count: {len(Player.playerList)}): '))
if imposterCount <= 0:
print('You need more than 0 imposters!')
if imposterCount > len(Player.playerList) / 2:
print('You need less than half the player count as imposter!')
except:
print('Not a valid number!')
# Get the category
clearScreen()
index = 0
indexSelector = ''
userIndex = -1
print('Select a category.')
for x in dictList:
wordCount = len(open('dictionaries/' + x + '.txt', 'r').read().split('\n'))
print(f" {index}) {x} ({wordCount})")
index += 1
while userIndex <= -1 or userIndex >= index:
indexSelector = input(' >> ')
try:
userIndex = int(indexSelector)
except:
userIndex = -1
currentFile = dictList[userIndex]
# Select the word
clearScreen()
wordList = open('dictionaries/' + currentFile + '.txt', 'r').read().split('\n')
Player.setWord(choice(wordList))
# Set the imposters
if allImposter():
for x in Player.playerList:
x.setImposter()
else:
for x in sample(Player.playerList, imposterCount):
x.setImposter()
# Give players the word
for x in Player.playerList:
clearScreen()
print(x.getName())
input('Press enter to reveal the word...')
print(x.getWord())
input('Press enter continue...')
# Select first player
clearScreen()
print(f'{choice(Player.playerList).getName()} goes first.')
input('Press Enter to Vote')
# Vote Imposter
votedImposter = []
for x in range(imposterCount):
clearScreen()
index = 0
indexSelector = ''
userIndex = -1
remainingImposters = [y for y in Player.getNameList() if y not in votedImposter]
print('Vote who is imposter.')
for y in remainingImposters:
print(f" {index}) {y}")
index += 1
while userIndex <= -1 or userIndex >= index:
indexSelector = input(' >> ')
try:
userIndex = int(indexSelector)
except:
userIndex = -1
votedImposter.append(remainingImposters[userIndex])
# Reveal imposter
clearScreen()
input(f'{conjunction(votedImposter)} {"was" if imposterCount == 1 else "were"} voted imposter...')
input(f'{conjunction([x.getName() for x in Player.playerList if x.isImposter])}, what is the word? ')
# Ask user if they want to continue
clearScreen()
indexSelector = ''
userIndex = -1
print('Select an option.')
print(" 0) Continue with New Players")
print(" 1) Continue with Same Players")
print(" 2) Exit")
while userIndex <= -1 or userIndex >= 3:
indexSelector = input(' >> ')
try:
userIndex = int(indexSelector)
except:
userIndex = -1
if userIndex == 0:
Player.clearPlayers()
continueSame = False
elif userIndex == 1:
Player.clearImposters()
elif userIndex == 2:
continueSame = False
continueNew = False
if __name__ == "__main__":
main()