Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 25.30 KB | None | 0 0
  1. /*
  2. import java.awt.geom.Line2D;
  3. import java.awt.geom.QuadCurve2D;
  4.  
  5. class DrawH{
  6.     public static void main(String[] args) {
  7.         if(args[1] == null){
  8.             System.out.println("You have to input a second argument that is an integer from 3 to 20.\n");
  9.             return;
  10.         }
  11.         int L = Integer.parseInt(args[1]);
  12.         while ((L <= 20) && (L >= 3)) {
  13.  
  14.             if (args[0] == null || args[1] == null) {
  15.                 System.out.println("The first parameter needs to be a character from 'c,w,f,g' and the second an integer from 3 to 20.\n");
  16.                 return;
  17.             }
  18.  
  19.             if (args[0].equals("c")) {
  20.                 console(L);
  21.                 Scanner in = new Scanner(System.in);
  22.                 L = in.nextInt();
  23.             } else if (args[0].equals("w")) {
  24.                 window(L);
  25.                 L = Integer.parseInt(JOptionPane.showInputDialog(
  26.                         "Give me a number ",4));
  27.             } else if (args[0].equals("g")) {
  28.                 graphics(L);
  29.                 Scanner in = new Scanner(System.in);
  30.                 L = in.nextInt();
  31.             } else if (args[0].equals("f")) {
  32.                 file(L);
  33.                 Scanner in = new Scanner(System.in);
  34.                 L = in.nextInt();
  35.             } else {
  36.                 System.out.println("The first parameter needs to be a character from 'c,w,f,g'");
  37.                 break;
  38.             }
  39.         }
  40.         if ((L > 20) || (L < 3)) {
  41.             System.out.println("The second argument needs to be an integer from 3 to 20.\n");
  42.         }
  43.     }
  44.  
  45.     public static void console(int height){
  46.         for(int i = 0; i<height; i++){
  47.             if(i == (height/2) ){
  48.                 System.out.print("\n****");
  49.             }else{
  50.                 System.out.print("\n*  *");
  51.             }
  52.         }
  53.     }
  54.  
  55.     public static void window(int height){
  56.         String out = "";
  57.         for(int i = 0; i<height; i++){
  58.             if(i == (height/2) + 1){
  59.                 out = out + "****\n";
  60.             }else{
  61.                 out = out + "*   *\n";
  62.             }
  63.         }
  64.         JOptionPane.showMessageDialog(null, out, "Output window",JOptionPane.INFORMATION_MESSAGE);
  65.     }
  66.  
  67.     public static void file(int height){
  68.         PrintWriter writer;
  69.         try {
  70.             writer = new PrintWriter("C:\\temp\\H.html", "UTF-8");
  71.             writer.println("<!DOCTYPE html>\n" +
  72.                     "<html>\n" +
  73.                     "<head>\n" +
  74.                     "<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"/>\n" +
  75.                     "</head>\n" +
  76.                     "<body><font size=\""+ height + "\">" + "H with font size: "+ height + "</font></body>\n" +
  77.                     "</html>");
  78.             writer.close();
  79.         } catch (Exception e) {
  80.             System.out.println("Problem: "+e);
  81.         }
  82.     }
  83.  
  84.     static void graphics(int height) {
  85.         int mid_point = (height*19 - 50)/2;
  86.         Frame f = new Frame("Painting H") {
  87.             public void paint (Graphics g) {
  88.                 Graphics2D g2 = (Graphics2D) g;
  89.                 QuadCurve2D q = new QuadCurve2D.Double();
  90.                 g2.draw(new Line2D.Double(50, 50, 50, height*19)); // a line
  91.                 g2.draw(new Line2D.Double(200, 50, 200, height*19));
  92.                 g2.draw(new Line2D.Double(50, height*19 - mid_point, 200, height*19 - mid_point));
  93.                 q.setCurve(200, height*19, 225, height*19 - mid_point/3, 250, height*19);
  94.                 g2.draw(q);
  95.             }
  96.         };
  97.         f.setSize(400,400);
  98.         f.setVisible(true);
  99.     }
  100. }*/
  101.  
  102.  
  103.  
  104. /*
  105. package csd.uoc.gr.A12;
  106. import java.util.Arrays;
  107. import java.util.Scanner;
  108.  
  109. public class MagicSquareChecker{
  110.     private static int n;
  111.     public static void main(String[] args){
  112.         System.out.println("Please input the number of n(from 2 to 10).");
  113.         Scanner in = new Scanner(System.in);
  114.         n = in.nextInt();
  115.         if((n < 2) || (n>10)){
  116.             System.out.println("n should be a number from 2 to 10.");
  117.             return;
  118.         }
  119.         int[][] MagicSquare = new int[n][n];
  120.         for(int i =1; i <= n; i++){
  121.             for(int j=1; j <= n; j++){
  122.                 System.out.println("Please type the number that goes in the position: [" + i + "]["+ j + "]." );
  123.                 MagicSquare[i-1][j-1] = in.nextInt();
  124.             }
  125.         }
  126.  
  127.  
  128.         for(int i =0; i < MagicSquare.length; i++){
  129.             System.out.println(toString2(MagicSquare[i]));
  130.         }
  131.         boolean k = checkIsMagic(MagicSquare);
  132.         if(k){
  133.             System.out.println("The square is magic and the magic number is: "+ getMagicNumber(MagicSquare)+" !");
  134.         }else{
  135.             System.out.println("The square is isn't magic!");
  136.         }
  137.  
  138.         return;
  139.     }
  140.  
  141.     public static boolean checkIsMagic(int[][] s){
  142.         int magic_num = getMagicNumber(s);
  143.         if(hasDuplicates(s)){
  144.             System.out.println("The matrix you inputted contains duplicates.");
  145.             return false;
  146.         }
  147.         for(int i =0; i < s.length; i++){
  148.             int sum = sumOfRow(s,i);
  149.             if(magic_num != sum){
  150.                 System.out.println("Sum of Row "+ i+1 +" isn't equal to the magic number.");
  151.                 return false;
  152.             }
  153.         }
  154.         for(int i =0; i < s.length; i++){
  155.             int sum = sumOfCollumn(s,i);
  156.             if(magic_num != sum){
  157.                 System.out.println("Sum of Column "+ i+1 +" isn't equal to the magic number.");
  158.                 return false;
  159.             }
  160.         }
  161.  
  162.             int sum = sumOfDiagonal1(s);
  163.             if(magic_num != sum){
  164.                 System.out.println("Sum of Diagonal1 isn't equal to the magic number.");
  165.                 return false;
  166.             }
  167.  
  168.             sum = sumOfDiagonal2(s);
  169.             if(magic_num != sum){
  170.                 System.out.println("Sum of Diagonal2 isn't equal to the magic number.");
  171.                 return false;
  172.             }
  173.  
  174.         return true;
  175.     }
  176.  
  177.     private static int sumOfRow (int[][] s, int k){
  178.         int sum = 0;
  179.         int i =0;
  180.         do{
  181.             sum += s[k][i];
  182.             i++;
  183.         }while(i < s.length);
  184.         return sum;
  185.     }
  186.  
  187.     private static int sumOfCollumn (int[][] s, int k){
  188.             int sum = 0;
  189.             int i =0;
  190.         do{
  191.             sum += s[i][k];
  192.             i++;
  193.         }while(i < s.length);
  194.             return sum;
  195.     }
  196.  
  197.     private static int sumOfDiagonal1 (int[][] s){
  198.         int sum = 0;
  199.         int i =0;
  200.         do{
  201.             sum += s[i][i];
  202.             i++;
  203.         }while(i < s.length);
  204.         return sum;
  205.     }
  206.  
  207.     private static int sumOfDiagonal2 (int[][] s){
  208.         int sum = 0;
  209.         int i = 0;
  210.         int k = s.length -1;
  211.         do {
  212.             sum += s[i][k];
  213.             i++;
  214.             k--;
  215.         }while(k !=-1);
  216.         return sum;
  217.     }
  218.     public static int getMagicNumber(int[][] s){
  219.         return s.length*(((s.length*s.length)+1)/2);
  220.     }
  221.  
  222.     private static boolean hasDuplicates(int[][] s){
  223.         for(int i = 0; i < s.length; i++){
  224.             for(int j = 0; j < s.length; j++){
  225.                 int same = s[i][j];
  226.                 for(int k =0; k < s.length; k++){
  227.                     for(int m =0; m < s.length; m++){
  228.                         if(k == i && m == j ){
  229.                             if(m == s.length-1 && k != s.length-1){
  230.                                 k++;
  231.                                 m=0;
  232.  
  233.                             }else if(m != s.length-1 && k == s.length-1){
  234.                                 m++;
  235.                             }else{
  236.                                 break;
  237.                             }
  238.                         }
  239.                         if(same == s[k][m]){
  240.                             return true;
  241.                         }
  242.                     }
  243.                 }
  244.             }
  245.         }
  246.         return false;
  247.     }
  248.     public static String toString2(int[] a) {
  249.         if (a == null)
  250.             return "null";
  251.         int iMax = a.length - 1;
  252.         if (iMax == -1)
  253.             return "[]";
  254.  
  255.         StringBuilder b = new StringBuilder();
  256.         b.append('[');
  257.         for (int i = 0; ; i++) {
  258.             b.append(a[i]);
  259.             if (i == iMax)
  260.                 return b.append(']').toString();
  261.             b.append("| ");
  262.         }
  263.     }
  264. }
  265. */
  266.  
  267. /*package csd.uoc.gr.A13;
  268. import csd.uoc.gr.A12.MagicSquareChecker;
  269. import java.io.BufferedReader;
  270. import java.io.InputStreamReader;
  271. import java.net.MalformedURLException;
  272. import java.net.URL;
  273. import javax.swing.JOptionPane;
  274. import javax.swing.*;
  275. import java.io.File;
  276. import java.io.FileNotFoundException;
  277. import java.io.IOException;
  278. import java.io.PrintWriter;
  279. import java.nio.charset.StandardCharsets;
  280. import java.util.ArrayList;
  281. import java.util.Arrays;
  282. import java.util.Scanner;
  283.  
  284. class MagicSquareCheckerFile {
  285.     public static void main(String[] args) throws IOException {
  286.         System.out.println("Would you like to choose a file or download one?\nPress \"1\" to choose a file or \"2\" to download a file and hit enter.");
  287.         int choice;
  288.         Scanner in = new Scanner(System.in);
  289.         choice = in.nextInt();
  290.         in.close();
  291.         String[] all = new String[0];
  292.         if(choice < 1 || choice > 2){
  293.             System.out.println("Wrong input!");
  294.             System.exit(1);
  295.         }
  296.         String filepath = "";
  297.         if(choice == 1){
  298.             JFileChooser fileChooser = new JFileChooser();
  299.             fileChooser.setDialogTitle("Select a file");
  300.             int userSelection = fileChooser.showSaveDialog(null);
  301.             if (userSelection == JFileChooser.APPROVE_OPTION) {
  302.                 File file = fileChooser.getSelectedFile();
  303.                 filepath = file.getAbsolutePath();
  304.                 System.out.println("The path of the selected file is: " + filepath);
  305.                 all = fillArray(filepath);
  306.             }
  307.         }else {
  308.             filepath =
  309.                     JOptionPane.showInputDialog("The URL is: ","");
  310.             System.out.println(filepath);
  311.             all = downloadANDwrite(filepath);
  312.  
  313.         }
  314.  
  315.         int commas = 0;
  316.         char c;
  317.         int index = 0;
  318.         ArrayList<Integer>numOfcommas = new ArrayList<>();
  319.         for(int i=0;i<all.length;i++) {
  320.             commas = 0;
  321.             for (int j = 0; j < all[i].length(); j++) {
  322.                 c = all[i].charAt(j);
  323.                 if(c == ','){
  324.                     commas++;
  325.                 }
  326.                 if(j == all[i].length()-1){
  327.                     numOfcommas.add(index,commas);
  328.                     index++;
  329.                 }
  330.             }
  331.         }
  332.  
  333.         for(int i=0;i< numOfcommas.size();i++){
  334.             if(i <= numOfcommas.size()-2) {
  335.                 if (!numOfcommas.get(i).equals(numOfcommas.get(i + 1))) {
  336.                     System.out.println("The structure of the matrix is not appropriate");
  337.                     System.exit(69420);
  338.                 }
  339.             }
  340.         }
  341.         int size = all.length;
  342.         int[][] MagicSquare = new int[size][size];
  343.         StringBuilder s1 = new StringBuilder();
  344.         int counter = 0;
  345.         int k;
  346.         StringBuilder s;
  347.         ArrayList<Integer>Numbers = new ArrayList<>();
  348.         for (int i = 0; i < size; i++) {
  349.             s = new StringBuilder();
  350.             for(int j=0; j < all[i].length(); j++){
  351.                 c = all[i].charAt(j);
  352.                 if( c == ',' || c==' ' || c=='\n'){
  353.                     int L = Integer.parseInt(s.toString());
  354.                     Numbers.add(counter,L);
  355.                     counter++;
  356.                     s = new StringBuilder();
  357.                 }else{
  358.                     s.append(c);
  359.                     if(j == all[i].length()-1){
  360.                         int L = Integer.parseInt(s.toString());
  361.                         Numbers.add(counter,L);
  362.                         counter++;
  363.                         s = new StringBuilder();
  364.                     }
  365.                 }
  366.             }
  367.  
  368.         }
  369.         counter =0;
  370.         for(int i = 0;i<size;i++){
  371.             for(int j =0; j<size; j++){
  372.                 MagicSquare[i][j] = Numbers.get(counter);
  373.                 counter++;
  374.             }
  375.         }
  376.  
  377.         boolean check = MagicSquareChecker.checkIsMagic(MagicSquare);
  378.  
  379.         if(check){
  380.             String newfilepath = "";
  381.             System.out.println("The square is magic and the magic number is: " + MagicSquareChecker.getMagicNumber(MagicSquare)+".");
  382.             JFileChooser fileChooser = new JFileChooser();
  383.             fileChooser.setDialogTitle("Select a folder");
  384.             fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  385.             int userSelection = fileChooser.showSaveDialog(null);
  386.             if (userSelection == JFileChooser.APPROVE_OPTION) {
  387.                 File file = fileChooser.getSelectedFile();
  388.                 newfilepath = file.getAbsolutePath();
  389.                 System.out.println("The path of the selected file is: " + newfilepath);
  390.             }
  391.             System.out.println(newfilepath);
  392.             System.exit(1);
  393.             PrintWriter writer;
  394.             try {
  395.                 writer = new PrintWriter(newfilepath,"UTF-8");
  396.                 for(int i =0; i < MagicSquare.length; i++){
  397.                     writer.println(toString2(MagicSquare[i]));
  398.                 }
  399.                 writer.println("Magic square with magic number "+ MagicSquareChecker.getMagicNumber(MagicSquare));
  400.                 writer.close();
  401.             } catch (Exception e) {
  402.                 System.out.println("Problem: "+e);
  403.             }
  404.         }else{
  405.             System.out.println("The square isn't magic.");
  406.         }
  407.     }
  408.  
  409.     public static String[] fillArray(String filename) throws FileNotFoundException {
  410.         Scanner inputFile = new Scanner(new File(filename));
  411.         int count = 0;
  412.         while (inputFile.hasNextLine()) {
  413.             inputFile.nextLine();
  414.             count++;
  415.         }
  416.         inputFile.close();
  417.  
  418.         String[] array = new String[count];
  419.         inputFile = new Scanner(new File(filename));
  420.         for (int i = 0; i < count; i++) {
  421.             array[i] = inputFile.nextLine();
  422.         }
  423.         inputFile.close();
  424.         return array;
  425.     }
  426.     public static String toString2(int[] a) {
  427.         if (a == null)
  428.             return "null";
  429.         int iMax = a.length - 1;
  430.         if (iMax == -1)
  431.             return "[]";
  432.  
  433.         StringBuilder b = new StringBuilder();
  434.         b.append('[');
  435.         for (int i = 0; ; i++) {
  436.             b.append(a[i]);
  437.             if (i == iMax)
  438.                 return b.append(']').toString();
  439.             b.append("| ");
  440.         }
  441.     }
  442.     static String[] downloadANDwrite(String address) throws IOException {
  443.             int count = 0;
  444.             URL url = new URL(address);
  445.             Scanner in= new Scanner(
  446.                     new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
  447.             while (in.hasNextLine()) {
  448.                 in.nextLine();
  449.                 count++;
  450.             }
  451.             in.close();
  452.             String[] array = new String[count];
  453.             in= new Scanner(
  454.                     new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
  455.             for (int i = 0; i < count; i++) {
  456.                 array[i] = in.nextLine();
  457.             }
  458.             in.close();
  459.  
  460.         return array;
  461.     }
  462.  
  463. }
  464. */
  465.  
  466. */
  467. package csd.uoc.gr.A14;
  468. import csd.uoc.gr.A12.MagicSquareChecker;
  469.  
  470. import java.util.Random;
  471. import java.util.Scanner;
  472. import java.util.stream.IntStream;
  473.  
  474. class BacktrackingAlgorithm {
  475.  
  476.     private static final int BOARD_SIZE = 9;
  477.     private static final int SUBSECTION_SIZE = 3;
  478.     private static final int BOARD_START_INDEX = 0;
  479.  
  480.     private static final int NO_VALUE = 0;
  481.     private static final int MIN_VALUE = 1;
  482.     private static final int MAX_VALUE = 9;
  483.  
  484.     private static int[][] board = {
  485.             {8, 0, 0, 0, 0, 0, 0, 0, 0},
  486.             {0, 0, 3, 6, 0, 0, 0, 0, 0},
  487.             {0, 7, 0, 0, 9, 0, 2, 0, 0},
  488.             {0, 5, 0, 0, 0, 7, 0, 0, 0},
  489.             {0, 0, 0, 0, 4, 5, 7, 0, 0},
  490.             {0, 0, 0, 1, 0, 0, 0, 3, 0},
  491.             {0, 0, 1, 0, 0, 0, 0, 6, 8},
  492.             {0, 0, 8, 5, 0, 0, 0, 1, 0},
  493.             {0, 9, 0, 0, 0, 0, 4, 0, 0}
  494.     };
  495.  
  496.     public static void main(String[] args) {
  497.         System.out.println("How many boards you want to create?");
  498.         Scanner in = new Scanner(System.in);
  499.         int n = in.nextInt();
  500.         gameMake(n);
  501.     }
  502.  
  503.     private void printBoard() {
  504.         for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
  505.             for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
  506.                 System.out.print(board[row][column] + " ");
  507.             }
  508.             System.out.println();
  509.         }
  510.     }
  511.  
  512.     private static boolean solve(int[][] board) {
  513.         for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
  514.             for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
  515.                 if (board[row][column] == NO_VALUE) {
  516.                     for (int k = MIN_VALUE; k <= MAX_VALUE; k++) {
  517.                         board[row][column] = k;
  518.                         if (isValid(board, row, column) && solve(board)) {
  519.                             return true;
  520.                         }
  521.                         board[row][column] = NO_VALUE;
  522.                     }
  523.                     return false;
  524.                 }
  525.             }
  526.         }
  527.         return true;
  528.     }
  529.  
  530.     private static boolean isValid(int[][] board, int row, int column) {
  531.         return rowConstraint(board, row) &&
  532.                 columnConstraint(board, column) &&
  533.                 subsectionConstraint(board, row, column);
  534.     }
  535.  
  536.     private static boolean subsectionConstraint(int[][] board, int row, int column) {
  537.         boolean[] constraint = new boolean[BOARD_SIZE];
  538.         int subsectionRowStart = (row / SUBSECTION_SIZE) * SUBSECTION_SIZE;
  539.         int subsectionRowEnd = subsectionRowStart + SUBSECTION_SIZE;
  540.  
  541.         int subsectionColumnStart = (column / SUBSECTION_SIZE) * SUBSECTION_SIZE;
  542.         int subsectionColumnEnd = subsectionColumnStart + SUBSECTION_SIZE;
  543.  
  544.         for (int r = subsectionRowStart; r < subsectionRowEnd; r++) {
  545.             for (int c = subsectionColumnStart; c < subsectionColumnEnd; c++) {
  546.                 if (!checkConstraint(board, r, constraint, c)) return false;
  547.             }
  548.         }
  549.         return true;
  550.     }
  551.  
  552.     private static boolean columnConstraint(int[][] board, int column) {
  553.         boolean[] constraint = new boolean[BOARD_SIZE];
  554.         return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
  555.                 .allMatch(row -> checkConstraint(board, row, constraint, column));
  556.     }
  557.  
  558.     private static boolean rowConstraint(int[][] board, int row) {
  559.         boolean[] constraint = new boolean[BOARD_SIZE];
  560.         return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
  561.                 .allMatch(column -> checkConstraint(board, row, constraint, column));
  562.     }
  563.  
  564.     private static boolean checkConstraint(int[][] board, int row, boolean[] constraint, int column) {
  565.         if (board[row][column] != NO_VALUE) {
  566.             if (!constraint[board[row][column] - 1]) {
  567.                 constraint[board[row][column] - 1] = true;
  568.             } else {
  569.                 return false;
  570.             }
  571.         }
  572.         return true;
  573.     }
  574.  
  575.     private static boolean isValidBoard(int[][] brd){
  576.         for(int i = 0; i < BOARD_SIZE; i++){
  577.             for(int j = 0; j < BOARD_SIZE; j++){
  578.                 int same = brd[i][j];
  579.                 for(int k =0; k < BOARD_SIZE; k++){
  580.                     for(int m =0; m < BOARD_SIZE; m++){
  581.                         if(k == i && m == j ){
  582.                             if(m == BOARD_SIZE-1 && k != BOARD_SIZE-1){
  583.                                 k++;
  584.                                 m=0;
  585.                             }else if(m != BOARD_SIZE-1 && k == BOARD_SIZE-1){
  586.                                 m++;
  587.                             }else{
  588.                                 break;
  589.                             }
  590.                         }
  591.                         if((same == brd[k][m] && same !=0) || same < 0 || same > 9){
  592.                             return true;
  593.                         }
  594.                     }
  595.                 }
  596.             }
  597.         }
  598.         int sub_counter;
  599.  
  600.         for(sub_counter = BOARD_SIZE/SUBSECTION_SIZE;sub_counter >0; sub_counter--) {
  601.             int i = (SUBSECTION_SIZE * sub_counter)-1;
  602.             int j = (SUBSECTION_SIZE * sub_counter)-1;
  603.             int k = (SUBSECTION_SIZE * sub_counter)-1;
  604.             int m = (SUBSECTION_SIZE * sub_counter)-1;
  605.             while (i >= SUBSECTION_SIZE * (sub_counter-1)) {
  606.                 while (j >= SUBSECTION_SIZE * (sub_counter-1)) {
  607.                     int same = brd[i][j];
  608.                     while (k >= SUBSECTION_SIZE * (sub_counter-1)) {
  609.                         while (m >= SUBSECTION_SIZE * (sub_counter-1)) {
  610.                             if(i==k && j==m){
  611.                                 if(k != SUBSECTION_SIZE * (sub_counter-1) && m == SUBSECTION_SIZE * (sub_counter-1)){
  612.                                     k--;
  613.                                     m = (SUBSECTION_SIZE * sub_counter)-1;
  614.                                 }else if(k == SUBSECTION_SIZE * (sub_counter-1) && m != SUBSECTION_SIZE * (sub_counter-1)){
  615.                                     m--;
  616.                                 }
  617.                             }
  618.                             if((same == brd[k][m] && same !=0) || same < 0 || same > 9){
  619.                                 return true;
  620.                             }
  621.                             m--;
  622.                         }
  623.                         k--;
  624.                     }
  625.                     j--;
  626.                 }
  627.                 i--;
  628.             }
  629.         }
  630.         return false;
  631.     }
  632.     private static boolean isSolvableBoard(int[][] brd){
  633.         return solve(brd) && isValidBoard(brd);
  634.     }
  635.  
  636.     private static int[][] makeBoard(int x){
  637.         if(x >= 81){
  638.             System.out.println("At least one cell must have a value from 1 to 9.");
  639.             System.exit(0);
  640.         }
  641.  
  642.         int[][] newBoard = new int[9][9];
  643.         Random randomGenerator = new Random();
  644.  
  645.         for(int filled = 0; filled < 81 - x; filled++){
  646.             int randomi = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
  647.             int randomj = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
  648.             while(newBoard[randomi-1][randomj-1] != 0){
  649.                 randomi = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
  650.                 randomj = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
  651.             }
  652.             int randomvalue = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
  653.             newBoard[randomi-1][randomj-1] = randomvalue;
  654.         }
  655.  
  656.         return newBoard;
  657.     }
  658.     private static void gameMake(int n){
  659.         int wrong = 0;
  660.         int right = 0;
  661.         int unsolvable = 0;
  662.         int[][] newBoard;
  663.  
  664.         System.out.println("How many empty cells per board?");
  665.         Scanner in = new Scanner(System.in);
  666.         int x = in.nextInt();
  667.         long start = System.currentTimeMillis();
  668.         int i =1;
  669.         while(n >= i){
  670.             newBoard = makeBoard(x);
  671.             int[][] newBoard2 = newBoard;
  672.  
  673.             for(int k =0; k < newBoard2.length; k++){
  674.                 for(int m = 0; m < newBoard2.length; m++){
  675.                     System.out.print(newBoard2[k][m]+" ");
  676.                 }
  677.                 System.out.println();
  678.             }
  679.             CHECK:
  680.             if(isValidBoard(newBoard)){
  681.                 if(!isSolvableBoard(newBoard)){
  682.                     System.out.println("Unsolvable!");
  683.                     unsolvable++;
  684.                     i--;
  685.                     break CHECK;
  686.                 }
  687.                 System.out.println("Board #"+i);
  688.                 for(int k =0; k < newBoard2.length; k++){
  689.                     for(int m = 0; m < newBoard2.length; m++){
  690.                         System.out.print(newBoard2[k][m]+" ");
  691.                     }
  692.                     System.out.println();
  693.                 }
  694.                 System.out.println("The solution of Board #"+i);
  695.                 for(int k =0; k < newBoard.length; k++){
  696.                     for(int m = 0; m < newBoard.length; m++){
  697.                         System.out.print(newBoard[k][m]+" ");
  698.                     }
  699.                     System.out.println();
  700.                 }
  701.                 System.out.println();
  702.                 right++;
  703.             }else if(!isValidBoard(newBoard)){
  704.  
  705.                 System.out.println("Wrong!");
  706.                 wrong++;
  707.                 i--;
  708.             }
  709.             i++;
  710.         }
  711.         long end = System.currentTimeMillis();
  712.         float sec = (end - start) / 1000F; System.out.println(sec + " seconds");
  713.         System.out.println("Empty cells per board : "+ x);
  714.         System.out.println("Valid boards created : "+ right);
  715.         System.out.println("Invalid boards created : "+ wrong);
  716.         System.out.println("Invalid boards created : "+ unsolvable);
  717.         System.out.println("Elapsed time in seconds : "+ sec);
  718.  
  719.     }
  720. }
  721. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement