@Corsaka/

Basic_Challenges

Python

No description

fork
loading
Files
  • main.py
  • hwords.py
main.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#imports
from time import sleep
import random

def clear():
  print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") #clears console

clear()

#replay function
def goAgain(func):
  while True:
    if func == "dice": #special case for dice
      again = input("Different dice values?\nYes\nReturn to Menu\n").lower()
    else: #general case for everything else
      again = input("Go again?\nYes\nReturn to Menu\n").lower()
    if again == "yes":
      if func == "dice":
        dice()
      elif func == "mastermind":
        mastermind()
      elif func == "hangman":
        hangman()
      elif func == "adventure":
        #adventure()
        pass
      else:
        pass
    elif again == "return" or again == "return to menu" or again == "menu":
      break #breaks the while loop, running clear() and main()
    else:
      print("A valid input really isn't that hard.")
      continue #forces a while loop restart
  clear()
  main()

#number exception catch
def isNumber(value):
  while True:
    try:
      int(value) #attempts to turn value into a number
    except ValueError: #catches exception if above can't run
      print("A number would be beneficial.")
      continue
    else:
      break

#start dice
def dice():
  sleep(0.5)
  while True:
    strsides = input("Number of sides? (2-120)\n") #gets number of sides
    isNumber(strsides)
    sides = int(strsides)
    if sides > 120: #limits dice sides
      print("I can't read a " + sides + " sided die!")
      continue
    elif sides < 1: #ensures dice exists
      print("How does this die even exist?")
      continue
    elif sides == 1: #gets mad
      print("You can't roll a 1-sided die: The result would be the same every time.")
      continue
    else:
      break
  
  while True:
    strdice = input("Number of dice? (1-1000)\n")
    isNumber(strdice) #checks if it's a number
    dice = int(strdice)
    if dice > 1000: #limits dice number
      print("Too many dice!")
      continue
    elif dice < 1: #ensures there is a die
      print("You can't have less than one die...")
      continue
    else:
      break

  sleep(0.5)
  dicesides = sides * dice
  result = str(random.randrange(dice,dicesides))
  
  def roll():
    print("Rolling " + str(dice) + "d" + str(sides), end = '',flush=True) #prints the roll
    sleep(0.5)
    for i in range(1,3): #adds 3 dots
      print(".",end='',flush=True)
      sleep(0.5)
    print("Your result is " + result + "!") #prints result
  
  roll()
  sleep(1)

  while True:
    reroll = input("Roll again?\nYes\nNo").lower() #asks for reroll
    if reroll == "yes":
      roll() #rolls again
    elif reroll == "no":
      break #runs goAgain()
    else:
      print("Wanna try that again?") #ensures valid input
      continue
  
  goAgain("dice")
#end dice

#start mastermind
def mastermind():
  sleep(0.5)
  print("Welcome to mastermind.")
  sleep(1)
  while True:
    mTutorial = input("Do you want a tutorial?\nYes\nNo\n").lower() #requests tutorial
    if mTutorial == "yes":
      print("Mastermind is a very simple game.")
      sleep(1)
      print("The computer will pick four colours. You won't know what they are.")
      sleep(1)
      print("Once their selection is made, you can guess four colours.")
      sleep(1)
      print("The computer will tell you how many are correct and how many are wrong.")
      sleep(1)
      print("You need to get the exact order to guess the answer.")
      sleep(1)
      print("Duplicate colours are impossible!")
      sleep(1)
      print("Be careful! you only have 12 turns.")
      break
    elif mTutorial == "no":
      break #skips code
    else:
      print("There are two options. This is not one of them. Try again.")
      continue #ensures input

  colourList = ["red", "blue", "green", "yellow", "orange", "purple", "white", "black", "grey"] #sets up a list to shuffle
  printedColourList = ["red", "blue", "green", "yellow", "orange", "purple", "white", "black", "grey"] #sets up a list to print
  random.shuffle(colourList) #shuffles the list
  #assigns colours to shuffled list
  colourA = colourList[0]
  colourB = colourList[1]
  colourC = colourList[2]
  colourD = colourList[3]

  def question():
    sleep(1)
    if mastermindRound == 1: #first round case
      print("You'll be given 4 inputs. Answer with a colour from the incoming list for each input in the order you believe is correct. If you want to reset your inputs, type 'reset'.")
      sleep(3)

    #following four functions are nearly identical
    #only one will be commented
    #along with the reset function
    
    def mRoundaFunc():
      while True:
        global mRounda #allows value to be checked outside the function
        mRounda = input("First colour?\n")
        if mRounda not in colourList: #checks if input is a colour
          print("Please type an option that exists. (You can't reset on the first colour)")
          continue
        else:
          mRoundbFunc() #runs next guess
          break
    
    def mRoundbFunc():
      while True:
        global mRoundb
        mRoundb = input("Second colour?\n")
        if mRoundb == "reset": #checks for reset input
          print("Inputs reset.")
          mRoundaFunc() #starts from first guess
          break #stops while loop from re-activating post-reset
        elif mRoundb not in colourList:
          print("Please type an option that exists.")
          continue
        else:
          mRoundcFunc()
          break
    
    def mRoundcFunc():
      while True:
        global mRoundc
        mRoundc = input("Third colour?\n")
        if mRoundc == "reset":
          print("Inputs reset.")
          mRoundaFunc()
          break
        elif mRoundc not in colourList:
          print("Please type an option that exists.")
          continue
        else:
          mRounddFunc()
          break
    
    def mRounddFunc():
      while True:
        global mRoundd
        mRoundd = input("Fourth colour?\n")
        if mRoundd == "reset":
          print("Inputs reset.")
          mRoundaFunc()
          break
        elif mRoundd not in colourList:
          print("Please type an option that exists.")
          continue
        else:
          break

    mRoundaFunc() #runs first guess function

  def answer():
    position = 0
    colour = 0
    none = 0

    #big check for results
    if mRounda == colourA:
      position += 1
    elif mRounda == (colourB or colourC or colourD):
      colour += 1
    else:
      none += 1
    if mRoundb == colourB:
      position += 1
    elif mRoundb == (colourA or colourC or colourD):
      colour += 1
    else:
      none += 1
    if mRoundc == colourC:
      position += 1
    elif mRoundc == (colourA or colourB or colourD):
      colour += 1
    else:
      none += 1
    if mRoundd == colourD:
      position += 1
    elif mRoundd == (colourA or colourB or colourC):
      colour += 1
    else:
      none += 1
    #end big check for results

    if position == 4: #checks for victory
      print("You win! The answer was: " + colourA + ", " + colourB + ", " + colourC + ", " + colourD) #prints answer
      sleep(1)
      goAgain("mastermind") #repeats game
    else:
      print("Correct colour/position: " + str(position)) #prints correct position value
      sleep(1)
      print("Correct colour, wrong position: " + str(colour)) #prints correct colour value
      sleep(1)
      print("Wrong: " + str(none)) #prints incorrect value

  for mastermindRound in range(1,13): #rounds 1-12, 13 is loss
    if mastermindRound == 13: #loss condition
      print("You lose! The answer was: " + colourA + ", " + colourB + ", " + colourC + ", " + colourD) #prints answer
    elif mastermindRound == 12: #checks for final round
      print("Final round!")
    elif mastermindRound % 3 == 1: #checks for round being 1, 4, 7, or 10
      print("Possible colours:")
      sleep(1)
      print(printedColourList) #prints list of colours
    else:
      print("Round " + str(mastermindRound)) #prints current round
    if not mastermindRound == 13: #checks for round being 1-12
      question() #takes 4 guesses
      answer() #reveals answers for 4 guesses
    else:
      goAgain("mastermind")
#end mastermind

#start hangman
def hangman():
  print("Welcome to hangman.")
  sleep(1)
  while True:
    hTutorial = input("Would you like a tutorial?\n").lower()
    if hTutorial == "yes":
      print("You guess a letter until you get the word.")
      sleep(1)
      print("If you guess too many incorrect letters, you lose - this is represented by a man being hung, and each of his parts being introduced.")
      sleep(4)
      print("You can also guess a word if you're certain of it - else, it will simply claim that you haven't inputted a letter.")
      sleep(3)
      break
    elif hTutorial == "no":
      break
    else:
      print("Does your family have a history of lacking brain cells? It's a yes/no question.")
      sleep(1)
      continue
  
  import hwords #imports hwords.py
  lists = {4 : hwords.list4, 5 : hwords.list5, 6 : hwords.list6, 7 : hwords.list7, 8 : hwords.list8} #gets the lists from hwords.py

  while True:
    iLetters = input("How many letters? (4-8)\n")
    isNumber(iLetters) #ensures value is a number
    hLetters = int(iLetters)
    if hLetters >= 4 and hLetters <= 8:
      hWord = random.choice(lists[hLetters]) #sets a word from a random choice in the lists dictionary, based on input
      break
    else:
      print("Are numbers hard? 4 to 8.")
      continue
  
  hwLetters = list(hWord) #splits the word into letters

  stage = {1 : "\n|\n|\n|\n|\n|\n|\n",2 : " ______\n|\n|\n|\n|\n|\n|\n",3 : " ______\n|         |\n|\n|\n|\n|\n|\n",4 : " ______\n|         |\n|         0\n|\n|\n|\n|\n",5 : " ______\n|         |\n|         0\n|        /\n|\n|\n|\n",6 : " ______\n|         |\n|         0\n|        /|\n|\n|\n|\n",7 : " ______\n|         |\n|         0\n|        /|\\n|\n|\n|\n",8 : " ______\n|         |\n|         0\n|        /|\\n|        /\n|\n|\n",9 : " ______\n|         |\n|         0\n|        /|\\n|        / \\n|\n|\n"} #dictionary of hangman poses
  
  guessSoFar = [] #list with - and letters guessed
  joinedGuess = None #placeholder for when it is updated later

  for letter in hWord:
    guessSoFar.append("-") #original set of dashes, representing word length
  
  guesses = [] #list of letters already guessed
  
  hStage = 0
  while True:
    if hStage != 0:
      print(stage[hStage]) #prints the hangman stage
    else:
      pass
    joinedGuess = "".join(guessSoFar) #joins the list into a single string
    print(joinedGuess)

    if "-" not in guessSoFar:
      print("Congratulations!")
      sleep(0.5)
      break
    else:
      guess = input("Make a guess!\n").lower()

    if not guess.isalpha(): #checks if input is in alphabet
      print("That's not a proper guess...")
      continue
    elif guess == hWord: #check if input == word
      print("Congratulations!")
      sleep(0.5)
      break
    elif len(guess) != 1: #checks if input is a letter
      print("That's not a letter...")
      continue
    elif guess in guesses: #checks if they're already guessed the letter
      print("You've already guessed " + guess + "!")
      continue
    elif guess in hwLetters:
      print(guess + " was correct!")
      guesses.append(guess) #appends letter to guessed-list
      for idx, letter in enumerate(hWord):
        if guess == letter:
          guessSoFar[idx] = guess # replaces a dash in the list with the letter guessed
      continue
    else: #
      hStage += 1
      print("Incorrect!\n")
      if hStage == 9:
        print("You lose! The word was '" + hWord + "'.")
        sleep(0.5)
        break
      else:
        continue
  goAgain("hangman")
#end hangman

#start adventure

#end adventure

#select a game
def main():
  print("Pick an option:")
  sleep(1)
  a = 1
  while a == 1:
    pick = input("Dice\nMastermind\nHangman\nAdventure\n").lower()
    if pick == "dice":
      a = 0
      print("Running program: ", end='',flush=True)
      sleep(0.5)
      print("Dice...",flush=True)
      sleep(0.5)
      dice()
    elif pick == "mastermind":
      a = 0
      print("Running program: ", end='',flush=True)
      sleep(0.5)
      print("Mastermind...",flush=True)
      sleep(0.5)
      mastermind()
    elif pick == "hangman":
      a = 0
      print("Running program: ", end='',flush=True)
      sleep(0.5)
      print("Hangman...",flush=True)
      hangman()
    elif pick == "adventure":
      a = 0
      print("Running program: ", end='',flush=True)
      sleep(0.5)
      print("Adventure...",flush=True)
      sleep(0.5)
      # adventure()
    else:
      print("Trying would be useful. I'd recommend doing that.")

main()