-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGuess_Number.py
175 lines (149 loc) · 4.96 KB
/
Guess_Number.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# template for "Guess the number" mini-project
# input will come from buttons and an input field
# all output for the game will be printed in the console
import random
import simplegui
### globals
target = 0 # the number computer hold
rangeUpperLimit = 0 # 1-rangeUpperLimit is the guess range.
guessCnt = 0 # how many times user have guessed.
lineNumber = 0 # line number of output on the canvas.
textSize = 15 # each character will take textSize pixels on canvas.
lineHeight = textSize + 5
canvasHeight = 400 # canvas height in pixels
canvasWidth = 250
maxLineNumber = canvasHeight / lineHeight
outputStrings = [] # store all the outputs in current canvas.
### helper functions
def gen_new_random(begin, end):
return random.randint(begin, end)
# init the game parameters
def new_game():
# initialize global variables used in your code here
global target
global rangeUpperLimit
global guessCnt
target = gen_new_random(1, rangeUpperLimit)
guessCnt = 0
return
# print to canvas, not the console
def print_to_canvas(str):
global maxLineNumber
global lineNumber
global outputStrings
outputStrings += [str]
lineNumber += 1
if lineNumber > maxLineNumber:
outputStrings = outputStrings[1:len(outputStrings)]
return
### define event handlers for control panel
def range100():
# button that changes the range to [0,100) and starts a new game
global rangeUpperLimit
rangeUpperLimit = 100
new_game()
print_to_canvas( "game start!")
timer.start()
def range1000():
# button that changes the range to [0,1000) and starts a new game
global rangeUpperLimit
rangeUpperLimit = 1000
new_game()
print_to_canvas( "game start!")
timer.start()
def judge_guess(guess):
global target
global rangeUpperLimit
global guessCnt
if target == 0:
print_to_canvas( "You should choose a range first.")
return
if not guess.isdigit():
print_to_canvas("Please input a number!")
timer.stop()
timer.start()
return
guessCnt += 1
if int(guess) < 1 or int(guess) > rangeUpperLimit:
if rangeUpperLimit == 100:
print_to_canvas("input a number between 1 and 100")
else:
print_to_canvas("input a number between 1 and 1000")
timer.stop()
timer.start()
return
performance = 0
if int(guess) == target:
if rangeUpperLimit == 100:
if guessCnt <= 5:
performance = 10 # 10 means perfect.
else:
performance = 6 # non-negative, non-10, just good whatever.
if rangeUpperLimit == 1000:
if guessCnt <= 7:
performance = 10
else:
performance = 6
if performance == 10:
str = "OMG! JUST %(times)d TIMES."%{'times':guessCnt}
print_to_canvas(str)
print_to_canvas("/*******************\\")
print_to_canvas("|* you are genius! *|")
print_to_canvas("\\*******************/")
else:
print_to_canvas("CONGRATULATIONS!")
str = "You've used %(times)d times."%{'times':guessCnt}
print_to_canvas(str)
print_to_canvas("Wanna make it better?")
target = 0
timer.stop()
return
else:
if rangeUpperLimit == 100:
if guessCnt > 10:
performance = -1
if rangeUpperLimit == 1000:
if guessCnt > 13:
performance = -1
if performance == -1:
print_to_canvas("Game over. Do you give in?")
target = 0
timer.stop()
return
if int(guess) < target:
print_to_canvas("higher")
else:
print_to_canvas("lower")
timer.stop()
timer.start()
return
def boring():
print_to_canvas("> What are you thinking about ?!")
def stopTimer():
global target
if target == 0:
print_to_canvas("> We haven't started ...")
return
timer.stop()
target = 0
print_to_canvas("bye!")
# define draw handler
def result_reporter(canvas):
global outputStrings
global lineHeight
localLineNumber = 1
for outputLine in outputStrings:
canvas.draw_text(outputLine, [0, localLineNumber*lineHeight], textSize, "blue")
localLineNumber += 1
return
# create frame
frame = simplegui.create_frame("Guess Num Game", canvasWidth, canvasHeight + textSize/2)
frame.set_canvas_background("white")
frame.set_draw_handler(result_reporter)
timer = simplegui.create_timer(10000, boring)
# register event handlers for control elements and start frame
frame.add_button("Guess range (1-100)", range100, 200)
frame.add_button("Guess range (1-1000)", range1000, 200)
frame.add_input("Input your guess:", judge_guess, 195)
frame.add_button("Stop guessing", stopTimer, 120)
frame.start()