예제 #1
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();
    
    // initialize legal variable
    g.legal = true;
    
    // initialize won variable
    g.won = false;

    // 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)
        {
            // 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;
            
            // move cursor on the board   
            case KEY_DOWN:                
            case KEY_UP:
            case KEY_LEFT:
            case KEY_RIGHT:
                move_cursor(ch);
                break;
                
            case KEY_BACKSPACE:
            case KEY_DC:
            case 46: // .
            case 48: // 0
            case 49: // 1
            case 50: // 2
            case 51: // 3
            case 52: // 4
            case 53: // 5
            case 54: // 6
            case 55: // 7
            case 56: // 8
            case 57: // 9
                change_number(ch);
                if(!legal(ch))
                {
                    show_banner("Illegal Move...");
                    show_cursor();
                    g.legal = false;
                }
                else
                {
                    hide_banner();
                    show_cursor();
                    g.legal = true;
                }
                if(won(ch))
                {
                    show_banner("You Won The Game!!!");
                    g.won = true;
                }
                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;
}
예제 #2
0
파일: sudoku.c 프로젝트: nancymic2/cscie3
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;
}
예제 #3
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;
}
예제 #4
0
파일: sudoku.c 프로젝트: MTietze/SUDOKU
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;
}