Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class AsciiAnimator {
- public static void main(String[] args) {
- int ballRow = 5;
- int ballColumn = 5;
- int desiredInteriorColumns = 10;
- int desiredInteriorRows = 10;
- final int REQUIRED_BLOCK_BREAKS = 3;
- int numBreaks = 0;
- char[][] background = squareBackground(desiredInteriorRows + 2, desiredInteriorColumns + 2);
- while (true) {
- clearScreen();
- background[ballRow][ballColumn] = ' ';//Unpaint the ball from start location
- int newBallRow = ballRow;
- int newBallColumn = ballColumn;
- //decide where to move the ball;
- Random random = new Random();
- boolean changeColumn = random.nextBoolean();
- boolean changeRow = random.nextBoolean();
- if (changeRow) {
- if (random.nextBoolean()) {
- newBallRow--;//@todo logic here to break blocks
- } else {
- newBallRow++;
- }
- }
- if (changeColumn) {
- if (random.nextBoolean()) {
- newBallColumn--;//@todo logic here to break blocks
- } else {
- newBallColumn++;
- }
- }
- if (background[newBallRow][newBallColumn] == ' ') {
- ballRow = newBallRow;
- ballColumn = newBallColumn;//Move the ball
- } else {
- background[newBallRow][newBallColumn] = 'X';
- numBreaks++;
- }
- background[ballRow][ballColumn] = 'o'; //Paint ball in possibly new location
- printCharArray(background);
- if (numBreaks >= REQUIRED_BLOCK_BREAKS) {
- break;
- }
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public static char[][] squareBackground (int numRows, int numColumns) {
- char[][] response = new char[numRows][numColumns];
- response [0][0] = '.';//Drawing the top of the box
- for (int columnIndex = 1; columnIndex < (numColumns - 1); columnIndex++) {
- response[0][columnIndex] = '_';
- }
- response [0][numColumns-1] = '.';//Finished drawing the top of the box
- for (int rowIndex = 1; rowIndex < (numRows - 1); rowIndex++) { //Now drawing the "middle"
- response[rowIndex][0] = '|';
- for (int columnIndex = 1; columnIndex < (numColumns - 1); columnIndex++) {
- response [rowIndex][columnIndex] = ' ';
- }
- response[rowIndex][numRows-1] = '|';
- }//Finished drawing the middle
- //Now drawing the bottom of the box
- response[numRows-1][0] = '|';
- for (int columnIndex = 1; columnIndex < (numColumns - 1); columnIndex++) {
- response[numRows-1][columnIndex] = '_';
- }
- response[numRows-1][numColumns-1] = '|';
- return response;
- }
- public static void printCharArray (char [][] array) {
- for (char [] line : array) {
- System.out.println(line);
- }
- }
- public static void clearScreen () {
- String systemInfo = System.getProperty("os.name");
- if (systemInfo.contains("Windows")) {
- try {
- new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
- } catch (Exception exception) {
- }
- } else if (systemInfo.contains("Mac")) {
- System.out.print("\033[H\033[2J"); //This is safe in windows, it just doesn't clear the screen.
- } else {
- System.out.println("Unable to determine system info. Unable to clear screen.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement