-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
152 lines (138 loc) · 3.42 KB
/
game.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
#import pyb
import os
import ubinascii
import utime
try:
import pyb
print("Running on a PYB")
red = pyb.LED(1)
grn = pyb.LED(2)
yel = pyb.LED(3)
blu = pyb.LED(4)
sw = pyb.Switch()
except ImportError:
# Create dummy pyb module
print("Not running on a PYB")
class LED:
'Dummy LED class for non PYB boards'
state = 0
def __init__(self, name):
self.name = name
def on(self):
'turns LED on'
state = 1
return
def off(self):
'turns LED off'
state = 0
return
try:
red
except NameError:
print("WiPy Initialisation")
red = LED(1)
grn = LED(2)
yel = LED(3)
blu = LED(4)
# Timers 1,2,4,7-14 available for use
#ti = pyb.Timer(4, freq=4)
# Ensure all LEDs are off
red.off()
grn.off()
yel.off()
blu.off()
# Setup callback to toggle Yellow LED when the switch is pressed
try:
sw.callback(lambda:yel.toggle())
except NameError:
print('Stil need to implement switch input on WiPy')
# Declare and define Globals
Number_of_cards_to_deal = 5
Master_Pack = [1,2,3,4,5,6,7,8,9,10]
Pack = []
SelectedCards = []
def play():
#
# pack of cards
# select N from pack of cards
for i in range(0, len(Master_Pack)):
Pack.append(Master_Pack[i])
deal(Number_of_cards_to_deal)
#print("Remaining: ", Pack)
#print("Selected: ", SelectedCards)
N = len(SelectedCards)
M = 0
Score = 0
print("")
print("Welcome to MicroPython Play your Cards Right")
print("============================================")
while M < (N-1):
#print("Turn: ", M)
if turn(M):
Score = Score + 1
#print("Score: ", Score)
M += 1
utime.sleep_ms(2000)
# move to next card...
print("Score: ", Score, "out of ", N-1)
# Tidy Up
for i in range(0, N):
del SelectedCards[0]
for i in range(0,len(Pack)):
del Pack[0]
utime.sleep_ms(1000)
red.off()
grn.off()
yel.on()
print("Bye")
def deal(NumCardsToDeal):
#card = 1
for i in range(0, NumCardsToDeal):
#SelectedCards.append(random.choice(cards))
NumCards = len(Pack)
if 0 < NumCards:
# Select a card at random from those left in the pack
card = int(ubinascii.hexlify(os.urandom(1)), 16) % NumCards
SelectedCards.append(Pack[card])
Pack.remove(Pack[card])
def turn(M):
# reveal selected card M
if M == 0:
# First round we need to start with a card
print("First card: ", SelectedCards[M])
blu.on()
# request input H or L
Guess = input("Do you think the next card is higher or lower?")
Guess = Guess.lstrip() # Remove whitespace from left of string
Guess = Guess.upper() # Force string to upper case
#print("I think you said:", Guess)
blu.off()
# reveal selected card M+1
print("Card: ", SelectedCards[M+1], end=" ")
# compare card M and M+1 : > or <
Difference = SelectedCards[M+1] - SelectedCards[M]
#print("Difference:", Difference)
if Difference < 0:
# Lower
#print("Lower")
Result = 'L'
elif Difference > 0:
# Higher
#print("Higher")
Result = 'H'
else:
# Equal
Result = 'E'
# Score Result
if Result == Guess[0]:
# Correct
print("CORRECT - you win!!!")
grn.on()
red.off()
return 1
else:
# Wrong
print("WRONG - sorry")
red.on()
grn.off()
return 0