Ejemplo n.º 1
0
deck_t* deck_new(int number, int have_joker)
{
    deck_t* deck;
    card_t* p;
    int n,i,j,k;
    int idx;

    if(have_joker)
        n = 54;
    else
        n = 52;

    deck = (deck_t*)malloc(sizeof(deck_t));
    if(!deck)
        return 0;

    deck->poker = hand_new(number * n);
    if(!deck->poker){
        deck_free(deck);
        return 0;
    }
    deck->num_pack = number;
    deck->card_adv_id = 0;
    deck->deal_index = 0;
    deck->have_joker = have_joker;

    idx = 0;
    p = deck->poker->cards;
    for(i = 0; i < number; ++i){
        for(j = cdSuitDiamond; j <= cdSuitSpade; ++j){
            for(k = cdRankAce; k <= cdRankK; ++k){
                p->rank = k;
                p->suit = j;
                p++;
            }
        }
        if(have_joker){
            p->suit = cdSuitJoker;
            p->rank = cdRankSJoker;
            p++;
            p->suit = cdSuitJoker;
            p->rank = cdRankBJoker;
            p++;
        }
    }
    deck->poker->num = number * n;

    deck_shuffle(deck);

    return deck;
}
Ejemplo n.º 2
0
Archivo: gp.c Proyecto: huangtao/qipai
void gp_start(gp_t* gp)
{
    int i,j;
    int start_num;
    card_t card;

    if (!gp)
        return;
    gp->round = 0;
    gp->game_state = GP_GAME_PLAY;
    gp->inning++;

    memset(&gp->last_hand_type, 0, sizeof(hand_type));
    memset(gp->last_hand, 0, sizeof(card_t) * GP_MAX_CARDS);

    if (gp->mode == GP_MODE_SERVER) {
        deck_shuffle(gp->deck, gp->deck_all_num);
        gp->deck_deal_index = 0;
        gp->deck_valid_num = gp->deck_all_num;

        /* draw start cards for every player */
        if (gp->game_rule == GP_RULE_DEFAULT) {
            start_num = 16;
        } else {
            start_num = 15;
        }
        for (i = 0; i < start_num; ++i) {
            for (j = 0; j < gp->player_num; ++j) {
                gp_deal(gp, &card);
                cards_add(gp->players[j].cards, GP_MAX_CARDS, &card);
            }
        }

        /* sort cards */
        for (i = 0; i < gp->player_num; ++i) {
            gp_sort(gp->players[i].cards, GP_MAX_CARDS);
        }

        /* the first player */
        gp->first_player_no = rand() % gp->player_num;
        gp->curr_player_no = gp->first_player_no;
    } else {
        for (i = 0; i < 3; i++) {
            memset(gp->players[i].cards, 0, sizeof(card_t) * GP_MAX_PLAYER);
            memset(gp->players[i].cards_played, 0, sizeof(card_t) * GP_MAX_PLAYER);
        }
        gp->first_player_no = 0;
        gp->curr_player_no = 0;
    }
}
Ejemplo n.º 3
0
void texas_start(texas_t* texas)
{
    int i,j;
    card_t card;

    if(!texas)
        return;
    deck_shuffle(texas->deck);
    texas->round = 0;
    texas->game_state = TEXAS_GAME_PREFLOP;
    for(i = 0; i < TEXAS_MAX_PLAYER; ++i){
        card_player_reset(&(texas->players[i]));
    }

    /* draw two cards for every player */
    for(i = 0; i < 2; ++i){
        for(j = 0; j < texas->player_num; ++j){
            deck_deal(texas->deck, &card);
            card_player_draw(&(texas->players[j]), &card);
        }
    }

    /* draw five board cards */
    if(texas->b_burn){
        deck_deal(texas->deck, &card);
        for(i = 0; i < 3; ++i){
            deck_deal(texas->deck, &texas->board[i]);
        }
        deck_deal(texas->deck, &card);
        deck_deal(texas->deck, &texas->board[3]);
        deck_deal(texas->deck, &card);
        deck_deal(texas->deck, &texas->board[4]);
    }
    else{
        for(i = 0; i < 5; ++i){
            deck_deal(texas->deck, &texas->board[i]);
        }
    }

    /* the first player */
    if(!texas->inning)
        texas->dealer_player_no = rand() % texas->player_num;
    else{
        texas->dealer_player_no++;
        if(texas->dealer_player_no >= texas->player_num)
            texas->dealer_player_no = 0;
    }
    texas->inning++;
    texas->curr_player_no = texas->first_player_no; 
}
Ejemplo n.º 4
0
Archivo: gp.c Proyecto: huangtao/qipai
void gp_init(gp_t* gp, int rule, int mode, int player_num)
{
    int i,j,n;

    if(!gp)
        return;
    memset(gp, 0, sizeof(gp_t));

    if (mode == GP_MODE_SERVER)
        gp->mode = GP_MODE_SERVER;
    else
        gp->mode = GP_MODE_CLIENT;
    gp->player_num = player_num;
    gp->game_state = GP_GAME_END;
    gp->game_rule = rule;
    gp->inning = 0;
    gp->turn_time = 30;

    /* initialize gp's deck */
    n = 0;
    for (i = cdSuitDiamond; i <= cdSuitSpade; ++i) {
        for (j = cdRank3; j <= cdRankQ; ++j) {
            gp->deck[n].id = (i - 1) * 13 + j;
            gp->deck[n].suit = i;
            gp->deck[n].rank = j;
            n++;
        }
    }
    if (gp->game_rule == GP_RULE_DEFAULT) {
        /* one 2, three A and four K */
        for (i = cdSuitDiamond; i <= cdSuitSpade; ++i) {
            gp->deck[n].id = (i - 1) * 13 + cdRankK;
            gp->deck[n].suit = i;
            gp->deck[n].rank = cdRankK;
            n++;
            if (i != cdSuitDiamond) {
                gp->deck[n].id = (i - 1) * 13 + cdRankAce;
                gp->deck[n].suit = i;
                gp->deck[n].rank = cdRankAce;
                n++;
            }
        }
        gp->deck[n].id = CD_ID_S2;
        gp->deck[n].suit = cdSuitSpade;
        gp->deck[n].rank = cdRank2;
        gp->deck_all_num = 48;
    }
    else if (gp->game_rule == GP_RULE_ZHUJI) {
        /* one 2, one A and three K */
        for (i = cdSuitClub; i <= cdSuitSpade; ++i) {
            gp->deck[n].id = (i - 1) * 13 + cdRankK;
            gp->deck[n].suit = i;
            gp->deck[n].rank = cdRankK;
            n++;
        }
        gp->deck[n].id = CD_ID_SA;
        gp->deck[n].suit = cdSuitSpade;
        gp->deck[n].rank = cdRankAce;
        n++;
        gp->deck[n].id = CD_ID_S2;
        gp->deck[n].suit = cdSuitSpade;
        gp->deck[n].rank = cdRank2;
        gp->deck_all_num = 45;
    }
    deck_shuffle(gp->deck, gp->deck_all_num);
    gp->deck_deal_index = 0;
    gp->deck_valid_num = gp->deck_all_num;

    for (i = 0; i < GP_MAX_PLAYER; i++){
        gp->players[i].position = i;
    }
}
Ejemplo n.º 5
0
coup_t *
coup_newgame(
    int nplayers,
    const struct coup_cb_set *cb)
{
    if (nplayers < COUP_MIN_PLAYERS || nplayers > COUP_MAX_PLAYERS)
    {
        return NULL;
    }

    /* Allocate memory for game */
    coup_t *coup = (coup_t*)malloc(sizeof(coup_t));
    if (!coup)
    {
        return NULL;
    }
    memset(coup, 0x00, sizeof(coup_t));

    /* Initialize properties */
    coup->state = COUP_STATE_NEWGAME;
    coup->turn = 0;
    coup->cb = cb;

    /* Initialize court deck */
    deck_t *court = deck_alloc();
    deck_init(court);
    int i;
    for(i=0; i<3; i++)
    {
        deck_add_card(court, card_make("Captain", "\"Steal\" 2 coins from a player. Blocks Stealing."));
        deck_add_card(court, card_make("Ambassador", "\"Exchange\" 2 cards from the deck. Blocks Stealing."));
        deck_add_card(court, card_make("Duke", "\"Tax\" 3 coins. Blocks foreign aid."));
        deck_add_card(court, card_make("Contessa", "\"Block\" assassination."));
        deck_add_card(court, card_make("Assassin", "\"Assassinate\" a player for 3 coins."));
    }
    deck_shuffle(court);
    coup->court = court;

    /* Initialize discard deck */
    deck_t *discard = deck_alloc();
    deck_init(discard);
    coup->discard = discard;

    /* Initialize player data */
    coup->nplayers = nplayers;
    coup->player = (struct coup_player*)calloc(nplayers, sizeof(struct coup_player));
    if (!coup->player)
    {
        return NULL;
    }

    for(i=0; i<nplayers; i++)
    {
        struct coup_player *player = &coup->player[i];
        player->ncoins = COUP_START_COINS;
        player->hand = deck_alloc();
        deck_init(player->hand);
        card_t *card1 = deck_take_card(coup->court);
        card_t *card2 = deck_take_card(coup->court);
        deck_add_card(player->hand, card1);
        deck_add_card(player->hand, card2);
    }

    return coup;
}
Ejemplo n.º 6
0
int mc_findbest(Hand *hand, bool player){
	int i, j, k, n;
	double score = 0.0;
	int best=0;
	double best_ev=0;
	Hand *handcopy = (Hand *)malloc(sizeof(Hand));
	Deck *deck = (Deck *)malloc(sizeof(Deck));

	hand_init(handcopy);
    deck_init(deck);
    if(hand->numCards < NUM_CARDS){
    	printf("Error: Can only advise on a full hand.\n");
    	return 0;
    }
    hand_sort(hand);

    for(i=0; i<=31; i++){/*0-31 corresponds to 00000-11111 in binary, 1 means discard that card*/
    	unsigned int seed = time(NULL);
    	score = 0;
    	for(j=0; j<NUM_TRIALS; j++){
			for(k=0; k<NUM_CARDS; k++){
				hand_add_card(handcopy, deck_remove_card(deck, hand->cards[k]));
			}
			if(handcopy->numCards < NUM_CARDS){
				hand_print(hand);
				hand_print(handcopy);
				printf("Error: Could not find all cards in deck");
				return 0;
			}
			seed = (seed+5);
			deck_shuffle(deck, seed);
			n=i;
			for(k=NUM_CARDS-1; k>=0; k--){
				if(n >= power(2, k)){
					n -= power(2, k);
					hand_discard(handcopy, k, deck);
				}
			}
			hand_draw5(handcopy, deck);
			score += imp_score_hand(handcopy);
			for(k=0; k<NUM_CARDS; k++){
				hand_discard(handcopy, k, deck);
			}
    	}
    	if(score/NUM_TRIALS > best_ev){
    		best = i;
    		best_ev = score/NUM_TRIALS;
    	}
    }

	if(player){
		printf("Current hand score: %.2f\n", imp_score_hand(hand));
		printf("Best expected hand score: %.2f\n", best_ev);
		printf("Advise discarding cards:\n");
		for(k=NUM_CARDS-1; k>=0; k--){
			if(best >= power(2, k)){
				best -= power(2, k);
				printf("\t");
				printf("%d: ", k);
				card_print(hand->cards[k]);
				printf("\n");
			}
		}
		printf("\n");
	}
    hand_destroy(handcopy, deck);
    free(handcopy);
    deck_destroy(deck);
    free(deck);
    return best;
}