Homework 2
[Solutions]Due Date
This assignment is due before class on Monday, June 13th. Please refer to the page on electronic submission for more information. If you choose to use a single late day, the deadline becomes extended until 11:59pm on June 14th (so it extends the deadline by almost 30 hours). After that, each additional late day will extend the deadline an additional 24 hours. Remember that you can use at most 2 late days on this assignment.Programming
- (60 Points) [Required Code] Your job is
to write a video blackjack
program. We will use simplified rules -- there exist 1 dealer
and 1 player in the game. The dealer draws one card and shows it to the
player. The player draws two cards. Then, the player can choose
to hit (take another card) or
stand (finish the turn). The goal is to come as
close to 21 (summing the card values) as possible
without going over . Once the player stands, the dealer draws cards. The dealer automatically hits if the sum of his/her hand is less than or equal to 16 and stands at 17 or above. Once the dealer stands, dealer and player compare the sums of their hands. The one with the larger sum wins. If there is a tie, nobody wins (no money gained/lost).So you are writing a computer program to behave as the dealer. The person using the program will be the player. This is a one player game against a computer dealer.
The input/output is open ended on this problem, but it should have the following features:
- When the program loads, first ask the player to enter a dollar amount (this represents the player's total cash)
- Then, draw a random card for the dealer and show it to the user (and keep track of it)
- Draw two cards for the player (and print the sum).
- Ask the player to HIT or STAND (repeat if necessary)
- if the player's total ever goes above 21, player loses
- If the player STANDs at any point, you should draw cards for the dealer as described above. The dealer loses if the total goes over 21. Otherwise, compare the totals of the user and player. The one with the bigger total wins.
- Assume a player bets $5 each time. So a loss costs the player $5 while a win gains $5
- Allow the player to keep playing (loop). For simplicity, CTRL+C is how the user quits the program (you don't need to write any code for this to work).
$ gcc -Wall -o blackjack blackjack.c randomcard.c
$ ./blackjack
How much money are you bringing: 85
Dealer has 8
Player has 4
HIT or STAND: HIT
Player has 10
HIT or STAND: HIT
Player has 20
HIT or STAND: STAND
Dealer has 12
Dealer has 18
Player wins! ($90 total)
Above, anything after a colon was entered by the player. Also, the first "Player has" line must have drawn TWO cards. Don't forget that. (in this example, the player got stuck with 2 2's). Note that cards are just numbers from 2 to 11 (ace is 11 here... and ALWAYS considered 11 not 1. If you want to add code to allow for aces having two values, feel free, but it's not necessary). Also, there is no special "blackjack" case... as far as we are concerned, all 21s are equal. There are no "double down" or "split" bets.
The final question is how to generate cards from a deck. Here is where I give you code. You can download the .c and .h files for a program to randomly give you a number between 2 and 11 (inclusive). Think of this program as a deck -- you ask it for a card, it returns an integer representing the card (2 through 11). So part of this assignment is linking and compiling using my provided code.
This problem isn't anywhere near as bad as it sounds. Just construct a loop that keeps track of two sums, prints output, accepts user commands, and figures out which player has a higher total when both parties are done (neither has gone over 21 and both are done drawing cards). There isn't really any new stuff here, just constrcuting a program. Please use functions to make your code readable.
- (40 Points) Write a program that takes as input an integer money
amount in
cents (so you don't have to worry about floating point values) and returnsand integer array of size 4 where the 0th item is the number of pennies, the 1st is the number of nickels, the 2nd is the number of dimes, and the 3rd is the number of quarters where this is the lowest number of coins necessary to sum to the total value.Your program should have two functions: main and makechange(int). makechange should behave as described as above whereas all printf's/scanf's are placed in the main. main's job is to do all handling of user input/output and to call makechange when appropriate and reports its results to the user. The prototype of makechange should be as follows:
void makechange(int, int*);
While this seems like another instance of a search problem, think this through before writing a program with many nested loops. Instead, see if there is a strategy that always gives you the minimal number of coins having the combined value that you pass as input to the function. There may be a strategy that gives you the best result without having to search through all possible combinations as we did in the past.
Example:
$ gcc -Wall -o makechange makechange.c
$ ./makechange
Please enter amount of money you would like changed: 32
1 Quarter(s)
0 Dimes(s)
1 Nickel(s)
2 Penny(ies)
In the above case, somewhere in main you would have called makechange passing 32 as the value (using a variable, of course).
Grading
Grading will be based on correctness as mentioned in each problem. Read each problem very carefully to ensure that you've completed all that is required. A programmer needs to be able to read a paragraph description and implement the requirements rigorously. (80%)COMMENTS and description will account for 10% of each assignment. You must use comments to describe in a succinct fashion what is happening in your code. Also, name, id, etc should be placed in a comment at the very top of all your files.
Your code must be ANSI compliant. If you follow what the books say, this should not be an issue, but bad habits are known to pop up at this stage. (5%)
The last 5% is based on elegance/efficiency/style. For this project, efficiency won't be taken heavily, but formatting your code as we've been doing will be. Don't write code that completely defies any reasonable formatting convention (even if it is correct). For example, don't do things like:
int main(void) { int x;int y; int z; int c,d;c=5; if (c == 1) { d = 7; c = 4; } return 0; }
You are submitting two files: blackjack.c and makechange.c, each with a description of how your code works either in comments or in a separate README file. Don't submit binaries (like a.out). Place your .c files (and perhaps README file as plain text) in a directory on the cunix machine and see the submission instructions