软件工程大作业——数独游戏2
一、PSP表格
二、问题分析
三、系统设计
四、具体实现
五、单元测试
六、程序性能及质量分析
七、GUI
八、总结
代码地址:https://github.com/friyal0730/sudoku/
这篇文章主要是第四部分具体实现、第五部分单元测试和第六部分程序性能及质量分析
四、具体实现
Check.h:负责对用户输入的命令进行处理,如果命令输入错误,进行错误提示,下面是判断输入并执行的代码部分:
int solveinput() //判断并执行命令
{
if (argc != 3) //输入格式不正确
{
cout << "Illegal paramater number" << endl;
cout << "Input like this: [sudoku.exe -c n] or [sudoku.exe -s path]" << endl;
return 1;
}
if (strcmp(argv[1], "-c") && strcmp(argv[1], "-s")) //字母错误
{
cout << "The first parameter should only be -c or -s" << endl;
cout << "-c means to generate the sudoku to file." << endl;
cout << "-s means to solve the sudoku from the file." << endl;
return 2;
}
if (!strcmp(argv[1], "-c")) //创造数独终盘
{
int sum = 0; //sudoku的个数
int len = strlen(argv[2]);
for (int i = 0; i < len; i++)
{
if (!(argv[2][i] >= '0' && argv[2][i] <= '9')) //输入的字符不合法(不是数字)
{
cout << "The third paramater after -c should be number that indicate the sudoku you want." << endl;
if (argv[2][i] == '+' || argv[2][i] == '-' || argv[2][i] == '/' || argv[2][i] == '*')
{
cout << "Please input the number!" << endl;
return 8;
}
return 3;
}
sum = 10 * sum + argv[2][i] - '0';
}
if (sum > MAX || sum < 1) //数字过大
{
cout << "The number is too large,the number should be 1-1000000" << endl;
return 4;
}
/*----------------------------------*/
/*创建数独终盘对象*/
/*...........*/
FILE* file;
file = freopen("sudoku.txt", "w", stdout); //没有文件时可以创造
Base base(sum, file); //调用Generator
base.generate();
/*----------------------------------*/
return 5;
}
if (!strcmp(argv[1], "-s")) //解题
{
FILE* ans;
FILE* question; //数独题目
question = freopen(argv[2], "r", stdin);
if (!question)
{
cout << "The file path is not right,please check." << endl;
return 6;
}
/*----------------------------------*/
/*创建数独求解对象*/
/*...........*/
ans = freopen("sudoku.txt", "w", stdout);
Solver solver(question, ans); //调用Solver
flag = solver.in();
/*----------------------------------*/
return 7;
}
return 8; //正常执行,消除警告
}
Base.h:生成数独局的终盘,采用矩阵转换法,先换行,再换列,最后换数字,换行的代码如下:
void generate() //生成函数
{
int number = 0;
while (number < count)
{
Out();
number++;
Line_exchange_floor(&number);//换上面部分的行
Line_exchange_middle(&number);//换中间部分的行
Line_exchange_ground(&number);//换下面部分的行
if (number < count)
{
TransForm();
Change();
}
}
}
Answer.h:用来解决数独问题,采用回溯算法,dfs的具体代码如下:
bool dfs(int tot) //dfs搜索方法
{
if (tot > 80)
{
return true;
}
int line = tot / 9;
int col = tot % 9;
if (sudoku[line][col] > 0)
{
return dfs(tot + 1);
}
for(int i = 1;i <= 9;i++)
{
sudoku[line][col] = i;
if (check(line, col, i))
{
if (dfs(tot + 1))
{
return true;
}
}
sudoku[line][col] = 0;
}
return false;
}
question.cpp:用来生成数独题目的,用到了rand函数,具体实现如下:
void change()
{
int a = 0, b = 2;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 2; k++)
{
int ran1 = (rand() % (b - a + 1)) + a;
int ran2 = (rand() % (b - a + 1)) + a;
if ((ran1 + i * 3)*(ran2 + j * 3) != 0)
{
incom_sudoku[ran1 + i * 3][ran2 + j * 3] = 0;
}
}
}
}
a = 0, b = 8;
for (int i = 0; i < 42; i++)
{
int ran1 = (rand() % (b - a + 1)) + a;
int ran2 = (rand() % (b - a + 1)) + a;
if (ran1 != 0 && ran2 != 0)
{
incom_sudoku[ran1][ran2] = 0;
}
}
}
四、单元测试及代码说明
对于每一个模块,我都设计了单元测试,一共写了十个用例。
首先是对于输入的测试,一共有8种可能,对于输入算式等也进行了测试,代码如下:
UnitTest1()
{
/*测试命令 "sudoku.exe -c 100" */
argv = new char*[3];
argv[0] = new char[100];
strcpy_s(argv[0], 100, "sudoku.exe");
argv[1] = new char[100];
strcpy_s(argv[1], 100, "-c");
argv[2] = new char[100];
strcpy_s(argv[2], 100, "100");
}
TEST_METHOD(TestMethod1) //输入格式不正确的时候
{
/*测试命令 “sudoku.exe”*/
argc = 1;
Solve Solve1(argc, argv);
int result = Solve1.Solveinput();
assert(result == 1);
}
TEST_METHOD(TestMethod2) //输入的不是-c或者-s的时候
{
/*测试命令 "sudoku.exe -k 100" */
argc = 3;
argv = new char*[3];
argv[0] = new char[100];
strcpy_s(argv[0], 100, "sudoku.exe");
argv[1] = new char[100];
strcpy_s(argv[1], 100, "-k");
argv[2] = new char[100];
strcpy_s(argv[2], 100, "100");
Solve Solve2(argc, argv);
int result = Solve2.Solveinput();
assert(result == 2);
}
TEST_METHOD(TestMethod3) //输入不是数字的时候
{
/*测试命令 "sudoku.exe -c abc" */
argc = 3;
argv = new char*[3];
argv[0] = new char[100];
strcpy_s(argv[0], 100, "sudoku.exe");
argv[1] = new char[100];
strcpy_s(argv[1], 100, "-c");
argv[2] = new char[100];
strcpy_s(argv[2], 100, "abc");
Solve Solve3(argc, argv);
int result = Solve3.Solveinput();
assert(result == 3);
}
TEST_METHOD(TestMethod4) //输入的数字过大
{
/*测试命令 "sudoku.exe -c 1000009" */
argc = 3;
argv = new char*[3];
argv[0] = new char[100];
strcpy_s(argv[0], 100, "sudoku.exe");
argv[1] = new char[100];
strcpy_s(argv[1], 100, "-c");
argv[2] = new char[100];
strcpy_s(argv[2], 100, "1000009");
Solve Solve4(argc, argv);
int result = Solve4.Solveinput();
assert(result == 4);
}
TEST_METHOD(TestMethod5) //输入生成数独终盘命令正确
{
/*测试命令 "sudoku.exe -c 10000" */
argc = 3;
argv = new char*[3];
argv[0] = new char[100];
strcpy_s(argv[0], 100, "sudoku.exe");
argv[1] = new char[100];
strcpy_s(argv[1], 100, "-c");
argv[2] = new char[100];
strcpy_s(argv[2], 100, "10000");
Solve Solve5(argc, argv);
int result = Solve5.Solveinput();
assert(result == 5);
}
TEST_METHOD(TestMethod6) //解数独题的路径错误
{
/*测试命令 "sudoku.exe -s 100" */
argc = 3;
argv = new char*[3];
argv[0] = new char[100];
strcpy_s(argv[0], 100, "sudoku.exe");
argv[1] = new char[100];
strcpy_s(argv[1], 100, "-s");
argv[2] = new char[100];
strcpy_s(argv[2], 100, "100");
Solve Solve6(argc, argv);
int result = Solve6.Solveinput();
assert(result == 6);
}
TEST_METHOD(TestMethod7) //解数独题的路径正确
{
/*测试命令 "sudoku.exe -s solver.txt" */
argc = 3;
argv = new char*[3];
argv[0] = new char[100];
strcpy_s(argv[0], 100, "sudoku.exe");
argv[1] = new char[100];
strcpy_s(argv[1], 100, "-s");
argv[2] = new char[100];
strcpy_s(argv[2], 100, "solver.txt");
Solve Solve7(argc, argv);
int result = Solve7.Solveinput();
assert(result == 7);
}
TEST_METHOD(TestMethod8) //输入算式
{
/*测试命令 "sudoku.exe -c 10/5" */
argc = 3;
argv = new char*[3];
argv[0] = new char[100];
strcpy_s(argv[0], 100, "sudoku.exe");
argv[1] = new char[100];
strcpy_s(argv[1], 100, "-c");
argv[2] = new char[100];
strcpy_s(argv[2], 100, "10/5");
Solve Solve8(argc, argv);
int result = Solve8.Solveinput();
assert(result == 8);
}
对于Generator.h的测试:
主要目的:检查生成的矩阵是否有重复的情况
TEST_METHOD(TestMethod10) //证明没有生成重复的矩阵
{
int sudoku_number = 1000000;
FILE* file;
freopen_s(&file, "sudoku_temp.txt", "w", stdout);//写文件
assert(file != NULL);
//生成数独
Base sudoku_generator(sudoku_number, file);
sudoku_generator.generate();
fclose(stdout);
freopen_s(&file, "sudoku_temp.txt", "r", stdin);//读文件
assert(file != NULL);
string s1;
bool end = false;
//将数独加入集合,若集合里数独的个数与sudoku_number相等,则无重复
set<string> container;
while (true)
{
int temp;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (fscanf_s(file, "%d", &temp) == EOF)
{
end = true;
break;
}
s1.push_back(temp + '0');
}
if (end) break;
}
if (end) break;
container.insert(s1);
s1.clear();
}
fclose(stdin);
assert(container.size() != sudoku_number);
}
Answer.h的测试:
主要目的:检查生成的数独矩阵是否正确
TEST_METHOD(TestMethod9) //解题是否成功
{
/*测试命令 "sudoku.exe -s solver.txt" */
argc = 3;
argv = new char*[3];
argv[0] = new char[100];
strcpy_s(argv[0], 100, "sudoku.exe");
argv[1] = new char[100];
strcpy_s(argv[1], 100, "-s");
argv[2] = new char[100];
strcpy_s(argv[2], 100, "solver.txt");
Solve Solve7(argc, argv);
assert(Solve7.flag == 0);
}
覆盖率测试:
五、程序性能及质量分析
起初,我选用随机法生成数独终盘,生成1000000个数独终盘要花40s的时间。所以我换用矩阵转换法,节省了大量的时间,效率非常高,性能上也提高了很多。我还删除了一些不必要的循环,最后的算法生成1000000个数独终盘只用了不到4s的时间,性能提高了十倍。下面是我的性能分析: