#!/usr/bin/python3
'''
This is a program to show random coin flips and other random things
Authors: Jeff Kinne, jkinne@cs.indstate.edu
Change log:
Dec 3, 2019 - initial version
Dec 4, 2019 - added something...
Known bugs/issues:
Starting point: https://docs.python.org/3/howto/curses.html
Sources: link above, that's all
Assistance from: nobody
To use: just run it, either ./randomness.py or python3 randomness.py or
run it in idle3
'''
import curses, random
# Get a choice from the user from a set of options
# The user is supposed to type the first letter of one of the choices
# scr - a curses screen/window for drawing on
# choices - list of strings for user to choose from
def getChoice(scr, choices):
scr.clear() # clear screen
scr.border() # border
# loop through choices and display each like: (c)hoice
# and also add first letters to firstLetters list
lineNo = 10
firstLetters = []
for s in choices:
if len(s) < 2: continue # assume choice is at least 2 characters long
scr.addstr(lineNo, 10, '('+s[0]+')'+s[1:])
firstLetters.append(s[0].lower())
lineNo += 1
scr.refresh() # screen isn't redrawn until this is called
# get their choice and check if it was one of the options
choice = scr.getkey()
while choice.lower() not in firstLetters:
choice = scr.getkey()
return choice.lower()
# Initialize some curses things
# scr - a curses screen/window to initialize
def init(scr):
scr.keypad(True) # enable using arrow keys
curses.noecho() # don't display keys types
curses.cbreak() # get keys before they hit enter
curses.curs_set(False) # don't display cursor
# The coins part of this program - display coin flips
# It keeps showing more coins as long as the user keeps pressing c
# scr - a curses screen/window to use
def coins(scr):
scr.clear() # start with an empty screen
scr.border()
# instructions to the user
scr.addstr(11, 10, 'Press c to flip another coin, any other key to quit')
counts = {'H':0, 'T':0}
xPos = 10 # position to put next coin flip at
# as long as they keep typing c, flip another coin
choice = 'c'
while choice.lower() == 'c':
xPos += 1 # move over one to put new coin
coin = random.choice(['H','T']) # this is the coin flip
scr.addstr(10, xPos, coin) # put it on the screen
counts[coin] += 1 # add to our counts
# display the counts
scr.addstr(12, 10, 'Heads: ' + str(counts['H']) + ', Tails: ' +
str(counts['T']) + ' ')
scr.refresh()
choice = scr.getkey()
# Flipping dice and displaying results
# Currently just flips one die
# scr - a curses window/screen
def dice(scr):
scr.clear()
scr.border()
die = random.randint(1, 6)
scr.addstr(10, 10, str(die))
scr.addstr(11, 10, 'press any key to continue')
scr.refresh()
scr.getkey()
# Main curses program, this is the entry point into our program
# scr - a curses window/screen to use
def main(scr):
init(scr) # initialize things
# Get what they want to do, then do it, then ask again
# what they want to do (if they didn't choose to quit)
choice = ''
while choice != 'q':
choice = getChoice(scr, ['Coins', 'Dice', 'Quit'])
if choice == 'c':
coins(scr)
elif choice == 'd':
dice(scr)
# Calling min like this does some initialization/cleanup for us
# and make sure we don't mess up the terminal if the program crashes.
curses.wrapper(main)