Esempio n. 1
0
int hasSolution(sudokuGrid game){
    int solved;
    cell candidateCell;
    value trialValue;

    if(isFull(game)){
        solved = TRUE;
    }else{
        candidateCell = getEmptyCell(game);
        trialValue = MIN_VALUE;
        solved = FALSE;
        while(!solved && (trialValue <= MAX_VALUE)){
            if(isLegal(game, candidateCell, trialValue)){
                setCell(game, candidateCell, trialValue);

                if(hasSolution(game)){
                    solved = TRUE;
                }else{
                    clearCell(game, candidateCell);
                }
            }
            trialValue++;
        }
    }
    return solved;
}
Esempio n. 2
0
int solutionCount(sudokuGrid *game,int solutionsCuttoff) {
	//this function destroys the board in the process of counting the number of solutions//
	int position, trialValue, count;
	
	//if the board is full up, we are done
	if (isBoardFull(game)) {
		count = 1;
	} else {
		count = 0;
		
		position = getEmptyCell(game);
		trialValue = MIN_VALUE;
		while ((count < solutionsCuttoff) && (trialValue <= MAX_VALUE)) {
			if (isLegalMove(game,position,trialValue)) {
				commitMove(game,position,trialValue);
				if (solutionCount(game,1)) {
					//game with trial value in this position is solvable
					count++;
				}
				
				//reset to a blank value to try another solution
				commitMove(game,position,BLANK_VALUE);
			}
			
			trialValue++;
		}
	}
	
	return count;
}
Esempio n. 3
0
int isBoardFull(sudokuGrid *game) {
	if (getEmptyCell(game) == -1) {
		return TRUE;
	} else {
		return FALSE;
	}
}
Esempio n. 4
0
int hasSolution(sudokuGrid *game) {
	int position, trialValue, solved;
	
	//if the board is full, we are done
	if (isBoardFull(game)) {
		solved = TRUE;
	} else {
		solved = FALSE;
		position = getEmptyCell(game);
		trialValue = MIN_VALUE;
		
		while (!solved && (trialValue <= MAX_VALUE)) {
			if (isLegalMove(game,position,trialValue)) {
				commitMove(game,position,trialValue);
				
				if (hasSolution(game)) {
					//game with trial value in this position is solvable
					solved = TRUE;
				} else {
					//reset to a blank value to try another solution
					commitMove(game,position,BLANK_VALUE);
				}
			}
			
			trialValue++;
		}
	}
	
	return solved;
}
Esempio n. 5
0
int generateRandomBoard(sudokuGrid *game) {
	int position, trialValue, solved;
	int trialValues[NUM_VALUES];
	int i,j;
	
	//if the board is full, we are done
	if (isBoardFull(game)) {
		solved = TRUE;
	} else {
		//populate and then shuffle array of trial values
		j = 0;
		for (i = MIN_VALUE; i <= MAX_VALUE; i++) {
			trialValues[j] = i;
			j++;
		}
		shuffleArr(trialValues,NUM_VALUES);
		
		solved = FALSE;
		position = getEmptyCell(game);
		j = 0;
		
		while (!solved && (j < NUM_VALUES)) {
			trialValue = trialValues[j];
			
			if (isLegalMove(game,position,trialValue)) {
				commitMove(game,position,trialValue);
				
				if (generateRandomBoard(game)) {
					//game with trial value in this position is solvable
					solved = TRUE;
				} else {
					//reset to a blank value to try another solution
					commitMove(game,position,BLANK_VALUE);
				}
			}
			
			j++;
		}
	}
	
	return solved;
}