import simplegui import random draw_list = [] step = 0 review_list = ['pls give me your number'] a1win = 0 a2win = 0 even = 0
def draw(canvas): o = DrawText(draw_list) p = o.draw(canvas,[20,20])
def ai_input_handler(txt): timer.stop() global draw_list global review_list,a1win,a2win,even
#验证input是否符合要求
error_input_message = ['pls input POSITIVE number[0,1000]']
if not is_int(txt):
draw_list = error_input_message
return
else:
guess_number = int(txt)
if guess_number<0 or guess_number>1000:
draw_list = error_input_message
return
draw_list = []
review_list = []
guess_time1 = alg1(guess_number)
draw_list.append(' ')
guess_time2 = alg2(guess_number)
if guess_time1>guess_time2:
msg = 'Algorithm 2 Win'
a2win+=1
elif guess_time1<guess_time2:
msg = 'Algorithm 1 Win'
a1win+=1
else:
msg = 'Algorithms ended in a draw'
even+=1
draw_list.append(' ')
draw_list.append(msg)
draw_list.append(' ')
draw_list.append('we played ' + str(a1win+a2win+even) + ' times')
draw_list.append('Algorithm1 win: ' + str(a1win))
draw_list.append('Algorithm2 win: ' + str(a2win))
draw_list.append('Algorithms play even : ' + str(even))
review_list = draw_list
pass
def alg1(guess_number):
global draw_list
ai_number = 500
lower_number = 0
upper_number = 1000
i = 0
draw_list.append('Algorithm 1:Direct Dichotomy')
while ai_number <> guess_number:
i+=1
if ai_number>guess_number:
draw_list.append('The ' + str(i) + ' guess:'+str(ai_number)+ u',too high')
upper_number = ai_number
ai_number = (ai_number+lower_number)/2
else:
draw_list.append('The ' + str(i) + ' guess:'+str(ai_number)+ u',too lower')
lower_number = ai_number
ai_number = (ai_number+upper_number)/2
draw_list.append('Bingo! The ' + str(i+1) + ' guess: '+str(ai_number)+ ' is correct')
return i+1
def alg2(guess_number):
global draw_list
ai_number = 500
lower_number = 0
upper_number = 1000
i = 0
draw_list.append('Algorithm 2:Random Dichotomy')
while ai_number <> guess_number:
i+=1
if ai_number>guess_number:
draw_list.append('The ' + str(i) + ' guess:'+str(ai_number)+ u',too high')
upper_number = ai_number
ai_number = random.randint(lower_number+1,ai_number-1)
else:
draw_list.append('The ' + str(i) + ' guess:'+str(ai_number)+ u',too lower')
lower_number = ai_number
ai_number = random.randint(ai_number+1,upper_number-1)
draw_list.append('Bingo! The ' + str(i+1) + ' guess:'+str(ai_number)+ ' is correct')
return i+1
pass
def ai_handler():
ai_input_handler(obj_ai_input.get_text())
pass
def review_handler(): if timer.is_running(): timer.stop() else: timer.start() pass
def is_int(txt):
global draw_list
_is_int = all(c in "0123456789" for c in txt)
if _is_int and txt<>'':
return True
else:
return False
pass
def timer_handler(): global draw_list global step if step<=len(review_list)+1: step+=1 draw_list = review_list[0:step] pass class DrawText():
def __init__(self,ls):
self.ls = ls
def __str__(self):
return ls
def draw(self,canvas,pos):
ls = self.ls
for l in ls:
pos[1] = pos[1] + 15
canvas.draw_text(str(l), pos, 15, "Red")
return pos
def set_ls(self,ls):
self.ls = ls
frame = simplegui.create_frame("Home", 500, 800 ) frame.set_canvas_background('white')
obj_ai_input = frame.add_input(u"输入你的数字[0,1000]让电脑猜猜",ai_input_handler,50) timer = simplegui.create_timer(600,timer_handler)
frame.add_button(u"回车或者点击此处开始", ai_handler) frame.add_button(u"回放", review_handler) frame.set_draw_handler(draw)
frame.start()