Return to site

Craps In Python

broken image


Planning Strategies¶

Python Projects for $250 - $750. I want a web-based multi-player craps game in python. Please include some basic pseudo code and architecture with your bid. Below I've included the Wikipedia description and a example program. Python实现国外赌场热门游戏Craps(双骰子) 运行方法: 1. 打开python2 IDLE: 2. 输入 from craps import. 3. 按提示输入运行命令.例如,玩游戏就输入play:查看余额就输入checkbankroll: 自动玩看胜率就输入auto craps.py import random pointset = False bet = 10 bankroll = 1000 simwin = 0 simlose = 0 print '&quo. Hopefully many of the high school students reading this have not spent extended time in a casino and do not know the rules of the dice game, Craps. We will be programming a simple version of the game with the following rules: At the beginning of the game, the user needs to have their bank account set to $100.

After each round, the program should ask the user if he/she wants to continue playing. The game continues until the player runs out of money, or the player chooses to exit.

Similar to Rock Paper Scissors (as well as any multi-step, complex program), planning out your code will be highly beneficial in writing cleaner and more organized solutions. However, this time, you will be coming up with your own functions. Please remember to heed the following guidelines:
  • The majority of your program should be functions. Break the game down into simple steps or instructions - each of those will have its own function.
  • Each function should have a single purpose. If you find that you have written a function that is doing multiple things, it means you should break it down further into more functions. It's okay to refactor code mid-development!
  • The one exception to the above should be your main game function, which will contain the game loop, as well as any persistent variables. Try to keep this one as small as possible, utilizing functions as much as you can.
  • Take advantage of return statements. Your code should not have any global variables. Data should be passed to functions through arguments, and from functions through the return statement.
  • Which variables will you have to keep track of during all the rounds, and after all the rounds are completed? Make a note of which ones should be initialized outside of your loop.
  • Be sure to provide the player with the appropriate prompts, letting them know their options and limitations for input.
  • Consider what control structures are necessary for the program.
  • Consider the order of occurrences - what should the program do first?

However, unlike Rock Paper Scissors, in this lab you will be required to validate user's inputs. This means that:

  • Players should not be able to enter a negative number as a bet
  • Players should not be able to bet more money than they have in their bank
  • Players should not be able to bet decimal amounts of money - only whole numbers. (recall: A boolean to check if x is an integer or not would be: xint(x). This will return True is x is an integer and False otherwise. It works because of math.)
  • When prompted with the choice of ending the game or continuing, the question should repeat until the user has entered one choice or the other.

This also means that your labs will have quite a few while loops. Keep that in mind!

A good place to start would be to write the functions that you plan on writing. You do not need to write the code itself, only the purpose of each function. Here are a few examples:
  • function name: roll2dice
    • arguments: none
    • purpose: generates a random dice roll for two dice and prints out that the two rolls are
    • returns: the sum of the dice roll
  • function name: get_bet
    • arguments: bank amount
    • purpose: to get the amount to be bet from the user. Bet must be a positive integer and no more than they have in their bank. Should repeatedly ask the user to make a bet until they enter a valid one
    • returns: the chosen valid bet amount

And so on. The above are examples, and do not have to be followed. There are many ways these games can be organized. However, notice that the names of my functions are verbs - just remember that functions should do things, and therefore have names that reflect what they do.

You should name your file FILN_craps.py, where FILN is your first initial and last name, no space.

In this chapter we will develop two programs involving loops that are slightly more involved than the simple examples from the last chapter.

1.1. Example: the game of craps¶

For the first example, we'll write a program that allows a user to play the game of 'craps'. It is played by rolling a pair of dice. Normally, the player (and spectators) bet on the outcome. We assume that a player starts with a fixed amount of money, called the 'bankroll.'

Craps is played like this. Each round of the game has two 'phases':

In the first phase, you roll the dice once. If the result is 7 or 11, you win immediately and the round ends. If the result is 2, 3, or 12, you lose immediately, and the round ends. In all other cases the number you roll (which has to be a 4, 5, 6, 8, 9, or 10) is called the 'point', and you go on to the second phase.

Muckleshoot casino jobs. Muckleshoot Casino exercises Indian Preference in hiring practices. Pre-employment drug testing is required. Team members must obtain and maintain a Muckleshoot Indian Commission Gaming License. The Human Resources department is located on the second floor of the west side of the casino. At Muckleshoot Bingo, we're always looking for outstanding employees to join our team, and offer a wide variety of career choices. Check out our open positions and fill out an application. Be sure to visit our Employment page often, as we are always updating it with the most current openings.

In the second phase you keep rolling the dice until you roll a 7 or you roll the value of the 'point' from the previous phase. If you roll a 7 first, you lose, and if you roll the point first, you win.

The payoff for the bets is always one-to-one: if you bet $10 and win, the bank gives you $10.

We can simulate rolling the dice using the randint function. To make it interactive, we'll use an input statement to pause until the player presses the 'Enter' key. Then we just generate two random values 1 through 6 and add them together.

import random# Returns the value of a simulated roll of two dicedef roll_dice(): input('Press ENTER to roll the dice..') a = random.randint(1, 6) b = random.randint(1, 6) return a + bprint(roll_dice())

We'll start by writing a function to play a single round. We can assume that the amount of the bet is a parameter to the function, and the return value of the function is the amount won, where a negative number means a loss. The logic for the first phase just needs a conditional statement.

import randomdef play_one_round(bet): # Returns the value of a simulated roll of two dice def roll_dice(): input('Press ENTER to roll the dice..') a = random.randint(1, 6) b = random.randint(1, 6) return a + b roll = roll_dice() # First phase: 7 or 11 wins, 2, 3, or 12 loses if roll 7 or roll 11: print('You win!') return bet elif roll 2 or roll 3 or roll 12: print ('Sorry, you lose.') return -bet else: point = roll print() print ('The point is now', point, '.')play_one_round(100000000000)

For the second phase, the player has to keep rolling the dice until she rolls a 7 or rolls the point. There is no way to predict how long that might take, right? So we'll need a while-loop.

Step 1: What is the repeated action?

Step 2: How do we start out?

Rolling the dice the first time.

Step 3: How do we know whether to keep doing it?

When the loop finishes, we know that the value of ?roll? is either equal to 7 or equal to the point. We just have to check which one, in order to determine whether the player won or lost.

Here is the complete function:

import random# Returns the value of a simulated roll of two dicedef roll_dice(): input('Press ENTER to roll the dice..') a = random.randint(1, 6) b = random.randint(1, 6) return a + b# Plays one round of craps using the given amount as the bet. Returns the# amount won as a positive number or the amount lost as a negative number.def play_one_round(bet): roll = roll_dice() # First phase: 7 or 11 wins, 2, 3, or 12 loses if roll 7 or roll 11: print ('You win!') return bet elif roll 2 or roll 3 or roll 12: print ('Sorry, you lose.') return -bet else: point = roll print() print ('The point is now', point, '.') # Second phase roll = roll_dice() print ('You rolled ', roll) while roll != 7 and roll != point: roll = roll_dice() print ('You rolled ', roll) # after loop, roll is 7 or is equal to point if roll 7: print ('Sorry, you lose.') return -bet else: print ('You win!') return betplay_one_round(100000000000)

To make this into a complete application, we need a user interface. The interface should allow the user to play again or quit, it should allow her to choose how much to bet, and should keep track of how much she's won. This could be similar to the user interface for the number-guessing game in the last chapter. This part is left as an exercise.

1.2. Example: a loan table¶

In this section, the problem we are solving is based on the following scenario. Suppose you buy a car and you are making monthly payments. Two years later, you discover you need to trade it in for a minivan. How much do you still owe on your car?

The answer to a question like this is found by creating something called an 'amortization table' for the loan. This is just a table that shows for each payment, how much went to pay interest, how much went towards paying off what you owe, called the principal, and how much you still owe, called the balance.

As always, we'll start with a small concrete example. Suppose you borrow $100 at an interest rate of 10% per month, and you make monthly payments of $30. A month goes by, and you make your first payment. After that, how much do you owe?

The interest you owed for the month is 10% of 100 dollars, or 10 dollars.

Craps In Python

The amount paid on your loan is 30 - 10 , or 20 dollars.

The balance you owe is now 100 - 20, or 80 dollars.

We can summarize all this by writing one line in a table:

Then, you make your second payment.

Next month you make another payment.

The process is similar for the next payment.

But now something different happens.

The interest you owe is 10% of 7.18, or 72 cents.The total amount you still owe is the 7.18 balance + 72 cents interest, which is a total of 7.90.Another 30 dollar payment would be too much! So you make a final payment of 7.90, and then the balance is zero

We'll put the code into a function. The function needs three pieces of information: the amount of the loan, the payment amount, and the interest rate, so our function will have three parameters.

defprint_loan_table(amount,payment,annual_rate):

Craps Game In Python

Let?s analyze what we have done and try to write the code.

Step 1: what is the repeated action?

Look at the calculations we did over and over again:

Introducing some appropriate variable names, we can write these steps by following what we did for the examples. For instance, in the first step, we had a balance of 100 dollars. We found the interest by multiplying the balance by the monthly rate of 10 percent. We found the amount paid on the principal by subracting the interest from the payment amount. We found the new balance by subtracting the principal from the balance.

To print a line of the table, we?ll use a format string with 12 spaces per column. Using a format specifier of ?percent 12 point 2 f? will round everything to two decimal places.

print('%12.2f%12.2f%12.2f'%(interest,principal,balance))

That takes care of the loop body.

Step 2: how do we start out?

Initially the balance is the original amount borrowed. The monthly rate is the annual rate, divided by 12. At this point we have this much written. The statements representing the 'repeated action' become the loop body.

Step 3: How do we know whether to keep doing it?

Looking back at our example, we knew it was the end when the amount we owed was only 7.90, which was less than the payment. So the loop should look something like this:

How did we know the amount owed? It was the balance, plus the interest for the month, that is:

balance+balance*monthly_rate

So we could write the condition as

while(balance+balance*monthly_rate)<=payment:

Blackjack in python

The amount paid on your loan is 30 - 10 , or 20 dollars.

The balance you owe is now 100 - 20, or 80 dollars.

We can summarize all this by writing one line in a table:

Then, you make your second payment.

Next month you make another payment.

The process is similar for the next payment.

But now something different happens.

The interest you owe is 10% of 7.18, or 72 cents.The total amount you still owe is the 7.18 balance + 72 cents interest, which is a total of 7.90.Another 30 dollar payment would be too much! So you make a final payment of 7.90, and then the balance is zero

We'll put the code into a function. The function needs three pieces of information: the amount of the loan, the payment amount, and the interest rate, so our function will have three parameters.

defprint_loan_table(amount,payment,annual_rate):

Craps Game In Python

Let?s analyze what we have done and try to write the code.

Step 1: what is the repeated action?

Look at the calculations we did over and over again:

Introducing some appropriate variable names, we can write these steps by following what we did for the examples. For instance, in the first step, we had a balance of 100 dollars. We found the interest by multiplying the balance by the monthly rate of 10 percent. We found the amount paid on the principal by subracting the interest from the payment amount. We found the new balance by subtracting the principal from the balance.

To print a line of the table, we?ll use a format string with 12 spaces per column. Using a format specifier of ?percent 12 point 2 f? will round everything to two decimal places.

print('%12.2f%12.2f%12.2f'%(interest,principal,balance))

That takes care of the loop body.

Step 2: how do we start out?

Initially the balance is the original amount borrowed. The monthly rate is the annual rate, divided by 12. At this point we have this much written. The statements representing the 'repeated action' become the loop body.

Step 3: How do we know whether to keep doing it?

Looking back at our example, we knew it was the end when the amount we owed was only 7.90, which was less than the payment. So the loop should look something like this:

How did we know the amount owed? It was the balance, plus the interest for the month, that is:

balance+balance*monthly_rate

So we could write the condition as

while(balance+balance*monthly_rate)<=payment:

That takes care of the loop, but we're not quite done. We still have to write the very last line of the table showing the final payment.

The complete function is shown below.

Craps Game In Python

# Prints out an amortization tabledef print_loan_table(amount, payment, annual_rate): balance = amount monthly_rate = annual_rate / 12.0 while payment <= balance + balance * monthly_rate: interest = balance * monthly_rate principal = payment - interest balance = balance - principal print ('%12.2f%12.2f%12.2f' % (interest, principal, balance)) # print last line interest = balance * monthly_rate print ('%12.2f%12.2f%12.2f' % (interest, balance, 0))print_loan_table(100, 20, 1.2)




broken image