Created
January 6, 2012 02:29
-
-
Save andrewrcollins/1568642 to your computer and use it in GitHub Desktop.
#TJHSST ~ Random Walks
This file contains hidden or 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
| /* | |
| Random Walks | |
| */ | |
| #include "stdio.h" | |
| #include "stdlib.h" | |
| #include "time.h" | |
| #include "conio.h" | |
| #include "graphics.h" | |
| #define MAXX 40 | |
| #define MAXY 40 | |
| #define EMPTY 0 | |
| #define OCCUPIED 1 | |
| #define direction (random(3)-1) | |
| #define magnitude 1 | |
| #define outside(x,y) ((x>=(MAXX-1))||(x<1)||(y>=(MAXY-1))||(y<1)) | |
| char matrix[MAXY][MAXX]; | |
| void ginit(void) | |
| { | |
| int gdriver,gmode; | |
| gdriver=CGA; | |
| gmode=CGA; | |
| initgraph(&gdriver,&gmode,""); | |
| } | |
| int start(x,y) | |
| int *x,*y; | |
| { | |
| switch(random(100)%4) { | |
| case 0 : | |
| *x=1; | |
| *y=random(MAXY-2)+1; | |
| break; | |
| case 1 : | |
| *x=random(MAXX-2)+1; | |
| *y=1; | |
| break; | |
| case 2 : | |
| *x=MAXX-2; | |
| *y=random(MAXY-2)+1; | |
| break; | |
| case 3 : | |
| *x=random(MAXX-2)+1; | |
| *y=MAXY-2; | |
| break; | |
| } | |
| } | |
| int touched(x,y) | |
| int x,y; | |
| { | |
| int sx,sy; | |
| for(sy=(-1);sy<2;sy++) | |
| for(sx=(-1);sx<2;sx++) | |
| if(matrix[y+sy][x+sx]==OCCUPIED) | |
| return 1; | |
| return 0; | |
| } | |
| int main() | |
| { | |
| int x,y,out; | |
| ginit(); | |
| cleardevice(); | |
| for(y=0;y<MAXY;y++) | |
| for(x=0;x<MAXX;x++) | |
| matrix[y][x]=EMPTY; | |
| matrix[MAXY/2][MAXX/2]=OCCUPIED; | |
| putpixel(MAXX/2,MAXY/2,2); | |
| randomize(); | |
| while(!kbhit()) { | |
| start(&x,&y); | |
| out=outside(x,y); | |
| while(!touched(x,y)&&!out&&!kbhit()) { | |
| x+=direction*magnitude; | |
| y+=direction*magnitude; | |
| out=outside(x,y); | |
| } | |
| if(!out) { | |
| matrix[y][x]=OCCUPIED; | |
| putpixel(x,y,2); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| /* | |
| Random Walks | |
| */ | |
| #include "stdio.h" | |
| #include "string.h" | |
| #include "sys/time.h" | |
| #define MAXX 100 | |
| #define MAXY 100 | |
| #define EMPTY 0 | |
| #define OCCUPIED 1 | |
| #define direction (random(3)-1) | |
| #define outside(x,y) ((x>=(MAXX-1))||(x<1)||(y>=(MAXY-1))||(y<1)) | |
| char matrix[MAXY][MAXX]; | |
| int start(x,y) | |
| int *x,*y; | |
| { | |
| switch(random()&3) { | |
| case 0 : | |
| *x=1; | |
| *y=random()%(MAXY-2)+1; | |
| break; | |
| case 1 : | |
| *x=random()%(MAXX-2)+1; | |
| *y=1; | |
| break; | |
| case 2 : | |
| *x=MAXX-2; | |
| *y=random()%(MAXY-2)+1; | |
| break; | |
| case 3 : | |
| *x=random()%(MAXX-2)+1; | |
| *y=MAXY-2; | |
| break; | |
| } | |
| } | |
| int touched(x,y) | |
| int x,y; | |
| { | |
| int sx,sy; | |
| for(sy=(-1);sy<2;sy++) | |
| for(sx=(-1);sx<2;sx++) | |
| if(matrix[y+sy][x+sx]==OCCUPIED) | |
| return 1; | |
| return 0; | |
| } | |
| int main(argc,argv) | |
| int argc; | |
| char *argv[]; | |
| { | |
| int x,y,out,count; | |
| FILE *outfile; | |
| char filename[80] | |
| strcpy(filename,argv[1]); | |
| for(y=0;y<MAXY;y++) | |
| for(x=0;x<MAXX;x++) | |
| matrix[y][x]=EMPTY; | |
| matrix[MAXY/2][MAXX/2]=OCCUPIED; | |
| count=1; | |
| srandom(time(0)); | |
| while(!kbhit()&&count<MAXATOMS) { | |
| start(&x,&y); | |
| out=outside(x,y); | |
| while(!touched(x,y)&&!out&&!kbhit()) { | |
| x+=direction; | |
| y+=direction; | |
| out=outside(x,y); | |
| } | |
| if(!out) { | |
| matrix[y][x]=OCCUPIED; | |
| count++; | |
| } | |
| } | |
| outfile=fopen(filename,"w"); | |
| for(y=0;y<MAXY;y++) | |
| for(x=0;x<MAXX;x++) | |
| if(matrix[y][x]==OCCUPIED) | |
| fprintf(outfile,"%d %d\n",x,y); | |
| fclose(outfile); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment