Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

KenKen KenKen is a mathematics game similar to Sudoku, in which a N × N grid of

ID: 3834573 • Letter: K

Question

KenKen

KenKen is a mathematics game similar to Sudoku, in which a N × N grid of numbers must be solved so that each square contains a number from 1 up to N. As in Sudoku, every row and column of KenKen can contain a particular number only once, and every number from 1 to N must be in each row and column. KenKen does not use the 3 x 3 squares uniqueness rule of Sudoku; instead, each KenKen puzzle breaks up the squares into various irregular “cages”, and in each cage a single target value and a single operator is placed. When the operator is applied to the squares of the cage, the given value is obtained. The order that the operator is applied to the squares is not fixed, which is important for division and subtraction. See Wikipedia for an explanation.

Adapt the Sudoku solver from lecture to solve the KenKen puzzle given here: http://www.calcudoku.org/en/2016-11-29/6/3.

You need to retain the code that forces unique values for each entry in a row/column, but do not need the 3 x 3 squares code. You will need to directly encode the values and operators for each cage into the logic itself. For example, if the first two squares in the first row are a cage with 3+ written in the puzzle, then you would need a relation forcing the sum of A1 and A2 to be 3.

Implementation notes:

1. The library clpfd used in the Sudoku solver will be needed here. This library uses its own syntax for checking arithmetic values for equality. Do not use is or = for enforcing cage constraints; you need to use #=.

2. If a cage is marked 3-, then you will need an OR relation because the order of the squares is not xed, e.g. (A1 - A2 #= 3; A2 - A1 #= 3). Likewise for division.

Explanation / Answer

import java.awt.*; // Uses AWT's Layout Managers
import java.awt.event.*; // Uses AWT's Event Handlers
import javax.swing.*; // Uses Swing's Container/Components

/**
* The Sudoku game.
* to unravel the quantity puzzle, each row, every column, and every of the
* 9 3×3 sub-grids shall contain all of the digits from one to nine
*/
public category Sudoku extends JFrame the sport properties
public static final int GRID_SIZE = 9; // Size of the board
public static final int SUBGRID_SIZE = 3; // Size of the sub-grid

// Name-constants for UI management (sizes, colours and fonts)
public static final int CELL_SIZE = 60; // Cell width/height in pixels
public static final int CANVAS_WIDTH = CELL_SIZE * GRID_SIZE;
public static final int CANVAS_HEIGHT = CELL_SIZE * GRID_SIZE;
// Board width/height in pixels
public static final Color OPEN_CELL_BGCOLOR = Color.YELLOW;
public static final Color OPEN_CELL_TEXT_YES = new Color(0, 255, 0); // RGB
public static final Color OPEN_CELL_TEXT_NO = Color.RED;
public static final Color CLOSED_CELL_BGCOLOR = new Color(240, 240, 240); // RGB
public static final Color CLOSED_CELL_TEXT = Color.BLACK;
public static final Font FONT_NUMBERS = new Font("Monospaced", Font.BOLD, 20);

// the sport board composes of 9x9 JTextFields,
// every containing String "1" to "9", or empty String
personal JTextField[][] tfCells = new JTextField[GRID_SIZE][GRID_SIZE];

// Puzzle to be resolved and also the mask (which is accustomed management the
// issue level).
// Hardcoded here. further credit for automatic puzzle generation
// with varied issue levels.
personal int[][] puzzle =
,
,
,
,
,
,
,
,
};
// For testing, open solely a pair of cells.
personal boolean[][] masks =
,
,
,
,
,
,
,
,
};

/**
* creator to setup the sport and also the UI parts
*/
public Sudoku() instrumentality cp = getContentPane();
cp.setLayout(new GridLayout(GRID_SIZE, GRID_SIZE)); // 9x9 GridLayout

// allot a typical auditor because the ActionEvent auditor for all the
// JTextFields
// ... [TODO 3] (Later) ....

// Construct 9x9 JTextFields and augment the content-pane
for (int row = 0; row < GRID_SIZE; ++row) mountain pass = 0; mountain pass < GRID_SIZE; ++col) {
tfCells[row][col] = new JTextField(); // allot part of array
cp.add(tfCells[row][col]); // ContentPane adds JTextField
if (masks[row][col]) {
tfCells[row][col].setText(""); // set to empty string
tfCells[row][col].setEditable(true);
tfCells[row][col].setBackground(OPEN_CELL_BGCOLOR);

// Add ActionEvent auditor to method the input
// ... [TODO 4] (Later) ...
} else guide on a way to run the creator
.......
}

// outline the auditor Inner category
// ... [TODO 2] (Later) ...
}
3. Event Handling
Next, we have a tendency to shall program the event handling.
We shall use a typical instance of a Named Inner category (called InputListener) because the ActionEvent auditor for "all" the editable JTextFields. Hence, within the actionPerformed(), we want to spot the actual JTextField (in terms of row and col) that trigger the event. you'll use the ActionEvent.getSource() methodology to retrieve the supply object that has dismissed the event and compare the article with all the 9×9 JTextFields.
Place the inner category at [TODO 2].
// [TODO 2]
// Inner category to be used as ActionEvent auditor for ALL JTextFields
personal category InputListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
// All the 9*9 JTextFileds invoke this handler. we want to work out
// that JTextField (which row and column) is that the supply for this invocation.
int rowSelected = -1;
int colSelected = -1;

// Get the supply object that dismissed the event
JTextField supply = (JTextField)e.getSource();
// Scan JTextFileds for all rows and columns, and match with the supply object
Boolean found = false;
for (int row = 0; row < GRID_SIZE && !found; ++row) {
for (int mountain pass = 0; mountain pass < GRID_SIZE && !found; ++col)
}
}