Advertisement
DecaK

Untitled

Jul 7th, 2018
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. from random import randint
  2.  
  3. class Combination(object):
  4. def __init__(self, combination_number=4, max_attemps=0):
  5. self.__combination = [randint(1,6) for i in range(combination_number)]
  6. self.max_attemps = max_attemps;
  7. self.attemps = [];
  8. self.tried = 0;
  9. self.surrended = False;
  10. def is_combination(self, comb):
  11. if self.surrended: return -1;
  12. if self.tried >= self.max_attemps and self.max_attemps > 0: return -2;
  13. if len(comb) != len(self.__combination): return -3;
  14. if any(i < 1 or i > 6 for i in comb): return -4;
  15. self.attemps.append(comb);
  16. self.tried+=1;
  17. info = self.__returnit(self.__correct(comb), self.__guessed(comb));
  18. return info if info[0]!=len(self.__combination) else True;
  19. def get_info(self):
  20. print("You tried {0}{1} times.".format(self.tried, '/'+str(self.max_attemps) if self.max_attemps else ''))
  21. for i in range(len(self.attemps)):
  22. print("{0}{1} try: {2}".format(i+1, "st" if not i else "nd" if i==1 else "rd" if i==2 else "th", self.attemps[i]))
  23. def surrend(self):
  24. self.surrended = True;
  25. print(self.__combination)
  26. return self.show();
  27. def show(self):
  28. return self.__combination if self.surrended else 0;
  29. def __returnit(self, correct, guessed):
  30. return [correct, guessed-correct if guessed-correct > 0 else 0]
  31. def __correct(self, comb):
  32. return len([i for i in range(len(comb)) if comb[i]==self.__combination[i]])
  33. def __guessed(self, comb):
  34. new_comb, guess = comb+[], 0;
  35. for i in self.__combination:
  36. if i in new_comb:
  37. guess+=1
  38. new_comb.remove(i);
  39. return guess;
  40.  
  41. # newAttempt function just demonstrates what bad output means(return from -1 to -4), it won't be a part of program
  42. def newAttempt(attempt, obj):
  43. if attempt == [0]:
  44. obj.surrend();
  45. return False;
  46. response = obj.is_combination(attempt)
  47. if response == -1:
  48. print("Session is over, you already surrended!")
  49. return False
  50. elif response == -2:
  51. print("You don't have any more attempts. Let's surrend.")
  52. obj.surrend()
  53. return False
  54. elif response == -3:
  55. print("Your attempt's length isn't same as combination's length")
  56. return True
  57. elif response == -4:
  58. print("Your symbols must be numbers from 1 to 6")
  59. return True
  60. elif response:
  61. print("Good job! You solved it!")
  62. return False;
  63. print("{0} on right place and {1} on wrong".format(response[0], response[1]))
  64. return True
  65.  
  66. b = Combination(4, 10)
  67. while newAttempt([int(i) for i in input("Enter combination(or 0 for surrend): ") if i.isdigit()], b):
  68. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement