CPSC120A
Fundamentals of Computer Science I

Lab 24

Lists

Use the command line to create a new directory called lab24 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.

Blackjack

Blackjack

Scotty is a card shark. If you want to play blackjack with him and not lose all of your money, you will need to practice. If you don't know the game blackjack, it's simple. Each player is dealt two cards. Face cards are worth 10 points, aces are worth 11 points, and all other cards are worth face value. You can hit, or ask for, as many more cards as you want. The player with the most points, without going over 21 is the winner.

Details

Create a program in a file called blackjack.py that plays a single hand of blackjack. The program should do the following:

  1. Initialize the deck to contain 52 strings representing the cards of a standard playing deck. You can shuffle the deck using the random.shuffle function.

  2. Deal two cards from the deck to the player's hand list.

  3. Repeatedly prompt the user if they want to add more cards and print the player's hand.

  4. Print the total points of the player's hand.

Example

Your Hand: 10♦ K♠
Enter h to hit: h
Your Hand: 10♦ K♠ 2♥
Enter h to hit: s
You have 22 points.

Hint

This program can be broken into several functions:

  1. The function player_turn(deck, hand) should have a while loop runs while the user's input is not "h". The body of the loop should print the hand list and then prompt the user to enter an "h". If the user enters an "h", the last item in the deck list should be popped and appended to the hand list.

  2. The function card_points(card) should return the point value of the card parameter. A card string can be converted to its point value by slicing off the value and converting it to an int. The strings "A", "K", "Q", and "J" can be converted using an if statement. The strings "1" through "10" can be converted using the built-in int function.

  3. The function hand_points(hand) should return the sum of all of the cards in the hand list. It should iterate over the hand list and use the card_points(card) function to convert each card string into an int.

 

Challenge

This solitary version of Blackjack is kind of boring. Add a dealer to the mix. The game is still very similar. At the beginning, you will deal cards to both the player and the dealer. You should display one of the dealer's cards to the player, as is typical with a game of blackjack.

After the player hits until they are content, it is then the dealers turn. On the dealer's turn, repeatedly add cards to the dealer's hand until the total is 17 points or more.

After the dealer's turn, you should show the entirety of the dealers hand, display the total points for both the player and the dealer, and print who won

  Dealer's Hand: 2♠ ##
~~~~~Your Turn~~~~~
Your Hand: 10♦ K♠
Enter h to hit: h
Your Hand: 10♦ K♠ 2♥
Enter h to hit: s
~~~Dealer's Turn~~~
Dealer hits.
Dealer's Hand: 2♠ 10♣ 8♥
Dealer stays.
You have 22 points.
Dealer has 20 points.
Dealer wins.
  

 

Challenge

To really have a chance beating Scotty, you will need to count cards. By counting cards you can know when you have a higher probability of winning a hand. When you have a higher probability of winning you can bet more money. Unless you are a savant, counting all the cards difficult. Instead, it's not to difficult to estimate how many high cards are left in deck by using a hi-lo system. Every card in the deck is given a value.

Card Value Hi-lo Value
2 -- 6 -1
7 -- 9 0
10 -- A 1

When the sum of the hi-lo value of every dealt card is large, there is a higher probability that the next card dealt will be a 10 through ace. Modify your blackjack program to also print the hi-lo card count every time a card is dealt.

Dealer's Hand: 2♠ ##
Hi-lo: -1
~~~~~Your Turn~~~~~
Your Hand: 10♦
Hi-lo: 0
Your Hand: 10♦ K♠
Hi-lo: 1
Enter h to hit: h
Your Hand: 10♦ K♠ 2♥
Hi-lo: 0
Enter h to hit: s
~~~Dealer's Turn~~~
Dealer hits.
Dealer's Hand: 2♠ 10♣ 8♥
Dealer stays.
You have 22 points.
Dealer has 20 points.
Dealer wins.

Who Wrote It

Who Wrote It?

You may have heard some crazy conspiracy theories that William Shakespeare did not write his own plays. You have likely read at least one of his plays, but not much else is known about him. It is speculated that perhaps Shakespeare did not write all (or even any) of his own plays. In fact, there are a few conspiracy theories that say that other well known authors wrote the Shakespeare plays. Maybe we could help the world out, and exonerate some of the common suspects

Theoretically, figuring out who wrote a piece of text is easy: Compare a sample of writing by a known author, versus the text whose author is to be identified. If there are enough similarities between the writings, then there is enough evidence to state that there is more than a coincidental chance that the two texts were written by the same person, the author of the known text. This process is much more complicated when put into actual practice. The difficulty arises in defining what similarities are important for comparison, and what "enough similarities" actually means.

One potential metric for measuring similarity between two arbitrary texts are the words used within the text. It is seems like a decent assumption that an authors vocabulary does not drastically change between written texts. Given this assumption, we could say that if the set words used in the two sample texts are similar enough, they were likely written by the same author. In order to determine this, we need to be able to get the set words used out of some text.

Details

Create a function called word_set(text1) in a file called authorship.py. Your program should do the following:

  1. Remove all punctuation from the text string by replacing them with a single space. You can use the replace method of strings to replace periods with the " " character.
  2. Create a list to represent a set of words, essentially a list of all of the words from the string parameter without duplicates.
  3. Return the set of words.

Example

>>eggs_and_ham = "That Sam I Am.  I do not like that Sam I Am."
>>print(word_set(eggs_and_ham))
['that', 'sam', 'i', 'am', 'do', 'not', 'like']

Hint

The function word_set(text), should create a an empty list to represent the set. It can convert the input text to a list of words using the split method. It should iterate over the list of words and add any words that are not already in the set list.

 

Design Document

Create a design document for your assignment 7 game, on paper. The design document should include a diagram of what the game will look like. It should also have a text description of how to play the game including how to win or lose.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.