Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.07 KB | None | 0 0
  1. package com.javarush.task.task20.task2027;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /*
  7. Кроссворд
  8. */
  9. public class Solution {
  10.     public static void main(String[] args) {
  11.         int[][] crossword2 = new int[][]{
  12.                 {'r', 'm', 'a', 'r', 'r', 'e'},
  13.                 {'m', 'e', 'a', 'e', 'e', 'm'},
  14.                 {'s', 'a', 'm', 'e', 's', 'o'},
  15.                 {'m', 'o', 'p', 'o', 'o', 'h'},
  16.                 {'h', 'r', 'e', 'm', 'h', 'h'}
  17.         };
  18.         detectAllWords(crossword2, "home", "same", "homer", "r").forEach(System.out::println);
  19.         /*Ожидаемый результат
  20.         home - (5, 3) - (2, 0)
  21.         same - (1, 1) - (4, 1)*/
  22.     }
  23.  
  24.     public static List<Word> detectAllWords(int[][] crossword, String... words) {
  25.         List<Word> result = new ArrayList<>();
  26.         for (String s : words) {
  27.             char [] sDisassembled = s.toCharArray();
  28.             char firstChar = sDisassembled[0];
  29.             ArrayList<Integer[]> possibleStarts = new ArrayList<>();
  30.             for (int y = 0; y < crossword.length; y++) {
  31.                 for (int x = 0; x < crossword[y].length; x++) {
  32.                     if (crossword[y][x] == firstChar)
  33.                         possibleStarts.add(new Integer[]{y, x});
  34.                 }
  35.             }
  36.             //
  37.             for (Integer[] startCoords : possibleStarts) {
  38.                 if (s.length() > 1) {
  39.                     int[] neighbors = getNeighbors(crossword, startCoords[0], startCoords[1]);
  40.                     for (int i = 0; i < 8; i++) {
  41.                         if (neighbors[i] == sDisassembled[1]) {
  42.                             CheckingString checkingString = getWordByDirection(crossword, i, startCoords[0], startCoords[1], s.length() - 1);
  43.                             if (s.equals(checkingString.getValue())) {
  44.                                 Word currentWord = new Word(s);
  45.                                 currentWord.setStartPoint(startCoords[1], startCoords[0]);
  46.                                 currentWord.setEndPoint(checkingString.getxPos(), checkingString.getyPos());
  47.                                 result.add(currentWord);
  48.                             }
  49.                         }
  50.                     }
  51.                 } else {
  52.                     Word currentWord = new Word(s);
  53.                     currentWord.setStartPoint(startCoords[1], startCoords[0]);
  54.                     currentWord.setEndPoint(startCoords[1], startCoords[0]);
  55.                     result.add(currentWord);
  56.                 }
  57.             }
  58.         }
  59.         return result;
  60.     }
  61.  
  62.     public static CheckingString getWordByDirection(int[][] crossword, int direction, int yPos, int xPos, int charCount) {
  63.         String result = "" + (char) crossword[yPos][xPos];
  64.         switch (direction) {
  65.             case 0 : {
  66.                 if (charCount <= yPos) {
  67.                     while (charCount > 0) {
  68.                         yPos--;
  69.                         result += (char) crossword[yPos][xPos];
  70.                         charCount--;
  71.                     }
  72.                 }
  73.                 break;
  74.             }
  75.             case 1 : {
  76.                 if ((charCount <= yPos)  && (charCount < crossword[yPos].length - xPos)) {
  77.                     while (charCount > 0) {
  78.                         yPos--;
  79.                         xPos++;
  80.                         result += (char) crossword[yPos][xPos];
  81.                         charCount--;
  82.                     }
  83.                 }
  84.                 break;
  85.             }
  86.             case 2 : {
  87.                 if (charCount < crossword[yPos].length - xPos) {
  88.                     while (charCount > 0) {
  89.                         xPos++;
  90.                         result += (char) crossword[yPos][xPos];
  91.                         charCount--;
  92.                     }
  93.                 }
  94.                 break;
  95.             }
  96.             case 3 : {
  97.                 if ((charCount < crossword[yPos].length - xPos) && (charCount < crossword.length - yPos)) {
  98.                     while (charCount > 0) {
  99.                         yPos++;
  100.                         xPos++;
  101.                         result += (char) crossword[yPos][xPos];
  102.                         charCount--;
  103.                     }
  104.                 }
  105.                 break;
  106.             }
  107.             case 4 : {
  108.                 if (charCount < crossword.length - yPos) {
  109.                     while (charCount > 0) {
  110.                         yPos++;
  111.                         result += (char) crossword[yPos][xPos];
  112.                         charCount--;
  113.                     }
  114.                 }
  115.                 break;
  116.             }
  117.             case 5 : {
  118.                 if ((charCount < crossword.length - yPos) && (charCount < xPos)) {
  119.                     while (charCount > 0) {
  120.                         yPos++;
  121.                         xPos--;
  122.                         result += (char) crossword[yPos][xPos];
  123.                         charCount--;
  124.                     }
  125.                 }
  126.                 break;
  127.             }
  128.             case 6 : {
  129.                 if (charCount <= xPos) {
  130.                     while (charCount > 0) {
  131.                         xPos--;
  132.                         result += (char) crossword[yPos][xPos];
  133.                         charCount--;
  134.                     }
  135.                 }
  136.                 break;
  137.             }
  138.             case 7 : {
  139.                 if ((charCount <= yPos) && (charCount <= xPos)) {
  140.                     while (charCount > 0) {
  141.                         yPos--;
  142.                         xPos--;
  143.                         result += (char) crossword[yPos][xPos];
  144.                         charCount--;
  145.                     }
  146.                 }
  147.                 break;
  148.             }
  149.         }
  150.         return new CheckingString(yPos, xPos, result);
  151.     }
  152.  
  153.     public static int[] getNeighbors(int[][] crossword, int yPos, int xPos) {
  154.         int [] result = new int[] {'.', '.', '.', '.', '.', '.', '.', '.'};
  155.         // 0 = up, 1 = upRight, 2 = right, 3 = downRight, 4 = down, 5 = downLeft, 6 = left, 7 = upLeft
  156.         if (yPos > 0) {
  157.             result[0] = crossword[yPos - 1][xPos]; // up
  158.             if (xPos < crossword[yPos].length - 1)
  159.                 result[1] = crossword[yPos - 1][xPos + 1]; // upRight
  160.             if (xPos > 0) result[7] = crossword[yPos - 1][xPos - 1]; // upLeft
  161.         }
  162.         //
  163.         if (xPos < crossword[yPos].length - 1) {
  164.             result[2] = crossword[yPos][xPos + 1]; // right
  165.             if (yPos < crossword.length - 1)
  166.                 result[3] = crossword[yPos + 1][xPos + 1]; //downRight
  167.         }
  168.         //
  169.         if (yPos < crossword.length - 1) {
  170.             result[4] = crossword[yPos + 1][xPos]; // down
  171.             if (xPos > 0) result[5] = crossword[yPos + 1][xPos - 1]; // downLeft
  172.         }
  173.         //
  174.         if (xPos > 0) result[6] = crossword[yPos][xPos - 1]; // left
  175.         //
  176.         return result;
  177.     }
  178.  
  179.     public static class CheckingString {
  180.         private int xPos;
  181.         private int yPos;
  182.         private String value;
  183.  
  184.         public CheckingString(int yPosArg, int xPosArg, String valueArg) {
  185.             this.value = valueArg;
  186.             this.xPos = xPosArg;
  187.             this.yPos = yPosArg;
  188.         }
  189.  
  190.  
  191.         public int getxPos() {
  192.             return xPos;
  193.         }
  194.  
  195.  
  196.         public int getyPos() {
  197.             return yPos;
  198.         }
  199.  
  200.         public String getValue() {
  201.             return value;
  202.         }
  203.     }
  204.  
  205.     public static class Word {
  206.         private String text;
  207.         private int startX;
  208.         private int startY;
  209.         private int endX;
  210.         private int endY;
  211.  
  212.         public Word(String text) {
  213.             this.text = text;
  214.         }
  215.  
  216.         public void setStartPoint(int i, int j) {
  217.             startX = i;
  218.             startY = j;
  219.         }
  220.  
  221.         public void setEndPoint(int i, int j) {
  222.             endX = i;
  223.             endY = j;
  224.         }
  225.  
  226.         @Override
  227.         public String toString() {
  228.             return String.format("%s - (%d, %d) - (%d, %d)", text, startX, startY, endX, endY);
  229.         }
  230.     }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement