void ofApp::updateAnim(){ if(ofGetFrameNum() < 39) { for(int i = 0; i <= 20; i++) { line[i].y = drunkWalk(line[i].y); } lines.push_back(line); } if(ofGetFrameNum() >= 40) { if(lines.size() == 1) { renderGif(); } else { lines.pop_back(); } } }
int drunkWalk(char start[84]) { int stuck = 0; //printf("starting at %s\n", start); if(!strcmp(start, "Home")) { return 1; } else if(numWalks == 10000) { printf("You've taken 10,000 walks and run out of hope (infinite loop)...\n"); return 0; } else { //delay //clock_t endtime = clock() + 1 * CLOCKS_PER_SEC; //while ( ( clock() < endtime ) ); numWalks++; //printf("%d\n", numWalks); char dest[84]; struct doubleLink *current = head; while(current != NULL) { if(!strcmp(current->name, start)) break; else current = current->next; } int i = rand()%10; //printf("i = %d\n", i); while(!strcmp(current->self->destList[i], "")) { i = rand()%10; stuck++; if (stuck == 1000) { printf("You're stuck!"); return 0; } //printf("i = %d\n", i); } globalCost += current->self->costList[i]; //printf("going to %s with a cost of %d\n\n", current->self->destList[i], current->self->costList[i]); drunkWalk(current->self->destList[i]); } }
int main(int argc, char *argv[]) { globalCost = 0; numWalks = 0; //curtime = time(NULL); //srand(curtime); srand(12212012); head = NULL; tail = NULL; char fname[84]; char dest[84]; char fileName[100]; char start[84]; int fcost; printf("Enter data file: "); scanf("%s", fileName); FILE *dataFile; dataFile = fopen(fileName, "r"); if (dataFile == NULL) { printf("cannot open file\n"); exit(0); } while(fscanf(dataFile, "%s", fname) != EOF) { if(!strcmp(fname, "STOP")) { break; } else { insert(fname); } } while(fscanf(dataFile, "%s %s %d", fname, dest, &fcost) != EOF) { if(!strcmp(fname, "STOP")) { break; } else { createLinks(fname, dest, fcost); } } fscanf(dataFile, "%s", start); int x; if(argc>1) { switch(atoi(argv[1])) { case 0: printf("\nDrunkard's Walk\n"); x = drunkWalk(start); break; case 1: printf("\nGreedy Walk\n"); x = greedy(start); break; case 2: printf("\nGenerous Walk\n"); x = generous(start); break; default: printf("\nNon-applicable argument\n0-Drunkard's Walk\n1-Greedy Walk\n2-Generous Walk\n"); break; } } else { printf("Need command line argument\n"); } //printForwardList(); //printBackwardList(); //printAll(); if(x!=0) { printf("\nYou started at %s\nYou're home and it only cost you %d!\n\n", start, globalCost); } else { printf("Please try again\n\n"); } fclose(dataFile); freeStuff(); }