/KenKen

KenKen puzzle game

Primary LanguageJava

KenKen

This KenKen puzzle generator creates KenKen puzzles.

Rules

KenKen puzzles are comprised of a grid (around 4x4 usually) and several regions within it. Each region has a target number and an operator. The goal of the puzzle is fill each space in the grid, such that:

  1. Each row and column has every digit from 1 to N.
  2. No row or column has duplicate digits.
  3. If the given operation is performed on the values of a region, it will equal the target.

The operations are performed such that:

  1. Addition/Multiplication: add or multiply all of the values
  2. Subtraction/Division: subtract or divide the largest value by all the others.
  3. No operator: Only used in size-1 regions, solution value is always the target

Storing the puzzles

These puzzles are stored in the following format:

(size) (number of regions)

(solution, if requested)

(region 1's target) (region 1's operator)
(region 2's target) (region 2's operator)
(...)
(region N's target) (region N's operator)

(region grid)

3x3 Example:

3 2

1 2 3
3 1 2
2 3 1

10 +
12 *

1 1 1
1 1 2
2 2 2

20x20 Example:

20 39

16 13 17 2 5 4 12 10 6 20 1 8 14 18 7 3 19 9 15 11 
7 12 19 16 4 9 10 6 18 1 15 3 5 13 2 17 11 14 8 20 
5 9 10 1 15 13 6 3 7 11 20 17 18 8 4 16 14 19 12 2 
8 11 13 5 6 7 2 19 17 4 18 12 3 10 20 14 16 15 1 9 
20 15 12 14 17 16 3 18 4 5 13 9 10 7 11 6 8 1 2 19 
4 8 2 7 11 15 5 14 3 16 17 13 6 20 19 1 9 10 18 12 
19 16 9 17 10 2 13 11 20 15 12 18 4 3 8 5 1 7 6 14 
1 5 18 12 7 11 16 9 8 14 19 2 15 4 6 13 20 17 10 3 
18 3 1 19 2 12 9 15 16 13 7 4 20 14 5 10 17 6 11 8 
10 2 11 20 1 6 4 16 5 3 8 15 7 9 18 19 13 12 14 17 
9 17 8 4 18 3 1 13 10 6 14 20 12 16 15 11 2 5 19 7 
3 7 15 13 20 5 18 12 11 8 4 1 9 19 14 2 6 16 17 10 
11 20 3 8 16 14 19 1 2 17 6 7 13 15 9 12 10 4 5 18 
17 10 4 3 14 19 20 5 15 2 9 6 1 11 12 18 7 8 16 13 
2 19 5 15 3 18 8 7 9 12 16 14 17 1 10 20 4 11 13 6 
6 4 20 10 19 8 14 17 12 9 5 16 11 2 13 15 3 18 7 1 
15 1 6 9 13 17 7 8 14 19 11 10 16 12 3 4 18 2 20 5 
13 18 14 6 12 20 17 2 19 7 10 11 8 5 1 9 15 3 4 16 
12 14 16 11 8 10 15 20 1 18 3 19 2 6 17 7 5 13 9 4 
14 6 7 18 9 1 11 4 13 10 2 5 19 17 16 8 12 20 3 15 

4998913920 *
60 +
105 +
51197432832000 *
6840 *
886828800 *
121968000 *
147867033600 *
108 +
65 +
65 +
2476089907200000 *
26147552895959040 *
134 +
125 +
61 +
80 +
3921372000 *
77 +
3250368000 *
68 +
16005600 *
2133226368000 *
18506880 *
65 +
93 +
67 +
199 +
7915252001575010304 *
90 +
64 +
5376 *
45 +
2590963200 *
8719808034435497984 *
215 +
9504000 *
1219276800 *
1281772800 *

27 27 27 27 31 31 31 31 31 11 11 11 25 25 25 33 33 33 10 10 
27 27 30 30 22 22 22 31 31 11 11 26 25 25 2 2 2 33 10 10 
37 37 30 30 22 22 22 31 1 1 11 26 21 25 32 32 2 5 5 10 
37 37 30 30 34 34 22 22 1 1 1 26 21 21 20 32 2 5 5 10 
37 37 30 30 34 34 34 34 1 1 1 26 21 21 20 32 19 5 5 17 
16 37 24 24 8 8 34 34 34 14 1 26 38 21 20 19 19 19 17 17 
16 24 24 8 8 8 8 8 34 14 1 26 38 20 20 19 19 39 17 17 
16 24 24 9 9 9 8 8 8 14 26 26 38 20 20 19 19 39 39 17 
16 24 7 9 9 9 9 9 8 14 18 18 38 38 20 19 6 39 39 17 
16 24 7 7 9 4 9 9 14 14 29 18 38 38 20 6 6 39 23 23 
16 24 7 3 13 4 4 4 14 14 29 18 38 38 20 6 6 39 23 23 
7 7 7 3 13 4 4 4 14 14 29 18 18 18 18 12 6 39 39 23 
7 7 3 3 13 13 4 4 14 14 29 28 18 18 12 12 6 6 6 23 
15 15 3 3 13 13 4 4 29 14 29 28 28 28 12 12 12 6 35 23 
36 15 3 3 13 13 4 4 29 14 29 28 28 28 12 12 12 35 35 23 
36 15 3 3 13 13 4 4 29 29 29 28 28 28 12 12 12 35 35 23 
36 15 3 15 13 13 13 4 29 29 29 28 28 28 12 12 12 35 35 23 
36 15 3 15 13 36 36 36 29 29 29 28 28 28 12 35 12 35 35 23 
36 15 15 15 13 36 36 36 29 29 29 28 28 28 35 35 12 35 35 23 
36 36 36 36 36 36 36 36 36 29 29 28 28 35 35 35 35 35 35 23 

Running the program

  1. $ git clone https://www.github.com/Michael-Bianconi/KenKen

  2. $ cd KenKen

  3. $ javac *.java

  4. $ java KenKenCreator size [-s] [file]

     size        size of the grid
     -s          show solution
     file        output to this file (if not given, print instead)
    

NOTE!

Due to overflow, the maximum puzzle size is determined by

(N!)^N < 2^63 - 1 --> N = 6

For any N larger than 6, there is possibility for overflow.

edit:

I changed it so overflow errors are caught and the addition operator is used instead. Now, the maximum grid size is determined by

N * (sum j, j=1, N) < 2^63 - 1 --> N = 264,225