Vanilla_Fury

laba_6_2_java

May 28th, 2021
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.51 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. public class Main {
  9.  
  10.     private static Scanner scConsole;
  11.     public static MyBoard board = new MyBoard();
  12.     public static MyKnight knight = new MyKnight();
  13.  
  14.     private static short choose(String sQuestion, String[] Options) { // не больше 9-ти вариантов ответа
  15.         short nChoice;
  16.         String answer;
  17.         StringBuilder optionsDigits = new StringBuilder();
  18.  
  19.         System.out.println(sQuestion + "\n\tВарианты: ");
  20.         for (int i = 0; i < Options.length; i++) {
  21.             System.out.println("\t" + (i + 1) + " - " + Options[i]);
  22.             optionsDigits.append(i + 1);
  23.         }
  24.  
  25.         answer = getAnythingFromConsole("", "^[" + optionsDigits + "]$", "Нужно ввести " +
  26.                 "цифру (одну из предложенных).");
  27.         nChoice = (short) (Short.parseShort(answer) - 1);
  28.  
  29.         return nChoice;
  30.     }
  31.  
  32.     private static String getAnythingFromConsole(String Question, String regEx, String clarification) {
  33.         String sInput;
  34.         String sOutput;
  35.         final String stringIfNothingIsFoundInString = "";
  36.         boolean nothingIsFoundIsNotAllowed = findRegEx(regEx, "\\^\\$", "")[0].equals("");
  37.  
  38.         if (!Question.equals("")) {
  39.             System.out.println(Question);
  40.         }
  41.  
  42.         do {
  43.             sInput = scConsole.nextLine().trim();
  44.             sOutput = findRegEx(sInput, regEx, stringIfNothingIsFoundInString)[0];
  45.             if (sOutput.equals(stringIfNothingIsFoundInString) && nothingIsFoundIsNotAllowed) {
  46.                 System.err.println("Данные введены неверно. " + clarification + "\nПовторите попытку:");
  47.             }
  48.         } while (sOutput.equals(stringIfNothingIsFoundInString) && nothingIsFoundIsNotAllowed);
  49.  
  50.         System.out.println();
  51.         return sOutput;
  52.     }
  53.  
  54.     private static String[] findRegEx(String sInput, String regEx, String outputIfNothingFound) {
  55.         ArrayList<String> arrStringOutput = new ArrayList<>();
  56.         Pattern pattern = Pattern.compile(regEx);
  57.         Matcher matcher = pattern.matcher(sInput);
  58.  
  59.         if (matcher.find()) {
  60.             do {
  61.                 arrStringOutput.add(matcher.group());
  62.             } while (matcher.find());
  63.         } else
  64.             arrStringOutput.add(outputIfNothingFound);
  65.  
  66.         return arrStringOutput.toArray(new String[0]);
  67.     }
  68.  
  69.     private static void showHelp() {
  70.         short answerOnQuestion;
  71.         do {
  72.             answerOnQuestion = choose("Справка", new String[]{"задание", "автор", "назад"});
  73.             switch (answerOnQuestion) {
  74.                 case 0 -> System.out.println("Задание:\n\tОбойти шахматную доску ходом коня.\n");
  75.                 case 1 -> System.out.println("Автор:\n\tПанев Александр, гр. 051007\n\tМинск, 2021\n");
  76.             }
  77.         } while (answerOnQuestion != 2);
  78.     }
  79.  
  80.     private static void prepareBeforeWalk() {
  81.         board.reset();
  82.         knight.reset();
  83.         String coordinates = getAnythingFromConsole("Введите начальные координаты коня. Например, \"b6\"", "^\\s*[a-h][1-8]\\s*$",
  84.                 "Надо вводить букву от a до h и цифру от 1 до 8. Например, \"a1\", \"с4\" или \"h8\"");
  85.         byte posX = (byte) ((int) findRegEx(coordinates, "[a-h]", "a")[0].charAt(0) - 97);
  86.         byte posY = (byte) (8 - Integer.parseInt(findRegEx(coordinates, "[1-8]", "1")[0]));
  87.         knight.moveKnightToCell(posX, posY);
  88.         knight.setKnightIsWalking(true);
  89.     }
  90.  
  91.     public static void doAWalk() {
  92.         board.print();
  93.         for (int i = 0; i < 63; i++) {
  94.             knight.doOneMove();
  95.             board.print();
  96.             try {
  97.                 Thread.sleep(300);
  98.             } catch (InterruptedException e) {
  99.                 //
  100.             }
  101.  
  102.         }
  103.     }
  104.  
  105.     private static void doPreparingAndWalk() {
  106.         prepareBeforeWalk();
  107.         doAWalk();
  108.     }
  109.  
  110.     private static void mainMenu() {
  111.         short answerOnQuestion;
  112.         do {
  113.             answerOnQuestion = choose("Главное меню",
  114.                     new String[]{"обойти шахматную доску ходом коня", "показать справку", "закрыть программу"});
  115.             switch (answerOnQuestion) {
  116.                 case 0 -> doPreparingAndWalk();
  117.                 case 1 -> showHelp();
  118.             }
  119.         } while (answerOnQuestion != 2);
  120.     }
  121.  
  122.     public static void main(String[] args) {
  123.         scConsole = new Scanner(System.in);
  124.  
  125.         mainMenu();
  126.         System.out.println("До свидания");
  127.  
  128.         scConsole.close();
  129.     }
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. package com.company;
  140.  
  141. public class MyKnight {
  142.     byte posX;
  143.     byte posY;
  144.     byte quantityWalkedCells;
  145.     boolean knightIsWalking;
  146.  
  147.     public MyKnight() {
  148.         reset();
  149.     }
  150.  
  151.     public void reset() {
  152.         quantityWalkedCells = 0;
  153.         knightIsWalking = false;
  154.     }
  155.  
  156.     public void moveKnightToCell(final byte posX, final byte posY) {
  157.         if (knightIsWalking && quantityWalkedCells == 0) {
  158.             Main.board.setIsWalked(this.posX, this.posY, true);
  159.             quantityWalkedCells++;
  160.         }
  161.         this.posX = posX;
  162.         this.posY = posY;
  163.  
  164.         if (knightIsWalking) {
  165.             Main.board.setIsWalked(posX, posY, true);
  166.             quantityWalkedCells++;
  167.         }
  168.     }
  169.  
  170.     public byte getPosX() {
  171.         return posX;
  172.     }
  173.  
  174.     public byte getPosY() {
  175.         return posY;
  176.     }
  177.  
  178.     public void setKnightIsWalking(final boolean knightIsWalking) {
  179.         this.knightIsWalking = knightIsWalking;
  180.     }
  181.  
  182.     public void doOneMove() {
  183.         short[][] arrMoves = getMovesFromCell(posX, posY);
  184.  
  185.         int quantityOfAvailMoves;
  186.         int minAvailMoves = 1000;
  187.         int indexOfMin = 0;
  188.         for (int i = 0; i < arrMoves.length; i++) {
  189.             quantityOfAvailMoves = getMovesFromCell(posX + arrMoves[i][0], posY + arrMoves[i][1]).length;
  190.             if (i == 0) {
  191.                 minAvailMoves = quantityOfAvailMoves;
  192.                 indexOfMin = 0;
  193.             } else {
  194.                 if (quantityOfAvailMoves < minAvailMoves) {
  195.                     minAvailMoves = quantityOfAvailMoves;
  196.                     indexOfMin = i;
  197.                 }
  198.             }
  199.         }
  200.  
  201.         moveKnightToCell((byte) (posX + arrMoves[indexOfMin][0]), (byte) (posY + arrMoves[indexOfMin][1]));
  202.     }
  203.  
  204.     private short[][] getMovesFromCell(final int FromX, final int FromY) {
  205.         final short[][] allowedMoves = new short[][]{{-2, 1}, {2, -1}, {-2, -1}, {-1, -2}, {2, 1}, {1, 2}, {-1, 2}, {1, -2}};
  206.         short[][] arrMovesBig = new short[8][2];
  207.         byte quantOfMoves = 0;
  208.         for (int i = 0; i < 8; i++) {
  209.             if (FromX + allowedMoves[i][0] < 8 && FromX + allowedMoves[i][0] > -1 && FromY + allowedMoves[i][1] < 8 &&
  210.                     FromY + allowedMoves[i][1] > -1 &&
  211.                     !Main.board.getIsWalked(FromX + allowedMoves[i][0], FromY + allowedMoves[i][1])) {
  212.                 arrMovesBig[quantOfMoves] = allowedMoves[i];
  213.                 quantOfMoves++;
  214.             }
  215.         }
  216.  
  217.         short[][] arrMoves = new short[quantOfMoves][2];
  218.         System.arraycopy(arrMovesBig, 0, arrMoves, 0, quantOfMoves);
  219.  
  220.         return arrMoves;
  221.     }
  222. }
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234. package com.company;
  235.  
  236. public class MyBoard {
  237.     private MyCell[][] arrOfCells;
  238.  
  239.     public MyBoard() {
  240.         reset();
  241.     }
  242.  
  243.     public void reset() {
  244.         arrOfCells = new MyCell[8][8];
  245.  
  246.         for (byte i = 0; i < 8; i++) {
  247.             for (byte j = 0; j < 8; j++) {
  248.                 arrOfCells[i][j] = new MyCell(i, j);
  249.             }
  250.         }
  251.     }
  252.  
  253.     public void setIsWalked(final byte posX, final byte posY, final boolean walked) {
  254.         arrOfCells[posX][posY].setWalked(walked);
  255.     }
  256.  
  257.     public boolean getIsWalked(final int posX, final int posY) {
  258.         return arrOfCells[posX][posY].isWalked();
  259.     }
  260.  
  261.     public void print() {
  262.         String oneStr;
  263.         for (int i = 0; i < 8; i++) {
  264.             oneStr = "|";
  265.             for (int j = 0; j < 8; j++) {
  266.                 if (Main.knight.getPosX() == j && Main.knight.getPosY() == i) {
  267.                     oneStr = oneStr + " K|";
  268.                 } else {
  269.                     if (getIsWalked(j, i)) {
  270.                         oneStr = oneStr + " .|";
  271.                     } else {
  272.                         oneStr = oneStr + "  |";
  273.                     }
  274.                 }
  275.             }
  276.             System.out.print(oneStr + "\n");
  277.         }
  278.         System.out.println("\n");
  279.     }
  280. }
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291. package com.company;
  292.  
  293. public class MyCell {
  294.     private boolean isWalked;
  295.     private byte posX;
  296.     private byte posY;
  297.  
  298.     public MyCell(final byte posX, final byte posY) {
  299.         isWalked = false;
  300.         this.posX = posX;
  301.         this.posY = posY;
  302.     }
  303.  
  304.     public void setWalked(final boolean walked) {
  305.         isWalked = walked;
  306.     }
  307.  
  308.     public boolean isWalked() {
  309.         return isWalked;
  310.     }
  311.  
  312.     public byte getPosX() {
  313.         return posX;
  314.     }
  315.  
  316.     public byte getPosY() {
  317.         return posY;
  318.     }
  319.  
  320.     public void setPos(final byte posX, final byte posY) {
  321.         this.posX = posX;
  322.         this.posY = posY;
  323.     }
  324. }
  325.  
  326.  
Advertisement
Add Comment
Please, Sign In to add comment