Skip to content

Instantly share code, notes, and snippets.

@lwillmeth
Created January 11, 2014 19:07
Show Gist options
  • Save lwillmeth/8375321 to your computer and use it in GitHub Desktop.
Save lwillmeth/8375321 to your computer and use it in GitHub Desktop.
CS161_Lab1 ASCII Initials
// * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Name: Levi Willmeth Folder Name: willmeth_l
// CS161 Winter 2014 Assignment: Lab 1
// Class time: Mon/Wed/Fri 12:00pm - 1:20 / 1:50
// Program Name: initials.class
// Program Description:
// Prints my initials 'LJW' in big asci text.
// * * * * * * * * * * * * * * * * * * * * * * * * * * *
import java.util.*; // required for Collections.shuffle
public class initials{ // initial class must be same as filename
// revealString method slowly reveals each line
public static void revealString(String raw_str){
// Convert raw_str to list of individual characters
char[] char_list = raw_str.toCharArray();
// visible_str holds the mix of garbage and real letters
StringBuilder visible_str = new StringBuilder();
// Prepare random numbers for visible_str
Random garbage_number = new Random();
// Create a list of keys to use while replacing visible_str
List<Integer> keys = new ArrayList<Integer>();
for ( int k = 0; k < char_list.length; k++ ){
// each key is the index of a visible_str character
keys.add(k);
// fill visible_str with garbage 1s and 0s
visible_str.append(garbage_number.nextInt(2));
}
// Shuffle the keys, randomizing reveal order
Collections.shuffle(keys);
// Iterate over keys, swapping a visible_char for its
// real character using the index number in char_list.
for (int key : keys){
/* Couldnt find a better way to delay the script,
using Thread.sleep(time in ms); delays the entire thread
and is very inefficient. Also, it must be used in a
try / except catch because Java throws an
InterruptedException for the thread. */
try{
Thread.sleep(10);
} catch (Exception e){
// ignore errors
}
// Replace one character in visible_str each loop
visible_str.setCharAt(key,char_list[key]);
// Write over previous output each loop until finished.
System.out.print("\r"+visible_str);
}
// Used for debugging:
//System.out.println(visible_str);
//System.out.println(keys);
System.out.println();
} // end revealString Method
public static void main(String []args){
revealString("LLLL JJJJJJJJJJJJJJJ WWWWWW WWWWWW");
revealString("LLLL JJJJJJJJJJJJJJJ WWWW WWWW ");
revealString("LLLL JJJJ WWWW WWWW ");
revealString("LLLL JJJJ WWWW WWWW ");
revealString("LLLL JJJJ WWWW WWW WWWW ");
revealString("LLLL JJJJ WWWW WWWWW WWWW ");
revealString("LLLL JJJ JJJJ WWWW WWW WWW WWWW ");
revealString("LLLL JJJJ JJJJ WWWW WWW WWW WWWW ");
revealString("LLLLLLLLLLLL JJJJJJJJJJ WWWWWW WWWWWW ");
revealString("LLLLLLLLLLLL JJJJJJJJ WWWW WWWW ");
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment