Question

Hi i am making a kenken solver which is a puzzle just like sudoku. I have a cage structure which has number of cells for a cage. I want to apply constraints whenever i am trying a value for the cage. For this i am calling roe/column/cage constraint to my puzzle every time.

However i am struck in cage constraint for the problem. Here is my code for all 3 constraints. For cage constraint i would like to see all cells for the particular cell's cage and see if the number passed satisfy our criteria.

    //Row Constraint Check: Checks if num is an acceptable value for the given Row
public static boolean rowConstraintCheck(int rowIndex, int num){
    for(int columnIndex = 0; columnIndex < puzzleDimension; columnIndex++){
        if(puzzleArray[rowIndex][columnIndex] == num){
            return false;
        }
    }
    return true;
}
//Column Constraint Check: Checks if num is an acceptable value for the given Column    
public static boolean columnConstraintCheck(int columnIndex, int num){
    for(int rowIndex = 0; rowIndex < puzzleDimension; rowIndex++){
        if(puzzleArray[rowIndex][columnIndex] == num){
            return false;
        }
    }
    return true;
}
//Cage constraint Check: Checks if num is an acceptable value for the given Cage
public static boolean cageConstraintCheck(int rowIndex, int columnIndex, int num){
    if(true){
        int cageToCell =  cellToCageMapper[rowIndex][columnIndex];          
        String currentOperator = cages.get(cageToCell).cageOperator;
        int currentTotal = cages.get(cageToCell).cageValue;
        int numberOfCages = cages.get(cageToCell).placeHolders.length;            
        //System.out.println(rowIndex+"."+ columnIndex+"."+ cageToCell +"."+ currentOperator +"."+ currentTotal +"."+ numberOfCages);

         int flagNonZeroCages = 0;
         for(int j=0;j<numberOfCages;j++) {
            int tempIndex = cages.get(cageToCell).placeHolders[j];
            int tempCellRow = (int) (Math.floor(tempIndex/puzzleDimension));
            int tempCellCol = (tempIndex % puzzleDimension);
            if(puzzleArray[tempCellRow][tempCellCol] != 0){
                flagNonZeroCages++;System.out.println("bingo"+j);   
            }
        }
        if(flagNonZeroCages == numberOfCages){
            System.out.println("bingo");            
        }

         System.out.println();
        return true;
    }
    return false;
}

Now i am stuck here in my approach.. i do not know how to go for cage constraint checking. this is what i attempted, but not sure what i am missing and what to do next.

Was it helpful?

Solution

/**
 * Cage constraint Check: Checks if num is an acceptable value for the
 * given Cage
 *
 * Precondition: Given cell is empty, and has passed rowConstraintCheck() and
 * columnConstraintCheck()
 */
public static boolean cageConstraintCheck(
        int rowIndex, int columnIndex, int num) {

    int cageIndex =  cellToCageMapper[rowIndex][columnIndex];          
    Cage cage = cages.get(cageIndex); // or whatever class-name you are using

    String currentOperator = cage.cageOperator;
    int targetValue = cage.cageValue;

    // Sum and product of all cells in cage, including the new one.
    int sum = num;
    int product = num;

    // Last non-zero value seen in the cage, not counting the new one.
    int last = -1;

    int numberOfEmptyCellsInCage = 0;
    int numberOfCellsInCage = cage.placeHolders.length;            

    if (numberOfCellsInCage == 1)
    {
        // Single-cell cage
        return (targetValue == num);
    }

    for (int j = 0; j < numberOfCellsInCage; j++) {
        int cellIndex = cage.placeHolders[j];
        int cellRow = (cellIndex / puzzleDimension); // Integer division
        int cellCol = (cellIndex % puzzleDimension);
        int cellValue = puzzleArray[cellRow][cellCol];
        if (cellValue == 0) {
            // Empty cell
            numberOfEmptyCellsInCage++;
        }
        else {
            // Update the tracking variables
            sum += cellValue;
            product *= cellValue;
            last = cellValue;
        }
    }

    if (numberOfEmptyCellsInCage == 1 && last != -1) {
        // The new number will be placed in the only empty spot in the cage.

        // For subtraction and division, there will only be two cells. Sort
        // their values onto 'low' and 'high'.
        int low = num < last ? num : last;
        int high = num + last - low;

        switch (currentOperator.charAt(0)) {
            case '+':
                if (targetValue != sum) {
                    // The new value would produce an incorrect sum
                    return false;
                }
                break;
            case '*':
                if (targetValue != product) {
                    // The new value would produce an incorrect product
                    reutrn false;
                }
                break;
            case '-':
                if (targetValue != high - low) {
                    // The new value would produce an incorrect difference
                    return false;
                }
                break;
            case '/':
                if (high % low != 0 || targetValue != high / low) {
                    // The new value would produce an incorrect quotient
                    return false;
                }
                break;
        }
    }

    return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top