Ejemplo n.º 1
0
int main(void)
{
	WINDOW *mainw; /* An NCurses window to draw on. */
	Pile *deck; /* A deck of cards. */

	setlocale(LC_ALL, ""); /* Important for UTF-8 to print properly. */
	srandom(time(NULL)); /* Need to seed RNG for shuffling. */

	deck = standard_deck(1); /* Create a 52 card deck (1 copy). */
	shuffle_pile(&deck, &random); /* Shuffle it using standard random() */

	/* Init NCurses and setup a colour for printing our stuff. */
    if(!initscr() || start_color() == ERR)
		return 1;
	init_pair(1, COLOR_WHITE, COLOR_BLACK);
	mainw = newwin(24,80,0,0);
	clicards_init_ncurses(); /* This call to make colours work right. */
	refresh();
	flushinp();

	wattrset(mainw, COLOR_PAIR(1));
	wprintw(mainw, "%i cards:\n", num_cards(deck));
	wprint_pile(deck, mainw); /* Print the shuffled deck. */
	wrefresh(mainw);
	getch(); /* Allow user to see it before closing. */
	delwin(mainw);
	endwin(); /* Close NCurses session. */

	free_pile(deck); /* Free up memory used by deck. */
	return 0;
}
Ejemplo n.º 2
0
int go() {
    int i;
    int *deck = XCALLOC(cards * sizeof(int));
    int ret;
    pixel *image;
    /* init deck */
    for (i = 0; i < cards; i++) {
        deck[i] = i * 360 / cards;
    }
    /* shuffle */
    for (i = 0; i < reps; i++) {
        switch (mode) {
        case PILE:
            shuffle_pile  (deck);
            break;
        case RIFFLE:
            shuffle_riffle(deck);
            break;
        case RANDOM:
            shuffle_random(deck);
            break;
        default:
            assert(0);
        }
    }
    image = flatten(deck);
    free(deck);
    ret = writeFile(file, ROWS(), COLS(), image);
    free(image);
    return ret;
}
Ejemplo n.º 3
0
int main(void)
{
	WINDOW *mainw;
	Pile *deck, *plr_hand, *cpu_hand, *tmp;
	int input, num;
	char change[5];

	setlocale(LC_ALL, "");
	srandom(time(NULL));

	deck = standard_deck(1);

    if(!initscr() || start_color() == ERR)
		return 1;
	noecho();
	init_pair(1, COLOR_WHITE, COLOR_BLACK);
	mainw = newwin(5,48,0,0);
	clicards_init_ncurses();
	refresh();
	flushinp();

	for(;;)
	{
		for(input = 0; input < 5; ++input)
			change[input] = 0;
		shuffle_pile(&deck, &random);
		plr_hand = draw_n_cards(&deck, 5);
		cpu_hand = draw_n_cards(&deck, 5);
		tmp = NULL;
		wattrset(mainw, COLOR_PAIR(1));
		wclear(mainw);
		waddstr(mainw, "Your hand: ");
		wprint_pile(plr_hand, mainw);
		waddstr(mainw, "\n           0  1  2  3  4\n"
			"Select cards to change, 'd' when done.");
		wrefresh(mainw);
		while(tolower(input = getch()) != 'd')
		{
			if(input >= '0' && input <= '4')
			{
				input -= '0';
				change[input] = !change[input];
				wmove(mainw, 1, 12+input*3);
				waddch(mainw, change[input] ? '*' : ' ');
				wrefresh(mainw);
			}
		}
		for(input = num = 0; input < 5; ++input)
		{
			if(change[input])
			{
				add_pile_to_pile(&tmp, nth_card(&plr_hand, input-num));
				++num;
			}
		}
		if(num)
			add_pile_to_pile(&plr_hand, draw_n_cards(&deck, num));
		sort_pile(&plr_hand, 0, 1);
		mvprint_pile(plr_hand, mainw, 11, 0);
		wmove(mainw, 1, 0);
		wclrtoeol(mainw);
		wmove(mainw, 2, 0);
		waddstr(mainw, "Computer's hand: ");
		sort_pile(&cpu_hand, 0, 1);
		wprint_pile(cpu_hand, mainw);
		wclrtoeol(mainw);
		waddstr(mainw, "\nHit 'q' to quit or any other key to deal again.");
		add_pile_to_pile(&deck, tmp);
		add_pile_to_pile(&deck, plr_hand);
		add_pile_to_pile(&deck, cpu_hand);
		wrefresh(mainw);
		if(getch() == 'q')
			break;
	}
	delwin(mainw);
	endwin();
	free_pile(deck);
	return 0;
}