本程序还有两个bin文件提供数独数据,可以在我的资源里面下载.下面是makefile 头文件 和 源文件....
#
# Makefile
#
# Computer Science 50
# Problem Set 4
#
sudoku: Makefile sudoku.c sudoku.h
gcc -ggdb -std=c99 -Wall -Werror -Wformat=0 -Wno-unused-but-set-variable -o sudoku sudoku.c -lncurses -g
clean:
rm -f *.o a.out core log.txt sudoku
/****************************************************************************
* sudoku.h
*
* Computer Science 50
* Problem Set 4
*
* Compile-time options for the game of Sudoku.
***************************************************************************/
// game's author
#define AUTHOR "Sam Brown"
// game's title
#define TITLE "Sudoku"
// banner's colors
#define FG_BANNER COLOR_CYAN
#define BG_BANNER COLOR_BLACK
// grid's colors
#define FG_GRID COLOR_WHITE
#define BG_GRID COLOR_BLACK
// border's colors
#define FG_BORDER COLOR_WHITE
#define BG_BORDER COLOR_RED
// logo's colors
#define FG_LOGO COLOR_YELLOW
#define BG_LOGO COLOR_BLACK
// nicknames for pairs of colors
enum { PAIR_BANNER = 1, PAIR_GRID, PAIR_BORDER, PAIR_LOGO, PAIR_WRONG, PAIR_PICE };
/****************************************************************************
* sudoku.c
*
* Computer Science 50
* Problem Set 4
*
* Implements the game of Sudoku.
***************************************************************************/
#include "sudoku.h"
#include <ctype.h>
#include <ncurses.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// macro for processing control characters
#define CTRL(x) ((x) & ~0140)
// size of each int (in bytes) in *.bin files
#define INTSIZE 4
// wrapper for our game's globals
struct
{
// the current level
char *level;
// the game's board
int board[9][9];
// the board's number
int number;
// the board's top-left coordinates
int top, left;
// the cursor's current location between (0,0) and (8,8)
int y, x;
} g;
// prototypes
void draw_grid(void);
void draw_borders(void);
void draw_logo(void);
void draw_numbers(void);
void hide_banner(void);
bool load_board(void);
void handle_signal(int signum);
void log_move(int ch);
void redraw_all(void);
bool restart_game(void);
void show_banner(char *b);
void show_cursor(void);
void shutdown(void);
bool startup(void);
void move_step( void );
bool can_put( int key );
/*
* 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") == 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