CS 330 Lecture 13 – Types
Agenda
- what ?s
- program this
- a definition of type
- scalars
- arrays
- enums
- structs
- enums in C vs. enums in Java
- type qualifiers
Program This
Write pseudocode to calculate a score for a given hand in Black Jack. In this game, jacks, queens, and kings are worth 10, numeric cards (2-9) are worth their numeric value, and aces are worth either 1 or 11. The score should be maximized, without exceeding 21.
Code
blackjack.c
#include <stdio.h>
#include <stdlib.h>
enum rank_t {
ACE = 1,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK = 10,
QUEEN = 10,
KING = 10
};
enum suit_t {
HEARTS, CLUBS, DIAMONDS, SPADES
};
struct card_t {
enum rank_t ranque;
enum suit_t soot;
};
int main(int argc, char **argv) {
enum rank_t r = 37;
printf("ACE: %d\n", ACE);
printf("TWO: %d\n", TWO);
printf("QUEEN: %d\n", QUEEN);
struct card_t queen_of_hearts = {QUEEN, HEARTS};
/* queen_of_hearts.ranque = QUEEN; */
/* queen_of_hearts.soot = HEARTS; */
/* #define NCARDS 3 */
const int NCARDS = 3;
/* int *address_of_NCARDS = (int *) &NCARDS; */
/* NCARDS = 7; */
/* *address_of_NCARDS = 1; */
printf("NCARDS: %d\n", NCARDS);
struct card_t hand[] = {
{ACE, CLUBS},
{ACE, SPADES},
{ACE, DIAMONDS}
};
int score = 0;
int hay_ace = 0;
for (int i = 0; i < NCARDS; ++i) {
score += hand[i].ranque;
if (hand[i].ranque == ACE) {
hay_ace = 1;
}
}
5 ? 6 : 7;
if (score <= 11 && hay_ace) {
score += 10;
}
printf("score: %d\n", score);
return 0;
}
Haiku
Black, woman, or young
Tweaked by amendments
voter_t
‘s no static typeTweaked by amendments