-
Notifications
You must be signed in to change notification settings - Fork 0
/
1a2bv3.py
111 lines (97 loc) · 3.33 KB
/
1a2bv3.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
import random
#
def compare(l1, l2): # compare random list and user's input
a = 0 # number and position both OK
b = 0 # number OK but position not
c = 0 # numer and position neither not OK
for i in xrange(len(l1)):
if l1[i] in l2: # check number
if l1[i] == l2[i]: # check position
a = a + 1 # N and P both OK
else:
b = b + 1 # N ok but P not
else:
c = c + 1 # neither N and P OK
return [a, b, c] # return list[a,b,c] and each element is integer
def Normalizelist(l1):
for i in xrange(len(l1)):
if l1[i].isdigit() and len(l1) == 4 and len(set(l1)) == 4:
l1[i] = int(l1[i])
else:
print 'fuck, you should type 4 different number'
return None
return l1
def userinput(): # save user input and check its type must be 4 number
state = True
while state:
l1 = list(raw_input("Make a Guass (input 4 different number): "))
l1 = Normalizelist(l1)
if l1 is None:
state = True
else:
return l1
def Guserinput():
state = True
while state:
l1 = list(raw_input("Input 4 different number to make computer gauss: (input 4 different number): "))
l1 = Normalizelist(l1)
if l1 is None:
state = True
else:
return l1
def modechecker(mode):
while not mode.isdigit():
mode = raw_input('Pleas input an integer, try again')
mode = int(mode)
return mode
def randtobegaussed(): # generate random 4 numbers
number_set = []
for i in xrange(0, 10, 1):
number_set.insert(i, i)
l2 = random.sample(number_set, 4)
return l2
def brtul():
import itertools
tot = [x for x in range(10)]
rand = random.sample(list(itertools.permutations(tot, 4)), 5040)
for i in range(len(rand)):
rand[i] = list(rand[i])
return rand
mode = raw_input('Chose mode: 1.player guass 2.computer gauss (type an integer and press Enter)')
mode = modechecker(mode)
if mode == 1:
state = True # define game on/off state
trail = 0 # count trail times
listTBG = randtobegaussed()
while state:
# user input and turn it into int.
l1 = userinput()
resul = compare(l1, listTBG) # compare 2 set
trail = trail + 1
print '%dA %dB %dC ,try %d times' % (resul[0], resul[1], resul[2], trail)
if resul[0] == 4:
state = False
print 'Congrate! you find it!'
elif mode == 2:
mode = raw_input('Chose mode: 1.brtual force (random gauss) 2.others (type an integer and press Enter)')
mode = modechecker(mode)
if mode == 1:
listTBG = Guserinput()
trail = 0
l = brtul()
for i in range(len(l)):
resul = compare(listTBG, l[i])
if resul[0] == 4:
trail = trail + 1
print 'Finally find it! the answer is ' + str(l[i]) + ' ,and try %d times' % (trail)
break
else:
trail = trail + 1
print str(l[i]) + ', %dA %dB %dC ,try %d times' % (resul[0], resul[1], resul[2], trail)
elif mode == 2:
print 'Not complete yet, end of the program.'
else:
print 'Not define, end of the program.'
pass
else:
print 'Not define, end of the program.'