Craps

Published on January 5th, 2022 at 2:14 am

What is it?

Craps is a popular dice game that is commonly played at casinos. This is a very simplified version of that game in a command-line editor. In this version, you can only play on the pass line. At the start of the game, you are given $100 to bet with. Your first roll will determine if you win, lose, or roll again. You will win if you roll either a 7 or an 11, however, you will lose if you roll either a 2, 3, or 12. If you roll anything else, your bet moves to “point”. While your bet is on “point”, you will have to continue to roll until your dice equal the same value as the roll to move to point. So if you rolled a 5, you will need to keep rolling until your roll 5 again, but if you roll a 7, you lose.

a screenshot of the craps game start screen
Screenshot of the Main Menu

What does it do?

When you start the application, you are presented with the start screen which contains some ASCII art and the main menu. Typing 3 will close the application, typing 2 will show you the rules, and typing 1 will start the game. The rules are shown below:

Here are the rules for this version of Craps.
	(1) You start with $100, and you make your first bet.
    (2) On your first roll, you win if you roll 7 or 11.
    	You lose if you roll 2, 3, or 12. If you roll anything else,
        that becomes the 'point'.
    (3) You continue to roll until one of two things happen:
    	(A) You roll the point again. In this case, you win.
        (B) You roll a 7. In this case, you lose.
    (4) You may continue to play or quit the game.

If you press 1, the game starts. The prompt will show that you have $100 and will ask how much you would like to bet. In normal craps, you can choose where you want to place your bet which would affect your odds of winning, but in this version, you can only play on the Pass Line. The Pass Line is a 1:1 wager, which means that you will earn back your bet plus a matched amount from the house if you win.

To play on the Pass Line, simply enter a number equal to or less than the amount of virtual money you have and press Enter. The screen will then tell you your bet amount and your new balance after subtracting the wager. Pressing Enter again will roll the dice. On your first roll, 3 things can happen:

  • You roll a 7 or an 11 and win the game.
  • You roll a 2, 3, or 12 and lose the game.
  • You roll anything else and a “point” is established at that value.

If your bet is on “point”, then you will have to continually roll the dice until one of two things happen:

  • You roll the same number as the point value and win the game.
  • You roll a 7 and lose the game.

After each round, you have the option to quit. This will reset your balance and start the game over.

How does it work?

This game was written in Python: a programming language written in the early 90’s to be a high-level general-purpose language. It is commonly used today in nearly all fields of computer science including enterprise applications, data science, and machine learning.

The main block of code starts at def main(). This prints the intro to the game and sets the main menu options to start or end the game. If the user starts the game, it runs playRound() which covers all of the logic for playing on the Pass Line in craps. It calls makeBet() which covers the code for making a bet, rolls the dice, and checks the result to see if the user wins or loses. makeBet() asks for an input of money to bet, but declines the transaction if the user does not have enough to bet. The full program is shown below:

Craps.py

####################
# Craps
# by Ryan Bains-Jordan
####################

#imports
import random
import time

#defaults
newGame = True
sMoney = 100	#starting money
money = sMoney	#game money

#dice images
a=['','','','','','']

a[0] =' _____ \n'
a[0]+='|     |\n'
a[0]+='|  *  |\n'
a[0]+='|_____|\n'

a[1] =' _____ \n'
a[1]+='|    *|\n'
a[1]+='|     |\n'
a[1]+='|*____|\n'

a[2] =' _____ \n'
a[2]+='|    *|\n'
a[2]+='|  *  |\n'
a[2]+='|*____|\n'

a[3] =' _____ \n'
a[3]+='|*   *|\n'
a[3]+='|     |\n'
a[3]+='|*___*|\n'

a[4] =' _____ \n'
a[4]+='|*   *|\n'
a[4]+='|  *  |\n'
a[4]+='|*___*|\n'

a[5] =' _____ \n'
a[5]+='|*   *|\n'
a[5]+='|*   *|\n'
a[5]+='|*___*|\n'

#intro
def printIntro():
    print("\n")
    print("***********************************************")
    print("***********************************************")
    print("**             _ _ _ _ _     _ _ _ _ _       **")
    print("** C          |          |  |          |     **")
    print("**   R        |  *    *  |  |       *  |     **")
    print("**     A      |          |  |     *    |     **")
    print("**       P    |  *    *  |  |   *      |     **")
    print("**         S  |_ _ _ _ _ |  |_ _ _ _ _ |     **")
    print("**                                           **")
    print("***********************************************")
    print("***********************************************")
    print("\n")

#rules
def printRules():
    print("")
    print("Here are the rules for this version of Craps.")
    print("  (1) You start with $100, and you make your first bet.")
    print("  (2) On your first roll, you win if you roll 7 or 11.")
    print("      You lose if you roll 2,3, or 12.  If you roll anything else,")
    print("      that becomes the \'point\'")
    print("  (3) You continue to roll until 1 of two things happens:    ")
    print("      (A) You roll the point again.  In this case, you win.")
    print("      (B) You roll a 7. In this case, you lose.")
    print("  (4) You may continue to play or quit the game.   ")
    print("")

#game opitons
def printOptions():
    print("")
    print("(1) Play Craps!")
    print("(2) Print Rules")
    print("(3) Quit")
    print("")

#end the game
def endGame():
	global money
	print("You have $" + str(money))
	
	if money > 0:
		play = ""
		while play != "y" and play != "n":
			play = input("\nMake another bet? y/n\n")
			if play == "y":
				playRound()
			elif play == "n":
				print("\nYou left with $" + str(money))
				main()
	else:
		print("\nYou are out of money. Better luck next time!")
		main()

#if you win the game
def winGame(bet):
	global money

	#double the bet
	bet = bet * 2
	money = money + bet
	endGame()

#if you lose the game
def loseGame():
	endGame()

#make the bet
def makeBet():
	global money
	bet = int(input("How much would you like to bet?\n"))
	if (money - bet) >= 0:
		print("\nYour bet = $" + str(bet))
		return bet
	else:
		print("You don't have that much money")
		playRound()

#get a random number from die
def rollDie():
	randNum = random.randint(1,6)
	return randNum

#roll the dice and get the sum
def rollDice():
	print("\nRolling Dice...")
	
	die1 = rollDie()
	time.sleep(1)
	print(a[die1 - 1])

	die2 = rollDie()
	time.sleep(1)
	print(a[die2 - 1])
	
	dieSum = die1 + die2
	time.sleep(1)
	print("You rolled a " + str(dieSum) + ".")

	return dieSum

#play a round of craps
def playRound():
	global money

	#make a bet
	bet = makeBet() 
	money = money - bet

	print("New balance = $" + str(money) + "\n")

	#craps numbers
	crapsNums = [2,3,12]

	#first roll
	input("Press Enter to Roll Dice")
	dieSum = rollDice()

	#check if first value is 7 or 11
	if dieSum == 7 or dieSum == 11:
		print("Instant Win.")
		winGame(bet)

	#check if first value is 2, 3 or 12
	elif dieSum in crapsNums:
		print("Instant Lose.")
		loseGame()

	#check if it is anything else
	else:
		print("\nBet moves to point")
		point = dieSum

	#second roll
	input("Press Enter to Roll Dice")
	dieSum = rollDice()
	
	#check if roll = point
	if dieSum == point:
		print("\nCongratulations, you rolled the point!")
		winGame(bet)
	else:
		print("\nSorry, you did not roll the point.")
		loseGame()

#main method
def main():
	#print the intro if it is a new game
	global newGame
	if newGame == True:
		printIntro()
		newGame = False

	#print the options
	play = 2
	while play == 2:
		printOptions()
		play = int(input("What would you like to do?\n"))

		#options
		if play == 1:
			global money
			money = sMoney
			print("\nYou have $" + str(money))
			playRound()
		elif play == 2:
			printRules()
		elif play == 3:
			quit()
		elif play < 1 or play > 3:
			print("invalid input")
			play = 2

#driver
main()