1. 程式人生 > >用於判斷設計模式的小遊戲

用於判斷設計模式的小遊戲

#coding=utf-8
import random
model = ['工廠方法','抽象工廠','單例','原型','建造者','介面卡','橋接','組合','裝飾','外觀','享元','代理','觀察者','訪問者','中介者','直譯器','迭代器','狀態','命令','策略','備忘錄','責任鏈','模板方法']

num = 0
trueNum = 0
type = ''
while(True):
	
	i = random.choice(range(23))
	if(i<5):
		type = '建立'
	elif(i>11):
		type = '行為'
	else:
		type = '結構'
	print(num+1,":請判斷<",model[i],">是什麼型別的設計模式")
	print("0:退出")
	print("1:建立")
	print("2:結構")
	print("3:行為")		
	typeNum = input()
	if(typeNum=='0'):
		print("此題不計入成績")
		break
	if(typeNum=='1'):
		if(i<5):
			print("猜對了")
			trueNum += 1
		else:
			print("猜錯了,是",type)
	if(typeNum=='2'):
		if(i>4 and i<12):
			print("猜對了")
			trueNum += 1
		else:
			print("猜錯了,是",type)
	if(typeNum=='3'):
		if(i>11):
			print("猜對了")
			trueNum += 1
		else:
			print("猜錯了,是",type)	
	if(typeNum=='0' or typeNum=='1' or typeNum=='2' or typeNum=='3'):
		num += 1
	else:
		print("輸入的格式有錯,此題不計入成績")
print("一共答了",num,"題,答對了",trueNum,"題")