void hand_set_contents (int which, char *contents) { if (contents == NULL) { contents = ""; } debug ("hand contents: %s\n", contents); get_hand (which)->text = g_strdup(contents); gtk_label_set_text(GTK_LABEL(get_hand (which)->label), contents); }
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); }
// Ex. 8 int main(void) { puts("Enter hand number 1"); card* hand1 = get_hand(); int eval1 = evaluate_hand(hand1); print_hand(hand1); printf("%s\n\n", hand_value[eval1]); puts("Enter hand number 2"); card* hand2 = get_hand(); int eval2 = evaluate_hand(hand2); print_hand(hand2); printf("%s\n\n", hand_value[eval2]); if(eval1 > eval2) puts("Hand number 1 wins"); else if(eval2 > eval1) puts("Hand number 2 wins"); else puts("It's a tie"); free(hand1); free(hand2); return(EXIT_SUCCESS); }
bool Hand::playable_from_hand(char * word) { //do a contains check for each letter in the word //We'll have an issue if the word requires double letters but... oh well if(!word) return false; bool does_match = true; int size = strlen(word); for(int i = 0; i < size; ++i) { char * temp_hand = NULL; temp_hand = get_hand(); if(!strchr(temp_hand, word[i])) does_match = false; } return does_match; }
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); }
bool dmpExecutorInterface::read(yarp::os::ConnectionReader& connection) { yarp::os::idl::WireReader reader(connection); reader.expectAccept(); if (!reader.readListHeader()) { reader.fail(); return false; } yarp::os::ConstString tag = reader.readTag(); while (!reader.isError()) { // TODO: use quick lookup, this is just a test if (tag == "run") { bool _return; _return = run(); yarp::os::idl::WireWriter writer(reader); if (!writer.isNull()) { if (!writer.writeListHeader(1)) return false; if (!writer.writeBool(_return)) return false; } reader.accept(); return true; } if (tag == "is_running") { bool _return; _return = is_running(); yarp::os::idl::WireWriter writer(reader); if (!writer.isNull()) { if (!writer.writeListHeader(1)) return false; if (!writer.writeBool(_return)) return false; } reader.accept(); return true; } if (tag == "stop") { bool _return; _return = stop(); yarp::os::idl::WireWriter writer(reader); if (!writer.isNull()) { if (!writer.writeListHeader(1)) return false; if (!writer.writeBool(_return)) return false; } reader.accept(); return true; } if (tag == "s") { bool _return; _return = s(); yarp::os::idl::WireWriter writer(reader); if (!writer.isNull()) { if (!writer.writeListHeader(1)) return false; if (!writer.writeBool(_return)) return false; } reader.accept(); return true; } if (tag == "execute_OPC") { int32_t id; if (!reader.readI32(id)) { reader.fail(); return false; } bool _return; _return = execute_OPC(id); yarp::os::idl::WireWriter writer(reader); if (!writer.isNull()) { if (!writer.writeListHeader(1)) return false; if (!writer.writeBool(_return)) return false; } reader.accept(); return true; } if (tag == "waitMotionDone") { double period; double timeout; if (!reader.readDouble(period)) { period = 0.5; } if (!reader.readDouble(timeout)) { timeout = 0; } bool _return; _return = waitMotionDone(period,timeout); yarp::os::idl::WireWriter writer(reader); if (!writer.isNull()) { if (!writer.writeListHeader(1)) return false; if (!writer.writeBool(_return)) return false; } reader.accept(); return true; } if (tag == "set_hand") { Hand newHand; int32_t ecast2; HandVocab cvrt3; if (!reader.readEnum(ecast2,cvrt3)) { reader.fail(); return false; } else { newHand = (Hand)ecast2; } bool _return; _return = set_hand(newHand); yarp::os::idl::WireWriter writer(reader); if (!writer.isNull()) { if (!writer.writeListHeader(1)) return false; if (!writer.writeBool(_return)) return false; } reader.accept(); return true; } if (tag == "get_hand") { Hand _return; _return = get_hand(); yarp::os::idl::WireWriter writer(reader); if (!writer.isNull()) { if (!writer.writeListHeader(1)) return false; if (!writer.writeI32((int32_t)_return)) return false; } reader.accept(); return true; } if (tag == "teach_start") { std::string actionName; Hand handToUse; if (!reader.readString(actionName)) { reader.fail(); return false; } int32_t ecast4; HandVocab cvrt5; if (!reader.readEnum(ecast4,cvrt5)) { handToUse = RIGHT; } else { handToUse = (Hand)ecast4; } bool _return; _return = teach_start(actionName,handToUse); yarp::os::idl::WireWriter writer(reader); if (!writer.isNull()) { if (!writer.writeListHeader(1)) return false; if (!writer.writeBool(_return)) return false; } reader.accept(); return true; } if (tag == "quit") { quit(); yarp::os::idl::WireWriter writer(reader); if (!writer.isNull()) { if (!writer.writeOnewayResponse()) return false; } reader.accept(); return true; } if (reader.noMore()) { reader.fail(); return false; } yarp::os::ConstString next_tag = reader.readTag(); if (next_tag=="") break; tag = tag + "_" + next_tag; } return false; }
static void move_donkeys() { DONKEY *donkey; int new_flags; int flags; int x, y; /* see the `hand' */ get_hand(); if (hand_state() == HAND_FULL) { kill_hand(); get_hand(); /* speed up */ move_freq -= get_level()->donkey.move_freq_countdown; if (move_freq < get_level()->donkey.move_freq_min) move_freq = get_level()->donkey.move_freq_min; } /* move the donkeys... */ /* EXIT */ /* go to first `exit' */ if (!get_first_hand_pos(&x, &y)) { for(ever) { /* get the donkey in the block */ donkey = get_block_donkey(x, y); /* ok? */ if (donkey) { flags = get_block_flags2(donkey->x, donkey->y); donkey->xold = donkey->x; donkey->yold = donkey->y; move_donkey_position(donkey, flags); /* the flags of the block to come */ new_flags = get_block_flags2(donkey->x, donkey->y); /* next position not is a `exit' block */ if (!(new_flags & BLOCK_FLAG_EXIT)) { if (hand_state() != HAND_BAD) { /* return to old position */ donkey->x = donkey->xold; donkey->y = donkey->yold; } } else { if ((get_block_donkey2(donkey->x, donkey->y)) && (get_block_donkey2(donkey->x, donkey->y) != donkey)) { if (hand_state() != HAND_BAD) { /* return to old position */ donkey->x = donkey->xold; donkey->y = donkey->yold; } } } /* restore `donkey' pointers in the block */ if (get_block_donkey2(donkey->xold, donkey->yold) == donkey) set_block_donkey2(donkey->xold, donkey->yold, NULL); set_block_donkey2(donkey->x, donkey->y, donkey); donkey->moved = TRUE; donkey->flip = (donkey->xold == donkey->x)? donkey->flip: (donkey->xold > donkey->x)? TRUE: FALSE; } /* go to next `exit' */ if (get_next_hand_pos(&x, &y)) break; } } /* OTHERS */ for (donkey=donkey_list; donkey; donkey=donkey->next) { if (donkey->moved) continue; flags = get_block_flags2(donkey->x, donkey->y); if (flags & BLOCK_FLAG_STOP) continue; donkey->xold = donkey->x; donkey->yold = donkey->y; move_donkey_position(donkey, flags); /* restore `donkey' pointers in the block */ if (get_block_donkey2(donkey->xold, donkey->yold) == donkey) set_block_donkey2(donkey->xold, donkey->yold, NULL); set_block_donkey2(donkey->x, donkey->y, donkey); donkey->moved = TRUE; donkey->flip = (donkey->xold == donkey->x)? donkey->flip: (donkey->xold > donkey->x)? TRUE: FALSE; } /* STOP */ for (donkey=donkey_list; donkey; donkey=donkey->next) { if (donkey->moved) { donkey->moved = FALSE; continue; } flags = get_block_flags2(donkey->x, donkey->y); donkey->xold = donkey->x; donkey->yold = donkey->y; move_donkey_position(donkey, flags); /* is new position occupied? */ if ((get_block_donkey2(donkey->x, donkey->y)) && (get_block_donkey2(donkey->x, donkey->y) != donkey)) { /* if yes... return to old position */ donkey->x = donkey->xold; donkey->y = donkey->yold; active_red_alarm(100); } /* restore `donkey' pointers in the block */ if (get_block_donkey2(donkey->xold, donkey->yold) == donkey) set_block_donkey2(donkey->xold, donkey->yold, NULL); set_block_donkey2(donkey->x, donkey->y, donkey); donkey->flip = (donkey->xold == donkey->x)? donkey->flip: (donkey->xold > donkey->x)? TRUE: FALSE; } }
char *hand_get_contents (int which) { return get_hand (which)->text; }
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); }