Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Main {
- private static Scanner scConsole;
- public static MyBoard board = new MyBoard();
- public static MyKnight knight = new MyKnight();
- private static short choose(String sQuestion, String[] Options) { // не больше 9-ти вариантов ответа
- short nChoice;
- String answer;
- StringBuilder optionsDigits = new StringBuilder();
- System.out.println(sQuestion + "\n\tВарианты: ");
- for (int i = 0; i < Options.length; i++) {
- System.out.println("\t" + (i + 1) + " - " + Options[i]);
- optionsDigits.append(i + 1);
- }
- answer = getAnythingFromConsole("", "^[" + optionsDigits + "]$", "Нужно ввести " +
- "цифру (одну из предложенных).");
- nChoice = (short) (Short.parseShort(answer) - 1);
- return nChoice;
- }
- private static String getAnythingFromConsole(String Question, String regEx, String clarification) {
- String sInput;
- String sOutput;
- final String stringIfNothingIsFoundInString = "";
- boolean nothingIsFoundIsNotAllowed = findRegEx(regEx, "\\^\\$", "")[0].equals("");
- if (!Question.equals("")) {
- System.out.println(Question);
- }
- do {
- sInput = scConsole.nextLine().trim();
- sOutput = findRegEx(sInput, regEx, stringIfNothingIsFoundInString)[0];
- if (sOutput.equals(stringIfNothingIsFoundInString) && nothingIsFoundIsNotAllowed) {
- System.err.println("Данные введены неверно. " + clarification + "\nПовторите попытку:");
- }
- } while (sOutput.equals(stringIfNothingIsFoundInString) && nothingIsFoundIsNotAllowed);
- System.out.println();
- return sOutput;
- }
- private static String[] findRegEx(String sInput, String regEx, String outputIfNothingFound) {
- ArrayList<String> arrStringOutput = new ArrayList<>();
- Pattern pattern = Pattern.compile(regEx);
- Matcher matcher = pattern.matcher(sInput);
- if (matcher.find()) {
- do {
- arrStringOutput.add(matcher.group());
- } while (matcher.find());
- } else
- arrStringOutput.add(outputIfNothingFound);
- return arrStringOutput.toArray(new String[0]);
- }
- private static void showHelp() {
- short answerOnQuestion;
- do {
- answerOnQuestion = choose("Справка", new String[]{"задание", "автор", "назад"});
- switch (answerOnQuestion) {
- case 0 -> System.out.println("Задание:\n\tОбойти шахматную доску ходом коня.\n");
- case 1 -> System.out.println("Автор:\n\tПанев Александр, гр. 051007\n\tМинск, 2021\n");
- }
- } while (answerOnQuestion != 2);
- }
- private static void prepareBeforeWalk() {
- board.reset();
- knight.reset();
- String coordinates = getAnythingFromConsole("Введите начальные координаты коня. Например, \"b6\"", "^\\s*[a-h][1-8]\\s*$",
- "Надо вводить букву от a до h и цифру от 1 до 8. Например, \"a1\", \"с4\" или \"h8\"");
- byte posX = (byte) ((int) findRegEx(coordinates, "[a-h]", "a")[0].charAt(0) - 97);
- byte posY = (byte) (8 - Integer.parseInt(findRegEx(coordinates, "[1-8]", "1")[0]));
- knight.moveKnightToCell(posX, posY);
- knight.setKnightIsWalking(true);
- }
- public static void doAWalk() {
- board.print();
- for (int i = 0; i < 63; i++) {
- knight.doOneMove();
- board.print();
- try {
- Thread.sleep(300);
- } catch (InterruptedException e) {
- //
- }
- }
- }
- private static void doPreparingAndWalk() {
- prepareBeforeWalk();
- doAWalk();
- }
- private static void mainMenu() {
- short answerOnQuestion;
- do {
- answerOnQuestion = choose("Главное меню",
- new String[]{"обойти шахматную доску ходом коня", "показать справку", "закрыть программу"});
- switch (answerOnQuestion) {
- case 0 -> doPreparingAndWalk();
- case 1 -> showHelp();
- }
- } while (answerOnQuestion != 2);
- }
- public static void main(String[] args) {
- scConsole = new Scanner(System.in);
- mainMenu();
- System.out.println("До свидания");
- scConsole.close();
- }
- }
- package com.company;
- public class MyKnight {
- byte posX;
- byte posY;
- byte quantityWalkedCells;
- boolean knightIsWalking;
- public MyKnight() {
- reset();
- }
- public void reset() {
- quantityWalkedCells = 0;
- knightIsWalking = false;
- }
- public void moveKnightToCell(final byte posX, final byte posY) {
- if (knightIsWalking && quantityWalkedCells == 0) {
- Main.board.setIsWalked(this.posX, this.posY, true);
- quantityWalkedCells++;
- }
- this.posX = posX;
- this.posY = posY;
- if (knightIsWalking) {
- Main.board.setIsWalked(posX, posY, true);
- quantityWalkedCells++;
- }
- }
- public byte getPosX() {
- return posX;
- }
- public byte getPosY() {
- return posY;
- }
- public void setKnightIsWalking(final boolean knightIsWalking) {
- this.knightIsWalking = knightIsWalking;
- }
- public void doOneMove() {
- short[][] arrMoves = getMovesFromCell(posX, posY);
- int quantityOfAvailMoves;
- int minAvailMoves = 1000;
- int indexOfMin = 0;
- for (int i = 0; i < arrMoves.length; i++) {
- quantityOfAvailMoves = getMovesFromCell(posX + arrMoves[i][0], posY + arrMoves[i][1]).length;
- if (i == 0) {
- minAvailMoves = quantityOfAvailMoves;
- indexOfMin = 0;
- } else {
- if (quantityOfAvailMoves < minAvailMoves) {
- minAvailMoves = quantityOfAvailMoves;
- indexOfMin = i;
- }
- }
- }
- moveKnightToCell((byte) (posX + arrMoves[indexOfMin][0]), (byte) (posY + arrMoves[indexOfMin][1]));
- }
- private short[][] getMovesFromCell(final int FromX, final int FromY) {
- final short[][] allowedMoves = new short[][]{{-2, 1}, {2, -1}, {-2, -1}, {-1, -2}, {2, 1}, {1, 2}, {-1, 2}, {1, -2}};
- short[][] arrMovesBig = new short[8][2];
- byte quantOfMoves = 0;
- for (int i = 0; i < 8; i++) {
- if (FromX + allowedMoves[i][0] < 8 && FromX + allowedMoves[i][0] > -1 && FromY + allowedMoves[i][1] < 8 &&
- FromY + allowedMoves[i][1] > -1 &&
- !Main.board.getIsWalked(FromX + allowedMoves[i][0], FromY + allowedMoves[i][1])) {
- arrMovesBig[quantOfMoves] = allowedMoves[i];
- quantOfMoves++;
- }
- }
- short[][] arrMoves = new short[quantOfMoves][2];
- System.arraycopy(arrMovesBig, 0, arrMoves, 0, quantOfMoves);
- return arrMoves;
- }
- }
- package com.company;
- public class MyBoard {
- private MyCell[][] arrOfCells;
- public MyBoard() {
- reset();
- }
- public void reset() {
- arrOfCells = new MyCell[8][8];
- for (byte i = 0; i < 8; i++) {
- for (byte j = 0; j < 8; j++) {
- arrOfCells[i][j] = new MyCell(i, j);
- }
- }
- }
- public void setIsWalked(final byte posX, final byte posY, final boolean walked) {
- arrOfCells[posX][posY].setWalked(walked);
- }
- public boolean getIsWalked(final int posX, final int posY) {
- return arrOfCells[posX][posY].isWalked();
- }
- public void print() {
- String oneStr;
- for (int i = 0; i < 8; i++) {
- oneStr = "|";
- for (int j = 0; j < 8; j++) {
- if (Main.knight.getPosX() == j && Main.knight.getPosY() == i) {
- oneStr = oneStr + " K|";
- } else {
- if (getIsWalked(j, i)) {
- oneStr = oneStr + " .|";
- } else {
- oneStr = oneStr + " |";
- }
- }
- }
- System.out.print(oneStr + "\n");
- }
- System.out.println("\n");
- }
- }
- package com.company;
- public class MyCell {
- private boolean isWalked;
- private byte posX;
- private byte posY;
- public MyCell(final byte posX, final byte posY) {
- isWalked = false;
- this.posX = posX;
- this.posY = posY;
- }
- public void setWalked(final boolean walked) {
- isWalked = walked;
- }
- public boolean isWalked() {
- return isWalked;
- }
- public byte getPosX() {
- return posX;
- }
- public byte getPosY() {
- return posY;
- }
- public void setPos(final byte posX, final byte posY) {
- this.posX = posX;
- this.posY = posY;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment