The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
untitled C Code
Posted by: something wild | May 8, 2010 @ 1:58pm
C Code
[
Download
]
#include <stdlib.h> #include <stdio.h> #include <time.h> FILE* world_file; char buffer[20]; enum tile_type { floor , wall , robot }; enum tile_type world[50][50]; enum orientation { north, northeast, east, southeast, south, southwest, west, northwest}; enum orientation robot_orientation; int x, old_x; int y, old_y; enum tile_type n,ne,e,se,s,sw,w,nw; void print_world() { int x, y; printf("N:%u NE:%u E:%u SE:%u S:%u SW:%u W:%u NW:%u\n", n, ne, e, se, s, sw, w, nw); for (y = 49; y >= 0; y--) { for (x = 0; x < 50; x++) { if (world[x][y] == floor) { putchar('_'); } else if (world[x][y] == wall) { putchar('#'); } else { putchar('!'); } } putchar('\n'); } } void sense() { n = world[x][y + 1]; ne = world[x + 1][y + 1]; e = world[x + 1][y]; se = world[x + 1][y - 1]; s = world[x][y - 1]; sw = world[x - 1][y - 1]; w = world[x - 1][y]; nw = world[x - 1][y + 1]; } void plan() { if (robot_orientation == north) { if (nw != wall && ne != wall && n == wall) { if (rand() % 2) //toss a coin { robot_orientation = northwest; } else { robot_orientation = northeast; } return; } if (nw == wall && ne == wall && n != wall) { return; } if (nw == wall && ne == wall && n == wall) { if (rand() % 2 && e != wall) { robot_orientation = east; } else if (rand() % 2 && w != wall) { robot_orientation = west; } else if (rand() % 2 && se != wall) { robot_orientation = southeast; } else if (rand() % 2 && sw != wall) { robot_orientation = southwest; } else { robot_orientation = south; } return; } if (nw == wall && ne != wall) { robot_orientation = northeast; return; } if (ne == wall && nw != wall) { robot_orientation = northwest; return; } } else if (robot_orientation == northeast) { if (e != wall && n != wall && ne == wall) { if (rand() % 2) //toss a coin { robot_orientation = east; } else { robot_orientation = north; } return; } if (e == wall && n == wall && ne != wall) { return; } if (e == wall && n == wall && ne == wall) { if (rand() % 2 && nw != wall) { robot_orientation = northwest; } else if (rand() % 2 && se != wall) { robot_orientation = southeast; } else if (rand() % 2 && s != wall) { robot_orientation = south; } else if (rand() % 2 && w != wall) { robot_orientation = west; } else { robot_orientation = southwest; } return; } if (n == wall && e != wall) { robot_orientation = east; return; } if (e == wall && n != wall) { robot_orientation = north; return; } } else if (robot_orientation == east) { if (ne != wall && se != wall && e == wall) { if (rand() % 2) //toss a coin { robot_orientation = northeast; } else { robot_orientation = southeast; } return; } if (ne == wall && se == wall && e != wall) { return; } if (ne == wall && se == wall && e == wall) { if (rand() % 2 && n != wall) { robot_orientation = north; } else if (rand() % 2 && s != wall) { robot_orientation = south; } else if (rand() % 2 && sw != wall) { robot_orientation = southwest; } else if (rand() % 2 && nw != wall) { robot_orientation = northwest; } else { robot_orientation = west; } return; } if (ne == wall && se != wall) { robot_orientation = southeast; return; } if (se == wall && ne != wall) { robot_orientation = northeast; return; } } else if (robot_orientation == southeast) { if (e != wall && s != wall && se == wall) { if (rand() % 2) //toss a coin { robot_orientation = east; } else { robot_orientation = south; } return; } if (e == wall && s == wall && se != wall) { return; } if (e == wall && s == wall && se == wall) { if (rand() % 2 && ne != wall) { robot_orientation = northeast; } else if (rand() % 2 && sw != wall) { robot_orientation = southwest; } else if (rand() % 2 && w != wall) { robot_orientation = west; } else if (rand() % 2 && n != wall) { robot_orientation = north; } else { robot_orientation = northwest; } return; } if (e == wall && s != wall) { robot_orientation = south; return; } if (s == wall && e != wall) { robot_orientation = east; return; } } else if (robot_orientation == south) { if (se != wall && sw != wall && s == wall) { if (rand() % 2) //toss a coin { robot_orientation = southeast; } else { robot_orientation = southwest; } return; } if (se == wall && sw == wall && s != wall) { return; } if (se == wall && sw == wall && s == wall) { if (rand() % 2 && ne != wall) { robot_orientation = northeast; } else if (rand() % 2 && nw != wall) { robot_orientation = northwest; } else if (rand() % 2 && e != wall) { robot_orientation = east; } else if (rand() % 2 && w != wall) { robot_orientation = west; } else { robot_orientation = north; } return; } if (se == wall && sw != wall) { robot_orientation = southwest; return; } if (sw == wall && se != wall) { robot_orientation = southeast; return; } } else if (robot_orientation == southwest) { if (s != wall && w != wall && sw == wall) { if (rand() % 2) //toss a coin { robot_orientation = south; } else { robot_orientation = west; } return; } if (s == wall && w == wall && sw != wall) { return; } if (s == wall && w == wall && sw == wall) { if (rand() % 2 && nw != wall) { robot_orientation = northwest; } else if (rand() % 2 && se != wall) { robot_orientation = southeast; } else if (rand() % 2 && e != wall) { robot_orientation = east; } else if (rand() % 2 && n != wall) { robot_orientation = north; } else { robot_orientation = northeast; } return; } if (s == wall && w != wall) { robot_orientation = west; return; } if (w == wall && s != wall) { robot_orientation = south; return; } } else if (robot_orientation == west) { if (sw != wall && nw != wall && w == wall) { if (rand() % 2) //toss a coin { robot_orientation = southwest; } else { robot_orientation = northwest; } return; } if (sw == wall && nw == wall && w != wall) { return; } if (sw == wall && nw == wall && w == wall) { if (rand() % 2 && ne != wall) { robot_orientation = northeast; } else if (rand() % 2 && se != wall) { robot_orientation = southeast; } else if (rand() % 2 && s != wall) { robot_orientation = south; } else if (rand() % 2 && n != wall) { robot_orientation = north; } else { robot_orientation = east; } return; } if (sw == wall && nw != wall) { robot_orientation = northwest; return; } if (nw == wall && sw != wall) { robot_orientation = southwest; return; } } else if (robot_orientation == northwest) { if (w != wall && n != wall && nw == wall) { if (rand() % 2) //toss a coin { robot_orientation = west; } else { robot_orientation = north; } return; } if (w == wall && n == wall && nw != wall) { return; } if (w == wall && n == wall && nw == wall) { if (rand() % 2 && ne != wall) { robot_orientation = northeast; } else if (rand() % 2 && sw != wall) { robot_orientation = southwest; } else if (rand() % 2 && s != wall) { robot_orientation = south; } else if (rand() % 2 && e != wall) { robot_orientation = east; } else { robot_orientation = southeast; } return; } if (w == wall && n != wall) { robot_orientation = north; return; } if (n == wall && w != wall) { robot_orientation = west; return; } } } void act() { world[x][y] = floor; switch(robot_orientation) { case north: y = y + 1; break; case northeast: x = x + 1; y = y + 1; break; case east: x = x + 1; break; case southeast: x = x + 1; y = y - 1; break; case south: y = y - 1; break; case southwest: x = x - 1; y = y - 1; break; case west: x = x - 1; break; case northwest: x = x - 1; y = y + 1; break; } world[x][y] = robot; } int main(int argc, char ** argv) { srand(time(NULL)); if (argc < 4) { fputs("Usage: robot <world_file> <robot_x> <robot_y>\n",stderr); exit(1); } for (x = 0; x < 50; x++) { for (y = 0; y < 50; y++) { world[x][y] = floor; } } if ((world_file = fopen(argv[1], "r")) == NULL) { fputs("Can not open world file!\n",stderr); exit(1); } fgets(buffer,20,world_file); //get rid of comment while ((fgets(buffer,20,world_file)) != NULL) { sscanf(buffer,"%u %u", &x, &y); world[x][y] = wall; } fclose(world_file); x = atoi(argv[2]); y = atoi(argv[3]); if (world[x][y] != floor) { fputs("Robot position is a wall! Aborting\n",stderr); exit(1); } else { world[x][y] = robot; } robot_orientation = north; sense(); print_world(); getchar(); while (1) { sense(); plan(); act(); print_world(); getchar(); } return(0); }
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.62 / 8
76 Votes
http://codebin.yi.org/637
page generated in 0.00 seconds