#!/usr/bin/env python
import random
class Player:
    "Contains all the information pertaining to the player of the Craps game"
    def __init__(self):
        "Set initial player funds to $1000, set stake to zero"
        self.funds = 1000
        self.stake = 0

    def placeBet(self, betAmount):
        "Place a bet for the specified amount"
        if betAmount > self.funds:
            return False
        else:
            self.stake = betAmount
            self.funds -= betAmount
            return True

    def getFunds(self):
        "Get the player's current cash stores"
        return self.funds

    def addFunds(self, amount):
        "Add an amount on to the player's total cash"
        self.funds += amount

    def getStake(self):
        "Find out how much the player bet"
        return self.stake

class Game:
    "The main game logic for Craps"
    
    def __init__(self):
        "Set the initial state of the game to 'not running'. Instantiate a new Player class"
        self.gameRunning = False
        self.playerOne = Player()

    def rollDice (self, numberOfDice, numberOfSides):
        "Generate a random number emulating the roll of a dice"
        diceTotal = 0
        #Roll the specified number of dice, adding the rolled result on to the total
        for i in range(numberOfDice):
            diceTotal += random.randint(1, numberOfSides)
        return diceTotal

    def showInstructions (self):
        "Print the game instructions to the screen"
        print """Craps is a game of chance. You start with $1000. Place bets on the roll of 
the dice. Specify your stake and choose to bet on either 2 or 12, 4 or 10, or
6 or 8. The payouts are as follows:\n
Bet\t\tPayout\n2 or 12\t\t5:1\n4 or 10\t\t2.5:1\n6 or 8\t\t1.5:1\n"""

    def isRunning(self):
        "check the game's current state"
        return self.gameRunning

    def startGame(self):
        "Sets the game's state to running"
        self.gameRunning = True

    def processGame(self):
        """The main game loop for Craps. First, check to see if the player
        has funds remaining. If they do not, inform them and stop the game running.
        Next, print the player's current funds to screen and input their desired bet.
        Place the bet with the player and input the type of bet requested.
        Next, roll the dice and calculate the winnings based on the result and the
        type of bet requested by the player. If the player does not have funds to cover
        the bet, print a message to the screen"""
        #Check player has funds
        if self.playerOne.getFunds() <= 0:
            print "You've run out of money! Better get the hell out of here before they find out!"
            self.gameRunning = False
            return
        #input bet amount from player
        print "You currently have $%.2f to your name" % self.playerOne.getFunds()
        print "how much would you like to bet?\n"
        betAmount = 0
        #make sure the player enters an integer value
        while betAmount == 0:
            try:
                betAmount = input()
                if type(betAmount) != int:
                    print "Please enter an integer value!"
                    betAmount = 0
            except NameError:
                print "Please enter a numeric value!"
            except SyntaxError:
                print "Please enter a numeric value, no whitespace!"
        #place bet and ask the player what type of bet they wish to make
        #placeBet will return false if the player has insufficient funds
        if self.playerOne.placeBet(betAmount):
            print "Please enter the type of bet you wish to place:\n1 = 2 or 12, 2 = 4 or 10, 3 = 6 or 8"
            betType = 0
            #make sure the player enters an integer value between 1 and 3
            while betType == 0:
                try:
                    betType = input()
                    if (betType < 1 or betType > 3) or type(betType) != int:
                        print "Please enter an integer value between 1 and 3"
                        betType = 0
                except NameError:
                    print "Please enter a numeric value!"
                except SyntaxError:
                    print "Stop fucking about"
            #roll the dice
            result = self.rollDice(2, 6)
            print "You roll the dice and score a %i" % result
            #calculate winnings based on bet type
            if result == 2 or result == 12:
                if betType == 1:
                    print "Well done!"
                    print "You win $%.2f!" % (self.playerOne.getStake()*5)
                    self.playerOne.addFunds(playerOne.getStake()*6)
                else :
                    print "Hard luck!"
            elif result == 4 or result == 10:
                if betType == 2:
                    print "Well done!"
                    print "You win $%.2f!" % (self.playerOne.getStake()*2.5)
                    self.playerOne.addFunds(self.playerOne.getStake()*3.5)
                else:
                    print "Hard Luck!"
            elif result == 6 or result == 8:
                if betType == 3:
                    print "Well done!"
                    print "You win $%.2f!" % (self.playerOne.getStake()*1.5)
                    self.playerOne.addFunds(self.playerOne.getStake()*2.5)
                else:
                    print "Hard Luck!"
            else :
                print "Hard luck!"
        else:
            print "You don't have enough funds to cover that bet!\n"

if __name__ == "__main__":
    myGame = Game()
    myGame.showInstructions()
    myGame.startGame()
    while myGame.isRunning():
        myGame.processGame()
