Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.lang.reflect.Array;
  4. import java.util.Arrays;
  5.  
  6. class Main {
  7. private static String[][] res;
  8.  
  9. public static String[][] createEmptryGrid(Integer x, Integer y) {
  10. String[][] grid = new String[y][];
  11. for (int i = 0; i < y; i++) {
  12. String[] row = new String[x];
  13. Arrays.fill(row, "-");
  14. grid[i] = row;
  15. }
  16. return grid;
  17.  
  18. }
  19.  
  20. public static <T> T[] splice(final T[] array, int start, final int deleteCount, final T... items) {
  21. if (start < 0)
  22. start += array.length;
  23.  
  24. final T[] spliced = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length - deleteCount + items.length);
  25. if (start != 0)
  26. System.arraycopy(array, 0, spliced, 0, start);
  27.  
  28. if (items.length > 0)
  29. System.arraycopy(items, 0, spliced, start, items.length);
  30.  
  31. if (start + deleteCount != array.length)
  32. System.arraycopy(array, start + deleteCount, spliced, start + items.length, array.length - start - deleteCount);
  33.  
  34. return spliced;
  35. }
  36.  
  37. public static String[][] getMMLogo(Integer n) {
  38. String[][] grid = Main.createEmptryGrid(n * 5, n + 1);
  39. // Start from the bottom left corner, going up
  40. Integer x = 0;
  41. Integer y = n;
  42. Boolean goingUp = true;
  43. while (x <= (n * 5 - n)) {
  44. // Replace the n characters at index x with '*'
  45. String[] items = new String[n];
  46. Arrays.fill(items, "-");
  47. grid[y] = Main.splice(res[y], x, n, items);
  48.  
  49. // Change direction if needed (up or down)
  50. if ((y == n && !goingUp) || (y == 0 && goingUp)) {
  51. goingUp = !goingUp;
  52. }
  53.  
  54. x++;
  55. y += goingUp ? -1 : 1;
  56. }
  57.  
  58. return grid;
  59. }
  60.  
  61. public static void main(String[] args) {
  62. System.out.println("Hello world!");
  63. System.out.println(Main.getMMLogo(5));
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement