Skip to content

Instantly share code, notes, and snippets.

@haideralipunjabi
Created February 10, 2020 09:36
Show Gist options
  • Save haideralipunjabi/a1f37f8502dcdef73a24194879c363e9 to your computer and use it in GitHub Desktop.
Save haideralipunjabi/a1f37f8502dcdef73a24194879c363e9 to your computer and use it in GitHub Desktop.
Code snippets for Blog
class Walker {
int x;
int y;
Walker() {
// Constructor function to initailize data
// width & height have the width & height of the output.
x = width/2;
y = height/2;
}
}
void step(){
int choice = int(random(4));
if(choice == 0){
x++; // Move right
}
else if(choice == 1){
x--; // Move left
}
else if(choice == 2){
y++; // Move up
}
else {
y--; // Move down
}
}
void step(){
int choice = int(random(9));
if(choice == 0){
x++; // Move right
}
else if(choice == 1){
x--; // Move left
}
else if(choice == 2){
y++; // Move up
}
else if(choice == 3){
y--; // Move down
}
else if(choice == 4){
// Move up-right
x++;
y++;
}
else if(choice == 5){
// Move up-left
x--;
y++;
}
else if(choice == 6){
// Move down-left
x--;
y--;
}
else if(choice == 7){
// Move down-right
x++;
y--;
}
// We don’t need to write code for staying
}
void step(){
int xchoice = int(random(3));
int ychoice = int(random(3));
if(xchoice == 0){
x++; // Move right
}
else if(xchoice == 1){
x--; // Move left
}
if(ychoice == 0){
y++; // Move up
}
else if(ychoice == 1){
y--; // Move down
}
}
void step(){
x += int(random(3)) – 1; // int(random(3)) – 1; returns -1, 0 or 1
y += int(random(3)) - 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment