-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPythonHomework#11.py
238 lines (194 loc) · 5.61 KB
/
PythonHomework#11.py
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
import sys, os, time, platform, random
import urllib.request, urllib.error
guessed_letters = set([])
word = ""
remaining_letters = 100
guess = ""
wrong_guess_cnt = 0
class Player:
def __init__(self,name = "name", game_role = ""):
self.name = name
self.game_role = game_role
def screenClear():
if platform.system() == "Windows":
os.system("cls")
else:
os.system("clear")
def draw_gallows_map(wrong_guess_cnt):
map = [
[3,3,3,3,3,3,0,0],
[2,0,0,0,0,4,0,0],
[2,0,0,0,0,5,0,0],
[2,0,0,0,6,7,8,0],
[2,0,0,9,0,10,0,11],
[2,0,0,0,12,0,13,0],
[2,0,0,14,0,0,0,15],
[2,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1]
]
build_seq = {
0:" ",
1:"=",
2:"|",
3:"-",
4:"|",
5:"O",
6:"/",
7:"|",
8:"\ ",
9:"|",
10:"|",
11:"|",
12:"/",
13:"\ ",
14:"|",
15:"|"
}
sequence = wrong_guess_cnt
#build gallow and hangman
if len(build_seq) == wrong_guess_cnt:
print("YOU LOSE")
print("the word was:", word)
exit()
for y in map:
for x in y:
if x <= sequence:
print(build_seq[x],end="")
print()
Player1 = Player()
Player2 = Player()
def define_player_names():
print(
"""
1. single player
or
2. two player game
""")
choice = input(">>>: ")
if choice == "1":
Player1.name = input("Enter your name: ")
Player1.game_role = "hangman"
single_player_game()
elif choice == "2":
print("Set players and roles")
print("---------------------")
Player1.name = input("Player 1 name: ")
Player2.name = input("Player 2 name: ")
define_player_roles()
else:
print("invalid choice, try again.")
define_player_names()
def define_player_roles():
print("---------------------")
print("Executioner ","1:",Player1.name,"2:",Player2.name)
choice = str(input(">>>: "))
if choice == "1":
Player1.game_role = "executioner"
Player2.game_role = "hangman"
elif choice == "2":
Player2.game_role = "executioner"
Player1.game_role = "hangman"
else:
print("invalid choice, try again ")
define_player_roles()
two_player_game()
def draw_guess_row(word,guessed_letters):
screenClear()
global remaining_letters
size = len(word)
remaining_letters = size
letters = [] # word to be guessed entered by executioner
print()
for i in word:
letters.append(i)
#replace letters that are not guessed by "_"
displayed_letters = letters
for i in displayed_letters:
if i in guessed_letters:
print(i +" ",end = "")
remaining_letters -= 1
else:
print("_ ", end = "")
print()
print("Already used letters:", sorted(guessed_letters))
def two_player_game():
global word
if Player1.game_role == "executioner":
executioner = Player1.name
else:
executioner = Player2.name
print(executioner,"enter your word: ")
word = input(">>>: ").upper()
print(word)
time.sleep(1)
screenClear()
play()
def single_player_game():
global word
try:
url = "https://raw.githubusercontent.com/nsspsup/pirpleClass_nspssup/master/wordlist.txt"
print("wordlist download started!")
filename, headers = urllib.request.urlretrieve(url,filename="wordlist.txt")
print("wordlist download complete!")
with open("wordlist.txt","r") as mywords:
lines = mywords.readlines()
randline = random.choice(lines)
word = randline.strip("\n").upper()
#print(word)
play()
except urllib.error.URLError as e:
print(e.reason)
print()
print("""
Unable to download wordlist.
If your internet connection works fine other reasons can be connection over proxy or VPN
continue in 2 player mode? Y/N
""")
choice = input(">>>:").lower()
if choice == "y":
define_player_names()
elif choice == "n":
print("exiting game...")
else:
print("invalid choice, try again")
single_player_game()
except Exception as e:
print(e)
def make_guess():
global guess
print()
if Player1.game_role == "hangman":
print(f"{Player1.name} make a guess")
guess = input(">>>: ").upper()
if guess in guessed_letters:
print("letter already used, choose again")
make_guess()
elif guess == "":
print("No letter was typed, please try again")
make_guess()
elif len(guess) > 1:
print("Enter only 1 letter per guess, try again")
make_guess()
else:
guessed_letters.add(guess)
guessed_letters.add(guess)
elif Player2.game_role == "hangman":
print(f"{Player2.name} make a guess")
guess = input(">>>: ").upper()
if guess in guessed_letters:
print("letter already used, choose again")
make_guess()
else:
guessed_letters.add(guess)
def play():
global wrong_guess_cnt
while remaining_letters > 0:
remaining_letters_antes = remaining_letters
make_guess()
draw_guess_row(word, guessed_letters)
if remaining_letters_antes == remaining_letters:
wrong_guess_cnt += 1
elif remaining_letters == 0:
print(" YOU WIN ")
draw_gallows_map(wrong_guess_cnt)
define_player_names()