1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4.  
  5. FILE* world_file;
  6. char buffer[20];
  7.  
  8. enum tile_type { floor , wall , robot };
  9. enum tile_type world[50][50];
  10.  
  11. enum orientation { north, northeast, east, southeast, south, southwest, west, northwest};
  12. enum orientation robot_orientation;
  13. int x, old_x;
  14. int y, old_y;
  15.  
  16. enum tile_type n,ne,e,se,s,sw,w,nw;
  17.  
  18. void print_world()
  19. {
  20. int x, y;
  21. 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);
  22. for (y = 49; y >= 0; y--)
  23. {
  24. for (x = 0; x < 50; x++)
  25. {
  26. if (world[x][y] == floor)
  27. {
  28. putchar('_');
  29. }
  30. else if (world[x][y] == wall)
  31. {
  32. putchar('#');
  33. }
  34. else
  35. {
  36. putchar('!');
  37. }
  38. }
  39. putchar('\n');
  40. }
  41. }
  42.  
  43. void sense()
  44. {
  45. n = world[x][y + 1];
  46. ne = world[x + 1][y + 1];
  47. e = world[x + 1][y];
  48. se = world[x + 1][y - 1];
  49. s = world[x][y - 1];
  50. sw = world[x - 1][y - 1];
  51. w = world[x - 1][y];
  52. nw = world[x - 1][y + 1];
  53. }
  54.  
  55. void plan()
  56. {
  57. if (robot_orientation == north)
  58. {
  59. if (nw != wall && ne != wall && n == wall)
  60. {
  61. if (rand() % 2) //toss a coin
  62. {
  63. robot_orientation = northwest;
  64. }
  65. else
  66. {
  67. robot_orientation = northeast;
  68. }
  69. return;
  70. }
  71. if (nw == wall && ne == wall && n != wall)
  72. {
  73. return;
  74. }
  75. if (nw == wall && ne == wall && n == wall)
  76. {
  77. if (rand() % 2 && e != wall)
  78. {
  79. robot_orientation = east;
  80. }
  81. else if (rand() % 2 && w != wall)
  82. {
  83. robot_orientation = west;
  84. }
  85. else if (rand() % 2 && se != wall)
  86. {
  87. robot_orientation = southeast;
  88. }
  89. else if (rand() % 2 && sw != wall)
  90. {
  91. robot_orientation = southwest;
  92. }
  93. else
  94. {
  95. robot_orientation = south;
  96. }
  97. return;
  98. }
  99. if (nw == wall && ne != wall)
  100. {
  101. robot_orientation = northeast;
  102. return;
  103. }
  104. if (ne == wall && nw != wall)
  105. {
  106. robot_orientation = northwest;
  107. return;
  108. }
  109. }
  110. else if (robot_orientation == northeast)
  111. {
  112. if (e != wall && n != wall && ne == wall)
  113. {
  114. if (rand() % 2) //toss a coin
  115. {
  116. robot_orientation = east;
  117. }
  118. else
  119. {
  120. robot_orientation = north;
  121. }
  122. return;
  123. }
  124. if (e == wall && n == wall && ne != wall)
  125. {
  126. return;
  127. }
  128. if (e == wall && n == wall && ne == wall)
  129. {
  130. if (rand() % 2 && nw != wall)
  131. {
  132. robot_orientation = northwest;
  133. }
  134. else if (rand() % 2 && se != wall)
  135. {
  136. robot_orientation = southeast;
  137. }
  138. else if (rand() % 2 && s != wall)
  139. {
  140. robot_orientation = south;
  141. }
  142. else if (rand() % 2 && w != wall)
  143. {
  144. robot_orientation = west;
  145. }
  146. else
  147. {
  148. robot_orientation = southwest;
  149. }
  150. return;
  151. }
  152. if (n == wall && e != wall)
  153. {
  154. robot_orientation = east;
  155. return;
  156. }
  157. if (e == wall && n != wall)
  158. {
  159. robot_orientation = north;
  160. return;
  161. }
  162. }
  163. else if (robot_orientation == east)
  164. {
  165. if (ne != wall && se != wall && e == wall)
  166. {
  167. if (rand() % 2) //toss a coin
  168. {
  169. robot_orientation = northeast;
  170. }
  171. else
  172. {
  173. robot_orientation = southeast;
  174. }
  175. return;
  176. }
  177. if (ne == wall && se == wall && e != wall)
  178. {
  179. return;
  180. }
  181. if (ne == wall && se == wall && e == wall)
  182. {
  183. if (rand() % 2 && n != wall)
  184. {
  185. robot_orientation = north;
  186. }
  187. else if (rand() % 2 && s != wall)
  188. {
  189. robot_orientation = south;
  190. }
  191. else if (rand() % 2 && sw != wall)
  192. {
  193. robot_orientation = southwest;
  194. }
  195. else if (rand() % 2 && nw != wall)
  196. {
  197. robot_orientation = northwest;
  198. }
  199. else
  200. {
  201. robot_orientation = west;
  202. }
  203. return;
  204. }
  205. if (ne == wall && se != wall)
  206. {
  207. robot_orientation = southeast;
  208. return;
  209. }
  210. if (se == wall && ne != wall)
  211. {
  212. robot_orientation = northeast;
  213. return;
  214. }
  215. }
  216. else if (robot_orientation == southeast)
  217. {
  218. if (e != wall && s != wall && se == wall)
  219. {
  220. if (rand() % 2) //toss a coin
  221. {
  222. robot_orientation = east;
  223. }
  224. else
  225. {
  226. robot_orientation = south;
  227. }
  228. return;
  229. }
  230. if (e == wall && s == wall && se != wall)
  231. {
  232. return;
  233. }
  234. if (e == wall && s == wall && se == wall)
  235. {
  236. if (rand() % 2 && ne != wall)
  237. {
  238. robot_orientation = northeast;
  239. }
  240. else if (rand() % 2 && sw != wall)
  241. {
  242. robot_orientation = southwest;
  243. }
  244. else if (rand() % 2 && w != wall)
  245. {
  246. robot_orientation = west;
  247. }
  248. else if (rand() % 2 && n != wall)
  249. {
  250. robot_orientation = north;
  251. }
  252. else
  253. {
  254. robot_orientation = northwest;
  255. }
  256. return;
  257. }
  258. if (e == wall && s != wall)
  259. {
  260. robot_orientation = south;
  261. return;
  262. }
  263. if (s == wall && e != wall)
  264. {
  265. robot_orientation = east;
  266. return;
  267. }
  268. }
  269. else if (robot_orientation == south)
  270. {
  271. if (se != wall && sw != wall && s == wall)
  272. {
  273. if (rand() % 2) //toss a coin
  274. {
  275. robot_orientation = southeast;
  276. }
  277. else
  278. {
  279. robot_orientation = southwest;
  280. }
  281. return;
  282. }
  283. if (se == wall && sw == wall && s != wall)
  284. {
  285. return;
  286. }
  287. if (se == wall && sw == wall && s == wall)
  288. {
  289. if (rand() % 2 && ne != wall)
  290. {
  291. robot_orientation = northeast;
  292. }
  293. else if (rand() % 2 && nw != wall)
  294. {
  295. robot_orientation = northwest;
  296. }
  297. else if (rand() % 2 && e != wall)
  298. {
  299. robot_orientation = east;
  300. }
  301. else if (rand() % 2 && w != wall)
  302. {
  303. robot_orientation = west;
  304. }
  305. else
  306. {
  307. robot_orientation = north;
  308. }
  309. return;
  310. }
  311. if (se == wall && sw != wall)
  312. {
  313. robot_orientation = southwest;
  314. return;
  315. }
  316. if (sw == wall && se != wall)
  317. {
  318. robot_orientation = southeast;
  319. return;
  320. }
  321. }
  322. else if (robot_orientation == southwest)
  323. {
  324. if (s != wall && w != wall && sw == wall)
  325. {
  326. if (rand() % 2) //toss a coin
  327. {
  328. robot_orientation = south;
  329. }
  330. else
  331. {
  332. robot_orientation = west;
  333. }
  334. return;
  335. }
  336. if (s == wall && w == wall && sw != wall)
  337. {
  338. return;
  339. }
  340. if (s == wall && w == wall && sw == wall)
  341. {
  342. if (rand() % 2 && nw != wall)
  343. {
  344. robot_orientation = northwest;
  345. }
  346. else if (rand() % 2 && se != wall)
  347. {
  348. robot_orientation = southeast;
  349. }
  350. else if (rand() % 2 && e != wall)
  351. {
  352. robot_orientation = east;
  353. }
  354. else if (rand() % 2 && n != wall)
  355. {
  356. robot_orientation = north;
  357. }
  358. else
  359. {
  360. robot_orientation = northeast;
  361. }
  362. return;
  363. }
  364. if (s == wall && w != wall)
  365. {
  366. robot_orientation = west;
  367. return;
  368. }
  369. if (w == wall && s != wall)
  370. {
  371. robot_orientation = south;
  372. return;
  373. }
  374. }
  375. else if (robot_orientation == west)
  376. {
  377. if (sw != wall && nw != wall && w == wall)
  378. {
  379. if (rand() % 2) //toss a coin
  380. {
  381. robot_orientation = southwest;
  382. }
  383. else
  384. {
  385. robot_orientation = northwest;
  386. }
  387. return;
  388. }
  389. if (sw == wall && nw == wall && w != wall)
  390. {
  391. return;
  392. }
  393. if (sw == wall && nw == wall && w == wall)
  394. {
  395. if (rand() % 2 && ne != wall)
  396. {
  397. robot_orientation = northeast;
  398. }
  399. else if (rand() % 2 && se != wall)
  400. {
  401. robot_orientation = southeast;
  402. }
  403. else if (rand() % 2 && s != wall)
  404. {
  405. robot_orientation = south;
  406. }
  407. else if (rand() % 2 && n != wall)
  408. {
  409. robot_orientation = north;
  410. }
  411. else
  412. {
  413. robot_orientation = east;
  414. }
  415. return;
  416. }
  417. if (sw == wall && nw != wall)
  418. {
  419. robot_orientation = northwest;
  420. return;
  421. }
  422. if (nw == wall && sw != wall)
  423. {
  424. robot_orientation = southwest;
  425. return;
  426. }
  427. }
  428. else if (robot_orientation == northwest)
  429. {
  430. if (w != wall && n != wall && nw == wall)
  431. {
  432. if (rand() % 2) //toss a coin
  433. {
  434. robot_orientation = west;
  435. }
  436. else
  437. {
  438. robot_orientation = north;
  439. }
  440. return;
  441. }
  442. if (w == wall && n == wall && nw != wall)
  443. {
  444. return;
  445. }
  446. if (w == wall && n == wall && nw == wall)
  447. {
  448. if (rand() % 2 && ne != wall)
  449. {
  450. robot_orientation = northeast;
  451. }
  452. else if (rand() % 2 && sw != wall)
  453. {
  454. robot_orientation = southwest;
  455. }
  456. else if (rand() % 2 && s != wall)
  457. {
  458. robot_orientation = south;
  459. }
  460. else if (rand() % 2 && e != wall)
  461. {
  462. robot_orientation = east;
  463. }
  464. else
  465. {
  466. robot_orientation = southeast;
  467. }
  468. return;
  469. }
  470. if (w == wall && n != wall)
  471. {
  472. robot_orientation = north;
  473. return;
  474. }
  475. if (n == wall && w != wall)
  476. {
  477. robot_orientation = west;
  478. return;
  479. }
  480. }
  481. }
  482.  
  483. void act()
  484. {
  485. world[x][y] = floor;
  486. switch(robot_orientation)
  487. {
  488. case north:
  489. y = y + 1;
  490. break;
  491. case northeast:
  492. x = x + 1;
  493. y = y + 1;
  494. break;
  495. case east:
  496. x = x + 1;
  497. break;
  498. case southeast:
  499. x = x + 1;
  500. y = y - 1;
  501. break;
  502. case south:
  503. y = y - 1;
  504. break;
  505. case southwest:
  506. x = x - 1;
  507. y = y - 1;
  508. break;
  509. case west:
  510. x = x - 1;
  511. break;
  512. case northwest:
  513. x = x - 1;
  514. y = y + 1;
  515. break;
  516. }
  517. world[x][y] = robot;
  518. }
  519.  
  520. int main(int argc, char ** argv)
  521. {
  522. srand(time(NULL));
  523. if (argc < 4)
  524. {
  525. fputs("Usage: robot <world_file> <robot_x> <robot_y>\n",stderr);
  526. exit(1);
  527. }
  528.  
  529. for (x = 0; x < 50; x++)
  530. {
  531. for (y = 0; y < 50; y++)
  532. {
  533. world[x][y] = floor;
  534. }
  535. }
  536.  
  537. if ((world_file = fopen(argv[1], "r")) == NULL)
  538. {
  539. fputs("Can not open world file!\n",stderr);
  540. exit(1);
  541. }
  542. fgets(buffer,20,world_file); //get rid of comment
  543. while ((fgets(buffer,20,world_file)) != NULL)
  544. {
  545. sscanf(buffer,"%u %u", &x, &y);
  546. world[x][y] = wall;
  547. }
  548. fclose(world_file);
  549.  
  550. x = atoi(argv[2]);
  551. y = atoi(argv[3]);
  552.  
  553. if (world[x][y] != floor)
  554. {
  555. fputs("Robot position is a wall! Aborting\n",stderr);
  556. exit(1);
  557. }
  558. else
  559. {
  560. world[x][y] = robot;
  561. }
  562. robot_orientation = north;
  563.  
  564. sense();
  565. print_world();
  566. getchar();
  567.  
  568. while (1)
  569. {
  570. sense();
  571. plan();
  572. act();
  573. print_world();
  574. getchar();
  575. }
  576.  
  577. return(0);
  578. }
  579.