Exemple #1
0
void		draw(t_env *e)
{
	t_vect2i	p;
	t_vect2i	s;

	s = set_vect2i((COLS - 1) / 4, (LINES - 1) / 4 - 1);
	draw_ui(ft_itoa(e->score), s);
	p.y = -1;
	check_end(e->score, e->map);
	while (++p.y <= (s.y * 4) + 1)
	{
		p.x = -1;
		while (++p.x <= (s.x * 4))
		{
			if (p.x < (s.x * 4) && (p.y % (s.y) == 0 || p.y == (s.y * 4)))
			{
				while (p.x < (s.x * 4))
					mvprintw(p.y + 3, p.x, ((p.x++) % (s.x) == 0) ? "+" : "-");
				mvprintw(p.y + 3, p.x, "+");
			}
			else if (p.y < (s.y * 4)
						&& (p.x % (s.x) == 0 || p.x == (s.x * 4)))
				mvprintw(p.y + 3, p.x, "|");
		}
	}
	draw_numbers(e->map, s);
}
bool
restart_game(void)
{
    // reload current game
    if (!load_board())
        return false;

    // redraw board
    draw_grid();
    draw_numbers();
    copy_board();

    // get window's dimensions
    int maxy, maxx;
    getmaxyx(stdscr, maxy, maxx);

    // move cursor to board's center
    g.y = g.x = 4;
    show_cursor();

    // remove log, if any
    remove("log.txt");

    // w00t
    return true;
}
Exemple #3
0
/*F***********************************************************
 * display(void)
 * 
 * PURPOSE : Our glutDisplayFunc, it draws the borders of the board
 *           and it draws the values inside of the board to the screen
 *
 * RETURN :  void
 *
 * NOTES :   
 *F*/
void display(void) {

  draw_board();
  draw_numbers();
  
  glutPostRedisplay();
  return;
}
void
change_number(int num)
{
    // if the value is zero and the game has not been won
    if(changable() && !g.won)
    {
        char ch[2];
        sprintf(ch, "%c", num);
        g.board[g.y][g.x] = atoi(ch);
        
        draw_numbers();
        show_cursor();
    }
}
void
redraw_all(void)
{
    // reset ncurses
    endwin();
    refresh();

    // clear screen
    clear();

    // re-draw everything
    draw_borders();
    draw_grid();
    draw_logo();
    draw_numbers();

    // show cursor
    show_cursor();
}
void factoroids_draw(asteroid_type *asteroid, tuxship_type *tuxship, FF_laser_type *laser,
                     int bonus, int bonus_time, int *digits, int wave, int score, int num,
                     int tux_img, int button_pressed)
{

    int i, offset;
    int xnum, ynum;
    char str[64];
    SDL_Surface* surf;
    SDL_Rect dest;

    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));

    /************ Draw Background ***************/

    factoroids_draw_bkgr();

    /******************* Draw laser *************************/
    for (i=0;i<MAX_LASER;i++){
        if(laser[i].alive)
        {
            if(laser[i].count>0)
            {
                laser[i].x += tuxship->xspeed*FC_time_elapsed;
                laser[i].y += tuxship->yspeed*FC_time_elapsed;
                laser[i].destx += tuxship->xspeed*FC_time_elapsed;
                laser[i].desty += tuxship->yspeed*FC_time_elapsed;

                draw_line(screen, laser[i].x, laser[i].y, laser[i].destx, laser[i].desty,
                        laser[i].count*laser_coeffs[laser[i].n][0], laser[i].count*laser_coeffs[laser[i].n][1], laser[i].count*laser_coeffs[laser[i].n][2]);

                laser[i].count -= 15*FC_time_elapsed;
            } else if (laser[i].count <= 0)
            {
                laser[i].alive=0;
            }
        }
    }
    /*************** Draw Ship ******************/

    if(!tuxship->hurt || (tuxship->hurt && tuxship->hurt_count%2==0)){
        dest.x = (tuxship->x - (IMG_tuxship[tuxship->angle/DEG_PER_ROTATION]->w/2));
        dest.y = (tuxship->y - (IMG_tuxship[tuxship->angle/DEG_PER_ROTATION]->h/2));
        dest.w = IMG_tuxship[tuxship->angle/DEG_PER_ROTATION]->w;
        dest.h = IMG_tuxship[tuxship->angle/DEG_PER_ROTATION]->h;

        //Change the image based on if the rocket is thrusting
        //Google code in task

        if(!tuxship->thrust) {
            SDL_Surface **_IMG_ship = bonus == TB_CLOAKING && bonus_time>0 ? IMG_tuxship_cloaked : IMG_tuxship;
            SDL_BlitSurface(_IMG_ship[tuxship->angle/DEG_PER_ROTATION], NULL, screen, &dest);
        } else {
            SDL_Surface **_IMG_ship = bonus == TB_CLOAKING && bonus_time>0 ? IMG_tuxship_thrust_cloaked : IMG_tuxship_thrust;
            SDL_BlitSurface(_IMG_ship[tuxship->angle/DEG_PER_ROTATION], NULL, screen, &dest);
        }



        if(bonus == TB_FORCEFIELD && bonus_time > 0) {
            SDL_Rect tmp = {tuxship->x - images[IMG_FORCEFIELD]->w/2, tuxship->y - images[IMG_FORCEFIELD]->h/2};
            SDL_BlitSurface(images[IMG_FORCEFIELD], NULL, screen, &tmp);
        }
    }

    /************* Draw Asteroids ***************/
    for(i=0; i<MAX_ASTEROIDS; i++){
        if(asteroid[i].alive>0){

            xnum=0;
            ynum=0;

            dest.x = asteroid[i].x;
            dest.y = asteroid[i].y;

            surf=get_asteroid_image(asteroid[i].size,asteroid[i].angle);

            dest.w = surf->w;
            dest.h = surf->h;

            SDL_BlitSurface(surf, NULL, screen, &dest);

            // Wrap the numbers of the asteroids
            if((asteroid[i].centery)>23 && (asteroid[i].centery)<screen->h)
            {
                if((asteroid[i].centerx)>0 && (asteroid[i].centerx)<screen->w)
                {
                    xnum=asteroid[i].centerx-3;
                    ynum=asteroid[i].centery;
                }
                else if((asteroid[i].centerx)<=0){
                    xnum=20;
                    ynum=asteroid[i].centery;
                }
                else if((asteroid[i].centerx)<=screen->w){
                    xnum=screen->w-20;
                    ynum=asteroid[i].centery;
                }
            }
            else if((asteroid[i].centery)<=23)
            {
                xnum=asteroid[i].centerx;
                ynum=23;
            }
            else if((asteroid[i].centery)>=screen->h)
            {
                xnum=asteroid[i].centerx;
                ynum=screen->h-7;
            }

            //Draw Numbers
            if(FF_game==FACTOROIDS_GAME)
            {
                sprintf(str, "%.1d", asteroid[i].fact_number);
                draw_nums(zoom, str, xnum, ynum, &white);
            }
            else if (FF_game==FRACTIONS_GAME)
            {
                sprintf(str, "%d", asteroid[i].a);
                draw_nums(zoom, str, xnum, ynum, &white);
                draw_line(screen, xnum, ynum + 4, xnum + 30, ynum + 4,
                        255, 255, 255);
                sprintf(str, "%d", asteroid[i].b);
                draw_nums(zoom, str, xnum, ynum + 35, &white);
            }
        }
    }
    /*************** Draw Steam ***************/
    for(i=0; i<MAX_ASTEROIDS; i++)
    {
        if(asteroid[i].isdead) {
            dest.x = asteroid[i].xdead;
            dest.y = asteroid[i].ydead;
            SDL_BlitSurface(images[IMG_STEAM1+asteroid[i].countdead], NULL, screen, &dest);
            if(bonus == TB_POWERBOMB && bonus_time > 0)
                draw_line(screen, asteroid[i].x, asteroid[i].y, tuxship->x, tuxship->y,
                          (5 - asteroid[i].countdead)*4*laser_coeffs[digits[1]*10+digits[2]][0],
                          (5 - asteroid[i].countdead)*4*laser_coeffs[digits[1]*10+digits[2]][1],
                          (5 - asteroid[i].countdead)*4*laser_coeffs[digits[1]*10+digits[2]][2]);
        }


        if(asteroid[i].isdead) {
            dest.x = asteroid[i].xdead;
            dest.y = asteroid[i].ydead;
            SDL_BlitSurface(images[IMG_STEAM1+asteroid[i].countdead], NULL, screen, &dest);
            asteroid[i].countdead++;
            if(asteroid[i].countdead > 5)
            {
                asteroid[i].isdead = 0;
                asteroid[i].countdead = 0;
            }
        }
    }

    /* Draw wave: */
    if (1)//Opts_BonusCometInterval())
        offset = images[IMG_EXTRA_LIFE]->w + 5;
    else
        offset = 0;

    dest.x = offset;

    dest.y = 0;
    dest.w = images[IMG_WAVE]->w;
    dest.h = images[IMG_WAVE]->h;

    SDL_BlitSurface(images[IMG_WAVE], NULL, screen, &dest);

    sprintf(str, "%d", wave);
    draw_numbers(screen, str, offset+images[IMG_WAVE]->w + (images[IMG_NUMBERS]->w / 10), 0);

    /* Draw "score" label: */
    dest.x = (screen->w - ((images[IMG_NUMBERS]->w/10) * 7) -
            images[IMG_SCORE]->w -
            images[IMG_STOP]->w - 5);
    dest.y = 0;
    dest.w = images[IMG_SCORE]->w;
    dest.h = images[IMG_SCORE]->h;

    SDL_BlitSurface(images[IMG_SCORE], NULL, screen, &dest);

    sprintf(str, "%.6d", score);
    draw_numbers(screen, str,
                screen->w - ((images[IMG_NUMBERS]->w / 10) * 6) - images[IMG_STOP]->w - 5,
                0);

    /* Draw stop button: */
    //  if (!help_controls.x_is_blinking || (frame % 10 < 5)) {
    dest.x = (screen->w - images[IMG_STOP]->w);
    dest.y = 0;
    dest.w = images[IMG_STOP]->w;
    dest.h = images[IMG_STOP]->h;

    SDL_BlitSurface(images[IMG_STOP], NULL, screen, &dest);
    // }

    /************* Draw pre answer ************/


    if(screen->w < 800 && screen->h < 600)
    {
        sprintf(str, "%.3d", num);
        draw_numbers(screen, str, ((screen->w)/2) - 50, (screen->h) - 30);
    }
    else
    {
        factoroids_draw_led_console(wave, num, button_pressed);
        draw_console_image(tux_img);
    }

    /************** Draw lives ***************/
    dest.y = screen->h;
    dest.x = 0;

    for(i = 1; i <= tuxship->lives; i++)
    {
        if(tuxship->lives <= 5)
        {
            dest.y = dest.y - (IMG_lives_ship->h);
            SDL_BlitSurface(IMG_lives_ship, NULL, screen, &dest);
        }
        else if(tuxship->lives > 4)
        {
            dest.y = screen->h - (IMG_lives_ship->h);
            SDL_BlitSurface(IMG_lives_ship, NULL, screen, &dest);
            sprintf(str, "%d", tuxship->lives);
            draw_numbers(screen, str, 10, (screen->h) - 30);
        }
    }

    /*** Draw Bonus Indicator ***/
    static int blink = 0;
    if(bonus_time == 0)
        blink = 0;
    else if(bonus_time - SDL_GetTicks() > 3000)
        blink = 5;
    else
        blink = (blink + 1) % 10;
    if(bonus != -1 && blink>4) {
        SDL_Surface *indicator = images[bonus_img_ids[bonus]];
        SDL_Rect pos = {screen->w - indicator->w, screen->h - indicator->h};
        SDL_BlitSurface(indicator, NULL, screen, &pos);
    }
}
Exemple #7
0
/*
 * Main driver for the game.
 */
int
main(int argc, char **argv) {
    // define usage
    const char *usage = "Usage: sudoku n00b|l33t [#]\n";

    // ensure that number of arguments is as expected
    if (argc != 2 && argc != 3) {
        fprintf(stderr, usage);
        return 1;
    }

    // ensure that level is valid
    if (!strcmp(argv[1], "debug"))
        g.level = "debug";
    else if (!strcmp(argv[1], "n00b"))
        g.level = "n00b";
    else if (!strcmp(argv[1], "l33t"))
        g.level = "l33t";
    else {
        fprintf(stderr, usage);
        return 2;
    }

    // n00b and l33t levels have 1024 boards; debug level has 9
    int max = (!strcmp(g.level, "debug")) ? 9 : 1024;

    // ensure that #, if provided, is in [1, max]
    if (argc == 3) {
        // ensure n is integral
        char c;
        if (!sscanf(argv[2], " %d %c", &g.number, &c) == 1) {
            fprintf(stderr, usage);
            return 3;
        }

        // ensure n is in [1, max]
        if (g.number < 1 || g.number > max) {
            fprintf(stderr, "That board # does not exist!\n");
            return 4;
        }

        // seed PRNG with # so that we get same sequence of boards
        srand(g.number);
    }
    else {
        // seed PRNG with current time so that we get any sequence of boards
        srand(time(NULL));

        // choose a random n in [1, max]
        g.number = rand() % max + 1;
    }

    // start up ncurses
    if (!startup()) {
        fprintf(stderr, "Error starting up ncurses!\n");
        return 6;
    }

    // register handler for SIGWINCH (SIGnal WINdow CHanged)
    signal(SIGWINCH, (void (*)(int)) handle_signal);

    // start the first game
    if (!restart_game()) {
        shutdown();
        fprintf(stderr, "Could not load board from disk!\n");
        return 7;
    }
    redraw_all();

    // let the user play!
    int ch;
    do {
        // refresh the screen
        refresh();

        // get user's input
        ch = getch();

        // capitalize input to simplify cases
        ch = toupper(ch);

        // process user's input
        switch (ch) {

            // left arrow key
            case KEY_LEFT:
            case 'H':
                if (--g.x < 0)
                    g.x += DIM;
                hide_banner();
                show_cursor();
                break;
            // right arrow key
            case KEY_RIGHT:
            case 'L': 
                if (++g.x >= DIM)
                    g.x -= DIM;
                hide_banner();
                show_cursor();
                break;
            // up arrow key
            case KEY_UP:
            case 'K':
                if (--g.y < 0)
                    g.y += DIM;
                hide_banner();
                show_cursor();
                break;
            // down arrow key
            case KEY_DOWN:
            case 'J':
                if (++g.y >= DIM)
                    g.y -= DIM;
                hide_banner();
                show_cursor();
                break;

            // enable user to enter numbers
            case '1': case '2': 
            case '3': case '4': 
            case '5': case '6': 
            case '7': case '8': 
            case '9':
                if (g.given[g.y][g.x] == false && validate(ch) == true) {
                    enter_number(ch);
                    draw_numbers();
                    show_cursor();
                    won();
                }
                break; 

            // delete a number
            case KEY_BACKSPACE:
            case KEY_DC:
            case '.':
            case '0':
                if (g.given[g.y][g.x] == false) {
                    g.board[g.y][g.x] = 0; 
                    draw_numbers();
                    hide_banner();
                    show_cursor();
                }
                break;
            // start a new game
            case 'N': 
                g.number = rand() % max + 1;
                if (!restart_game()) {
                    shutdown();
                    fprintf(stderr, "Could not load board from disk!\n");
                    return 7;
                }
                break;

            // restart current game
            case 'R': 
                if (!restart_game()) {
                    shutdown();
                    fprintf(stderr, "Could not load board from disk!\n");
                    return 7;
                }
                break;

            // let user manually redraw screen with ctrl-L
            case CTRL('l'):
                redraw_all();
                break;
        }   // end switch statement

        // log input (and board's state) if any was received this iteration
        if (ch != ERR)
            log_move(ch);

    } while (ch != 'Q');

    // shut down ncurses
    shutdown();

    // tidy up the screen (using ANSI escape sequences)
    printf("\033[2J");
    printf("\033[%d;%dH", 0, 0);

    // kthxbai
    printf("\nkthxbai!\n\n");
    return 0;
}
Exemple #8
0
int
main(int argc, char *argv[])
{
    // define usage
    const char *usage = "Usage: sudoku n00b|l33t [#]\n";

    // ensure that number of arguments is as expected
    if (argc != 2 && argc != 3)
    {
        fprintf(stderr, usage);
        return 1;
    }

    // ensure that level is valid
    if (strcmp(argv[1], "debug") == 0)
        g.level = "debug";
    else if (strcmp(argv[1], "n00b") == 0)
        g.level = "n00b";
    else if (strcmp(argv[1], "l33t") == 0)
        g.level = "l33t";
    else
    {
        fprintf(stderr, usage);
        return 2;
    }

    // n00b and l33t levels have 1024 boards; debug level has 9
    int max = (strcmp(g.level, "debug") == 0) ? 9 : 1024;

    // ensure that #, if provided, is in [1, max]
    if (argc == 3)
    {
        // ensure n is integral
        char c;
        if (sscanf(argv[2], " %d %c", &g.number, &c) != 1)
        {
            fprintf(stderr, usage);
            return 3;
        }

        // ensure n is in [1, max]
        if (g.number < 1 || g.number > max)
        {
            fprintf(stderr, "That board # does not exist!\n");
            return 4;
        }

        // seed PRNG with # so that we get same sequence of boards
        srand(g.number);
    }
    else
    {
        // seed PRNG with current time so that we get any sequence of boards
        srand(time(NULL));

        // choose a random n in [1, max]
        g.number = rand() % max + 1;
    }

//////////////

    for (int m=0; m<9; m++)
    {
	for (int n=0; n<9; n++)
	    {
	    	g.badMoves[m][n]=0;
	    }
    }

    // start up ncurses
    if (!startup())
    {
        fprintf(stderr, "Error starting up ncurses!\n");
        return 5;
    }

    // register handler for SIGWINCH (SIGnal WINdow CHanged)
    signal(SIGWINCH, (void (*)(int)) handle_signal);

    // start the first game
    if (!restart_game())
    {
        shutdown();
        fprintf(stderr, "Could not load board from disk!\n");
        return 6;
    }
    redraw_all();

    // let the user play!
    int ch;
    do
    {
        // refresh the screen
        refresh();

        // get user's input
        ch = getch();
	//check won

        // capitalize input to simplify cases
        ch = toupper(ch);

        // process user's input
        switch (ch)
        {
            // start a new game
            case 'N': 
                g.number = rand() % max + 1;
                if (!restart_game())
                {
                    shutdown();
                    fprintf(stderr, "Could not load board from disk!\n");
                    return 6;
                }
                break;

            // restart current game
            case 'R': 
                if (!restart_game())
                {
                    shutdown();
                    fprintf(stderr, "Could not load board from disk!\n");
                    return 6;
                }
                break;

            // let user manually redraw screen with ctrl-L
            case CTRL('l'):
                redraw_all();
                break;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////


            //getting input as a char and logging as the ascii value

            case '9':
            movenumber=9;
            if (tempBoard[g.y][g.x]!=0)///
	     break;///
            if (check_valid(movenumber))
	     {
	        g.board[g.y][g.x]=9;
               draw_numbers();
               show_cursor();////

	        //printf ("%c" g.board[3][3]);
	        //printf ("%c" tempBoard[3][3]);
             }
             break;
                
            case '8':
 	     movenumber=8;
            if (tempBoard[g.y][g.x]!=0)///
	     break;///
	     if (check_valid(movenumber))
	     {
	     g.board[g.y][g.x]=8;
            draw_numbers();
            show_cursor();////
            }
	     break;
            
            case '7':
            movenumber=7;
            if (tempBoard[g.y][g.x]!=0)///
	     break;///
	     if (check_valid(movenumber))
	     {
            g.board[g.y][g.x]=7;
            draw_numbers();
            show_cursor();////
            }
            break;
            
            
            case '6':
 	     movenumber=6;
            if (tempBoard[g.y][g.x]!=0)///
	     break;///
check_valid(movenumber);
	     g.board[g.y][g.x]=6;
            draw_numbers();
            show_cursor();////
            
            break;
            
            case '5':
            movenumber=5;
            if (tempBoard[g.y][g.x]!=0)///
	     break;///
check_valid(movenumber);
	     g.board[g.y][g.x]=5;
            draw_numbers();
            show_cursor();////
            
            break;
            
            case '4':
	     movenumber=4;
            if (tempBoard[g.y][g.x]!=0)///
	     break;///
check_valid(movenumber);
	     g.board[g.y][g.x]=4;
            draw_numbers();
            show_cursor();////
            
            break;
            
            
            case '3':
 	     movenumber=3;
            if (tempBoard[g.y][g.x]!=0)///
	     break;///
check_valid(movenumber);
	     g.board[g.y][g.x]=3;
            draw_numbers();
            show_cursor();////
            
            break;
            
            
            case '2':
 	     movenumber=2;
            if (tempBoard[g.y][g.x]!=0)///
	     break;///
check_valid(movenumber);
	     g.board[g.y][g.x]=2;
            draw_numbers();
            show_cursor();////
            
            break;
            
            case '1':
 	     movenumber=1;
            if (tempBoard[g.y][g.x]!=0)///
	     break;///
check_valid(movenumber);
	     g.board[g.y][g.x]=1;
            draw_numbers();
            show_cursor();////
        
            break;
           
             
             case '0':
                        
             break;

		case KEY_UP:
			
              // move cursor to new place
		if (g.y<1)
		{
		g.y = 8;
		}
		else
   		g.y = g.y-1;
		
		
   		show_cursor();
		break;
		
		case KEY_DOWN:
			
              // move cursor to new place
		if (g.y>7)
		{
		g.y = 0;
		}
		else
   		g.y = g.y+1;
		
		
   		show_cursor();
		break;

		case KEY_RIGHT:
			
              // move cursor to new place
		if (g.x>7)
		{
		g.x = 0;
		}
		else
   		
		g.x = g.x+1;
		
		
   		show_cursor();
		break;

		case KEY_LEFT:
			
              // move cursor to new place
		if (g.x<1)
		{
		g.x = 8;
		}
		else
   		
		g.x = g.x-1;
		
		
   		show_cursor();
		break;

        }

        // log input (and board's state) if any was received this iteration
        if (ch != ERR)
            log_move(ch);


		if (!badBanner())
		{
		hide_banner();
		show_cursor();
		}


		////call badBanner

    }          ///////////////////////////////goes with do way above////////


    while (ch != 'Q');

    // shut down ncurses
    shutdown();

    // tidy up the screen (using ANSI escape sequences)
    printf("\033[2J");
    printf("\033[%d;%dH", 0, 0);

    // that's all folks
    printf("\nkthxbai!\n\n");
    return 0;
}
Exemple #9
0
int
main(int argc, char *argv[])
{
    // define usage
    const char *usage = "Usage: sudoku n00b|l33t [#]\n";

    // ensure that number of arguments is as expected
    if (argc != 2 && argc != 3)
    {
        fprintf(stderr, usage);
        return 1;
    }

    // ensure that level is valid
    if (strcmp(argv[1], "debug") == 0)
        g.level = "debug";
    else if (strcmp(argv[1], "n00b") == 0)
        g.level = "n00b";
    else if (strcmp(argv[1], "l33t") == 0)
        g.level = "l33t";
    else
    {
        fprintf(stderr, usage);
        return 2;
    }

    // n00b and l33t levels have 1024 boards; debug level has 9
    int max = (strcmp(g.level, "debug") == 0) ? 9 : 1024;

    // ensure that #, if provided, is in [1, max]
    if (argc == 3)
    {
        // ensure n is integral
        char c;
        if (sscanf(argv[2], " %d %c", &g.number, &c) != 1)
        {
            fprintf(stderr, usage);
            return 3;
        }

        // ensure n is in [1, max]
        if (g.number < 1 || g.number > max)
        {
            fprintf(stderr, "That board # does not exist!\n");
            return 4;
        }

        // seed PRNG with # so that we get same sequence of boards
        srand(g.number);
    }
    else
    {
        // seed PRNG with current time so that we get any sequence of boards
        srand(time(NULL));

        // choose a random n in [1, max]
        g.number = rand() % max + 1;
    }

    // start up ncurses
    if (!startup())
    {
        fprintf(stderr, "Error starting up ncurses!\n");
        return 5;
    }

    // register handler for SIGWINCH (SIGnal WINdow CHanged)
    signal(SIGWINCH, (void (*)(int)) handle_signal);

    // start the first game
    if (!restart_game())
    {
        shutdown();
        fprintf(stderr, "Could not load board from disk!\n");
        return 6;
    }
    redraw_all();
  

    // let the user play!
    int ch;
    
    int last[3] = {10};
    do
    {
        // refresh the screen
        refresh();

        // get user's input
        ch = getch();

        // capitalize input to simplify cases
        ch = toupper(ch);
         

        // process user's input
        switch (ch)
        {
            // start a new game
            case 'N': 
                g.number = rand() % max + 1;
                if (!restart_game())
                {
                    shutdown();
                    fprintf(stderr, "Could not load board from disk!\n");
                    return 6;
                }
                break;

            // restart current game
            case 'R': 
                if (!restart_game())
                {
                    shutdown();
                    fprintf(stderr, "Could not load board from disk!\n");
                    return 6;
                }
                break;

            // let user manually redraw screen with ctrl-L
            case CTRL('l'):
                redraw_all();
                break;
                
            
            
            case 'U': case CTRL('Z'):
                if (last[0] != 10 && !game_won())
                {
                    g.board[last[0]][last[1]] = last[2]; 
                    draw_numbers();
                    if (check_cols(1) || check_rows(1) || check_squares(1))
                    hide_banner();
                    show_cursor();
                    warn();
                }
               
                break; 
                
                // move cursor 
                
            case KEY_UP: 
                if (g.y > 0)
                {
                    g.y -= 1;
                    show_cursor();
                }
                else
                {
                    g.y = 8;
                    show_cursor();
                }          
                break; 
            
            case KEY_DOWN: 
                if (g.y < 8)
                {
                    g.y += 1;
                    show_cursor();
                }          
                else
                {
                    g.y = 0;
                    show_cursor();
                }    
                break; 
                
            case KEY_LEFT: 
                if (g.x > 0)
                {
                    g.x -= 1;
                    show_cursor();
                }          
                else
                {
                    g.x = 8;
                    show_cursor(); 
                }
                break; 
              
             case KEY_RIGHT: 
                if (g.x < 8)
                {
                    g.x += 1;
                    show_cursor();
                }    
                else
                {
                    g.x = 0;
                    show_cursor(); 
                }      
                break; 
                
             //input number and check if game is won
             case '1' ... '9':                
                if (g.init_board[g.y][g.x] == 0)
                {                    
                    last[0] = g.y;
                    last[1] = g.x;
                    last[2] = g.board[g.y][g.x];
                    g.board[g.y][g.x] = ch - 48; 
                    draw_numbers(); 
                    if (check_cols(1) || check_rows(1) || check_squares(1))
                    hide_banner(); 
                    show_cursor();
                }
                game_won();
                warn();                                
                
                break; 
                
             //return to blank space
             case '0': case '.': case KEY_BACKSPACE: case KEY_DC:
                if (g.init_board[g.y][g.x] == 0)
                {
                    last[0] = g.y;
                    last[1] = g.x;
                    last[2] = g.board[g.y][g.x];
                    g.board[g.y][g.x] = 0; 
                    draw_numbers();
                    if (check_cols(1) || check_rows(1) || check_squares(1))
                    hide_banner(); 
                    show_cursor();                   
                }               
                warn(); 
                break;          
        }

        // log input (and board's state) if any was received this iteration
        if (ch != ERR)
            log_move(ch);
    }
    while (ch != 'Q');

    // shut down ncurses
    shutdown();

    // tidy up the screen (using ANSI escape sequences)
    printf("\033[2J");
    printf("\033[%d;%dH", 0, 0);

    // that's all folks
    printf("\nkthxbai!\n\n");
    return 0;
}
Exemple #10
0
static void garnish_distibution(unsigned int num_counts, const char *title)
{
    unsigned int i;

    /* Set normal font */
    fprintf(distribution.out, "/Helvetica findfont ");
    fprintf(distribution.out, "%u scalefont setfont ", NORMAL_FONT_SIZE);

    /* Figure out max string length: answer on stack */
    max_length(distribution.out,
               REMARKS,
               distribution.remarks[TOP],
               distribution.remarks[BOTTOM],
               num_counts);

    /* We need to be at far right + width of remarks column (on stack) */
    fprintf(distribution.out, "dup %u add 0 moveto\n",
            distribution.remark);
    /* Takes column width from the stack, moves cursor LEFT. */
    draw_column(distribution.out, REMARKS,
                distribution.remarks[TOP],
                distribution.remarks[BOTTOM],
                NORMAL_FONT_SIZE,
                num_counts,
                false);

    /* Move to origin. */
    fprintf(distribution.out, "0 0 moveto\n");

    /* Moves cursor left */
    draw_numbers(distribution.out, num_counts, false);

    /* Draw table title */
    fprintf(distribution.out, "/Helvetica findfont ");
    fprintf(distribution.out, "%u scalefont setfont ",
            TOP_LEFT_INFO_FONT_SIZE);
    /* Move up a little */
    fprintf(distribution.out, "0 %u rmoveto\n", TOP_LEFT_INFO_FONT_SIZE);
    fprintf(distribution.out,"gsave (Table II - Distribution of the Effective Votes - %s)"
            " show grestore\n",
            get_time_string());

    /* Draw quota calculation */
    if (distribution.quota != 0) {
        fprintf(distribution.out,
                "0 %u rmoveto gsave\n", TOP_LEFT_INFO_FONT_SIZE*3);
        fprintf(distribution.out, "(Quota = ) show\n");
        fprintf(distribution.out,
                "gsave 0 %u rmoveto (   %u) show grestore\n",
                TOP_LEFT_INFO_FONT_SIZE/2+3,
                distribution.num_formals);
        fprintf(distribution.out,
                "gsave  0 -%u rmoveto ( %u+1) show grestore\n",
                TOP_LEFT_INFO_FONT_SIZE/2+3,
                distribution.num_seats);
        fprintf(distribution.out,
                "gsave %u 0 rlineto stroke grestore\n",
                TOP_LEFT_INFO_FONT_SIZE * 5);
        fprintf(distribution.out,
                "%u 0 rmoveto (  + 1 = %u) show grestore\n",
                TOP_LEFT_INFO_FONT_SIZE * 5, distribution.quota);
    }

    /* Draw total for casual vacancy. */
    if (distribution.vacancy_total_votes != 0) {
        fprintf(distribution.out,
                "0 %u rmoveto gsave"
                " (Total votes to be distributed = %u)"
                " show grestore\n",
                TOP_LEFT_INFO_FONT_SIZE*3,
                distribution.vacancy_total_votes);
    }

    /* Draw title */
    fprintf(distribution.out, "/Helvetica findfont ");
    fprintf(distribution.out, "%u scalefont setfont ", TITLE_FONT_SIZE);

    fprintf(distribution.out,
            "0 %u rmoveto\n",
            TOP_LEFT_INFO_FONT_SIZE + TITLE_FONT_SIZE*2);
    fprintf(distribution.out,
            "gsave"
            " (Scrutiny Sheet for %s - Division of %s) "
            "show grestore\n",
            title, distribution.ename);

    for (i = 0; i < num_counts; i++) {
        free(distribution.remarks[TOP][i]);
        distribution.remarks[TOP][i] = NULL;
        free(distribution.remarks[BOTTOM][i]);
        distribution.remarks[BOTTOM][i] = NULL;
    }
}
Exemple #11
0
static void garnish_counting(unsigned int num_counts, const char *title)
{
    unsigned int i;

    fprintf(counting.out, "/Helvetica findfont ");
    fprintf(counting.out, "%u scalefont setfont ", DESCRIP_FONT_SIZE);
    /* Figure out max string length: answer on stack */
    max_length(counting.out,
               COUNT_DESCRIP,
               counting.count_descrip[TOP],
               counting.count_descrip[BOTTOM],
               num_counts);

    /* Move to origin. */
    fprintf(counting.out, "0 0 moveto\n");

    /* Takes column width from the stack, moves cursor LEFT. */
    /* Note that draw_column considers pairs above and below the line,
       and Table 1 considers the pairs to be those within a "box".
       So TOP and BOTTOM are reversed, and TOP is move "up" one */
    draw_column(counting.out, COUNT_DESCRIP,
                counting.count_descrip[BOTTOM],
                counting.count_descrip[TOP]+1,
                DESCRIP_FONT_SIZE,
                num_counts,
                true);
    /* Moves cursor left again */
    draw_numbers(counting.out, num_counts, true);

    /* Draw table title */
    fprintf(counting.out, "/Helvetica findfont ");
    fprintf(counting.out, "%u scalefont setfont ",
            TOP_LEFT_INFO_FONT_SIZE);

    /* Move up a little */
    fprintf(counting.out, "0 %u rmoveto\n", TOP_LEFT_INFO_FONT_SIZE);
    fprintf(counting.out,"gsave (Table I - Counting of the Choices - %s) show grestore\n",
            get_time_string());

    /* Draw number of votes */

    fprintf(counting.out,
            "0 %u rmoveto\n", TOP_LEFT_INFO_FONT_SIZE*3);
    /* mod for cas vac */
    if (counting.num_formals == 0) counting.num_formals = distribution.vacancy_total_ballots;
    fprintf(counting.out,
            "gsave (Number of formal papers: %u."
            "  Number of informal papers: %u.) show grestore\n",
            counting.num_formals, counting.num_informals);

    /* Draw title */
    fprintf(counting.out, "/Helvetica findfont ");
    fprintf(counting.out, "%u scalefont setfont ", TITLE_FONT_SIZE);
    fprintf(counting.out,
            "0 %u rmoveto\n",
            TOP_LEFT_INFO_FONT_SIZE + TITLE_FONT_SIZE*2);
    fprintf(counting.out,
            "gsave"
            " (Scrutiny Sheet for %s - Division of %s) "
            "show grestore\n",
            title, counting.ename);

    for (i = 0; i < num_counts; i++) {
        free(counting.count_descrip[TOP][i]);
        counting.count_descrip[TOP][i] = NULL;
        free(counting.count_descrip[BOTTOM][i]);
        counting.count_descrip[BOTTOM][i] = NULL;
    }
}