forked from tjferry14/Pythonista-UI-Games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguessing.py
37 lines (32 loc) · 1.23 KB
/
guessing.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
import random
import ui
global rounds, player_wins, computer_wins
MINIMUM, MAXIMUM = (0, 1000)
player_wins = 0
computer_wins = 0
rounds = 0
def perform_guessing(number):
player_guess = int(v['player_guess'].text)
computer_guess = random.randint(MINIMUM, MAXIMUM)
v['comp_guess'].text = str(computer_guess)
v['the_number'].text = str(number)
player_score = abs(player_guess - number)
computer_score = abs(computer_guess - number)
return player_score < computer_score
def guess(sender):
v['label3'].text = 'The number was'
global rounds, player_wins, computer_wins
rounds += 1
if perform_guessing(random.randint(MINIMUM, MAXIMUM)):
player_wins += 1
v['outcome'].text = 'Player Wins!'
v['outcome'].text_color = 0.00, 0.50, 0.00
else:
computer_wins += 1
v['outcome'].text = 'Computer Wins!'
v['outcome'].text_color = 1.00, 0.00, 0.00
v['comp_wins'].text = str(computer_wins)
v['player_wins'].text = str(player_wins)
v = ui.load_view('guessing')
v['textview1'].text = str('Welcome to the guessing game! A number will be randomly chosen from %i to %i. The player will make a guess, and then the computer will guess. Whoever is closest wins that round!') % (MINIMUM, MAXIMUM)
v.present('sheet')