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

C program Assignment: Assignment : Write algorithms and programs to simulate a d

ID: 3592673 • Letter: C

Question

C program Assignment:

Assignment: Write algorithms and programs to simulate a deck of cards and deal hands of cards. Conventional deck of cards has 52 cards: 13 ranks and 4 suits. Your simulation should be able to create a deck of cards, display the original deck, shuffle the deck, display the shuffled deck, deal the specified number of cards to the specified number of players (via command-line) and display each of the hands of cards.

Output: Display the original deck (ordered) and the shuffled deck (random), and display each of the player's hands of cards. Decks, hands and cards should be clean, clear and appropriately labeled, and easy to read - do not simply list as a long column or row of values. No graphics for suit/rank is required (but is attractive), but a simple code for each will suffice - as long as it is easy to understand.

Input: Accept input via the command-line arguments. Validate command-line input. Input will specify the # of cards/hand and the # of hands (players), in that order.

Requirements: Use only material covered in the first seven chapters of [D/D] and first five chapters of [K/R]. Style requirements as discussed in class expected. Efficiency should always be considered. Always select the most appropriate loop/decision structures and variable/constant types. Functions should focus on a single task. Main() should be high-level tasks only. Use of pointers is encouraged but not required. Use of constants and enumerated types is required and will prove useful...

Your choice on how to represent the cards, hands, and deck; but hands should be separate entities from the deck. Validate input so that there will be from [1-13] players and from [1-13] cards per hand, and remember there are only 52 cards to deal.

For more on shuffling, see Knuth (or Fisher-Yates) Shuffle:

  https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm

Refer to page 46 in the [K/R] and sections 5.10 & 5.11 in [D/D] for use of rand() and srand(), and <time.h>.

Minimum of two source code files — one for main() and at least one for other required functions, along with a proper user-defined header file.

Explanation / Answer

#include #include #include /*This is the time header file, you must include this when you are using random numbers*/ int rand_int(int n); void draw_a_card(); /*Notice how char *suits and char *ranks are global variables*/ char *suits[4]={"spades","clubs","diamonds","hearts"}; char *ranks[13]={"ace","two","three","four","five","six","seven","eight","nine","ten","jack", "queen","king"}; int main() { int x,y; srand(time(NULL)); /*This is to set the seed for random numbers*/ do{ printf(" Enter a number of cards to draw (Press 0 to exit) "); scanf("%i",&y); if(y==0) break; for(x=1; x