Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help needed! Please, help me out. Thank you so much! Project 1 For our first pro

ID: 3790189 • Letter: H

Question

Help needed! Please, help me out. Thank you so much!

Project 1

For our first project we are focusing on selection, e.g. making choices in a program. So I’ve written a skeleton program you should use to get started and then you add in your code to do the computation and output.

Details

The skeleton code is listed below and is explained in the comments in the code. If you have questions about what it does, please ask. Your job is to use the variables that are provided and add any that you need to play the game of Blackjack also called 21.

The basic idea is cards are “drawn”, or in our case, read from a file, and added to a sum. The player will stop playing when their sum equals 17 or greater. If the sum is over 21, then the player has busted and can not win the game. After the first player has played and gotten their final sum the dealer, or player 2, will draw cards until their sum is equal to 17 or greater. After both players have finished drawing cards, the winner, if there is one, will be determined.

The winner is the player with the highest score but under 22. If both players have the same score it is known as a push game and if both players bust, then no one wins. See the output section for more details.

Input

The input is already handled for you in the skeleton. If you choose to modify the skeleton program make sure you know what you’re doing. It is written in the patter I’m going to be teaching and will correctly read in all the data and stop when the data ends. The format for the data is as follows:

You may add conditions to end the loop before the input fails, however you should not remove the input failure check.

There will be more than 1 line in the input files and as stated the skeleton correctly handles this. We will talk about how to write this pattern yourself and you’ll be doing that once we complete project 1.

Cards:

The cards in the input will be one a set of the following cards, their values are listed after each card.

A - 1 or 11

2 - 2

3 - 3

4 - 4

5 - 5

6 - 6

7 - 7

8 - 8

9 - 9

10 - 10

J - 10

Q - 10

K - 10

So basically each card is worth the value on the face, 2 - 10. The Jack, Queen, King are also worth 10. The Ace is worth either 1 or 11. If you read an Ace, or A, and the sum for the player is is such that you can add 11 and not bust, then you add 11. If adding 11 would bust the player, then you add 1, regardless of what it does to the sum, i.e. bust the player or not. If you’ve read an Ace and added it as 11, you will not go and change the value to a 1 later if the player busts.

Output

The output will be generated in your program in the loop that is reading in the data. I’ll show an example below:

All of the output will follow this pattern:

Note the punctuation and capitalization for each of the lines. It must have the same or it is wrong.

Here are samples of other Line 4:

Of course in the case where player two wins, then the line 4 would have the word two instead of one.

Skeleton

Sample Files

See the readme in the zip file but basically each of the tests that Web-CAT runs will create an input file. Your program will produce an output file and then my code will run and produce an output file. The files that start with dave are the files produced by my program and should be considered as guides. I encourage you to test your program with the given input files. You can modify the main that is given below to pass in a different input file to your function so that you can test each case.

The sample includes files from only some of the tests. You should construct other input files for the other situations.

Input

So this is a sample input file. The corresponding output is here:

So we can see from the output that the first player read cards A and 10 and got 21. Then player 2 read the cards 3 10 and 4 and got 17. So player 1 one.

Main

The main for this project is very short. Basically it just calls your function. If you change the word input.txt to test1input.txt for example then you code would try to open and use test1input.txt. In this way you can change just the main and have your program use each of the different test files.

project1.h

This video attempts to explain why we have all these files:

To compile the files you can do this:

That will compile and link a file named project1.cpp and main.cpp into an exe named project1.exe. That assumes your cpp file is project1.cpp. If you name is something else, then use that instead.

To run your project do this:

That assumes the name of the exe is project1.exe. Make sure your input file is in the directory with your exe and it will make the output file for you.

Requirements

Each program will have a set of requirements that you have to meet in order to receive full credit. If you have any questions about them, please ask.

You must name the header file project1.h and the function blackjack must be in it as it it in the code above.

You may not store the input in an array, vector or list. You must process each line 1 at a time.

You may not use the STL, other than string and the ifstream and ofstream.

You must comment your code follow the Code Style Guidelines as given in Canvas.

You must use a selection statement in your code.

You may not use functions, classes, structs, etc.

You may not work with other students on this or any other project.

Grading

For each project you will submit your code to Web-CAT. For this project you may submit your project 10 times. Your final score will be a combination of the correctness from Web-CAT and the TAs will grade the rest based on how well you followed the requirements above.

Correctness earns 75 points. Following the requirements earns the remaining 25 points.

Naming

When you name your files, you may not use any punctuation, other than the . in the file extension. You may not use spaces. Use only letters and numbers. When you zip your files together don’t zip up a directory, just the files. Same naming rules apply for naming the zip file as naming the cpp and h files.

Explanation / Answer

# This program is practice for using classes, and doubles as a useful card deck program. Hopefully this can be of some use.
# Made by [brilliantlyInsane].

from random import randint as rand
from random import shuffle

suits = ("Spades","Hearts","Clubs","Diamonds")

class Card:
    def __init__(self, rank, suit):
        if rank not in range(1, 14):
            raise TypeError('Rank must be an integer between 1 and 13.')
        if suit not in suits:
            raise TypeError('Suit must be a string: "Spades", "Hearts", "Clubs", or "Diamonds".')
        # The quick check above makes sure the card being made actually exists in a standard deck of 52.
        # If so, the card is created succesfully.
        self.rank = rank
        self.suit = suit


    def cardName(self):
        """
        Returns a string containing the card's name in common terms.
        """
        if self.rank == 1:
            trueRank = "Ace"
        elif self.rank == 11:
            trueRank = "Jack"
        elif self.rank == 12:
            trueRank = "Queen"
        elif self.rank == 13:
            trueRank = "King"
        else:
            trueRank = str(self.rank)
        return "{rank} of {suit}".format(rank = trueRank, suit = self.suit)

    def flip(self):
        """
        Reveals the requested card.
        """
        print(self.cardName())

def newDeck():
    """
    Resets the deck to ascending order, containing all 52 cards.
    """
    global cardDeck
    cardDeck = [Card(rank, suit) for suit in suits for rank in range(1, 14)]
    cardDeck.reverse() # So that the bottom of the list is the top of the deck, i.e. the Ace of Spades is drawn first by 'cardDeck.pop()'.

newDeck()   # To generate the deck at the start. Note that it is not shuffled at first.

def shuffleDeck():
    """
    Self-explanatory. Shuffles the deck.
    """
    global cardDeck
    for i in range(0, 3):
        shuffle(cardDeck)   # Python's pseudorandom generator is slightly patterned unless shuffled multiple times.

def draw():
    """
    Draws a single card to a variable.
    Useful for replacing and discarding individual cards in a hand, such as replacing cards in poker.
    To do so: <hand>[<card to replace>] = cards.draw()
    Remember that the list for a hand starts from 0, not 1.
    """
    randCard = cardDeck.pop()
    return randCard

def drawFaceUp():
    randCard = cardDeck.pop()
    randCard.flip()
    return randCard

def drawHand(size):
    """
    Draws a <size>-card hand from the deck.
    """
    return [draw() for i in range(0, size)]

def showHand(hand):
    size = len(hand)
    for i in range(0, size):
        hand[i].flip()

def newCard():
    """
    Generates a random card outside of the established deck, and prints its value.
    While occasionally useful, using newCard() for hands is discouraged. Duplicates of preexisting cards will result.
    """
    suit = suits[rand(0, 3)]
    rank = rand(1,13)
    randCard = Card(rank,suit)
    print("The {card} has been generated.".format(card = str(randCard.cardName())))
    return randCard

def cardHelp():
    """
    Gives a set of instructions explaining the use of the 'cards.py' module.
    """
    print(' ' + '=' * 72)
    print('=' * 13 + " [brilliantlyInsane]'s Python Cards: Instructions " + '=' * 14)
    print('=' * 72 + ' ')

    print('—' * 16 + " The Cards " + '—' * 45)
    print('—' * 72)
    print('The "Card" object has two attributes:')
    print('rank - An integer between 1 and 13. (Ace = 1, Jack = 11, Queen = 12, King = 13.)')
    print('suit - A string value of either "Spades", "Hearts", "Clubs", or "Diamonds".')
    print('A specific card object can be made: "Card(<rank>,<suit>)". ')

    print('—' * 16 + " Drawing Cards " + '—' * 41)
    print('—' * 72)
    print('"Draw" cards to a variable with "<var> = cards.draw()".')
    print('Use "cards.drawFaceUp() to draw a card and print its value.')
    print('"Flip" a card (print its value) with "<var>.flip()".')
    print('Generate an entirely new random card using "cards.newCard()".')
    print('(Note that "newCard()" duplicates a card in the deck.) ')

    print('—' * 16 + " Hands " + '—' * 49)
    print('—' * 72)
    print('To draw an entire hand with <size> many cards, use "cards.drawHand(<size>)".')
    print('To show a hand, use "cards.showHand(<hand>)." ')

    print('—' * 16 + " Replacing Cards " + '—' * 39)
    print('—' * 72)
    print('You can replace individual cards in a hand using <hand>[card #] = cards.draw().')
    print('However, lists in Python start FROM 0, not 1!')
    print('"hand[1] = cards.draw()" will replace the SECOND card in your hand, not the first! ')

    print('—' * 16 + " The Deck " + '—' * 46)
    print('—' * 72)
    print('The deck is stored to a list under the variable "cards.cardDeck".')
    print('Shuffle using "shuffleDeck()". The deck is unshuffled by default.')
    print('Reset the deck completely using cards.newDeck().')
    print(' ' + '=' * 72 + ' ')

print('Type "cards.cardHelp()" to learn how to use this module.')