Ejemplo n.º 1
0
 	void check_top_ten(void)
 	{
	 if (players[10].points> players[9].points)
	     {bubblesort();}
	     save_score();
        highscore();

 	}
Ejemplo n.º 2
0
Archivo: main.c Proyecto: tmerlier/2048
int					main(int argc, char **argv)
{
	t_env				env;
	t_menu				ret_menu;

	main_init(&env, &argc, &argv);
	ret_menu = menu(&env);
	if (ret_menu == _play || ret_menu == _load)
	{
		main_loop(&env);
		save_score(&env);
	}
	endwin();
	return (0);
}
Ejemplo n.º 3
0
int main(int argc, char *argv[]) {
    int opt = 0;
    int longIndex = 0;

    /* Initialize globalArgs before we get to work. */
    globalArgs.version = NULL;    /* false */

	//prepare i18n
	setlocale(LC_ALL, "");
	bindtextdomain("4digits", LOCALE_PATH);
	textdomain("4digits");

    // Process the arguments with getopt_long(), then populate globalArgs
    opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
    while(opt != -1) {
        switch(opt) {
            case 'v':
                globalArgs.version = VERSION_STRING;
                printf("%s\n%s\n%s", VERSION, _(COPYRIGHT), _(AUTHOR));
                exit(1);
            case 'h': 
                err_exit(_(HELP));
                break;
            case '?': /* fall-through is intentional */
                err_exit(_("Usage: 4digits [OPTION]...\n"
                        "Try `4digits --help' for more information."));
                break; 
            case 0:    /* long option without a short arg */
                if(strcmp("version", longOpts[longIndex].name) == 0)
                    break; 
                break;
            default:
                /* You won't actually get here. */
                break;
        }
        opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
    }

    int ans_digits[4];
    gen_rand(ans_digits); /* array for the 4 digits of n*/
    time_t temp = time(NULL);
    time_t tic = temp;
    int guessed[8] = {0, 0, 0, 0, 0, 0, 0, 0};
    int i;
    bool dup = false;

    for (int num_guess = 0; num_guess < 8; num_guess++) {
        int A = 0, B = 0;
        int input = enter_number();

        for(int i=0; i < num_guess; i++)
            // duplicated input
            if (input == guessed[i]) {
                fprintf(stderr, _("You've already guessed it.\n"));
                --num_guess;
                dup = true;
                break;
            }

        if (dup == true) {
            dup = false;
            continue;
        }

        int in_digits[4]; /* arrays for the 4 digits of input*/
        for(i=0; i<4; i++) {
            in_digits[i]=(int) (input / tenpow(3-i) )%10;
        }

        compare(in_digits, ans_digits, &A, &B);
        printf("%dA%dB    ", A, B);
        if (num_guess != 7)
            printf(ngettext("\t %d time left.\n", "\t %d times left.\n", 7-num_guess), 7-num_guess);
        guessed[num_guess] = input;

        if(A == 4) {
            time_t toc = time(NULL);
            int score = (int)(toc-tic);
            printf(_("You win! :) Used %d sec.\n"), score);
            save_score(score); /* save score in the score file */
            return 0;
        }
    }
    printf(_("\nHaha, you lose. It is "));
    for(int i = 0; i < 4; i++)
        printf("%d", ans_digits[i]);
    printf(".\n");
    return 0;
}
Ejemplo n.º 4
0
/* parses options and stores the main game loop */
int main(int argc, char **argv)
{
    /* init ncurses environment */
    initscr();
    cbreak();
    noecho();
    curs_set(FALSE);

    /* init variables */
    file = ".hs2048g";
    hs = 0;
    s  = 0;
    sl = 0;
    SZ = 4;
    MAXVAL = 4;
    CALLOC2D(g, SZ);

    load_score();
    int n_blocks = 1;
    
    /* parse options */
    int c;
    while ((c = getopt(argc, argv, "rchs:b:")) != -1) {
        switch (c) {
            // color support - assumes your terminal can display colours
            // should still work regardless
            case 'c':
                start_color();
                init_pair(1, 1, 0);
                init_pair(2, 2, 0);
                init_pair(3, 3, 0);
                init_pair(4, 4, 0);
                init_pair(5, 5, 0);
                init_pair(6, 6, 0);
                init_pair(7, 7, 0);
                break;
            // different board sizes
            case 's':
                FREE2D(g, SZ);
                int optint = atoi(optarg);
                SZ = optint > 4 ? optint : 4;
                CALLOC2D(g, SZ);
                break;
            // different block spawn rate
            case 'b':
                n_blocks = atoi(optarg);
                break;
            // reset hiscores
            case 'r':
                endwin();
                printf("Are you sure you want to reset your highscores? (Y)es or (N)o\n");
                int response;
                if ((response = getchar()) == 'y' || response == 'Y') {
                    FILE *fd = fopen(file, "w+");
                    fclose(fd);
                }
                exit(EXIT_SUCCESS);
            // help menu
            case 'h':
                endwin();
                printf("Controls:\n"
                       "    hjkl, wasd      Movement\n"
                       "    q               Quit\n"
                       "\n"
                       "Usage:\n"
                       "    2048 [options]\n"
                       "\n"
                       "Options:\n"
                       "    -s <size>       Set the grid border length\n"
                       "    -b <rate>       Set the block spawn rate\n"
                       "    -c              Enables color support\n");
                exit(EXIT_SUCCESS);
        }
    }
    
    int width  = SZ * (MAXVAL + 2) + 1;
    int height = SZ * (MAXVAL + 2) + 3;

    // might center in middle of screen
    WINDOW *gamewin = newwin(height, width, 1, 1);
    keypad(gamewin, TRUE);

    /* random seed */
    srand((unsigned int)time(NULL));
    ITER(2, rand_block());
    draw_grid(gamewin);

    int key, moved;
    while (1) {
        /* will goto this if we didn't get a valid keypress */
        retry:;
        moved = 0;
        key = wgetch(gamewin);
        sl = 0;

        /* should check if anything changed during merge and if not retry */
        switch (key) {
            case 'h':
            case 'a':
            case KEY_LEFT:
                moved = TURN(DL);
                break;
            case 'l':
            case 'd':
            case KEY_RIGHT:
                moved = TURN(DR);
                break;
            case 'j':
            case 's':
            case KEY_DOWN:
                moved = TURN(DD);
                break;
            case 'k':
            case 'w':
            case KEY_UP:
                moved = TURN(DU);
                break;
            case 'q':
                FREE2D(g, SZ);
                erase();
                refresh();
                endwin();
                save_score();
                exit(EXIT_SUCCESS);
            default:
                goto retry;
        }
        
        if (!moves_available()) {
            endwin();
            printf("\n"
                   "YOU LOSE! - Your score was %d\n", s);
            save_score();
            exit(EXIT_SUCCESS);
        }

        if (moved) {
            ITER(n_blocks, rand_block());
            draw_grid(gamewin);
        }
    }
}