예제 #1
0
void free_hand(struct hand *hand)
{
    if(!hand)
    {
       return;
    }

    free_hand(hand->left);
    free_hand(hand->right);
    free(hand);
}
예제 #2
0
END_TEST

START_TEST (test_hand_blackjack)
{
  hand *h = get_hand();

  /* this is not blackjack */
  add_card_to_hand(h, 40);
  add_card_to_hand(h, 20);

  score_hand(h);
  print_hand(h);
  ck_assert(!hand_is_blackjack(h) );

  /* three cards with 21 is not blackjack*/
  //add_card_to_hand(
  
  /* this should be blackjack */
  reset_hand(h);
  add_card_to_hand(h, 1);
  add_card_to_hand(h, 50);
  score_hand(h);
  print_hand(h);
  ck_assert(hand_is_blackjack(h));
  
  free_hand(h);
} 
예제 #3
0
void init_card_encs(void)
{
    if (!is_encs_init) {
        struct Hand *hand = gen_ordered_deck();     /* Do this because get_card_encoding doesn't accept ints */
        for (int i = 0; i < 52; i++)
            g_card_encs[i] = get_card_encoding(&hand->cards[i]);

        free_hand(hand, 1);
        is_encs_init = 1;
    }
}
예제 #4
0
END_TEST  

START_TEST (test_hand_to_string)
{
  hand *h = get_hand();
  char buffer[256];
  add_card_to_hand(h, 33);
  add_card_to_hand(h, 23);
  add_card_to_hand(h, 27);

  score_hand(h);
  
  strcpy(buffer, "# player ");
  hand_to_string(h, buffer);

  printf("[%s]\n", buffer);
  free_hand(h);
}
예제 #5
0
END_TEST     

/**
 * test the scoring functions */

START_TEST (test_hand_score)
{
  int test_card = 33;
  int expected = 0;
  hand *h = get_hand();

  /* empty hand, no score */
  ck_assert_msg(score_hand(h) ==  0, "expected empty hands to have zero score, got %d", score_hand(h)); 

  /* one card, easy score */
  add_card_to_hand(h, test_card);
  score_hand(h);
  print_hand(h);
  expected = get_value(test_card);
  ck_assert_msg(h->score == expected, "expected score %d, got %d", expected, h->score);

  /* two cards, no aces */
  add_card_to_hand(h, test_card+8);
  score_hand(h);
  print_hand(h);  
  expected += get_value(test_card+8);
  ck_assert_msg(h->score == expected, "expected score %d, got %d", expected, h->score);

  /* one card, an ace */
  reset_hand(h);
  add_card_to_hand(h, 1);
  score_hand(h);
  print_hand(h);
  ck_assert_int_eq(h->score, 11);
  ck_assert_int_eq(h->low_score, 1);
    
  
  /* two cards, an ace and the test_card */
  reset_hand(h);
  add_card_to_hand(h,1);
  add_card_to_hand(h, test_card);
  score_hand(h);
  print_hand(h);
  ck_assert_int_eq(h->score, 20);
  ck_assert_int_eq(h->low_score, 10);
  

  /* two cards, test_card then ace */
  reset_hand(h);
  add_card_to_hand(h, test_card);
  add_card_to_hand(h,1);
  score_hand(h);
  print_hand(h);
  /* scores should be the same as before, we just changed the ordering of the cards */
  ck_assert_int_eq(h->score, 20);
  ck_assert_int_eq(h->low_score, 10);
  
  reset_hand(h);
  add_card_to_hand(h, test_card);
  add_card_to_hand(h,1);
  add_card_to_hand(h, test_card-8);
  score_hand(h);
  print_hand(h);

  ck_assert_int_eq(h->score, 17);
  ck_assert_int_eq(h->low_score, 17);

  /* two aces */
  /* score list should be: 1+1, 1+11, 11+11 */
  /* tain't working */
  reset_hand(h);
  add_card_to_hand(h,1);
  add_card_to_hand(h,2);
  add_card_to_hand(h,12);  
  score_hand(h);
  print_hand(h);

  ck_assert_int_eq(h->score, 16);
  ck_assert_int_eq(h->low_score, 6);

  
  reset_hand(h);
  add_card_to_hand(h,1);
  add_card_to_hand(h,11);    
  add_card_to_hand(h,2);
  add_card_to_hand(h,3);
  add_card_to_hand(h,12);  
  score_hand(h);
  print_hand(h);

  ck_assert_int_eq(h->score, 20);
  ck_assert_int_eq(h->low_score, 10);

  printf("# special weird case \n");
  reset_hand(h);
  add_card_to_hand(h,40);
  add_card_to_hand(h,2);    
  add_card_to_hand(h,1);
  add_card_to_hand(h,45);
  score_hand(h);
  print_hand(h);

  //ck_assert_int_eq(h->score, 20);
  //ck_assert_int_eq(h->low_score, 10);
  
  
  /* busted?*/
  printf("# bust case \n");
  reset_hand(h);
  add_card_to_hand(h, 43);
  add_card_to_hand(h, 44);
  add_card_to_hand(h, 45);
  score_hand(h);
  print_hand(h);

  ck_assert_int_eq(h->score, h->low_score);
  ck_assert_int_eq(h->score, 30);
  
  free_hand(h);
}