Python编程中的Hangman游戏
man子手是计算机上的著名游戏,它基本上将计算机生成的单词与您猜到的单词匹配。如果有比赛,您会得到奖励,否则游戏会继续要求新的输入。
在下面的游戏中,用户首先必须输入他们的姓名,然后要求其猜测任何字母。如果随机单词包含该字母,它将以正确的位置显示为输出,否则程序将要求您猜测另一个字母。用户将获得几轮可以更改的轮数,以猜测完整的单词。在the子手游戏中,用户必须猜测正确的单词才能挽救生命。
我们使用随机模块以及许多特殊字符在响应后绘制图表。当响应未能获得正确答案时,可以使该图看起来像人悬在空中。但是总的来说,这是一个猜谜游戏。
示例
import random class game(object): # Hangman game dangle = [] dangle.append(' *===*') dangle.append(' * *') dangle.append(' *') dangle.append(' *') dangle.append(' *') dangle.append(' *') dangle.append(' =====') lad = {} lad[0] = [' @ *'] lad[1] = [' @ *', ' ! *'] lad[2] = [' @ *', '/! *'] lad[3] = [' @ *', '/!\\ *'] lad[4] = [' @ *', '/!\\ *', '/ *'] lad[5] = [' @ *', '/!\\ *', '/ \\ *'] z = [] states = ''' gujarat kerala karnataka westbengal maharashtra uttarakhand odisha tamilnadu bihar telangana rajasthan chhattisgarh assam jharkhand haryana andhrapradesh punjab madhyapradesh goa newdelhi ladakh jammukashmir arunachalpradesh manipur nagaland mizoram tripura meghalaya sikkim '''.split() design = '@_@\'@_@\'@_@\'@_@\'@_@\'@_@\'@_@\'@_@\'@_@\'@_@\'' def __init__(K, *args, **kwargs): m, n = 2, 0 K.z.append(K.dangle[:]) for k in K.lad.values(): p, n = K.dangle[:], 0 for i in k: p[m + n] = i n += 1 K.z.append(p) def Select_Word(K): return K.states[random.randint(0, len(K.states) - 1)] def photo(K, x, wordLen): for slash in K.z[x]: print(slash) def game(K, term, value, notpresent): q = input() if q == None or len(q) != 1 or (q in value) or (q in notpresent): return None, False m = 0 p = q in term for i in term: if i == q: value[m] = i m += 1 return q, p def data(K, data): x = len(K.design) print(K.design[:-3]) print(data) print(K.design[3:]) def process(K): name = input("Enter your name: ") print("Good Luck ,",name) print('*****Hangman game starts*****') term = list(K.Select_Word()) outcome = list('_' * len(term)) print('The state is: ', outcome) y, m, k = False, 0, [] while m < len(K.z) - 1: print('Guess the state name : ', end='') x, x1 = K.game(term, outcome, k) if x == None: print('Repeated character.') continue print(''.join(outcome)) if outcome == term: K.data('Excellent ! You saved a life :)') y = True break if not x1: k.append(x) m += 1 K.photo(m, len(term)) print('Missed words: ', k) if not y: K.data('The state was \'' + ''.join(term) + '\' Game over ! Man was hanged') a = game().process()
输出结果
运行上面的代码给我们以下结果-
Enter your name: Suraj Good Luck , Suraj *****Hangman game starts***** The state is: ['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_'] Guess the state name : m m____________ *===* * * * * * * ===== Missed words: [] Guess the state name : s m__________s_ *===* * * * * * * ===== Missed words: [] Guess the state name : t m__________s_ *===* * * @ * * * * ===== Missed words: ['t'] Guess the state name : g m__________s_ *===* * * @ * ! * * * ===== Missed words: ['t', 'g'] Guess the state name : h m__h_______sh *===* * * @ * ! * * * ===== Missed words: ['t', 'g'] Guess the state name : a ma_h_a__a__sh *===* * * @ * ! * * * ===== Missed words: ['t', 'g'] Guess the state name : e ma_h_a__a_esh *===* * * @ * ! * * * ===== Missed words: ['t', 'g'] Guess the state name : y ma_hya__a_esh *===* * * @ * ! * * * ===== Missed words: ['t', 'g'] Guess the state name : b ma_hya__a_esh *===* * * @ * /! * * * ===== Missed words: ['t', 'g', 'b'] Guess the state name : w ma_hya__a_esh *===* * * @ * /!\ * * * ===== Missed words: ['t', 'g', 'b', 'w'] Guess the state name : v ma_hya__a_esh *===* * * @ * /!\ * / * * ===== Missed words: ['t', 'g', 'b', 'w', 'v'] Guess the state name : j ma_hya__a_esh *===* * * @ * /!\ * / \ * * ===== Missed words: ['t', 'g', 'b', 'w', 'v', 'j'] @_@'@_@'@_@'@_@'@_@'@_@'@_@'@_@'@_@'@ The state was 'madhyapradesh' Game over ! Man was hanged '@_@'@_@'@_@'@_@'@_@'@_@'@_@'@_@'@_@'