teaching machines

CS 330 Lecture 10 – Enums, Structs, Call by *

February 13, 2013 by . Filed under cs330, lectures, spring 2013.

Agenda

TODO

Code

cards.c

#include <stdio.h>
#include <stdlib.h>

enum rank_t {
  ACE = 1,
  DEUCE,
  THREE,
  FOUR,
  FIVE,
  SIX,
  SEVEN,
  EIGHT,
  NINE,
  TEN,
  JACK,
  QUEEN,
  KING
};

int main(int argc, char **argv) {
  enum rank_t card1 = SEVEN;  
  enum rank_t card2 = JACK;  

  int naces = 0;
  int score = 0;
  enum rank_t hand[] = {
    42,
    ACE,
    NINE,
    ACE
  };
  const int ncards = 3;

  // Add up all the non-ACE cards first...
  for (int i = 0; i < ncards; ++i) {
    if (hand[i] == ACE) {
      ++naces;
    } else if (hand[i] == KING || hand[i] == QUEEN || hand[i] == JACK) {
      score += 10;
    } else {
      score += hand[i];
    }
  }

  if (naces) {
    // Only one ace will ever be counted as 11, so let's go ahead and blindly
    // add all but one ace on as 1s.
    score += (naces - 1);

    // See if adding one "power ace" on would shoot us past 21. If not, let's
    // power up that ace to 11 and treat all others as 1. Otherwise, treat all
    // aces as 1.
    if (score + 11 <= 21) {
      score += 11;
    } else {
      ++score;
    }
  }

  printf("score: %d\n", score);

  return 0;
}

Rank.java

package cards;

public enum Rank {
  ACE(1),
  TWO(2),
  THREE(3),
  FOUR(4),
  FIVE(5),
  SIX(6),
  SEVEN(7),
  EIGHT(8),
  NINE(9),
  TEN(10),
  JACK(11),
  QUEEN(12),
  KING(13);
  
  public int value;
  
  private Rank(int value) {
    this.value = value;
  }
}

BlackJack.java

package cards;

import static cards.Rank.*;

public class BlackJack {
  
  public static void main(String[] args) {
    int score = 0;
    int naces = 0;
    
    Rank[] hand = {
//      42,
      ACE,
      NINE,
      ACE
    };
    int ncards = hand.length;
    
    // Add up all the non-ACE cards first...
    for (int i = 0; i < ncards; ++i) {
      if (hand[i] == ACE) {
        ++naces;
      } else if (hand[i] == KING || hand[i] == QUEEN || hand[i] == JACK) {
        score += 10;
      } else {
        score += hand[i].value;
      }
    }

    if (naces > 0) {
      // Only one ace will ever be counted as 11, so let's go ahead and blindly
      // add all but one ace on as 1s.
      score += (naces - 1);

      // See if adding one "power ace" on would shoot us past 21. If not, let's
      // power up that ace to 11 and treat all others as 1. Otherwise, treat all
      // aces as 1.
      if (score + 11 <= 21) {
        score += 11;
      } else {
        ++score;
      }
    }
    
    System.out.println(score);
  }
}

HomecookedEnumRank.java

package cards;

public class HomecookedEnumRank {
  public static final HomecookedEnumRank ACE = new HomecookedEnumRank(1);
  public static final HomecookedEnumRank TWO = new HomecookedEnumRank(2);
  public static final HomecookedEnumRank THREE = new HomecookedEnumRank(3);
  public static final HomecookedEnumRank FOUR = new HomecookedEnumRank(4);
  public static final HomecookedEnumRank FIVE = new HomecookedEnumRank(5);
  public static final HomecookedEnumRank SIX = new HomecookedEnumRank(6);
  public static final HomecookedEnumRank SEVEN = new HomecookedEnumRank(7);
  public static final HomecookedEnumRank EIGHT = new HomecookedEnumRank(8);
  public static final HomecookedEnumRank NINE = new HomecookedEnumRank(9);
  public static final HomecookedEnumRank TEN = new HomecookedEnumRank(10);
  public static final HomecookedEnumRank JACK = new HomecookedEnumRank(11);
  public static final HomecookedEnumRank QUEEN = new HomecookedEnumRank(12);
  public static final HomecookedEnumRank KING = new HomecookedEnumRank(13);
  
  public int value;
  
  private HomecookedEnumRank(int value) {
    this.value = value;
  }
}

Direction.java

package cards;

public final class Direction {
  public static final Direction EAST = new Direction();
  public static final Direction WEST = new Direction();
  public static final Direction NORTH = new Direction();
  public static final Direction SOUTH = new Direction();
}

Go.java

package cards;

import static cards.Direction.EAST;

public class Go {
  public static void main(String[] args) {
//    go(KittyCorner.NORTH_BY_NORTHWEST);
  }
  
  public static void go(Direction direction) {
  }
}

//class KittyCorner extends Direction {
//  public static final Direction NORTH_BY_NORTHWEST = new Direction();
//}

path.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv) {
  char dir[] = "/home/johnch";
  char file[] = "days_of_our_lives.mp4";
  /* char path[10]; */
  char path[256];

  strcpy(path, dir);
  strcat(path, "/");
  strcat(path, file);

  printf("path: %s\n", path);

  return 0;
}

bookmarks.c

#include <stdio.h>
#include <stdlib.h>

struct bookmark_t {
  char title[256];
  char url[256];
};

void print(struct bookmark_t mark);

int main(int argc, char **argv) {

  struct bookmark_t bookmark = {"Reddit", "http://www.reddit.com"};
  print(bookmark);

  /* bookmark.title = */

  return 0;
}

void print(struct bookmark_t mark) {
  printf("<a href=\"%s\">%s</a>\n", mark.url, mark.title); 
}

Haiku

Friend’s son will mow lawns.
Mailman wouldn’t send him mine.
Gave address instead.