0 Members and 1 Guest are viewing this topic.

#ifdef _MSC_VER#define _CRT_SECURE_NO_WARNINGS#endif#include <stdio.h>#include <stdlib.h>const char* fileLoc = "C:\\Users\\Andrew\\vs\\sudoku\\easyLevels.txt";FILE *myfile;int board[9][9] = { { 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 } };void initFile() { if ((myfile = fopen(fileLoc, "w")) == NULL) { printf("didnt open"); return; }}void closeFile() { fclose(myfile);}void printBoard() { fprintf(myfile, ",\""); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { fprintf(myfile, "%d", board[i][j]); if (j == 8) { if(i!=8) fprintf(myfile, "//"); } else fprintf(myfile, ","); } } fprintf(myfile, "\" ");}void getPossibleMoves(int *possibleMoves[], int x, int y) { int cpX = x / 3; cpX *= 3; int cpY = y / 3; cpY *= 3; int cpB[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) cpB[i][j] = board[cpX + i][cpY + j]; } int psbleMoves[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (int i = 0; i < 9; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { if (psbleMoves[i] == cpB[j][k]) psbleMoves[i] = 0; } } for (int j = 0; j < 9; j++) { if (psbleMoves[i] == board[x][j] || psbleMoves[i] == board[j][y]) psbleMoves[i] = 0; } } int numMoves = 0; for (int i = 0; i < 9; i++) { if (psbleMoves[i] != 0) numMoves++; } *possibleMoves = new int[numMoves + 1];//(int*)malloc((numMoves+1)*sizeof(int)); (*possibleMoves)[0] = numMoves; int prevInt = 1; for (int i = 0; i < 9; i++) { if (psbleMoves[i] != 0) { (*possibleMoves)[prevInt] = psbleMoves[i]; prevInt++; } }}void generateBoard() { for (int i = 0; i < 9; i++) board[0][i] = i+1; for (int i = 1; i < 9; i++) { for (int j = 0; j < 9; j++) board[i][j] = 0; } int prevMove; int *possibleMoves=new int[10]; int *prevMoves=new int[10]; for (int i = 1; i < 9; i++) { for (int j = 0; j < 9; j++) { prevMove = 0; getPossibleMoves(&possibleMoves, i, j); while (possibleMoves[0] == 0) { prevMove++; if (prevMoves[0] < prevMove) { //generateBoard(); return; } else { if (j != 0) board[i][j-1] = prevMoves[prevMove + 1]; else board[i - 1][8] = prevMoves[prevMove+1]; getPossibleMoves(&possibleMoves, i, j); } } board[i][j] = possibleMoves[1]; prevMove = 0; delete(prevMoves); prevMoves = possibleMoves; } } delete(possibleMoves);}int main() { initFile(); generateBoard(); printBoard(); closeFile(); return 0;}

#ifdef _MSC_VER#define _CRT_SECURE_NO_WARNINGS#endif#include <stdio.h>#include <stdlib.h>const char* fileLoc = "easyLevels.txt";FILE *myfile;int *possibleMoves;int board[9][9] = { { 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0 } };void initFile() { if ((myfile = fopen(fileLoc, "w")) == NULL) { printf("didnt open"); return; }}void closeFile() { fclose(myfile);}void printBoard() { fprintf(myfile, ",\""); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { fprintf(myfile, "%d", board[i][j]); if (j == 8) { if(i!=8) fprintf(myfile, "//"); } else fprintf(myfile, ","); } } fprintf(myfile, "\" ");}void getPossibleMoves(int *possibleMoves[], int x, int y) { int cpX = x / 3; cpX *= 3; int cpY = y / 3; cpY *= 3; int cpB[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) cpB[i][j] = board[cpX + i][cpY + j]; } int psbleMoves[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (int i = 0; i < 9; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { if (psbleMoves[i] == cpB[j][k]) psbleMoves[i] = 0; } } for (int j = 0; j < 9; j++) { if (psbleMoves[i] == board[x][j] || psbleMoves[i] == board[j][y]) psbleMoves[i] = 0; } } int numMoves = 0; for (int i = 0; i < 9; i++) { if (psbleMoves[i] != 0) numMoves++; } delete(*possibleMoves); *possibleMoves = new int[numMoves + 1];//(int*)malloc((numMoves+1)*sizeof(int)); (*possibleMoves)[0] = numMoves; int prevInt = 1; for (int i = 0; i < 9; i++) { if (psbleMoves[i] != 0) { (*possibleMoves)[prevInt] = psbleMoves[i]; prevInt++; } }}/*void generateBoard(int i, int j, int k, int h, int f) { if (i < 81) { board[i % 9][i / 9] = 0; int *possibleMoves; getPossibleMoves(&possibleMoves, i % 9, i / 9); if (possibleMoves[0] > 0) { board[i % 9][i / 9] = possibleMoves[rand() % possibleMoves[0] + 1]; delete(possibleMoves); if (j < 10) { if (i < h) { generateBoard(i + 1, j, k - 1, h, f); } else { generateBoard(i + 1, 0, 0, i + 1, 0); } } else { if (i == h) { if (f < 9) { for (int l = h; l > h - k; l--) board[l % 9][l / 9] = 0; generateBoard(h - k, 0, k + 1, h, f + 1); } else { for (int l = h; l > h - k-1; l--) board[l % 9][l / 9] = 0; generateBoard(h - k - 1, 0, k + 1, h, 0); } } else { generateBoard(i - 1, 0, k + 1, h, f); } } } else { delete(possibleMoves); generateBoard(i - 1, j+1, k+1, h, f); } } else { return; } return;}*/void generateBoard(int i, int j, int k) { if (i < 81) { board[i % 9][i / 9] = 0; getPossibleMoves(&possibleMoves, i % 9, i / 9); if (possibleMoves[0] > 0) { board[i % 9][i / 9] = possibleMoves[rand() % possibleMoves[0] + 1]; if (i == k) { generateBoard(i + 1, 0, i + 1); } else { generateBoard(i + 1, j, k); } } else { if (i == k) { for (int l = i; l > i - j - 1; l--) board[l % 9][l / 9] = 0; generateBoard(i - j - 1, j + 1, k); } else { board[i % 9][i / 9] = 0; generateBoard(i - 1, 1, i); } } } return;}int main() { initFile(); generateBoard(0, 0, 0); printBoard(); closeFile(); return 0;}

FacebookandTwitterFollow @Codewalr_us