This program is from an introductory C text
I understand everything until we get to the following If Statement (highlighted in orange in the full program):
Code:
if (!in_hand[suit][rank]){
}
I know that negation operator makes an expression 1 (true) if expr has the value 0 (false).
However, I'm having trouble sorting out in my head what the natural language equivalent of that if statement is and how it relates to the while loop that it's contained within. You can see what I've already figured out about the program in my comments for each line. Trying to tie things all together by figuring this out last part
Does it mean this in natural language? "If the following rank and suit has a value of zero (boolean false) in the array", execute the next statements?
I'm grasping at straws here. You can see my notes for what I've figured out already for what each line does. This is the missing piece
Note: I also included the author's text below the code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_SUITS 4 /*Spade, Club, Heart Diamond*/
#define NUM_RANKS 13 /*2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace*/
#define TRUE 1
#define FALSE 0
typedef int Bool;
int num_cards;
int rank;
int suit;
main()
{
printf("Enter number of cards in hand: ");
scanf_s("%d", &num_cards); /*user input*/
Bool in_hand[NUM_SUITS][NUM_RANKS] = { 0 }; /* 4 rows, 13 columns*/
const char rank_code[] = { '2','3','4','5','6','7','8', /*array - translating rank*/
'9','t','j','q','k','a' };
const char suit_code[] = { 'c','d','h','s' }; /*array - translating suit*/
srand((unsigned)time(NULL)); /* Pass return value of TIME function through SRAND. If you don't do this before the statements below, same cards will be dealt every instance of program */
printf("Your hand:");
while (num_cards > 0) { /* counting down from number of cards specified to 0*/
suit = rand() % NUM_SUITS; /* picks a random suit---the use of the % operator limits it from 0 to 3 */
rank = rand() % NUM_RANKS; /* picks a random rank---the use of the % operator limits it from 0 to 12 */
if (!in_hand[suit][rank]) {
in_hand[suit][rank] = TRUE;
num_cards--;
printf(" %c%c", rank_code[rank], suit_code[suit]); /*print translating card rank and suit based on array*/
}
}
printf("\n");
return 0;
}
Dealing a Hand of Cards
This program illustrates both two-dimensional arrays and constant arrays. The program deals a random hand from a standard deck of playing cards.
Enter number of cards in hand: 5
Your hand: 7c 2s 5d as 2h
It's not immediately obvious how we'd write a program
1). How do we pick cards randomly from a deck?
- Use several Standard Library functions:
-The time function ( <time.h> ) returns the current time, encoded in a single number
-The srand function ( <stdlib.h>) initializes C's random number generator
-Passing the return value of time to srand prevents the program from dealing the same cards every time we run it.
-The rand function ( <stdlib.h>) produces an apparently random number each time it's called. By using the % operator, we can scale the return value from rand so that it falls between 0 and 3 (for suits) or between 0 and 12 (for ranks)
2).How do we avoid picking the same card twice
- To avoid picking the same card twice, we'll need to keep track of which cards have already been chosen.
-For that purpose, we'll use an array named in_hand that has four rows (one for each suit) and 13 columns (one for each rank)
-In other words, each element in the array corresponds to one of the 52 cards in the deck
-All of the elements will be 0 (false) to begin with
-Each time we pick a card at random, we will check whether the element of in_hand corresponding to that card is true or false
-If it's true, we will have to pick another card
-If it's false, we'll store 1 in that card's array element to remind us later that this card has already been picked
Once, we've verified that a card is "new", we will need to translate its numerical rank and suit to characters to display it for the user.
-To do that, set up two arrays of characters---one for rank and one for suit---then use the numbers to subscript the arrays
-These arrays won't change during program execution, so we may as well declare them to be const