Advertisement
Alyks

Untitled

Mar 26th, 2020
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.70 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.lang.reflect.Field;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.HashSet;
  9. import java.util.Scanner;
  10.  
  11. class Cube {
  12.     String top;
  13.     String bottom;
  14.     String left;
  15.     String right;
  16.     String front;
  17.     String back;
  18.  
  19.     void rotate(int direction) {
  20.         String tmp;
  21.         switch (direction) {
  22.             case 1:
  23.                 tmp = left;
  24.                 left = front;
  25.                 front = right;
  26.                 right = back;
  27.                 back = tmp;
  28.                 break;
  29.             case 2:
  30.                 tmp = top;
  31.                 top = front;
  32.                 front = bottom;
  33.                 bottom = back;
  34.                 back = tmp;
  35.                 break;
  36.             case 3:
  37.                 tmp = top;
  38.                 top = right;
  39.                 right = bottom;
  40.                 bottom = left;
  41.                 left = tmp;
  42.                 break;
  43.         }
  44.     }
  45. }
  46.  
  47. public class Main {
  48.     static Cube createCube(String top, String bottom, String left, String right, String front, String back) {
  49.         Cube cube = new Cube();
  50.         cube.top = top;
  51.         cube.bottom = bottom;
  52.         cube.left = left;
  53.         cube.right = right;
  54.         cube.front = front;
  55.         cube.back = back;
  56.         return cube;
  57.     }
  58.  
  59.     static boolean allDistinct(ArrayList<String> arr) {
  60.         HashSet<String> arr2 = new HashSet<>(arr);
  61.         boolean result = arr2.size() == arr.size();
  62.         return result;
  63.     }
  64.  
  65.     static boolean validate(ArrayList<Cube> cubes) {
  66.         ArrayList<String> frontArr = new ArrayList<>();
  67.         ArrayList<String> topArr = new ArrayList<>();
  68.         ArrayList<String> backArr = new ArrayList<>();
  69.         ArrayList<String> bottomArr = new ArrayList<>();
  70.  
  71.         for (Cube cube : cubes) {
  72.             frontArr.add(cube.front);
  73.             topArr.add(cube.top);
  74.             backArr.add(cube.back);
  75.             bottomArr.add(cube.bottom);
  76.         }
  77.         boolean result = allDistinct(frontArr) && allDistinct(topArr) && allDistinct(backArr) && allDistinct(bottomArr);
  78.         return result;
  79.     }
  80.  
  81.     static void permute(ArrayList<Cube> cubes, int cubeNumber, int mistakes, int i, int j, int k) {
  82.         ArrayList<Cube> subcubes = new ArrayList<>();
  83.         for (int n = 0; n < cubeNumber; n++)
  84.             subcubes.add(cubes.get(n));
  85.         boolean stop = false;
  86.  
  87.         if (validate(subcubes)) {
  88.             if (cubeNumber < 4)
  89.                 permute(cubes, cubeNumber + 1, mistakes, 0, 0, 0);
  90.         } else {
  91.             if (i < 5)
  92.                 cubes.get(cubeNumber - 1).rotate(1);
  93.             else if (j < 5) {
  94.                 i = 0;
  95.                 cubes.get(cubeNumber - 1).rotate(2);
  96.                 j++;
  97.             } else if (k < 5) {
  98.                 i = 0;
  99.                 j = 0;
  100.                 cubes.get(cubeNumber - 1).rotate(3);
  101.                 k++;
  102.             } else {
  103.                 if (mistakes < 4) {
  104.                     mistakes++;
  105.                     cubes.get(0).rotate(2);
  106.                 } else {
  107.                     mistakes = 0;
  108.                     cubes.get(0).rotate(3);
  109.                 }
  110.                 permute(cubes, 2, mistakes, 0, 0, 0);
  111.                 stop = true;
  112.             }
  113.             if (!stop) {
  114.                 i++;
  115.                 permute(cubes, cubeNumber, mistakes, i, j, k);
  116.             }
  117.         }
  118.     }
  119.  
  120.     static boolean isUserWantToSave(Scanner scan) {
  121.         boolean notCorrect = true;
  122.         String choice = "";
  123.         System.out.println("Хотите сохранить результат в файл?");
  124.         while(notCorrect) {
  125.             System.out.println("Введите либо [д], либо [н]");
  126.             choice = scan.nextLine();
  127.             if(choice.equals("д") || choice.equals("н"))
  128.                 notCorrect = false;
  129.         }
  130.         return choice.equals("д");
  131.     }
  132.  
  133.     static void saveResult(String res) {
  134.         Scanner scan = new Scanner(System.in);
  135.         if(isUserWantToSave(scan)) {
  136.             System.out.println("Введите полное имя файла");
  137.             String filePath = scan.nextLine();
  138.             try(FileWriter fw = new FileWriter(filePath)) {
  139.                 fw.write(res);
  140.                 System.out.println("Результат успешно сохранен в файл.");
  141.             } catch (IOException err) {
  142.                 System.out.println("Произошла ошибка при попытке сохранить данные в файл.");
  143.             }
  144.         }
  145.     }
  146.  
  147.     static String getSolution(ArrayList<Cube> cubes) {
  148.         int cubeNum = 1;
  149.         String res = "";
  150.         for(Cube cube : cubes) {
  151.             res += "\nРешение для кубика №" + cubeNum + '\n';
  152.             Field[] fields = cube.getClass().getDeclaredFields();
  153.             try {
  154.                 for (Field field : fields) {
  155.                     field.setAccessible(true);
  156.                     String value = (String) field.get(cube);
  157.                     res += field.getName() + " = " + value + '\n';
  158.                 }
  159.                 cubeNum++;
  160.             } catch (IllegalAccessException err) {
  161.                 System.out.println("Возникла ошибка при выводе решения для кубика №" + cubeNum);
  162.             }
  163.         }
  164.         return res;
  165.     }
  166.  
  167.     public static void main(String[] args) {
  168.         System.out.println("Данная программа решает головоломку с кубиками, меняя стороны кубиков таким образом, чтобы они образовывали прямоугольную призму, каждая боковая грань которой раскрашена во все четыре цвета.");
  169.         Cube cube1 = createCube("голубой", "красный", "зеленый", "зеленый", "белый", "голубой");
  170.         Cube cube2 = createCube("зеленый", "красный", "красный", "красный", "белый", "голубой");
  171.         Cube cube3 = createCube("зеленый", "красный", "белый", "голубой", "белый", "зеленый");
  172.         Cube cube4 = createCube("белый", "зеленый", "красный", "голубой", "белый", "голубой");
  173.         ArrayList<Cube> cubes = new ArrayList<>(Arrays.asList(cube1, cube2, cube3, cube4));
  174.         int cubeNum = 2;
  175.         int mistakes = 1;
  176.         permute(cubes, cubeNum, mistakes, 0, 0, 0);
  177.         String solution = getSolution(cubes);
  178.         System.out.println(solution);
  179.         saveResult(solution);
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement