CS60 Project Assignment 2 -------------------------------- Due: Oct 17th at Midnight Black Jack with Linked Lists ---------------------------- This project asks your do design and implement a player vs. computer (dealer) blackjack game. On initialization, the game must ask the user to input how many decks of cards they would like the computer to use, and then proceed to play the game until the user decides to quit. The program must make use of a linked list to keep track of the dealer's deck. Playing Cards ------------- A 'deck' of playing cards consists of 52 cards, each with the following information encoded on them: - a number from 2 to 10, jack, queen, king and ace - one of four 'suits': diamonds, hearts, spades, and clubs So a standard deck appears as follows: num suit ------------- ace spades 2 spades 3 spades ... 10 spades jack spades queen spades king spades ace hearts 2 hearts ... king hearts ace diamonds ... etc 4*13 == 52. For simplicity of this game, we will assume that the 'ace' card (represented however you like in the program) is worth 11 points. Jacks, Queens, and Kings are all worth 10 points, and numbered cards (2-10) are worth the same number of points as their number (e.g. 5 of hearts is worth 5 points). BlackJack ---------------- The game of blackjack (sometimes called 21) is fairly straightforward, and starts with the dealer having a stack of playing cards. The dealer (computer) then deals the player two cards, and itself two cards; one visible to the player and one invisible. The object of the game is for the player to have cards in their hand whose sum is as close to or equal to 21 as possible without exceeding 21. The player may choose to 'hit', receive a card from the dealer, up to a maximum of five cards in their hand, until they choose to 'stay', when it becomes the dealer's turn. On the dealer's turn, the dealer 'hits' until it has exceeded the number 16, at which point it stays. After both players have stayed, the sums of the two hands are compared, and the player closest to 21 wins. If either the dealer or the player is dealt an initial hand that sums up to 21, that player immediately wins. If there is a tie, the dealer wins. If either player 'busts', exceeds 21, during the 'hit' phase, the other player immediately wins. After each round, all of the cards used during that round are removed from the game. If the draw deck is depleted during a round, the game should come to an end. Program Requirements -------------------- 1.) you must implement a linked list data structure in C that can hold information about a playing card in each node. - list must support the following operations: - choose and remove random node from list (draw a card) - insert node into list (to head or tail, your choice) - clear (frees all memory used by nodes and list itself) - print (walks through the list and prints the contents (for debugging) 2.) the game must ask how many decks of 52 cards to use for the initial dealer pile, and must initialize the pile with that many decks. The program must then print the contents of the deck using the linked list's print functionality. 3.) the game must correctly implement the various rules and winning/losing scenarios of the blackjack game. - player or dealer is initially dealt cards that sum to 21 (whomever has 21 instantly wins, if both then dealer wins) - player 'hits' until sum exceeds 21 (player loses) - player 'hits' until has up to 5 cards in hand - dealer draws while sum is less than 16, stops when sum > 16 - dealer exceeds 21 while drawing (dealer lose) - sums of hands are compared, highest sum <= 21 wins (dealer wins on tie) - dealer runs out of cards in deck (game ends) 4.) the game must 'keep score' for the player and the computer, and display the score at the beginning of each round. 5.) the game must allow the user to choose to reset the game, exit the game, or go more rounds at the end of each round. If the player chooses 'reset', the scores must be reset and the linked list of cards must be freed before a new deck is initialized. ALL memory aquired via 'malloc' must be freed. Turnin ------------------ The turnin tag for this assignment is 'project2' and, as usual, requires the turning in of the program code, a typescript showing as many of the win/lose/bust scenarios as possible (getting an initial 21 is random and so may be ommited), and an optional README that has special instructions for the grader. Note that for project2 and for all programming assignments from this point on, you MUST compile your program with the flags '-ansi -pedantic' and get NO warnings in order to recieve full credit. Sample Output ------------------ [nurmi@localhost project2]$ ./a.out How many decks shall we play with: 1 Printing Deck king/clubs, queen/clubs, jack/clubs, 10/clubs, 9/clubs, 8/clubs, 7/clubs, 6/clubs, 5/clubs, 4/clubs, 3/clubs, 2/clubs, ace/clubs, king/spades, queen/spades, jack/spades, 10/spades, 9/spades, 8/spades, 7/spades, 6/spades, 5/spades, 4/spades, 3/spades, 2/spades, ace/spades, king/diamonds, queen/diamonds, jack/diamonds, 10/diamonds, 9/diamonds, 8/diamonds, 7/diamonds, 6/diamonds, 5/diamonds, 4/diamonds, 3/diamonds, 2/diamonds, ace/diamonds, king/hearts, queen/hearts, jack/hearts, 10/hearts, 9/hearts, 8/hearts, 7/hearts, 6/hearts, 5/hearts, 4/hearts, 3/hearts, 2/hearts, ace/hearts, New Round Player=0 Dealer=0 ------------------------------------------------------ Dealer: #/# 5/clubs Player: 9/clubs 2/clubs ---> 11 Hit or Stay (h/s): h Player: 9/clubs 2/clubs 2/spades ---> 13 Hit or Stay (h/s): h Player: 9/clubs 2/clubs 2/spades 3/clubs ---> 16 Hit or Stay (h/s): h Player: 9/clubs 2/clubs 2/spades 3/clubs 10/diamonds ---> 26 Player Busts! Playagain, Reset, or Exit (p/r/e): p New Round Player=0 Dealer=1 ------------------------------------------------------ Dealer: #/# jack/spades Player: 9/spades 7/clubs ---> 16 Hit or Stay (h/s): s Player: 9/spades 7/clubs ---> 16 Final Hands ---------------- Dealer: jack/diamonds jack/spades ---> 20 Player: 9/spades 7/clubs ---> 16 Dealer Wins! Playagain, Reset, or Exit (p/r/e): p New Round Player=0 Dealer=2 ------------------------------------------------------ Dealer: #/# king/spades Player: jack/hearts 4/diamonds ---> 14 Hit or Stay (h/s): h Player: jack/hearts 4/diamonds 5/spades ---> 19 Hit or Stay (h/s): s Player: jack/hearts 4/diamonds 5/spades ---> 19 Final Hands ---------------- Dealer: king/hearts king/spades ---> 20 Player: jack/hearts 4/diamonds 5/spades ---> 19 Dealer Wins! Playagain, Reset, or Exit (p/r/e): p New Round Player=0 Dealer=3 ------------------------------------------------------ Dealer: #/# ace/hearts Player: 10/hearts queen/hearts ---> 20 Hit or Stay (h/s): s Player: 10/hearts queen/hearts ---> 20 Final Hands ---------------- Dealer: 3/hearts ace/hearts 6/clubs ---> 20 Player: 10/hearts queen/hearts ---> 20 Dealer Wins! Playagain, Reset, or Exit (p/r/e): p New Round Player=0 Dealer=4 ------------------------------------------------------ Dealer: #/# 6/diamonds Player: 2/hearts 8/spades ---> 10 Hit or Stay (h/s): h Player: 2/hearts 8/spades 3/spades ---> 13 Hit or Stay (h/s): h Player: 2/hearts 8/spades 3/spades 6/hearts ---> 19 Hit or Stay (h/s): s Player: 2/hearts 8/spades 3/spades 6/hearts ---> 19 Final Hands ---------------- Dealer: 4/spades 6/diamonds 4/hearts 8/hearts ---> 22 Player: 2/hearts 8/spades 3/spades 6/hearts ---> 19 Dealer Busts! Playagain, Reset, or Exit (p/r/e): p New Round Player=1 Dealer=4 ------------------------------------------------------ Dealer: #/# 3/diamonds Player: 6/spades queen/clubs ---> 16 Hit or Stay (h/s): s Player: 6/spades queen/clubs ---> 16 Final Hands ---------------- Dealer: 7/spades 3/diamonds ace/diamonds ---> 21 Player: 6/spades queen/clubs ---> 16 Dealer Wins! Playagain, Reset, or Exit (p/r/e): p New Round Player=1 Dealer=5 ------------------------------------------------------ Dealer: #/# queen/spades Player: queen/diamonds 5/diamonds ---> 15 Hit or Stay (h/s): s Player: queen/diamonds 5/diamonds ---> 15 Final Hands ---------------- Dealer: 8/clubs queen/spades ---> 18 Player: queen/diamonds 5/diamonds ---> 15 Dealer Wins! Playagain, Reset, or Exit (p/r/e): p New Round Player=1 Dealer=6 ------------------------------------------------------ Dealer: #/# 2/diamonds Player: jack/clubs 4/clubs ---> 14 Hit or Stay (h/s): h Player: jack/clubs 4/clubs 10/spades ---> 24 Player Busts! Playagain, Reset, or Exit (p/r/e): p New Round Player=1 Dealer=7 ------------------------------------------------------ Dealer: #/# 7/hearts Player: 7/diamonds 5/hearts ---> 12 Hit or Stay (h/s): h Player: 7/diamonds 5/hearts ace/clubs ---> 23 Player Busts! Playagain, Reset, or Exit (p/r/e): p New Round Player=1 Dealer=8 ------------------------------------------------------ Dealer: #/# 8/diamonds Player: king/clubs 9/diamonds ---> 19 Hit or Stay (h/s): s Player: king/clubs 9/diamonds ---> 19 Final Hands ---------------- Dealer: 10/clubs 8/diamonds ---> 18 Player: king/clubs 9/diamonds ---> 19 Player Wins! Playagain, Reset, or Exit (p/r/e): r How many decks shall we play with: 10 New Round Player=0 Dealer=0 ------------------------------------------------------ Dealer: #/# 8/spades Player: 4/clubs queen/spades ---> 14 Hit or Stay (h/s): h Player: 4/clubs queen/spades 8/clubs ---> 22 Player Busts! Playagain, Reset, or Exit (p/r/e): e Thanks For Playing! [nurmi@localhost project2]$