/* =========================================================================== * Spades_play */ void Spades_play() { char buf[1024], card_name[4]; struct dll_element * current; struct player *player, *first; struct card *card, *init, *high; unsigned int i, j, num, round, off; int game_end; /* Get the names and write them to all the players */ off = sprintf(buf, "Players are:"); for (player = get_array_b_element(Spades->players, 0); player; player = next_array_b_element(Spades->players)) { off += sprintf(buf + off, " %d %s", player->number, player->name); } off += sprintf(buf + off, ".\n"); write_all(Spades->players, buf, off); for (game_end = round = 0; (!game_end); round++) { /* * Figure out who goes first for this round. * Shuffle and deal the cards to the players. * Write the name of the player that starts the round to everyone. */ first = get_array_b_element(Spades->players, round); shuffle_deck(Spades->deck, standard_deck_shuffle_count); game_deal_deck_to_players(Spades, SPADES_PLAYER_CARD_COUNT); off = sprintf(buf, "Player %d %s starts the bidding.\n", first->number, first->name); write_all(Spades->players, buf, off); num = first->number; for (i = 0; i < Spades->player_count; i++) { player = get_array_b_element(Spades->players, num + i); write_player_unplayed_cards(player); read_spades_bid(player->fd, &player->game_data); Spades_misc->team_bid[player->number % SPADES_TEAM_COUNT] += player->game_data; off = sprintf(buf, "Player %d %s bid %d.\n", player->number, player->name, player->game_data); write_all(Spades->players, buf, off); } for (i = 0; i < SPADES_TEAM_COUNT; i++) { if (Spades_misc->team_bid[i] < 4) { Spades_misc->team_bid[i] = 4; } if (Spades_misc->team_bid[i] > SPADES_PLAYER_CARD_COUNT) { Spades_misc->team_bid[i] = SPADES_PLAYER_CARD_COUNT; } } /* Write the team bids out to everyone */ off = sprintf(buf, "Current bids are: Team 0: %d. Team 1: %d\n", Spades_misc->team_bid[0], Spades_misc->team_bid[1]); write_all(Spades->players, buf, off); /* Play a round now. */ for (j = 0; j < SPADES_PLAYER_CARD_COUNT; j++) { /* Write the name of the player that starts the round to everyone */ off = sprintf(buf, "Player %d %s starts the round.\n", first->number, first->name); write_all(Spades->players, buf, off); init = high = NULL; num = first->number; for (i = 0; i < Spades->player_count; i++) { player = get_array_b_element(Spades->players, num + i); try_again:; write_player_unplayed_cards(player); /* Get a card from the current player. */ for (read_spades_card(player->fd,READ_SPADES_CARD_PLAY,card_name); !find_card_dll_cards(&card, card_name, player->unplayed_cards, SPADES_PLAYER_CARD_COUNT); write(player->fd, "You don't have that card\n", 26), read_spades_card(player->fd, READ_SPADES_CARD_PLAY, card_name)); if (init) { /* Must follow suit if possible */ if ((card->suit != init->suit) && find_suit_dll_cards(init->suit, player->unplayed_cards)) { write(player->fd,"You must follow suit\n", 23); insert_card_dll_cards(card, player->unplayed_cards); goto try_again; } } else { init = card; } insert_before_dll_element(Spades_misc->current_cards,NULL,card); /* determine if this player should go first in the next round */ if ((NULL == high) || ((high->suit != 3)&&(card->suit == 3)) || ((high->suit == card->suit)&&(high->number < card->number))) { first = player; high = card; } write_all_current_cards(Spades->players, Spades_misc->current_cards); } /* * End the round by moving the current_cards to played_cards of the * first player of the next round. (The winner of this round.) */ while ((current = get_first_dll_element( Spades_misc->current_cards, (void **)&card))) { delete_dll_element(Spades_misc->current_cards, current); insert_before_dll_element(first->played_cards, NULL, card); } first->played_card_count += SPADES_PLAYER_COUNT; } Spades_score_round(); off = sprintf(buf, "Current scores are: Team 0: %d. Team 1: %d\n", Spades_misc->team_score[0], Spades_misc->team_score[1]); write_all(Spades->players, buf, off); game_collect_deck_from_players(Spades); } }next slide