/* * (Re)starts current game, returning true iff succesful. */ bool restart_game() { // reload current game if (!load_board()) return false; // redraw board draw_grid(); draw_numbers(); hide_banner(); // 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; }
bool load_board(void) { // open file with boards of specified level char filename[strlen(g.level) + 5]; sprintf(filename, "%s.bin", g.level); FILE *fp = fopen(filename, "rb"); if (fp == NULL) return false; // determine file's size fseek(fp, 0, SEEK_END); int size = ftell(fp); // ensure file is of expected size if (size % (81 * INTSIZE) != 0) { fclose(fp); return false; } // compute offset of specified board int offset = ((g.number - 1) * 81 * INTSIZE); // seek to specified board fseek(fp, offset, SEEK_SET); // read board into memory if (fread(g.board, 81 * INTSIZE, 1, fp) != 1) { fclose(fp); return false; } // w00t fclose(fp); //store a copy of initial board for (int i = 0; i < 9; i++) { for(int j = 0; j < 9; j++) { g.init_board[j][i] = g.board[j][i]; } } hide_banner(); return true; }
bool check_row(int y) { //check one row for duplicates, y = y axis int row[9]; for(int i = 0; i < 9; i++) { row[i] = g.board[y][i]; for(int k = 0; k < i; k++) { if(row[k] != 0 && row[k] == row[i]) { hide_banner(); show_banner("You've got a row issue"); return false; } } } return true; }
bool check_col(int x) { //check one column for duplicates, x = x axis int col[9]; for(int i = 0; i < 9; i++) { col[i] = g.board[i][x]; for(int k = 0; k < i; k++) { if(col[k] != 0 && col[k] == col[i]) { hide_banner(); show_banner("You've got a column problem"); return false; } } } return true; }
/* * Returns true iff number ch represents a valid move, else false. */ bool validate(int ch) { ch -= '0'; char message[100]; hide_banner(); // check all numbers in the same row for (int i = 0; i < DIM; i++) if (g.board[g.y][i] == ch && i != g.x) { sprintf(message, "Illegal move: there is another %i in this row.", ch); show_banner(message); show_cursor(); return false; } // check all numbers in the same column for (int j = 0; j < DIM; j++) if (g.board[j][g.x] == ch && j != g.y) { sprintf(message, "Illegal move: there is another %i in this column.", ch); show_banner(message); show_cursor(); return false; } // check all numbers in the same 3x3 square for (int y = (g.y/3)*3; y < (g.y/3)*3 + 3; y++) for (int x = (g.x/3)*3; x < (g.x/3)*3 + 3; x++) if (g.board[y][x] == ch && g.x != x && g.y != y) { sprintf(message, "Illegal move: there is another %i in this square.", ch); show_banner(message); show_cursor(); return false; } return true; }
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; }
/* * 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; }
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; }
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; }