示例#1
0
文件: sudoku.c 项目: MTietze/SUDOKU
void
warn(void)
{
    if (!check_cols(1)) 
    show_banner("You have a column problem"); 
    if (!check_rows(1))
    show_banner("You have a row issue"); 
    if (!check_squares(1))
    show_banner("You have a square snare"); 
    if (!check_cols(1) && !check_rows(1))
    show_banner("You have a column problem and a row issue");
    if (!check_cols(1) && !check_squares(1)) 
    show_banner("You have a column problem and a square snare");
    if (!check_rows(1) && !check_squares(1)) 
    show_banner("You have a row issue and a square snare");
    if (!check_rows(1) && !check_cols(1) && !check_squares(1)) 
    show_banner("You have a column problem, a row issue and a square snare");
    show_cursor();
}
示例#2
0
文件: sudoku.c 项目: MTietze/SUDOKU
bool
game_won(void)
{
    if(check_cols(0) && check_rows(0) && check_squares(0))
    {
         
        show_banner("You won, have a sandwich!"); 
        
        for (int i = 0; i < 9; i++)
           { 
               for(int j = 0; j < 9; j++)
                  {
                      g.init_board[j][i] = 9; 
                  }
           }     
         
         return true;
    }
    
    else
    return false; 
}
示例#3
0
double Board_2::check_board (const bool is_turn, const char player_piece) const
{
	bool is_limit = false;
	double utilitly = 0;
	double temp = 0;
	temp = check_columns(is_turn,player_piece,used_columns,columns,rows,r,board,is_limit);
	if (is_limit) 
		return temp; // if a killer move was found return that killer move
	else
		utilitly += temp;
	temp = check_rows(is_turn, player_piece, used_rows, columns, rows, r, board, is_limit);
	if (is_limit)
		return temp;
	else 
		utilitly += temp;
	temp = check_diagonals(is_turn,player_piece, columns, rows, r, board, is_limit);
	if (is_limit)
		return temp;
	else
		utilitly += temp;

	return utilitly;
}
示例#4
0
void test_rows(int matrix[HEIGHT][WIDTH], int expected_value) {
    test( check_rows(matrix) == expected_value ? "Pass" : "Fail" );
}
示例#5
0
文件: main.c 项目: myrjola/margames
// run_martet: The game loop. Returns final score.
int run_martet(SDL_Surface* screen, SDL_Surface* board) {
    Tetromino* active_tetromino;
    Tetromino* next_tetromino;
    // Create ingame menu
    struct Menu* ingame_menu = menu_create();
    menu_addelement(ingame_menu, "Continue");
    menu_addelement(ingame_menu, "Quit Current Game");
    ingame_menu->active_element = &ingame_menu->elements[0];
    ingame_menu->active_element->active = 1;
    int score = 0;
    srand((unsigned) time(NULL));
    board_create();
    next_tetromino   = tetcreaterand();
    update_status_bar(next_tetromino, screen, score);
    active_tetromino = tetcreaterand();
    active_tetromino->position[0] = 4;
    struct Timer* timer = create_timer();
    timer_change_alarm_interval(timer, GAME_SPEED);
    timer_start(timer);
    bool running = true;
    while (running){
        int event = process_key_events(active_tetromino, tetaction);
        if (event == KEYEVENT_EXIT)
            exit(0);
        else if (event == KEYEVENT_MENU) {
            if (ingame_menu_martet(screen, board, ingame_menu) == 1)
                running = false;
        }
        else if (event == KEYEVENT_PAUSE)
            pause_martet(screen, board);
        if (timer_update(timer)) {
            // If collision and tetromino not deleted
            if ( tetmove('d', active_tetromino) == 0
                && active_tetromino->color != TETROMINO_DELETE) {
                place_tetromino(active_tetromino);
                active_tetromino->color = TETROMINO_DELETE;
            }
            else if ( active_tetromino->color == TETROMINO_DELETE ) {
                free(active_tetromino);
                active_tetromino = next_tetromino;
                next_tetromino   = tetcreaterand();
                if (check_rows(&score)) { // If score has increased.
                    timer_change_alarm_interval(timer,
                                                GAME_SPEED /
                                                sqrt( (double) score/5) + 1);
                }
            }
            if ( next_tetromino == NULL ) // If game over
                break;
            update_status_bar(next_tetromino, screen, score);
        }
        clear_surface(board, NULL);
        draw_board(board);
        draw_tetromino(board, active_tetromino);
        draw_ghost_tetromino(board, active_tetromino);
        draw_surface(0, 0, board, screen, NULL);
        SDL_Flip(screen);
    }
    if (active_tetromino) {
        free(active_tetromino);
    }
    free(next_tetromino);
    free(timer);
    menu_destroy(ingame_menu);
    return score;
}
示例#6
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;
}