-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoinFlip.py
263 lines (231 loc) · 6.14 KB
/
CoinFlip.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
### Boring Legal Stuff ###
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# CoinFlip.py
#
# Copyright 2018 Nicholas Blackmon
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
### The actual program stuff ###
#######################################################################
# CoinFlip.py
#
# This is a simple program that will do some coin flipping
# and output some stats on the coins that were flipped
# including total coins flipped, total heads, total tails,
# and the most heads / tails flipped in a row
#
# Made in Geany using Kivy and Python 3
import kivy
kivy.require("1.10.0")
from kivy.app import App
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import random
# Main
# Builds the kv file instead of having it separate
class Main(App):
def build(self):
Builder.load_string(
"""
Container:
#Container holds all the other layouts
<Container>:
id: contain
orientation: "vertical"
#pos_hint: {'center_y': 0.5, 'center_x': 0.5}
#size_hint_y: None
#height: 0.5625 * root.width
StartMenu
<StartMenu>:
orientation: "vertical"
Button:
text: "Coin Flip"
on_press: root.goto_coin_menu()
Button:
text: "Exit"
on_press: root.quit_game()
<CoinMenu>:
orientation: "vertical"
BoxLayout:
size_hint_y: 0.05
orientation: "horizontal"
Label:
text: "Coins: "
TextInput:
id: coinInput
text: "100"
multiline: False
Button:
size_hint_y: 0.2
text: "Flip!"
font_size: "30sp"
on_press: root.flip_coins()
BoxLayout:
orientation: "vertical"
size_hint_y: 0.5
id: CoinOutput
BoxLayout:
orientation: "horizontal"
Label:
id: CoinsFlippedText
text_size: self.width, self.height
size: self.texture_size
valign: "top"
halign: "right"
text: "Coins flipped: "
Label:
id: CoinsFlippedNumber
text_size: self.width, self.height
size: self.texture_size
valign: "top"
halign: "left"
text: "0"
BoxLayout:
orientation: "horizontal"
Label:
id: HeadsText
text_size: self.width, self.height
size: self.texture_size
valign: "top"
halign: "right"
text: "Total Heads: "
Label:
id: HeadsNumber
text_size: self.width, self.height
size: self.texture_size
valign: "top"
halign: "left"
text: "0"
BoxLayout:
orientation: "horizontal"
Label:
id: TailsText
text_size: self.width, self.height
size: self.texture_size
valign: "top"
halign: "right"
text: "Total Tails: "
Label:
id: TailsNumber
text_size: self.width, self.height
size: self.texture_size
valign: "top"
halign: "left"
text: "0"
BoxLayout:
orientation: "horizontal"
Label:
id: HeadsComboText
text_size: self.width, self.height
size: self.texture_size
valign: "top"
halign: "right"
text: "Most Heads In A Row: "
Label:
id: HeadsComboNumber
text_size: self.width, self.height
size: self.texture_size
valign: "top"
halign: "left"
text: "0"
BoxLayout:
orientation: "horizontal"
Label:
id: TailsComboText
text_size: self.width, self.height
size: self.texture_size
valign: "top"
halign: "right"
text: "Most Tails In A Row: "
Label:
id: TailsComboNumber
text_size: self.width, self.height
size: self.texture_size
valign: "top"
halign: "left"
text: "0"
Button:
size_hint_y: 0.1
text: "Back"
on_press: root.goto_start_menu()
""")
global root
root = Container()
return root
# The primary gui piece that holds all the other gui pieces
class Container(BoxLayout):
pass
# The first menu the user sees
class StartMenu(BoxLayout):
def quit_game(self):
App.get_running_app().stop()
def goto_coin_menu(self):
root.clear_widgets()
root.add_widget(CoinMenu())
# The bulk of the coin flip program, this is where stuff happens
# Flipping more than a million coins can be laggy
class CoinMenu(BoxLayout):
def goto_start_menu(self):
root.clear_widgets()
root.add_widget(StartMenu())
def flip_coins(self):
total = 0
heads = 0 #total number of heads
headsCombo = 0 #current heads combo
headsComboBest = 0 #best heads combo
tails = 0 #total number of tails
tailsCombo = 0 #current tails combo
tailsComboBest = 0 #best heads combo
currentCombo = 0 #whether its a heads or tails combo
i = int(self.ids['coinInput'].text)
while i >= 1:
total += 1
flip = random.randint(1,2)
if flip == 1:
heads += 1
if(currentCombo != 1):
currentCombo = 1
tailsCombo = 0
headsCombo = 1
else:
headsCombo += 1
if headsCombo > headsComboBest:
headsComboBest = headsCombo
elif flip == 2:
tails += 1
if(currentCombo != 2):
currentCombo = 2
tailsCombo = 1
headsCombo = 0
else:
tailsCombo += 1
if tailsCombo > tailsComboBest:
tailsComboBest = tailsCombo
i -= 1
total = str(heads + tails)
self.ids['CoinsFlippedNumber'].text = total
self.ids['HeadsNumber'].text = str(heads)
self.ids['TailsNumber'].text = str(tails)
self.ids['HeadsComboNumber'].text = str(headsComboBest)
self.ids['TailsComboNumber'].text = str(tailsComboBest)
# Runs the program
if __name__ == '__main__':
Main().run()