ASCII Array Art
import java.util.concurrent.ThreadLocalRandom;
class carLoop {
//The area between class definition and the 1st method is where we keep data for object in Java
String [][] cars; //2D Array: AP CSA Unit 8: 2D array of strings
//2D array is like a grid [x][y]
// or like a spreadsheet [row][column]
/**
* Constructor initializes a 2D array of cars
*/
public carLoop() {
//Storing Data in 2D arrays
cars = new String[][]{ //2D array above is just a name, "new" makes a container ("object")
//car 0
{
" ______",
" /|_||_\\`.__",
"( _ _ _\\",
"=`-(_)--(_)-' "
},
//car 1
{
" ____",
" __/ |_\\_",
"| _ _``-.",
"'-(_)---(_)--'"
},
//car 2
{
" _.-.___\\__",
"| _ _`-. ",
"'-(_)----(_)--`"
},
//car 3
{
" ____",
" ____//_]|________",
" (o _ | -| _ o| ",
" `(_)-------(_)--'"
},
//car 4
{
" _____________",
" ____//__][__][___|",
"(o _| -| _ o|",
" `-(_)--------(_)-'"
},
};
}
/**
* Loop and print cars in array
* ... repeat until you reach zero ...
*/
public void printPoem() {
//begin the poem
System.out.println();
System.out.println("Car race poem");
// cars (non-primitive) defined in constructor knows its length
int carCount = cars.length;
int i = carCount;
while (i>=1) //loops through 2D array length backwards
{
//this print statement shows current count of cars
// concatenation (+) of the loop variable and string to form a countdown message
System.out.println(i + " Cars left in the race");
for (int row = 0; row < i; row++) { //cycles through "cells" of 2d array
/*cycles through columns to print
each car part by part, will eventually print entire column*/
for (int col = 0; col < cars[row].length; col++) {
// prints specific part of the car from the column
System.out.print(cars[row][col] + "\n");
//this is new line between separate parts
// System.out.println();
}
//this new line gives separation between stanza of poem
System.out.println();
}
//countdown for poem, decrementing carCount variable by 1
int randomNum = ThreadLocalRandom.current().nextInt(1, carCount);
carCount -= 1;
i -= randomNum;
}
//out of all the loops, prints finishing messages
System.out.println("WINNER");
System.out.println("0000000000000000000000000000000000");
System.out.println(" THE END ");
}
/**
* A Java Driver/Test method that is the entry point for execution
*/
public static void main(String[] args) {
new carLoop().printPoem(); //a new car list and output in one step
}
}
carLoop.main(null);