Random20

Number Place Generator Version 2.0 -- Sample Programs

How to call Java API of Number Place Generator Version 2.0.

Random 20

Let's write a Java program that makes 20 hint problem in a random manner.

  • standard board : size is 9.
  • 20 hint.
  • hint layouts are 90 degree rotational symmetric.
  • calculate the difficulty point.

Random20.java

package jp.gr.puzzle.npv2.sample;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import jp.gr.puzzle.npv2.core.*;

public class Random20 {
        private final static int numSize = 9;
        
        private static int[] hint = new int[numSize*numSize];
        private static int[] hidden = new int[numSize*numSize]; 
        private static SolverMethod method = new SolverMethod();

        private static void setHint(int x, int y, int h) {
                hint[y*numSize+x] = h;
        }
        private static int getHint( int x, int y ) {
                return hint[y*numSize+x];
        }
        
        private static void makeHint() {
                Random r = new Random();
                Arrays.fill(hint,0);
                for( int i=0; i<20; ) {
                        int x = r.nextInt(numSize);
                        int y = r.nextInt(numSize);
                        if( x==numSize/2 && y==numSize/2 )
                                continue;
                        if( getHint(x,y) != 0 )
                                continue;
                        setHint(x,y,1);
                        setHint(y,numSize-1-x,1);
                        setHint(numSize-1-x, numSize-1-y, 1 );
                        setHint(numSize-1-y, x, 1);
                        i += 4; 
                }
        }
        
        public static void main(String[] args) {
                System.out.println("Random20");

                ArrayList<Integer[]> blockarraylist = Utility.makeNormalBlock(numSize,
                                3, 3);
                BlockConstraint block = new BlockConstraint(blockarraylist, numSize);
                int[] problem = null;

                for (;;) {
                        makeHint();
                        Utility.printGrid(hint, numSize);

                        Generator generator = new Generator(numSize, hint, hidden, block);
                        generator.setMethod(method);

                        int time = 1;
                        while (problem == null && time <= 100) {
                                problem = generator.generate();
                                ++time;
                        }
                        System.out.println("loopcount : " + time);
                        if (problem != null)
                                break;
                        System.out.println("failed.");
                }

                if (problem != null) {
                        int point = (int) Evaluator.evaluate(numSize, block, problem);
                        System.out.println("point: " + point);
                        Utility.printGrid(problem, numSize);
                }
        }
}

This program tries to make a problem 100 times with a same hint pattern. If making problem fails, new hint pattern will be made.

Some hint patterns are impossible to make a problem, but this simple program doesn't check the immpossibility.

result

Random20
 1 0 1 0 0 1 0 0 1
 0 1 0 1 0 0 0 1 0
 0 0 0 0 0 0 0 0 1
 1 0 0 0 0 0 0 1 0
 0 0 0 0 0 0 0 0 0
 0 1 0 0 0 0 0 0 1
 1 0 0 0 0 0 0 0 0
 0 1 0 0 0 1 0 1 0
 1 0 0 1 0 0 1 0 1
loopcount : 101
failed.
 0 1 1 0 1 0 0 1 0
 1 0 0 1 0 0 0 0 1
 0 0 0 0 0 0 0 0 1
 0 0 0 0 0 0 0 1 0
 1 0 0 0 0 0 0 0 1
 0 1 0 0 0 0 0 0 0
 1 0 0 0 0 0 0 0 0
 1 0 0 0 0 1 0 0 1
 0 1 0 0 1 0 1 1 0
loopcount : 13
point: 4398
 0 9 3 0 5 0 0 1 0
 6 0 0 8 0 0 0 0 4
 0 0 0 0 0 0 0 0 3
 0 0 0 0 0 0 0 3 0
 7 0 0 0 0 0 0 0 8
 0 2 0 0 0 0 0 0 0
 2 0 0 0 0 0 0 0 0
 8 0 0 0 0 4 0 0 7
 0 5 0 0 3 0 2 9 0

The first trial was failed, but the secend trial was OK!

This diffilulty of the problem was 4398 point. This is a medium level problem.

Printed image is as bellow.

PUZZLE GeneRator JaPan
Copyright(C) 2007 Time Intermedia Corporation. All Rights Reserved.