数独游戏
写一个数独游戏的代码,但是没啥思路。用户能通过输入坐标来填充想写的数字(不是找数独答案的程序,是那种用户能玩的数独游戏代码), 想不出咋写,能不能帮忙写一个
目前只学到二维数组,求大佬帮忙
#include <stdio.h> #include <stdlib.h> #include <time.h> void genSuDo() { int i, j, k, a, b; static int array[][9] = { 7,8,9, 3,1,4, 6,5,2, 1,2,6, 8,7,5, 3,9,4, 5,4,3, 2,9,6, 7,1,8, 6,7,2, 1,5,3, 8,4,9, 4,9,1, 7,6,8, 2,3,5, 8,3,5, 4,2,9, 1,6,7, 3,1,7, 9,4,2, 5,8,6, 9,5,8, 6,3,7, 4,2,1, 2,6,4, 5,8,1, 9,7,3 }; srand(time(0)); for (k=0; k<5; k++) { a = (rand()%9) + 1; b = (rand()%9) + 1; for (i=0; i<9; i++) { for (j=0; j<9; j++) { if (array[i][j] == a) { array[i][j] = b; } else if (array[i][j] == b) { array[i][j] = a; } } } } a = rand()%3; b = (a+1+rand()%2)%3; for (i=0; i<3; i++) { for (j=0; j<9; j++) { array[a*3+i][j] ^= array[b*3+i][j]; array[b*3+i][j] ^= array[a*3+i][j]; array[a*3+i][j] ^= array[b*3+i][j]; } } a = rand()%3; b = (a+1+rand()%2)%3; for (i=0; i<3; i++) { for (j=0; j<9; j++) { array[j][a*3+i] ^= array[j][b*3+i]; array[j][b*3+i] ^= array[j][a*3+i]; array[j][a*3+i] ^= array[j][b*3+i]; } } for (i=0; i<9; i+=3) { a = rand()%3; b = (a+1+rand()%2)%3; for (j=0; j<9; j++) { array[i+a][j] ^= array[i+b][j]; array[i+b][j] ^= array[i+a][j]; array[i+a][j] ^= array[i+b][j]; } a = rand()%3; b = (a+1+rand()%2)%3; for (j=0; j<9; j++) { array[j][i+a] ^= array[j][i+b]; array[j][i+b] ^= array[j][i+a]; array[j][i+a] ^= array[j][i+b]; } } for (i=0; i<9; i++) { for (j=0; j<9; j++) { if (rand()%2 == 0) { printf("_ "); } else { printf("%d ", array[i][j]); } } printf("\n"); } printf("\n\n"); } void main() { genSuDo(); }