How would you write this code?
in
Programming Questions
•
2 years ago
For the purpose of learning i always like to see how others would do it (like here:
link ).
So i hope some of you guys give it a try.
So i hope some of you guys give it a try.
- PFont font1;
String t1 = "HOW WOULD YOU WRITE THIS CODE?";
int[] bytes = {
};
HackText ct; //
void setup() {
size(100, 100);
frameRate(200);
font1 = createFont("Courier", 12);
textFont(font1);
bytes = createBytes(bytes);
ct = new HackText(t1, 10, 10, bytes, 80, 80);
}
void draw() {
background(255);
fill(0);
ct.display();
}
int[] createBytes(int[] intArray) {
float charWidth;
for(int i = -128; i <= 127; i++) {
charWidth = textWidth(char(i));
if(charWidth > 0) {
intArray = append(intArray, i);
}
}
return(intArray);
}
class HackText {
boolean isHacked = false;
String data;
String hacked_data = "";
int dataIndex = 0;
int byteIndex = 0;
float x;
float y;
float tWidth;
float tHeight;
int[] bytes;
HackText(String t_data, float t_x, float t_y, int[] t_bytes) {//
data = t_data;
x = t_x;
y = t_y;
bytes = t_bytes;
}
HackText(String t_data, float t_x, float t_y, int[] t_bytes, float t_width, float t_height) {//
data = t_data;
x = t_x;
y = t_y;
bytes = t_bytes;
tWidth = t_width;
tHeight = t_height;
}
void display() {
if(isHacked == false) {
char tryByte = char(bytes[byteIndex]);//the byte to try
char theByte = data.charAt(dataIndex);//the byte to find
//found
if(tryByte == theByte) {
//add the char
hacked_data += tryByte;
//check if everything is found
if(hacked_data.length() == data.length()) {
isHacked = true;
}
else {
byteIndex = 0;
dataIndex++;
}
}
//not found
else {
byteIndex++;
if(byteIndex > bytes.length-1) {
println("can't be found");
hacked_data += theByte;
//check if the String is complete now
if(hacked_data.length() == data.length()) {
isHacked = true;
}
else {
byteIndex = 0;
dataIndex++;
}
}
}
if(tWidth > 0) {
text(hacked_data+tryByte, x, y, tWidth, tHeight);
}
else {
text(hacked_data+tryByte, x, y);
}
}
else {//text is hacked
if(tWidth > 0) {
text(hacked_data, x, y, tWidth, tHeight);
}
else {
text(hacked_data, x, y);
}
}
}
}
1