/* gets called for target widget */ static void card_drag_drop (HandDisplay *handdisp, int card, int on_card, int *to_seat) { PROTECT_BEGIN; board *b = CUR_BOARD; printf("Dropped: %s on seat %c.\n", card_string(card), "WNES"[*to_seat - 1]); if (on_card >= 0) printf("Dropped on: %s.\n", card_string(on_card)); if (b->dealt_cards[card] && b->dealt_cards[card] == *to_seat) /* card didn't move */ PROTECT_RETURN; if (b->dealt_cards[card] && !b->cards[card]) { board_statusbar(_("Card is in play and cannot be moved")); PROTECT_RETURN; } seat from_seat = b->dealt_cards[card]; if (on_card >= 0) { /* exchange 2 cards */ if (b->dealt_cards[on_card] && !b->cards[on_card]) { board_statusbar(_("Card is in play and cannot be exchanged")); PROTECT_RETURN; } remove_card(b, *to_seat, on_card); if (from_seat) { remove_card(b, from_seat, card); add_card(b, from_seat, on_card); } add_card(b, *to_seat, card); } else { /* move single card */ if (b->hand_cards[*to_seat-1] == 13) { board_statusbar(_("Hand has already 13 cards")); PROTECT_RETURN; } if (from_seat) remove_card(b, from_seat, card); add_card(b, *to_seat, card); } b->par_score = -1; invalidate_dd_scores (b); board_statusbar(NULL); card_window_update(b->dealt_cards); show_board(b, REDRAW_HANDS | REDRAW_PAR); PROTECT_END; }
int board_parse_line(const char *line, board *b, char handsep, char suitsep) { const char *c = line; seat se = west; int su = spade; while (*c && *c != '\n') { int ra; if (*c == suitsep) { su--; if (su < 0) return 0; } else if (*c == handsep) { se++; su = spade; if (se > south) return 0; } else if ((ra = parse_rank_char(*c)) >= 0) { if (add_card(b, se, (su * 13) + ra) != 1) return 0; } else { printf ("parse error at char %ld: %s\n", c - line + 1, line); return 0; } c++; } return 1; }
int test_queue(){ int i,j; Card card; Deck * deck = malloc(sizeof(Deck)); Deck * deck2 = malloc(sizeof(Deck)); Hand * hand = malloc(sizeof(Hand)); build_deck(deck); shuffle_deck(deck); build_deck(deck2); shuffle_deck(deck2); i = 0; for (;;){ printf("Initialized Deck and Hand\n"); print(deck->card, DECK_SIZE); print(hand->card, HAND_SIZE); for (j = 0; j < HAND_SIZE; ++j){ card = deal_card(deck); hand->card[j] = card; } printf("\nAdded cards to hand\n"); print(deck->card, DECK_SIZE); print(hand->card, HAND_SIZE); for (j = 0; j < HAND_SIZE; ++j){ add_card(deck, hand->card[j]); hand->card[j].suite = -1; hand->card[j].value = -1; } printf("\nPut cards back into deck\n"); print(deck->card, DECK_SIZE); print(hand->card, HAND_SIZE); printf("\n\n"); i++; if (i == 100) break; } sort_cards(deck->card, DECK_SIZE); sort_cards(deck2->card, DECK_SIZE); for (i = 0; i < DECK_SIZE; ++i) if (deck->card[i].value != deck2->card[i].value) printf("Bad Shuffle\n"); printf("Good Shuffle\n"); return 0; }
struct hand *build_hand(struct pile *pile) { struct hand *new_hand = NULL; while(pile) { add_card(&new_hand, pile->card); pile = pile->next; } return new_hand; }
/* exchanging cards from the deck into the exchange struct */ Exchange * exchange_card(Deck * deck, Exchange * e, int count){ Card card; int i; for (i = 0; i < count; ++i){ card = deal_card(deck); add_card(deck, e->card[i]); e->card[i] = card; } return e; }
static int board_parse_lin (window_board_t *win, char *line, FILE *f) { char *saveptr; char *tok; int card_nr = 0; setlocale (LC_NUMERIC, "C"); board *b = board_new (win->n_boards + 1); int board_filled = 0; board_window_append_board (win, b); /* global list of names for vugraph files */ char *name_arr[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; int name_n = 0; /* IMP/MP list */ char *mp_str = NULL; char *mp_ptr = NULL; do { for (tok = sane_strtok_r(line, "|", &saveptr); tok; tok = STRTOK) { /* single hand */ if (!strcmp(tok, "pn")) { /* SWNE */ tok = STRTOK; char *nameptr; char *name = sane_strtok_r(tok, ",", &nameptr); int i = 0; do { if (i < 4) g_string_printf(b->hand_name[seat_mod (i++) - 1], "%s", name); if (name_n < 8) name_arr[name_n++] = strdup(name); } while ((name = sane_strtok_r(NULL, ",", &nameptr))); } else if (!strcmp(tok, "md")) { /* make deal */ tok = STRTOK; if (*tok == '0') { printf ("md|0| (keep deal) mode not supported\n"); continue; } if (board_filled) { /* start new board */ card_nr = 0; board_filled = 0; b = board_new (win->n_boards + 1); board_window_append_board (win, b); /* initialize player names, required for vugraph files */ int i; for (i = 0; i < 4; i++) { g_string_printf(b->hand_name[seat_mod (i) - 1], "%s", name_arr[i + (tok[0] == 'c' ? 4 : 0)]); } } char *c; seat se = south; suit su = spade; b->dealer = seat_mod(*tok - '0' - 1); for (c = tok + 1; *c; c++) { int i; if ((i = parse_suit(*c)) != -1) { su = i; } else if ((i = parse_rank_char(*c)) != -1) { if (add_card(b, se, (su * 13) + i) != 1) goto error; } else if (*c == ',') { se = seat_mod(se + 1); } else { printf("Parse error: %s", tok); goto error; } } // TODO: end positions deal_random(b); /* compute east hand */ board_filled = 1; /* consider this board finished on next qx token */ } else if (!strcmp(tok, "ah")) { /* board name */ g_string_printf(b->name, "%s", STRTOK); } else if (!strcmp(tok, "qx")) { /* board number, o1, c1, o2, ... */ tok = STRTOK; if (board_filled) { /* first token in new vugraph board */ card_nr = 0; board_filled = 0; b = board_new (win->n_boards + 1); board_window_append_board (win, b); /* initialize player names, required for vugraph files */ int i; for (i = 0; i < 4; i++) { g_string_printf(b->hand_name[seat_mod (i) - 1], "%s", name_arr[i + (tok[0] == 'c' ? 4 : 0)]); } } if (strlen (tok) >= 1) g_string_printf(b->name, "%s %s", tok[0] == 'c' ? _("Closed") : (tok[0] == 'o' ? _("Open") : _("Board")), tok + 1); /* for now assume qx|| is present in all lin files with mp|| */ if (mp_str) { // FIXME: skip leading boards that were kibitzed but not played // (01-26-08-3.lin) char *score = mp_ptr ? sane_strtok_r (NULL, ",", &mp_ptr) : sane_strtok_r (mp_str, ",", &mp_ptr); b->mp[0] = score ? round (strtod (score, NULL) * 100.0) : 0; score = sane_strtok_r (NULL, ",", &mp_ptr); b->mp[1] = score ? round (strtod (score, NULL) * 100.0) : 0; } } else if (!strcmp(tok, "sv")) { tok = STRTOK; switch (*tok) { case '0': case 'o': b->vuln[0] = 0; b->vuln[1] = 0; break; case 'n': b->vuln[0] = 1; b->vuln[1] = 0; break; case 'e': b->vuln[0] = 0; b->vuln[1] = 1; break; case 'b': b->vuln[0] = 1; b->vuln[1] = 1; break; default: printf("Unknown vulnerability: sv|%s|\n", tok); } } else if (!strcmp(tok, "mb")) { /* mb|-ppp1Cp1Hp3Np4Dp4Hppp| */ tok = STRTOK; char *bidp = tok; char *al = strchr (bidp, '!'); if (al) { *al++ = '\0'; } do { int bid = parse_bid(&bidp); if (bid == -1) { printf("Invalid bid %s/%s\n", tok, bidp); break; } board_append_bid(b, bid, 1); if (al) { board_set_alert (b, al); al = NULL; } } while (*bidp); } else if (!strcmp(tok, "an")) { tok = STRTOK; board_set_alert (b, !strcmp (tok, "!") ? "" : tok); /* filter uninteresting ! */ } else if (!strcmp(tok, "pc")) { int c = parse_card(tok = STRTOK); if (c == -1) { printf("Invalid card %s\n", tok); continue; } if (card_nr < 52) b->played_cards[card_nr++] = c; } else if (!strcmp(tok, "mc")) { tok = STRTOK; // TODO: store number of (total) claimed tricks b->played_cards[card_nr] = claim_rest; // no card_nr increment here b->declarer_tricks = atoi (tok); /* vugraph file */ } else if (!strcmp(tok, "vg")) { /* match title */ tok = STRTOK; //printf ("Match title: %s\n", tok); if (win->title) free (win->title); if (win->subtitle) free (win->subtitle); if (win->team1) free (win->team1); if (win->team2) free (win->team2); char *t_ptr; char *title = sane_strtok_r (tok, ",", &t_ptr); if (title) win->title = strdup (title); char *subtitle = sane_strtok_r (NULL, ",", &t_ptr); if (subtitle) win->subtitle = strdup (subtitle); sane_strtok_r (NULL, ",", &t_ptr); /* scoring I IMPs P MPs B board-a-match */ sane_strtok_r (NULL, ",", &t_ptr); /* first board nr */ sane_strtok_r (NULL, ",", &t_ptr); /* last board nr */ char *team1 = sane_strtok_r (NULL, ",", &t_ptr); if (team1) win->team1 = strdup (team1); sane_strtok_r (NULL, ",", &t_ptr); /* carry-over score team 1 */ char *team2 = sane_strtok_r (NULL, ",", &t_ptr); if (team2) win->team2 = strdup (team2); /* carry-over score team 2 */ } else if (!strcmp(tok, "pw")) { /* more player names */ tok = STRTOK; //printf ("Players: %s\n", tok); } else if (!strcmp(tok, "bn")) { /* board numbers */ tok = STRTOK; //printf ("Board numbers: %s\n", tok); } else if (!strcmp(tok, "rs")) { /* results */ tok = STRTOK; //printf ("Results: %s\n", tok); } else if (!strcmp(tok, "mp")) { /* MP result */ tok = STRTOK; //printf ("Scores: %s\n", tok); mp_str = strdup (tok); } else if (!strcmp(tok, "nt")) { /* comment (new text) */ tok = STRTOK; //printf ("Comment: %s\n", tok); } else if (!strcmp(tok, "at")) { /* add text */ STRTOK; } else if (!strcmp(tok, "cr") || !strcmp(tok, "cg") || !strcmp(tok, "cb")) { /* color */ STRTOK; } else if (!strcmp(tok, "hc") || !strcmp(tok, "lc") || !strcmp(tok, "hs") || !strcmp(tok, "ls")) { STRTOK; /* hilight card, suit */ } else if (!strcmp(tok, "pg")) { STRTOK; /* page break, e.g. after trick or comment */ } else if (!strcmp(tok, "rh")) { /* reset heading */ STRTOK; } else if (!strcmp(tok, "sk")) { /* set kibitzed */ STRTOK; } else if (!strcmp(tok, "st")) { /* small text */ STRTOK; } else if (!strcmp(tok, "up")) { /* undo play */ tok = STRTOK; } else if (!*tok || *tok == '\n' || *tok == '\r') { /* empty token, hopefully end of line */ } else { printf("Unknown token '%s|%s|'\n", tok, STRTOK); } } } while (fgets(line, 1023, f)); int ret = 1; int i; goto ok; error: ret = 0; ok: for (i = 0; i < name_n; i++) free (name_arr[i]); if (mp_str) free (mp_str); setlocale (LC_NUMERIC, ""); return ret; }
string solve(string command,user*& cur_user) { vector <string> commands; string x; string sol; commands=parse(command); x=commands[0]; if(x=="exit" || x=="disconnect") return x; if(x=="signup") sol=signup(cur_user,commands); if(x=="signin") sol=signin(cur_user,commands); if(x=="signout" && commands.size()==1) { cur_user=NULL; return "signing out completed.\n"; } if(x=="show_boards") sol=show_boards(cur_user); if(x=="enter_board") sol=enter_board(cur_user,commands); if(x=="add_user") sol=add_user(cur_user,commands); if(x=="remove_user_from_board") sol=remove_user_from_board(cur_user,commands); if(x=="show_lists" && commands.size()==1) sol=show_lists(cur_user); if(x=="show_cards") sol=show_cards(cur_user,commands); if(x=="show_card") sol=show_card(cur_user,commands); if(x=="create_board") sol=create_board(cur_user,commands); if(x=="remove_board") sol=remove_board(cur_user,commands); if(x=="add_list") sol=add_list(cur_user,commands); if(x=="remove_list") sol=remove_list(cur_user,commands); if(x=="add_card") sol=add_card(cur_user,commands); if(x=="remove_card") sol=remove_card(cur_user,commands); if(x=="move_card") sol=move_card(cur_user,commands); if(x=="rename_card") sol=rename_card(cur_user,commands); if(x=="edit_card_description") sol=edit_card_des(cur_user,commands); if(x=="edit_card_due_date") sol=edit_card_date(cur_user,commands); if(x=="assign_user") sol=assign(cur_user,commands); if(x=="remove_user") sol=remove_user_from_card(cur_user,commands); if(x=="comment") sol=commenting(cur_user,commands); if(x=="filter") sol=filter(cur_user,commands); if(sol.size()==0) sol="Invalid command.\n"; return sol; }