Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- class Combination(object):
- def __init__(self, combination_number=4, max_attemps=0):
- self.__combination = [randint(1,6) for i in range(combination_number)]
- self.max_attemps = max_attemps;
- self.attemps = [];
- self.tried = 0;
- self.surrended = False;
- def is_combination(self, comb):
- if self.surrended: return -1;
- if self.tried >= self.max_attemps and self.max_attemps > 0: return -2;
- if len(comb) != len(self.__combination): return -3;
- if any(i < 1 or i > 6 for i in comb): return -4;
- self.attemps.append(comb);
- self.tried+=1;
- info = self.__returnit(self.__correct(comb), self.__guessed(comb));
- return info if info[0]!=len(self.__combination) else True;
- def get_info(self):
- print("You tried {0}{1} times.".format(self.tried, '/'+str(self.max_attemps) if self.max_attemps else ''))
- for i in range(len(self.attemps)):
- 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]))
- def surrend(self):
- self.surrended = True;
- return self.show();
- def show(self):
- return self.__combination if self.surrended else 0;
- def __returnit(self, correct, guessed):
- return [correct, guessed-correct if guessed-correct > 0 else 0]
- def __correct(self, comb):
- return len([i for i in range(len(comb)) if comb[i]==self.__combination[i]])
- def __guessed(self, comb):
- new_comb, guess = comb+[], 0;
- for i in self.__combination:
- if i in new_comb:
- guess+=1
- new_comb.remove(i);
- return guess;
- def newAttempt(attempt, obj):
- response = obj.is_combination(attempt)
- if response == -1:
- print("Session is over, you already surrended!")
- return False
- elif response == -2:
- print("You don't have any more attempts. Let's surrend.")
- obj.surrend()
- return False
- elif response == -3:
- print("Your attempt's length isn't same as combination's length")
- return True
- elif response == -4:
- print("Your symbols must be numbers from 1 to 6")
- return True
- elif response==True:
- print("Good job! You solved it!")
- return False;
- print("{0} on right place and {1} on wrong".format(response[0], response[1]))
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement