Skip to content

Instantly share code, notes, and snippets.

@SubOptimal
Created July 11, 2014 17:06
Show Gist options
  • Save SubOptimal/1d15386b7a5cd2794cf1 to your computer and use it in GitHub Desktop.
Save SubOptimal/1d15386b7a5cd2794cf1 to your computer and use it in GitHub Desktop.
text mode scroller like in the movie "The Matrix"
/**
*
* Today was on stackoverflow a question about how to convert a C++ source into
* Java source. The code was implementing a scoller like in the movie "The Matrix".
*
* As the question was not fulfilling the stackoverflow requirements for a valid
* questions, it has been deleted in the meantime by the moderators. Which is a
* little bit bad, as the code was nice.
*
* So I create this gist with my answer I would like to have posted to the
* questioner.
*
*
* stackoverflox question:
* http://stackoverflow.com/questions/24666716/can-anyone-here-debug-this-java-code-this-code-implements-the-matrix-movie-s
*
* based on source:
* http://www.snip2code.com/Snippet/62524/http---www-quora-com-What-is-the-best-C-
*
*/
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class Main {
static final int width = 158; // Width of terminal window - 1
static final int flipsPerLine = 5; // No. of columns changed per line
static final int millisecondsOfSleep = 50; // Delay between lines in millisecond
public static void main(String... args) {
// std::cout << set_color(GREEN);
// only if the terminal/console supports ANSI escape sequences
// System.out.print((char)27 + "[2J"); // blank screen
// System.out.print((char)27 + "[1;32m"); // foreground color green
//srand(time_t(NULL));
Random rand = new Random(System.nanoTime());
// bool switches[width] = { true };
boolean[] switches = new boolean[width];
for (int i = 0; i < switches.length; i++) {
switches[i] = true;
}
//const std::string garbage = "1234567890/*-+.,./;[]\\=_~`!@#$%^&*()";
String garbage = "1234567890/*-+.,./;[]\\=_~`!@#$%^&*()";
int glen = garbage.length();
while (true) { // Waiting for Ctrl-C
for (int i = 0; i != width; ++i) {
if (switches[i]) {
// std::cout << garbage[rand() % glen];
System.out.print(garbage.charAt(rand.nextInt(glen)));
} else {
// std::cout << ' ';
System.out.print(' ');
}
}
//std::cout << std::endl;
System.out.println("");
for (int i = 0; i != flipsPerLine; ++i) {
// int x = rand() % width;
int x = rand.nextInt(width);
switches[x] = !switches[x];
// Was switches[x] = (switches[x]) ? false : true; thanks to Adrian Petrescu
// Flipping switches
}
try {
TimeUnit.MILLISECONDS.sleep(millisecondsOfSleep);
} catch (InterruptedException ie) {
System.out.println("sleep interrupted");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment