石头剪刀布小游戏的没有界面的简单实现。

[代码] [Python]代码

view source
print ?
01 #!/usr/bin/python
02  
03 import random
04  
05  
06 guess_dict = {'stone' 0'scissors' 1'cloth' 2}
07 guess_list = ['stone''scissors''cloth']
08  
09 def compare(x, y):
10         if == y:
11             return 0
12  
13         if == guess_list[0and == guess_list[1]:
14             return 1
15  
16         if == guess_list[0and == guess_list[2]:
17             return -1
18  
19         if == guess_list[1and == guess_list[0]:
20             return -1
21  
22         if == guess_list[1and == guess_list[2]:
23             return 1
24  
25         if == guess_list[2and == guess_list[0]:
26             return 1
27  
28         if == guess_list[2and == guess_list[1]:
29             return -1
30  
31  
32 def genPriorList():
33         rz = []
34         for in guess_list:
35             res = []
36             for in guess_list:
37                 res.append(compare(x, y))
38             rz.append(res)
39         return rz
40                  
41  
42 if __name__ ==  '__main__':
43      
44     info = 'please input: (stone, scissors, cloth) or stop to exit:\n'
45     priorList = []
46     priorList = genPriorList()
47  
48     print '\n--------------------------guess begin----------------------------------\n'
49     while True:
50         computer_guess = random.choice(guess_list)
51         people_guess = raw_input(info)
52  
53         if people_guess == 'stop':
54             break
55  
56         if not people_guess in guess_list :
57             print 'input error'
58         else:
59             = guess_dict[computer_guess]
60             = guess_dict[people_guess]
61             res = priorList[m][n]
62             if res == 0:
63                 print ('computer guess is : [%s] \npeople guess is : [%s] \nnone win!!!\n'
64                 % (computer_guess, people_guess))
65             elif res == 1:
66                 print ('computer guess is : [%s] \npeople guess is : [%s] \ncomputer win!!!\n'
67                 % (computer_guess, people_guess))
68             else:
69                 print ('computer guess is : [%s] \npeople guess is : [%s] \npeople win!!!\n'
70                 % (computer_guess, people_guess))
71  
72         print '\n--------------------------next guess cycle----------------------------------\n'

你可能感兴趣的:(python)