Guest User

Untitled

a guest
Dec 9th, 2019
117
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2. import java.util.Random;
  3.  
  4. public class Main {
  5. static Random r;
  6. static final String ANSI_WHITE_BACKGROUND = "\u001B[43m";
  7. static final String ANSI_RED_BACKGROUND= "\u001B[41m";
  8. static final String ANSI_BLUE_BACKGROUND = "\u001B[44m";
  9. static final String ANSI_RESET = "\u001B[0m";
  10. public static void main(String[] args) {
  11. int [][] skin;
  12. skin = create(20,20);
  13. init(skin);
  14. putout(skin);
  15. r = new Random();
  16. for(int step = 1; step <= 11; step++) {
  17. for(int i = 0; i < skin.length; i++)
  18. for(int j = 0; j < skin[i].length; j++) {
  19. if(skin[i][j] == -4){
  20. skin[i][j] = 6;
  21. }
  22. else if(skin[i][j] != 6){
  23. int cell = r.nextInt(8);
  24. if(r.nextInt(2) == 1){
  25. switch(cell){
  26. case 0: infect(i-1, j-1, skin); break;
  27. case 1: infect(i-1, j, skin); break;
  28. case 2: infect(i-1, j+1, skin); break;
  29. case 3: infect(i, j-1, skin); break;
  30. case 4: infect(i, j+1, skin); break;
  31. case 5: infect(i+1, j-1, skin); break;
  32. case 6: infect(i+1, j, skin); break;
  33. case 7: infect(i+1, j+1, skin); break;
  34. }
  35.  
  36. }
  37. skin[i][j]--;
  38. }
  39. }
  40. putout(skin);
  41. }
  42. }
  43. static boolean infect(int i, int j, int[][] skin){
  44. if(i >= 0 && i < skin.length && j >= 0 && j < skin[i].length)
  45. if(skin[i][j] == 6) {
  46. skin[i][j] = 5;
  47. return true;
  48. }
  49. return false;
  50. }
  51. static void init(int [][] b) {
  52. for(int i = 0; i < b.length; i++)
  53. for(int j = 0; j < b[i].length; j++)
  54. b[i][j] = 6;
  55. b[7][4] = 5;
  56. b[6][15] = 5;
  57. b[17][10] = 5;
  58. }
  59. static int [][] create(int sz1, int sz2) {
  60. int b[][] = new int[sz1][];
  61. for (int i = 0; i < sz1; i++)
  62. b[i] = new int[sz2];
  63. return b;
  64. }
  65. static void putout(int [][]b) {
  66. int i, j;
  67. char toPrint;
  68. String color;
  69. for(i = 0; i < b.length; i++) {
  70. for(j = 0; j < b[i].length; j++) {
  71. switch(b[i][j]){
  72. case 6: toPrint = 'H'; color = ANSI_BLUE_BACKGROUND; break;
  73. case -1:;
  74. case -2:;
  75. case -3:;
  76. case -4: toPrint ='W'; color = ANSI_RESET; break;
  77. default: toPrint ='X'; color = ANSI_RED_BACKGROUND;
  78. }
  79. System.out.print(color + " " + toPrint + " ");
  80. }
  81. System.out.println(ANSI_RESET);
  82. }
  83. System.out.println();
  84. }
  85.  
  86. }
RAW Paste Data