Created
February 10, 2020 09:36
-
-
Save haideralipunjabi/a1f37f8502dcdef73a24194879c363e9 to your computer and use it in GitHub Desktop.
Code snippets for Blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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