Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class AsciiAnimator {
  4.  
  5. public static void main(String[] args) {
  6. int ballRow = 5;
  7. int ballColumn = 5;
  8. int desiredInteriorColumns = 10;
  9. int desiredInteriorRows = 10;
  10. final int REQUIRED_BLOCK_BREAKS = 3;
  11. int numBreaks = 0;
  12. char[][] background = squareBackground(desiredInteriorRows + 2, desiredInteriorColumns + 2);
  13. while (true) {
  14. clearScreen();
  15. background[ballRow][ballColumn] = ' ';//Unpaint the ball from start location
  16. int newBallRow = ballRow;
  17. int newBallColumn = ballColumn;
  18.  
  19.  
  20. //decide where to move the ball;
  21. Random random = new Random();
  22. boolean changeColumn = random.nextBoolean();
  23. boolean changeRow = random.nextBoolean();
  24. if (changeRow) {
  25. if (random.nextBoolean()) {
  26. newBallRow--;//@todo logic here to break blocks
  27. } else {
  28. newBallRow++;
  29. }
  30. }
  31. if (changeColumn) {
  32. if (random.nextBoolean()) {
  33. newBallColumn--;//@todo logic here to break blocks
  34. } else {
  35. newBallColumn++;
  36. }
  37. }
  38. if (background[newBallRow][newBallColumn] == ' ') {
  39. ballRow = newBallRow;
  40. ballColumn = newBallColumn;//Move the ball
  41. } else {
  42. background[newBallRow][newBallColumn] = 'X';
  43. numBreaks++;
  44. }
  45. background[ballRow][ballColumn] = 'o'; //Paint ball in possibly new location
  46. printCharArray(background);
  47. if (numBreaks >= REQUIRED_BLOCK_BREAKS) {
  48. break;
  49. }
  50. try {
  51. Thread.sleep(100);
  52. } catch (InterruptedException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56.  
  57. }
  58.  
  59. public static char[][] squareBackground (int numRows, int numColumns) {
  60. char[][] response = new char[numRows][numColumns];
  61.  
  62. response [0][0] = '.';//Drawing the top of the box
  63. for (int columnIndex = 1; columnIndex < (numColumns - 1); columnIndex++) {
  64. response[0][columnIndex] = '_';
  65. }
  66. response [0][numColumns-1] = '.';//Finished drawing the top of the box
  67.  
  68. for (int rowIndex = 1; rowIndex < (numRows - 1); rowIndex++) { //Now drawing the "middle"
  69. response[rowIndex][0] = '|';
  70. for (int columnIndex = 1; columnIndex < (numColumns - 1); columnIndex++) {
  71. response [rowIndex][columnIndex] = ' ';
  72. }
  73. response[rowIndex][numRows-1] = '|';
  74. }//Finished drawing the middle
  75.  
  76. //Now drawing the bottom of the box
  77. response[numRows-1][0] = '|';
  78. for (int columnIndex = 1; columnIndex < (numColumns - 1); columnIndex++) {
  79. response[numRows-1][columnIndex] = '_';
  80. }
  81. response[numRows-1][numColumns-1] = '|';
  82. return response;
  83. }
  84.  
  85. public static void printCharArray (char [][] array) {
  86. for (char [] line : array) {
  87. System.out.println(line);
  88. }
  89. }
  90.  
  91. public static void clearScreen () {
  92. String systemInfo = System.getProperty("os.name");
  93.  
  94. if (systemInfo.contains("Windows")) {
  95. try {
  96. new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
  97. } catch (Exception exception) {
  98. }
  99. } else if (systemInfo.contains("Mac")) {
  100. System.out.print("\033[H\033[2J"); //This is safe in windows, it just doesn't clear the screen.
  101. } else {
  102. System.out.println("Unable to determine system info. Unable to clear screen.");
  103. }
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement