Craps Write a Python program called q2py that plays the dice

Craps. Write a Python program (called q2.py) that plays the dice game Craps. The player begins by throwing two standard dice. If the sum of the dice is 7 or 11, the player wins. If the sum is 2, 3 or 12, the player loses. Otherwise, the sum becomes the player\'s point. The player continues to roll until either the point comes up again, in which case the player wins, or the player throws 7, in which case they lose. Consider writing the Craps program without showing a visual representation of the two dice. Show the two random dice values as integers first. Once your program is working, go back and add the code for converting the dice values into a visual representation. Finally, when writing the code for representing the dice visually, you might think about taking advantage of common drawing components among the dice. If you can find commonality among how the dice are drawn on the screen, you can reduce the amount of code written to solve the problem. Also, if you use lists in this program to visually represent your dice, you will need to convert the list into a string using the join operator. See the code below. # A program that converts a list to a string. In order for this # to work, each element in the list, must be a string. a_list = [\'$\', \'8884\', \'yes\'] # Joins list1 elements without a separator. a_list_string1 = \'\'.join(a_list) # Joins list elements wth XYZ as a separator. a_list_string2 = \'XYZ\'.join (a_list) print(a_list) print(a_list_string1) print(a_list_string2) After the title of the game is displayed (line 1), the player presses the key to roll the two dice (line 3). Then, the visual representation for the dice roll of 5 and 2 is displayed on the screen (lines 4-8). Since a 7 was rolled on the first try, the player wins (lines 9-10). Craps: A Popular Dice Game Press to roll the dice. You rolled a 7 on your first roll. You win! After the title of the game is displayed (line 1), the player presses the key to roll the two dice (line 3). Then, the visual representation for the dice roll of 2 and 1 is displayed on the screen (lines 4-8). Since a 3 was rolled on the first try, the player loses (lines 9-10). Craps: A Popular Dice Game Press to roll the dice. You rolled a 3 on your first roll. You lose !

Solution


#!/bin/usr/python

# ------------------------DICE ROLLING FUNCTION---------------------------

def roll_two_dice():
    import random  
    die1 = random.randint(1,6)
    die2 = random.randint(1,6)
    return die1 + die2

# ------------------------MAIN FUNCTION---------------------------

def main():
    play_game = raw_input(\"Would you like to play the game of Craps? (Y/n)\")
    if play_game == \"\": play_game = \"y\"
    if play_game[0].lower() != \"n\":
        instructions = raw_input(\"Do you need instructions? (y/N)\")
        if instructions == \"\": instructions = \"n\"
        if (instructions[0].lower() != \"n\"):
            print \"1. A player rolls two six-sided dice and adds the numbers rolled together.\"
            print \"2. On this first roll, a 7 or an 11 automatically wins, and a 2, 3, or 12\"
            print \"   automatically loses, and play is over. If a 4, 5, 6, 8, 9, or 10 are \"
            print \"   rolled on this first roll, that number becomes the \'point.\'\"
            print \"3. The player continues to roll the two dice again until one of two things\"
            print \"   happens: either they roll the \'point\' again, in which case they win; or\"
            print \"   they roll a 7, in which case they lose.\"
    while (play_game[0].lower() != \"n\"):
        pause = raw_input(\"Press <Enter> to roll the dice!\"),
        first_roll = roll_two_dice()
        print \"You rolled a\",first_roll,\"on your first roll...\"
        # Check to see if they won or lost
        if first_roll == 7 or first_roll == 11:
            print \"You WON!!!\"
        elif first_roll == 2 or first_roll == 3 or first_roll == 12:
            print \"You lost... :(\"
        else:     # They didn\'t win on first roll--try to make the point
            print \"That\'s your point. Try to roll it again before you roll 7 and lose!\"
            done = False
            while (not done):
                pause = raw_input(\"Press <Enter> to roll the dice!\"),
                point_roll = roll_two_dice()
                print \"You rolled a\",point_roll
                if first_roll == point_roll:
                    print \"You rolled your point and WON!\"
                    done = True
                elif point_roll == 7:
                    print \"You rolled a 7 and lost!!!\"
                    done = True
                else:
                    print \"Keep on rolling...\"
        play_game = raw_input(\"Wanna play again? (Y/n)\")
        if play_game == \"\": play_game = \"y\"
    print \"Thanks for visiting! See you next time!\"
  
# ------------------------START THE PROGRAM---------------------------

if __name__ == \"__main__\":
    main()

 Craps. Write a Python program (called q2.py) that plays the dice game Craps. The player begins by throwing two standard dice. If the sum of the dice is 7 or 11
 Craps. Write a Python program (called q2.py) that plays the dice game Craps. The player begins by throwing two standard dice. If the sum of the dice is 7 or 11

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site