KYO297

numble.py

May 20th, 2022
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. import random
  2.  
  3.  
  4. def passcode():
  5.     if passcode.counter != 0:
  6.         return passcode.code
  7.     length = random.randint(1, 6)
  8.     code = []
  9.     while len(code) < length:
  10.         digit = random.randint(0, 9)
  11.         if digit not in code:
  12.             code.append(digit)
  13.     passcode.counter += 1
  14.     passcode.code = code
  15.     return passcode.code
  16.  
  17.  
  18. def int_out(t1):
  19.     l1 = [int(a) for a in list(t1)]
  20.     trafienia = 0
  21.     dopasowania = 0
  22.     if int_checker(l1) == 0:
  23.         return "cyfra się powtarza"
  24.     if int_checker(l1) == 1:
  25.         return "odpowiedź za długa (>6 cyfr)"
  26.     for a in l1:
  27.         if a in passcode():
  28.             if l1.index(a) == passcode().index(a):
  29.                 trafienia += 1
  30.                 hint2_mem(a, passcode().index(a))
  31.             else:
  32.                 dopasowania += 1
  33.     if trafienia == len(passcode()):
  34.         wyn.score += 1
  35.         return "win"
  36.     else:
  37.         wyn.score += 1
  38.         return "trafienia: " + str(trafienia) + "\n" + "dopasowania: " + str(dopasowania)
  39.  
  40.  
  41. def int_checker(l1):
  42.     if len(set(l1)) != len(l1):
  43.         return 0
  44.     elif len(set(l1)) > 6:
  45.         return 1
  46.     else:
  47.         return 2
  48.  
  49.  
  50. def str_out(t1):
  51.     if t1 == "w":
  52.         return "aktualny wynik: " + str(wyn.score)
  53.     elif t1 == "h":
  54.         if str_out.counter == 0:
  55.             str_out.counter += 1
  56.             return hint1()
  57.         elif str_out.counter == 1:
  58.             str_out.counter += 1
  59.             return hint2()
  60.         else:
  61.             return hint3()
  62.     else:
  63.         return "nieznana komenda"
  64.  
  65.  
  66. def hint1():
  67.     return "długość hasła: " + str(len(passcode()))
  68.  
  69.  
  70. def hint2_mem(num, ind):
  71.     hint2_mem.out[ind] = num
  72.  
  73.  
  74. def hint2():
  75.     t1 = ""
  76.     for a in hint2_mem.out:
  77.         t1 += str(a)
  78.     return "hasło: " + t1
  79.  
  80.  
  81. def hint3():
  82.     prompt1 = str(input("czy chcesz powtórzyć podpowiedź? (t/n) "))
  83.     if prompt1 == "t":
  84.         prompt2 = input("którą? (1/2) ")
  85.         if prompt2 == "1":
  86.             wyn.score += 1
  87.             return hint1()
  88.         elif prompt2 == "2":
  89.             wyn.score += 5
  90.             return hint2()
  91.         else:
  92.             return "nieznana komenda"
  93.     elif prompt1 == "n":
  94.         return "\r"
  95.     else:
  96.         return "nieznana komenda"
  97.  
  98.  
  99. def inp():
  100.     t1 = input(":")
  101.     if inp_type(t1) == 1:
  102.         return int_out(t1)
  103.     else:
  104.         return str_out(t1)
  105.  
  106.  
  107. def inp_type(t1):
  108.     try:
  109.         int(t1)
  110.         return 1
  111.     except:
  112.         return 0
  113.  
  114.  
  115. def wyn(x):
  116.     wyn.score += x
  117.  
  118.  
  119. passcode.counter = 0
  120. str_out.counter = 0
  121. wyn.score = 0
  122. hint2_mem.out = ["*" for _ in passcode()]
  123. print("Istrukcja:\n\nCelem gry jest odgadnięcie wygenerowanego hasła (1-6 różnych cyfr), z jak najniższym wynikiem\n\nGra polega na wpisywaniu porpozycji, a program odpowie, ile cyfr z propozycji znajduje się na poprawnej pozycji (trafienia), a ile gdzieś indziej w haśle (dopasowania).\n\nkomendy:\nw - podaje aktualny wynik\nh - daje podpowiedź\n    po raz pierwszy podaje długość hasła\n    po raz drugi podaje aktualnie znalezione cyfry\n    po raz kolejny można powtórzyć podpowiedź 1 lub 2 kosztem odpowiednio 1 i 5 punktów")
  124. while True:
  125.     ot = inp()
  126.     print(ot)
  127.     if ot == "win":
  128.         break
  129. print("wynik: " + str(wyn.score))
  130.  
Advertisement
Add Comment
Please, Sign In to add comment