コード例 #1
0
int main(int argc, char *argv[])
{
  int sudoku[81];
  int *ptr;
  int i;
  i=0;
  if (argc!=2)
    {
      printf("USAGE: ./sudoku_solver FILENAME\n");
      printf("FILENAME is the file where the sudoku is stored. It has to be a string of 81 integers from 0 to 9, without spaces\n");
      printf("The 0's represent the blank cells\n");
      return 1;
    }
  // Open File
  if(!(file_2_sudoku(sudoku,argv[1])))
    printf("[FILE 2 SUDOKU] File format not valid");
  // Check that the given sudoku is valid
  if (check_all(sudoku))
    {
      printf("Error. Invalid Sudoku.\n");
      return 1;
    }
  printf("Problem\n");
  print_sudoku(sudoku);
  printf("Solving... Be patient!\n");
  solve_sudoku(sudoku);
  printf("Solution\n");
  print_sudoku(sudoku);
  return 0;
}
コード例 #2
0
ファイル: sudoku.c プロジェクト: vbmacher/learning-kit
int main()
{
  FILE *fr;
  int i,j, sq = 0;
  int zmena = 0, res;

  printf("\nSUDOKU v1.1");
  printf("\n(c) Copyright 2006, vbmacher");

  if ((fr = fopen("sudoku.txt", "r")) == NULL) {
    printf("\nFile sudoku.txt cannot be found.");
    return 0;
  }
  sq = 2;
  for (i = 0; i < 9; i++) {
    if (i && !(i % 3)) sq++;
    else sq -= 2;
    for (j = 0; j < 9; j++) {
      fscanf(fr, "%d ", &sudoku[i][j].mark);
      memset(sudoku[i][j].moves, 0, sizeof(int) * 9);
      if (j && !(j % 3)) sq++;
      sudoku[i][j].square = sq;
    }
  }
  fclose(fr);

  print_sudoku();
  printf("\n-------------------------------------------");

    findmoves();
zac:
    removebadmoves();
    removecrossmoves();
    if (markonly()) goto zac;
    for (i = 0; i < 9; i++) {
      if (markonlycol(i)) goto zac;
      if (markfreecol(i)) goto zac;
    }
    for (j = 0; j < 9; j++) {
      if (markonlyrow(j)) goto zac;
      if (markfreerow(j)) goto zac;
    }
    for (i = 0; i < 9; i++) {
      if (markonlysq(i)) goto zac;
      if (markfreesq(i)) goto zac;
    }

  print_sudoku();
  print_moves();

  getchar();
  getchar();
  return 0;
}
コード例 #3
0
ファイル: sudoku.c プロジェクト: mja054/Random-tweeks
int 
main (int argc, char *argv[])
{
//	find_box (2, 7);
	fill_row_elements();

	print_sudoku ();
	find_empty_space ();
	print_sudoku ();
	return 0;
}
コード例 #4
0
ファイル: sudoku-solver.c プロジェクト: rohit01/sudoku-solver
int main() {
    int sudoku[9][9];
    if (input_sudoku(sudoku) == 1) {
        return ERROR;
    }
    printf("\nSudoku entered:\n");
    print_sudoku(sudoku);
    if(solve_sudoku(sudoku) == 0) {
        printf("\nSolution:\n");
    }
    else {
        printf("\nInvalid sudoku - unable to solve. Partial result:\n");
    }
    print_sudoku(sudoku);
}
コード例 #5
0
ファイル: main.c プロジェクト: Qpaq/42Projects
int		main(int argc, char **argv)
{
	char	**sudo_tb;
	int		index;
	int		index_malloc;

	index_malloc = 0;
	if (argc == 10 && is_valid_number(argv) == 1)
	{
		index = 0;
		sudo_tb = (char**)malloc(9 * sizeof(char*));
		while (index_malloc < 9)
		{
			sudo_tb[index_malloc] = (char*)malloc(10 * sizeof(char));
			index_malloc++;
		}
		copy_sudo(argv, sudo_tb);
		if (is_valid_char(argv) == 1 && sudoku(sudo_tb, 0))
			print_sudoku(sudo_tb);
		else
			write(1, "Erreur\n", 7);
		free_grid(sudo_tb);
	}
	else
		write(1, "Errer\n", 7);
	return (0);
}
コード例 #6
0
ファイル: sudoku.cpp プロジェクト: Illedran/algoritmi
int main() {
  srand(time(NULL));
  int N = 9;
  std::vector<std::vector<int>> M(N, std::vector<int>(N, 0));
  std::vector<std::vector<bool>> given(N, std::vector<bool>(N, false));
  M[1][5] = 3;
  M[1][7] = 8;
  M[1][8] = 5;
  M[2][2] = 1;
  M[2][4] = 2;
  M[3][3] = 5;
  M[3][5] = 7;
  M[4][2] = 4;
  M[4][6] = 1;
  M[5][1] = 9;
  M[6][0] = 5;
  M[6][7] = 7;
  M[6][8] = 3;
  M[7][2] = 2;
  M[7][4] = 1;
  M[8][4] = 4;

  given[1][5] = true;
  given[1][7] = true;
  given[1][8] = true;
  given[2][2] = true;
  given[2][4] = true;
  given[3][3] = true;
  given[3][5] = true;
  given[4][2] = true;
  given[4][6] = true;
  given[5][1] = true;
  given[6][0] = true;
  given[6][7] = true;
  given[6][8] = true;
  given[7][2] = true;
  given[7][4] = true;
  given[8][4] = true;
  std::cout << "Input sudoku: " << std::endl;
  print_sudoku(M, N);
  solve_sudoku(M, given, N);
  std::cout << "Result sudoku: " << std::endl;
  print_sudoku(M, N);
  return 0;
}
コード例 #7
0
ファイル: main.c プロジェクト: ryan91/sudoku_solver
/**
 * This program tries to resolve a given Sudoku file.
 *
 * @param argc Argument count
 * @param argv Arguments
 */
int main(int argc, char** argv) {
    if (argc != 2) {
        printf("Usage: <solver> <sudoku file>\n");
        exit(EXIT_FAILURE);
    } else {
        int ret;

        // allocate matrix to read into
        char** sudoku_array = allocate_sudoku();

        // read input sudoku file
        ret = readSudoku(argv[1], sudoku_array);

        // abort if file is corrupt
        if (ret != 0) {
            printf("The read process failed - exiting\n");
            exit(EXIT_FAILURE);
        }

        // print input sudoku
        printf("<<INPUT  SUDOKU>>\n");
        print_sudoku(sudoku_array);
        printf("-----------------\n");

        // solve it
        ret = solve_sudoku(sudoku_array);

        // print output sudoku
        printf("<<OUTPUT SUDOKU>>\n");
        print_sudoku(sudoku_array);
        printf("-----------------\n");

        // print if the sudoku could be solved
        if (ret == 0)
            printf("The Sodoku was solved\n");
        else
            printf("The Sudoku is irresolvable\n");

        // free allocated matrix
        unallocate_sudoku(sudoku_array);

        exit(EXIT_SUCCESS);
    }
}
コード例 #8
0
ファイル: sudoku.c プロジェクト: Roger13/MC504-SO
int resolvedor (){
  printf("Insira sudoku incompleto para resolução.\n");
  int x = 0, i, j, erro = 0;
  int** grid = malloc_grid();
  if (scan_sudoku(grid)){             // Obtem a matriz com os coeficientes do sudoku
    printf("Erro no input.\n");
    return 2;
  }
  // Chama funcao recursiva preenchedora
  if (backTracker(grid) == 0){
    printf("Sudoku insolúvel!\n");
  }

  print_sudoku(grid);// IMPRIMIR MATRIZ PREENCHIDA
  free_grid(grid);
  return 0;
}
コード例 #9
0
ファイル: gamemodes.cpp プロジェクト: dalmemail/AmayaOS
int sudoku(unsigned int numbers_def[16])
{
	clear_window();
	unsigned int numbers[16];
	for (int i = 0; i < 16; i++) {
		numbers[i] = numbers_def[i];
	}
	char option = 'a';
	for (int i = 0; option != EXIT;) {
		print_sudoku(numbers, i);
		option = getchar();
		if (option == FIX) {
			clear_window();
			if (checkSudoku(numbers)) {
				printf("\n\t\t\t\tYou win!\n");
			}
			else {
				printf("\n\t\t\t\tYou lost\n");
			}
			printf("\t\t\t\tPress any key to exit\n");
			getchar();
			return 0;
		}
		else if (option == UP && i >= 4) {
			i -= 4;
		}
		else if (option == DOWN && i <= 11) {
			i += 4;
		}
		else if (option == LEFT && i > 0) {
			i--;
		}
		else if (option == RIGHT && i < 15) {
			i++;
		}
		else if (option >= '1' && option <= '4' && numbers_def[i] == 0) {
			numbers[i] = (option - '0');
		}
		else if (option == NEW) {
			return 1;
		}
	}
	return 0;
}
コード例 #10
0
ファイル: sss.c プロジェクト: schiermike/sudokusolver
int main(int argc, char** args) {
	if(argc != 2 || initialize(args[1])) {
		printf("Malformed or missing input!\nExemplary usage: sudoku 7.4.95...9.6.8...73.1.76.9.5...1.84613865472946.8......1.5..97.84.76.23..........\n\n");
		return -1;
	}

	while(!is_solved()) {
//		print_sudoku();

		if(simplify_nakedsingle())
			continue;

		if(simplify_hiddensingle())
			continue;

		if(simplify_nakedpair())
			continue;

		if(simplify_nakedtriple())
			continue;

		if(simplify_hiddenpair())
			continue;

		if(simplify_hiddentriple())
			continue;

		if(simplify_intersection())
			continue;

		if(simplify_xwing())
			continue;

		printf("THIS SUDOKU IS TOO HARD TO SOLVE FOR ME!\n");
		break;
	}

	print_sudoku();

	return 0;
}
コード例 #11
0
int main (int argc, char const* argv[])
{
	int sudoku[SUDOKU_SIZE][SUDOKU_SIZE];
	read_sudoku_from_file(sudoku);
	int blanks = count_blanks(sudoku);

	std::vector<int> possible_numbers;

	while(blanks)
	{
		for(int i = 0; i < SUDOKU_SIZE; ++i)
		{
			for(int j = 0; j < SUDOKU_SIZE; ++j)
			{
				if(!sudoku[i][j])
				{
					for(int k = 1; k <= 9; ++k)
					{
						if(rows(sudoku, i, k) && columns(sudoku, j, k) && square(sudoku, i, j, k))
						{
							possible_numbers.push_back(k);
						}
					}
					if(possible_numbers.size() == 1)
					{
						sudoku[i][j] = possible_numbers[0];
						--blanks;
					}
					else
					{
						possible_numbers.clear();
					}
				}
			}	
		}
	}	

	print_sudoku(sudoku);

	return 0;
}
コード例 #12
0
ファイル: sudoku.c プロジェクト: Roger13/MC504-SO
int dica(){
  int x = 0, i, j, erro = 0;
  int** grid = malloc_grid();
  SudokuData missing;
  printf("Insira sudoku incompleto para geração de dicas.\n");
  if (scan_sudoku(grid)){
    printf("Erro no input.\n");
    return 2;
  }
  missing = sudokuChecker(grid);
  
  if(hint_generator(grid, missing)){
    printf("Erro na verificação.\n");
    return 1;
  }

  print_sudoku(grid);
  free_grid(grid);
  free_sudokuData(missing);
  return 0;
}
コード例 #13
0
ファイル: main.c プロジェクト: sploadie/sudoku
int		main(int argc, char **argv)
{
	int		*blanks;
	char	**solution;

	if (is_sudoku(argc, argv) == 0)
		return (print_error());
	blanks = init_blanks(argv);
	if (blanks == NULL || blanks[0] == 0 || blanks[0] > 64)
		return (print_error());
	if (solve(argv, blanks, 1) == 0)
		return (print_error());
	solution = save_sudoku(argv);
	if (solution == NULL)
		return (print_error());
	if (solve(argv, blanks, blanks[0]) != 0)
		return (print_error());
	print_sudoku(solution);
	free(blanks);
	free(solution);
	return (0);
}
コード例 #14
0
ファイル: sudoku.c プロジェクト: surajshetiya/sudoku
void print_sudoku_processed(int arr[9][9],int rows[9][2],int cols[9][2],int boxes[9][2]) {
	// print the actual sudoku and also the value set in the rows,cols and boxes arrays.
	print_sudoku(arr);
	int i,j;
	printf("\nRows computation\nR stands for row number\nT stands for total set\nR T 123456789\n");
	for(i=0;i<9;i++) {
		printf("\n%d %d ",i+1,rows[i][0]);
		for(j=0;j<9;j++) {
			if(rows[i][1]&(1<<j)) {
				printf("1");
			} else {
				printf("0");
			}
		}
	}
	printf("\nColumns computation\nC stands for column number\nT stands for total set\nR T 123456789\n");
	for(i=0;i<9;i++) {
		printf("\n%d %d ",i+1,cols[i][0]);
		for(j=0;j<9;j++) {
			if(cols[i][1]&(1<<j)) {
				printf("1");
			} else {
				printf("0");
			}
		}
	}
	printf("\nBoxes computation\nB stands for box number\nT stands for total set\nR T 123456789\n");
	for(i=0;i<9;i++) {
		printf("\n%d %d ",i+1,boxes[i][0]);
		for(j=0;j<9;j++) {
			if(boxes[i][1]&(1<<j)) {
				printf("1");
			} else {
				printf("0");
			}
		}
	}
	return ;
}
コード例 #15
0
ファイル: gamemodes.cpp プロジェクト: TheFaico/AmayaOS
int sudoku(int mode)
{
	clear_window();
	int level = 1;
	/* we get a random number from 0 to 9 as a char value */
	char n[2] = {randomnumber(), '\0'};
	char path[256] = "/etc/sudoku/sudoku_";
	if (mode == LOAD_SUDOKU) {
		printf("Sudoku File Path: ");
		gets_s(path, 256);
	}
	else {
		if (mode == 0 || mode == 2) {
			strcat(path, "easy");
		}
		if (mode == 1 || mode == 3) {
			strcat(path, "difficult");
		}
		strcat(path, n);
	}
	int numbers[16];
	int numbers_def[16];
	/* get sudoku */
	for (int i = 0; i < 16; i++) {
		numbers_def[i] = make_sudoku(i, path);
		if (numbers_def[i] < 0 || numbers_def[i] > 4) {
			clear_window();
			printf("Error: El archivo '%s' es erroneo o corrupto\n", path);
			printf("Pulsa una tecla para continuar.\n");
			getchar();
			return -1;
		}
	}
	for (int i = 0; i < 16; i++) {
		numbers[i] = numbers_def[i];
	}
	int sudoku4x4[4][4];
	int subregions[4][4];
	char option = 'r';
	while (option != 'c' && option != 'C') {
		print_sudoku(numbers);
		for (int i = 0; i < 16; i++) {
			if (numbers[i] == 0) {
				printf("Introduce el valor a la X%d: ", i+1);
				numbers[i] = getnum();
				if (numbers[i] > 4 || numbers[i] == 0) {
					numbers[i] = 1;
				}
				/* if you press return key */
				if (numbers[i] == -1) {
					/* put numbers[i] and numbers[i-1] equal to 0
					 * if not you can't rewrite it 
					 */
					numbers[i] = 0;
					i--;
					if (numbers[i] != numbers_def[i] && i > 0) {
						numbers[i] = 0;
						i--;
					}
				}
				printf("%c\n", numbers[i]);
				print_sudoku(numbers);
			}
		}
		for (int i = 0, k = 0, p=0; k < 4; i++) {
			sudoku4x4[k][i] = numbers[p];
			if ((i+1) % 4 == 0) {
				k++;
				i = -1;
			}
			p++;
		}
		for(int i = 0, k = 0; i < 4; i++) {
			int action = 0;
			for(int j = 0; j < 4; j++) {
				subregions[i][j] = numbers[k];
				if ((k+1) % 2 == 0) {
					if ((k+1) == 8) {
						k = 5;
						action = 0;
					}
					if (action == 0) {
						k += 3;
						action++;
					}
					else {
						k -= 3;
						action--;
					}
				}
				else {
					k++;
				}
			}
		}
		int gamestate = WIN;
		/* check rows and columns */
		for (int i = 0, k = 0; k < 4; i++) {
			for (int x = 0; x < 4; x++) {
				if (sudoku4x4[k][i] == sudoku4x4[k][x] && i != x) {
					gamestate = LOST;
				}
			}
			if ((i+1) % 4 == 0) {
				k++;
				i = -1;
			}
		}
		for (int i = 0, k = 0; i < 4; k++) {
			for (int x = 0; x < 4; x++) {
				if (sudoku4x4[k][i] == sudoku4x4[x][i] && k != x) {
					gamestate = LOST;
				}
			}
			if ((k+1) % 4 == 0) {
				i++;
				k = -1;
			}
		}
		/* check subregions */
		for (int i = 0, k = 0; k < 4; i++) {
			for (int x = 0; x < 4; x++) {
				if (subregions[k][i] == subregions[k][x] && i != x) {
					gamestate = LOST;
				}
			}
			if (i+1 == 4) {
				k++;
				i = -1;
			}
		}
		if (gamestate == WIN && mode < 2) {
			printf("Nivel %d COMPLETADO\n", level);
			do {
				printf("[N]ivel %d o [C]errar\n", ++level);
				option = getchar();
			} while (option != 'n' && option != 'N' && option != 'c' && option != 'C');
			n[0] = randomnumber();
		}
		if (gamestate == LOST && mode < 2) {
			printf("Nivel %d NO COMPLETADO\n", level);
			do {
				printf("[R]eintentar o [C]errar\n");
				option = getchar();
			} while (option != 'r' && option != 'R' && option != 'c' && option != 'C');
		}
		if (mode == LOAD_SUDOKU) {
			if (gamestate == WIN) {
				printf("Sudoku COMPLETADO\n");
			}
			else {
				printf("Sudoku NO COMPLETADO\n");
			}
			printf("[R]eintentar o [C]errar\n");
			option = getchar();
		}
		if (option == 'r' || option == 'R' || option == 'n' || option == 'N') {
			for (int i = 0; i < 16; i++) {
				numbers[i] = make_sudoku(i, path);
			}
		}
		/* multiplayer mode */
		if (mode == COMPETITION_EASY && gamestate == WIN) {
			return 1;
		}
		if (mode == COMPETITION_DIFF && gamestate == WIN) {
			return 3;
		}
		if (mode == COMPETITION_EASY || mode == COMPETITION_DIFF) {
			if (gamestate == LOST) {
				return 0;
			}
		}
	}

	return 0;
}
コード例 #16
0
ファイル: sudoku.c プロジェクト: hrussel/kec-sat-solver
int main(int argc, char* argv[]){
    
    FILE *f, *in_pdf = NULL;
    int** t;
    char s[BUFFERSIZE];
    int n, i, j, index, s_size, n2;
    
    parse_args(argc, argv);
    
    f = fopen(input_filename, "r");
    
    if ( f == NULL ){
        printf("Error: can't open file %s.\n", input_filename);
        exit(1);
    }
    
    if ( output_pdf_filename != NULL ){
        in_pdf = fopen("auxiliar_pdf", "w");
        
        if ( in_pdf == NULL ){
            printf("Error: can't open file %s.\n", output_pdf_filename);
            exit(1);
        }
    }
    
    // It reads each instance of sudoku problem

    n = 3;
    n2 = n*n;

    // Allocate memmory for the board

    t = (int**)malloc(n2*sizeof(int*));
    for (i=0; i<n2; i++){
        t[i] = (int*)malloc(n2*sizeof(int));
    }
    
    int T = 0;
    while(fscanf(f, "%s", s) != EOF){
        
        printf("Sudoku #%d\n", ++T);
        
        system("rm -rf sudoku.cnf");
       
        s_size = strlen(s);
        
        // It reads the initial state of each cell in the board
        for (i=0; i<n2; i++){
            for (j=0; j<n2; j++){
                
                int num = s[i*n2 + j] - '0';
                
                t[i][j] = num;
            }
        }
        //Print initial sudoku
        if(output_pdf_filename == NULL){
            printf("Initial puzzle:\n");
            print_sudoku(t,n);
        }
        
        // It transforms the problem of sudoku to a SAT instance.
        sudoku2cnf(t, n, output_filename);
        
        // Call the SAT solvers with the cnf formula.
        
        if ( output_pdf_filename != NULL){
            
            fprintf(in_pdf, "%d\n", n);
            print_sudoku_pdf(t, n, in_pdf);
            
            fprintf(in_pdf, "1\n", n);
        }
            
        solve_and_read(command1, t, n, "kecosats", in_pdf);
            
        if(output_pdf_filename == NULL){
            print_sudoku(t,n);
            printf("\n");
        }
            
        system("rm -rf sudoku.out");

        if(output_filename == NULL)
            system("rm -rf sudoku.cnf");
        
    }
    // Free memory allocated to the sudoku board.
    for (i=0; i<n2; i++){
        free(t[i]);
    }
    free(t);
    
    // Generate the pdf
    
    if ( output_pdf_filename != NULL ){
        fprintf(in_pdf, "0\n");
        fclose(in_pdf);
        
        char command[1000];
        memset(command, 0, sizeof command);
        sprintf(command, "perl ./sudoku2cnf/sudoku2pdfLatex.pl auxiliar_pdf sudoku.tex");
        system(command);
       
        if(strcmp(output_pdf_filename,"sudoku.pdf")!=0){
            memset(command, 0, sizeof command);
            sprintf(command, "mv sudoku.pdf %s", output_pdf_filename);
            system(command);
        }
        
        system("rm -rf auxiliar_pdf sudoku.out sudoku.out2 sudoku.tex");

    }
    
    fclose (f);
}
コード例 #17
0
ファイル: sudoku.c プロジェクト: surajshetiya/sudoku
int main() {
	int sud[9][9];
	read_sudoku(sud);
	printf("\nEntered sudoku is");
	print_sudoku(sud);
	int rows[9][2];
	int cols[9][2];
	int boxes[9][2];
	if(preprocess(sud,rows,cols,boxes)) {
		printf("\nThere is some problem with the sudoku!");
		return 1;
	}
	print_sudoku_processed(sud,rows,cols,boxes);
	int prob[9][9];
	construct_prob(sud,rows,cols,boxes,prob);
	int val = prob_mat_eliminations(sud,rows,cols,boxes,prob);
	while(val) {
                if(val == -1) {
			print_sudoku(sud);
                        printf("\nThere is some problem with the sudoku!");
                        return 3;
                }
                printf("\nInserted %d values",val);
                val = prob_mat_eliminations(sud,rows,cols,boxes,prob);
        }
	int i = 0;
        while(i<9) {
                if(rows[i][0] != 9) {
                        break;
                }
                i++;
        }
        print_sudoku(sud);
	if(i==9) {
		printf("\nSuccess.. found the solution");
		return 0;
	}
	printf("\n\nCOPY MATRIX OPERATION\n\n");
	int **ptr_sud;
	ptr_sud = (int **)malloc(9*sizeof(int *));
	int j;
	for(i = 0;i<9;i++) {
		*(ptr_sud+i) = (int *)malloc(9*sizeof(int));
	}
	for(i = 0;i<9;i++) {
		for(j=0;j<9;j++) {
			*(*(ptr_sud+i)+j) = sud[i][j];
		}
	}	
	int failure = restart_operation(ptr_sud);
	if(!failure) {
		for(i = 0;i<9;i++) {
			for(j=0;j<9;j++) {
				sud[i][j] = *(*(ptr_sud+i)+j);
			}
		}
		print_sudoku(sud);
	}
	for(i = 0;i<9;i++) {
		free(*(ptr_sud+i));
	}
	free(ptr_sud);
	if(!failure) {
		printf("\nSuccess found at level %d\nTotal respawns = %d\n",success_level,total_respawns);
		return 0;
	}
	printf("\n\nFailure in finding a solution!\n");
	return 1;
}
コード例 #18
0
ファイル: sudoku.c プロジェクト: surajshetiya/sudoku
int restart_operation(int **orig_sud) {
	// If the solution to the sudoku is not found in eliminations, then we can fill up
	// a specific location with one of the probable values and try to find a solution.
	// If the fails then we do a rollback to the actual scenario and fill in a different
	// value in that location.
	// This process is done recursively.
	
	total_respawns++;
	int sud[9][9];
	int cop_x,cop_y;
	for(cop_x=0;cop_x<9;cop_x++) {
		for(cop_y=0;cop_y<9;cop_y++) {
			sud[cop_x][cop_y] = *(*(orig_sud+cop_x)+cop_y);
		}
	}
	print_sudoku(sud);
	int rows[9][2];
        int cols[9][2];
        int boxes[9][2];
        if(preprocess(sud,rows,cols,boxes)) {
                return 1;
        }
        int prob[9][9];
        construct_prob(sud,rows,cols,boxes,prob);
        int val = prob_mat_eliminations(sud,rows,cols,boxes,prob);
        while(val) {
                if(val == -1) {
			printf("Failure in elimination!");
			print_sudoku(sud);
                        return 3;
                }
                printf("\nInserted %d values",val);
                val = prob_mat_eliminations(sud,rows,cols,boxes,prob);
        }
	int i = 0;
	while(i<9) {
		if(rows[i][0] != 9) {
			break;
		}
		i++;
	}
	int pos_x,pos_y;
	if(i!=9) {
		// Unable to find a solution as of now.
		// Will need to spawn restart_operation with a location filled with one of its probabilities.
		// Finding the location which has least probabilities will reduce the task of multiple spawns.
		// Finding the location here, will spawn later.
		int min = 10;
		int j,k;
		for(j=0;j<9;j++) {
			for(k=0;k<9;k++) {
				if(sud[j][k] == 0) {
					int val = total_probables(prob[j][k]);
					if(val < min) {
						min = val;
						pos_x = j;
						pos_y = k;
					}
				}
			}
		}
	} else {
		int j,k;
                // Copy the matrix from the stack to the callers matrix
                printf("\n\nSUCCESS!\nCopying the matrix from the stack..\n");
		success_level++;
                for(j=0;j<9;j++) {
                	for(k=0;k<9;k++) {
                        	*(*(orig_sud+j)+k) = sud[j][k];
                        }   
                }
		return 0;
	}
	int prob_pos = prob[pos_x][pos_y];
	int value = 1;
	while(prob_pos != 0) {
		int unit_set = prob_pos % 2;
		if(unit_set == 1) {
			// Spawn a new restart_operation with a value set at location (pos_x,pos_y)
			int **mat;
			mat = (int **)malloc(9*sizeof(int *));
			for(i=0;i<9;i++) {
				*(mat+i) = (int *)malloc(9*sizeof(int));
			}
			int x,y;
			for(x=0;x<9;x++) {
				for(y=0;y<9;y++) {
					*(*(mat+x)+y) = sud[x][y];
				}
			}
			*(*(mat+pos_x)+pos_y) = value;
			int result = restart_operation(mat);
			if(result == 0) {
				// Success
				int j,k;
				// Copy the matrix from the stack to the callers matrix
				printf("\n\nSUCCESS!\nCopying the matrix from the stack..\n");
				success_level++;
				for(j=0;j<9;j++) {
		                        for(k=0;k<9;k++) {
						*(*(orig_sud+j)+k) = *(*(mat+j)+k);
					}
				}
			}
			int new_m=0;
			for(;new_m<9;new_m++) {
				free(*(mat+new_m));
			}
			free(mat);
			if(result == 0) {
				return 0;
			}
		}
		prob_pos = prob_pos >> 1;
		value++;
	}
	return 4;
}
コード例 #19
0
ファイル: gamemodes.cpp プロジェクト: victorhck/AmayaOS
int sudoku(int mode)
{
	clear_window();
	int level = 1;
	/* we get a random number to select a sudoku */
	int n = randomnumber();
	int numbers[16];
	int numbers_def[16];
	/* get sudoku */
	for (int i = 0; i < 16; i++) {
		numbers_def[i] = make_sudoku(i, n, mode);
	}
	for (int i = 0; i < 16; i++) {
		numbers[i] = numbers_def[i];
	}
	int sudoku4x4[4][4];
	int subregions[4][4];
	char option = 'r';
	while (option != 'c' && option != 'C') {
		print_sudoku(numbers);
		for (int i = 0; i < 16; i++) {
			if (numbers[i] == 0) {
				printf("Introduce el valor a la X%d: ", i+1);
				numbers[i] = getnum();
				if (numbers[i] == 0) {
					numbers[i] = 1;
					i--;
				}
				/* if you press return key */
				if (numbers[i] == -1) {
					/* put numbers[i] and numbers[i-1] equal to 0
					 * if not you can't rewrite it 
					 */
					numbers[i] = 0;
					i--;
					if (numbers[i] != numbers_def[i]) {
						numbers[i] = 0;
						i--;
					}
				}
				printf("%c\n", numbers[i]);
				print_sudoku(numbers);
			}
		}
		for (int i = 0, k = 0, p=0; k < 4; i++) {
			sudoku4x4[k][i] = numbers[p];
			if ((i+1) % 4 == 0) {
				k++;
				i = -1;
			}
			p++;
		}
		for(int i = 0, k = 0; i < 4; i++) {
			for(int j = 0; j < 4; j++) {
				subregions[i][j] = numbers[k];
				k++;
			}
		}
		int gamestate = WIN;
		/* check rows and columns */
		for (int i = 0, k = 0; k < 4; i++) {
			if (sudoku4x4[k][i] == sudoku4x4[k][0] && i != 0) {
				gamestate = LOST;
			}
			if (sudoku4x4[k][i] == sudoku4x4[k][1] && i != 1) {
				gamestate = LOST;
			}
			if (sudoku4x4[k][i] == sudoku4x4[k][2] && i != 2) {
				gamestate = LOST;
			}
			if (sudoku4x4[k][i] == sudoku4x4[k][3] && i != 3) {
				gamestate = LOST;
			}
			if ((i+1) % 4 == 0) {
				k++;
				i = -1;
			}
		}
		for (int i = 0, k = 0; i < 4; k++) {
			if (sudoku4x4[k][i] == sudoku4x4[0][i] && k != 0) {
				gamestate = LOST;
			}
			if (sudoku4x4[k][i] == sudoku4x4[1][i] && k != 1) {
				gamestate = LOST;
			}
			if (sudoku4x4[k][i] == sudoku4x4[2][i] && k != 2) {
				gamestate = LOST;
			}
			if (sudoku4x4[k][i] == sudoku4x4[3][i] && k != 3) {
				gamestate = LOST;
			}
			if ((k+1) % 4 == 0) {
				i++;
				k = -1;
			}
		}
		/* check subregions */
		for (int i = 0, k = 0; k < 4; i++) {
			if (subregions[k][i] == subregions[k][0] && i != 0) {
				gamestate = LOST;
			}
			if (subregions[k][i] == subregions[k][1] && i != 1) {
				gamestate = LOST;
			}
			if (subregions[k][i] == subregions[k][2] && i != 2) {
				gamestate = LOST;
			}
			if (subregions[k][i] == subregions[k][3] && i != 3) {
				gamestate = LOST;
			}
			if ((i+1) % 4 == 0) {
				k++;
				i = -1;
			}
		}
		if (gamestate == WIN && mode < 2) {
			printf("Nivel %d COMPLETADO\n", level);
			do {
				printf("[N]ivel %d o [C]errar\n", level+1);
				option = getchar();
			} while (option != 'n' && option != 'N' && option != 'c' && option != 'C');
			n = randomnumber();
			level++;
		}
		if (gamestate == LOST && mode < 2) {
			printf("Nivel %d NO COMPLETADO\n", level);
			do {
				printf("[R]eintentar o [C]errar\n");
				option = getchar();
			} while (option != 'r' && option != 'R' && option != 'c' && option != 'C');
		}
		if (option == 'r' || option == 'R' || option == 'n' || option == 'N') {
			for (int i = 0; i < 16; i++) {
				numbers[i] = make_sudoku(i, n, mode);
			}
		}
		/* multiplayer mode */
		if (mode == COMPETITION_EASY && gamestate == WIN) {
			return 1;
		}
		if (mode == COMPETITION_DIFF && gamestate == WIN) {
			return 3;
		}
		if (mode >= COMPETITION_EASY && gamestate == LOST) {
			return 0;
		}
	}
	return 0;
}