This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/coin.py

44 lines
1.2 KiB
Python
Raw Normal View History

2012-02-20 04:34:17 +01:00
# Created by Lukeroge, improved by TheNoodle
2011-11-20 10:23:31 +01:00
from util import hook
2012-04-23 03:16:08 +02:00
from random import getrandbits
2011-11-20 10:23:31 +01:00
2012-02-29 06:47:11 +01:00
2012-02-20 04:34:17 +01:00
# used for tails: x heads: y
def flip_simple(count):
2012-04-23 03:16:08 +02:00
heads, tails = 0, 0
for x in xrange(count):
c = getrandbits(1)
if c == 0:
heads += 1
else:
tails += 1
2012-02-29 06:47:11 +01:00
return [heads, tails]
2011-11-20 10:23:31 +01:00
@hook.command(autohelp=False)
def coin(inp):
2012-02-28 03:03:43 +01:00
".coin [amount] -- Flips [amount] of coins."
2012-02-29 06:47:11 +01:00
# checking for valid input. if valid input [count=inp],
# if invalid [return error], if no input [count=1]
if inp.isdigit():
count = int(inp)
else:
if inp:
2012-02-17 01:46:09 +01:00
return "Invalid input."
else:
count = 1
2012-02-17 01:46:09 +01:00
if count > 9001:
return "Too many coins! Maximum is 9001."
# depending on the count, we use two different methods to get the output
if count == 1:
2012-04-23 03:16:08 +02:00
flip = getrandbits(1)
if flip == 1:
2012-04-23 03:16:08 +02:00
return "You flip a coin and get heads."
else:
2012-04-23 03:16:08 +02:00
return "You flip a coin and get tails."
else:
flips = flip_simple(count)
2012-04-23 03:16:08 +02:00
return "You flip %i coins and get " \
"%i heads and %i tails." % (count, flips[0], flips[1])