Ejemplo n.º 1
0
Hand *create_hand (void)
{
    Hand *h = NULL;
    
    h = g_new(Hand, 1);
    
    if ( h == NULL )
    {
        g_error("Hand-CRITICAL **: Could not allocate memory for Hand\n");
    }
    else
    {
        h->cards = g_new(guint8, MAX_HAND_SIZE);
        
        if ( h->cards == NULL )
        {
            g_error("Hand-CRITICAL **: Could not allocate memory for Hand\n");
        }
        else
        {
            reset_hand( h );
        }
    }
    
    return h;
}
Ejemplo n.º 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);
} 
Ejemplo n.º 3
0
int
simulate_razz_game (const struct decided_cards *decided_cards,
		    unsigned long game_count,
		    void *arg,
		    rank_listener listener)
{
  unsigned long i;
  card_hand *my_hand;
  card_deck *deck;

  my_hand = create_hand (RAZZ_CARD_IN_HAND_COUNT, sort_card_by_rank);
  if (my_hand == NULL)
    {
      fprintf (stderr, "Cannot create a hand\n");
      return 1;
    }

  for (i = 0; i < game_count; i++)
    {
      deck = create_shuffled_deck ();
      if (deck == NULL)
	{
	  fprintf (stderr, "Cannot create a shuffled deck\n");
	  destroy_deck (&deck);
	  return 1;
	}
      strip_deck (deck, decided_cards);

      complete_hand (my_hand, decided_cards, deck);
      listener (arg, get_razz_rank (my_hand));

      reset_hand (my_hand);
      destroy_deck (&deck);
    }

  destroy_hand (&my_hand);
  destroy_deck (&deck);

  return 0;
}
Ejemplo n.º 4
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);
}