Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. ///////////////////////////////////////////////////////////////
  5. // structures
  6. ///////////////////////////////////////////////////////////////
  7. struct point_S
  8. {
  9.     short _x;
  10.     short _y;
  11.     short _z;
  12. };
  13.  
  14. struct snakeNode_S
  15. {
  16.     struct snakeNode_S* _next;
  17.     struct point_S _point;
  18. };
  19.  
  20. struct foodNode_S
  21. {
  22.     struct foodNode_S* _next;
  23.     struct point_S _point;
  24.     short _value;               // could be 1,2,3,4
  25.     int _distance;
  26. };
  27.  
  28. struct obstacleNode_S
  29. {
  30.     struct obstacleNode_S* _next;
  31.     struct point_S _point;
  32. };
  33.  
  34. ///////////////////////////////////////////////////////////////
  35. // global variables
  36. ///////////////////////////////////////////////////////////////
  37.  
  38. ///////////////////////////////////////////////////////////////
  39. // functions declarations
  40. ///////////////////////////////////////////////////////////////
  41. int pointEqualPoint(struct point_S a, struct point_S b);
  42. struct point_S pointPlusPoint(struct point_S a, struct point_S b);
  43. int point2pointDist(struct point_S a, struct point_S b);
  44.  
  45. int snake2foodDist(const struct snakeNode_S** snake, const struct foodNode_S* foodNode);
  46. void updateFoodDist(const struct snakeNode_S** snake, struct foodNode_S** food, struct point_S* closestFood);
  47.  
  48. void addFood(struct foodNode_S** food, struct point_S p, short val);
  49. int isFood(struct foodNode_S** food, struct point_S p);
  50. void removeFood(struct foodNode_S** food, struct point_S p);
  51.  
  52. void addObstacle(struct obstacleNode_S** obstacle, struct point_S p);
  53. int isObstacle(struct obstacleNode_S** obstacle, struct point_S p);
  54.  
  55. void initSnake(struct snakeNode_S** snake, struct point_S p1, struct point_S p2);
  56. void feedSnake(struct snakeNode_S** snake, const struct foodNode_S** food, struct point_S p); //not finished
  57.  
  58. void displaySnake(const struct snakeNode_S* snake);
  59. void displayObstacle(const struct obstacleNode_S* obstacle, const char* obstacleSign);
  60. void displayFood(const struct foodNode_S** food);
  61.  
  62. void moveSnake(struct point_S p, struct snakeNode_S** snake);
  63. void makeMove(char direction,
  64.               struct snakeNode_S** snake,
  65.               struct onstacleNode_S** obstacle,
  66.               struct foodNode_S** food);
  67.  
  68. ///////////////////////////////////////////////////////////////
  69. // functions definitions
  70. ///////////////////////////////////////////////////////////////
  71. int pointEqualPoint(struct point_S a, struct point_S b)
  72. {
  73.     if ((a._x == b._x) && (a._y == b._y) && (a._z == b._z))
  74.         return 1;
  75.     else
  76.         return 0;
  77. }
  78.  
  79. struct point_S pointPlusPoint(struct point_S a, struct point_S b)
  80. {
  81.     struct point_S result;
  82.     result._x = a._x + b._x;
  83.     result._x = a._y + b._y;
  84.     result._x = a._z + b._z;
  85.     return result;
  86. }
  87.  
  88. int point2pointDist(struct point_S a, struct point_S b)
  89. {
  90.     return (a._x - b._x) * (a._x - b._x) + (a._y - b._y) * (a._y - b._y) + (a._z - b._z) * (a._z - b._z);
  91. }
  92.  
  93. int snake2foodDist(const struct snakeNode_S** snake, const struct foodNode_S* food)
  94. {
  95.     return point2pointDist((*snake)->_point, food->_point);
  96. }
  97.  
  98. void updateFoodDist(const struct snakeNode_S** snake, struct foodNode_S** food, struct point_S* closestFood)
  99. {
  100.     int minDist;
  101.     struct point_S snakeHead    = (*snake)->_point;
  102.     struct foodNode_S* temp     = *food;
  103.  
  104.     temp->_value    = point2pointDist(snakeHead, temp->_point);
  105.     minDist         = temp->_value;
  106.     temp            = temp->_next;
  107.     *closestFood    = temp->_point;
  108.  
  109.     while (temp->_next != NULL)
  110.     {
  111.         temp->_value = point2pointDist(snakeHead, temp->_point);
  112.         if (minDist > temp->_value)
  113.         {
  114.             minDist         = temp->_value;
  115.             *closestFood    = temp->_point;
  116.         }
  117.     }
  118. }
  119.  
  120. void addFood(struct foodNode_S** food, struct point_S p, short val)
  121. {
  122.     struct foodNode_S* temp = (struct foodNode_S*)malloc(sizeof(struct foodNode_S));
  123.     temp->_distance = 0;
  124.     temp->_point    = p;
  125.     temp->_value    = val;
  126.     temp->_next     = (*food);
  127.     (*food)         = temp;
  128.  
  129. }
  130.  
  131. int isFood(struct foodNode_S** food, struct point_S p)
  132. {
  133.     struct foodNode_S* temp = *food;
  134.     while (temp != NULL)
  135.     {
  136.         if (pointEqualPoint(temp->_point, p))
  137.             return 1;
  138.         temp = temp->_next;
  139.     }
  140.     return 0;
  141. }
  142.  
  143. void removeFood(struct foodNode_S** food, struct point_S p)
  144. {
  145.     struct foodNode_S* actual   = *food;
  146.     struct foodNode_S* prev     = NULL;
  147.     struct foodNode_S* next     = (*food)->_next;
  148.     while (actual->_next != NULL)
  149.     {
  150.         if (pointEqualPoint(actual->_point, p))
  151.         {
  152.             if ((prev == NULL) && (next == NULL))
  153.             {
  154.                 free(actual);
  155.                 food = NULL;
  156.             }
  157.             if (prev == NULL)
  158.             {
  159.                 (*food) = next;
  160.                 free(actual);
  161.             }
  162.             else if (next == NULL)
  163.             {
  164.                 free(actual);
  165.                 prev->_next = NULL;
  166.             }
  167.             else
  168.             {
  169.                 prev->_next = next;
  170.                 free(actual);
  171.             }
  172.             break;
  173.         }
  174.         prev    = actual;
  175.         actual  = actual->_next;
  176.         next    = actual->_next; // now next can be NULL
  177.     }
  178. }
  179.  
  180. void addObstacle(struct obstacleNode_S** obstacle, struct point_S p)
  181. {
  182.     struct obstacleNode_S* temp = malloc(sizeof(struct obstacleNode_S));
  183.     temp->_point    = p;
  184.     temp->_next     = *obstacle;
  185.  
  186.     obstacle = &temp;
  187. }
  188.  
  189. int isObstacle(struct obstacleNode_S** obstacle, struct point_S p)
  190. {
  191.     struct obstacleNode_S* temp = *obstacle;
  192.     while (temp->_next != NULL)
  193.     {
  194.         if (pointEqualPoint(temp->_point, p))
  195.             return 1;
  196.         temp = temp->_next;
  197.     }
  198.     return 0;
  199. }
  200.  
  201. void initSnake(struct snakeNode_S** snake, struct point_S p1, struct point_S p2)
  202. {
  203.     struct snakeNode_S* s1 = malloc(sizeof(struct snakeNode_S));
  204.     struct snakeNode_S* s2 = malloc(sizeof(struct snakeNode_S));
  205.     s1->_point  = p1;
  206.     s1->_next   = s2;
  207.     s2->_point  = p2;
  208.     s2->_next   = NULL;
  209.     *snake      = s1;
  210. }
  211.  
  212. void feedSnake(struct snakeNode_S** snake, const struct foodNode_S** food, struct point_S p)
  213. {
  214.     struct snakeNode_S* temp = *snake;
  215.     while (temp->_next->_next != NULL)
  216.         temp = temp->_next;
  217.     //TODO...
  218. }
  219.  
  220. void displaySnake(const struct snakeNode_S* snake)
  221. {
  222.     struct snakeNode_S* temp = snake;
  223.     while (temp != NULL)
  224.     {
  225.         printf("^:%d%;%d%;%d|", temp->_point._x, temp->_point._y, temp->_point._z);
  226.         temp = temp->_next;
  227.     }
  228.     printf("\n");
  229. }
  230.  
  231. void displayObstacle(const struct obstacleNode_S* obstacle, const char* obstacleSign)
  232. {
  233.     struct obstacleNode_S* temp = obstacle;
  234.     while (temp != NULL)
  235.     {
  236.         printf("%c:%d%;%d%;%d", *obstacleSign, temp->_point._x, temp->_point._y, temp->_point._z);
  237.         temp = temp->_next;
  238.         printf("\n");
  239.     }
  240. }
  241.  
  242. void displayFood(const struct foodNode_S** food)
  243. {
  244.     struct foodNode_S* temp = (*food);
  245.     while (temp != NULL) {
  246.         char c;
  247.         if      (temp->_value == 1) c = '@';
  248.         else if (temp->_value == 2) c = '%';
  249.         else if (temp->_value == 3) c = '$';
  250.         else if (temp->_value == 4) c = '*';
  251.  
  252.         printf("%c%:%d;%d%;%d",c, temp->_point._x, temp->_point._y, temp->_point._z);
  253.         temp = temp->_next;
  254.         printf("\n");
  255.     }
  256. }
  257.  
  258. void moveSnake(struct point_S p, struct snakeNode_S** snake)
  259. {
  260.     struct snakeNode_S* newHead = malloc(sizeof(struct snakeNode_S));
  261.  
  262.     newHead->_point = p;
  263.     newHead->_next = *snake;
  264.  
  265.     snake = &newHead;
  266.  
  267.     while ((*snake)->_next->_next != NULL)
  268.         (*snake) = (*snake)->_next;
  269.  
  270.     free((*snake)->_next->_next);
  271.     (*snake)->_next = NULL;
  272.  
  273.     snake = &newHead;
  274. }
  275.  
  276. void makeMove(char direction,
  277.               struct snakeNode_S** snake,
  278.               struct onstacleNode_S** obstacle,
  279.               struct foodNode_S** food)
  280. {
  281.     struct point_S p = (*snake)->_point;
  282.  
  283.     // possible directions E(+x) S(+y) D(+z) W(-x) N(-y) U(-z)
  284.     switch (direction)
  285.     {
  286.     case 'E':
  287.         p._x++;
  288.         break;
  289.     case 'S':
  290.         p._y++;
  291.         break;
  292.     case 'D':
  293.         p._z++;
  294.         break;
  295.     case 'W':
  296.         p._x--;
  297.         break;
  298.     case 'N':
  299.         p._y--;
  300.         break;
  301.     case 'U':
  302.         p._z--;
  303.         break;
  304.     default:
  305.         printf("ERROR: unknown direction!\n");
  306.     }
  307.  
  308.     if(isFood(food, p))
  309.         feedSnake(snake, food, p);
  310.     else
  311.         moveSnake(p, snake);
  312. }
  313.  
  314. ///////////////////////////////////////////////////////////////
  315. // test functions
  316. ///////////////////////////////////////////////////////////////
  317.  
  318. void testFoodFunctions()
  319. {
  320.     struct point_S p[5];
  321.     p[0]._x = 0; p[0]._y = 0; p[0]._z = 0;
  322.     p[1]._x = 1; p[1]._y = 1; p[1]._z = 1;
  323.     p[2]._x = 2; p[2]._y = 2; p[2]._z = 2;
  324.     p[3]._x = 1; p[3]._y = 2; p[3]._z = 1;
  325.     p[4]._x = 1; p[4]._y = 2; p[4]._z = 0;
  326.     struct foodNode_S* food = NULL;
  327.     ////////////////////////////////////////////////////////////////////
  328.     printf("\nTest food functions start\n\n");
  329.     ////////////////////////////////////////////////////////////////////
  330.     int i;
  331.     for (i = 0; i < 5; i++)
  332.     {
  333.         addFood(&food, p[i], i % 4 + 1);
  334.     }
  335.  
  336.     displayFood(&food);
  337.  
  338.     if (isFood(&food, p[3]))
  339.     {
  340.         printf("p[3] = {%d, %d, %d} is food\n", p[3]._x, p[3]._y, p[3]._z);
  341.         removeFood(&food, p[3]);
  342.         displayFood(&food);
  343.     }
  344.  
  345.     if (!isFood(&food, p[3]))
  346.     {
  347.         printf("p[3] = {%d, %d, %d} is NOT food\n\n", p[3]._x, p[3]._y, p[3]._z);
  348.     }
  349.  
  350.     if (isFood(&food, p[1]))
  351.     {
  352.         printf("p[1] = {%d, %d, %d} is food\n", p[1]._x, p[1]._y, p[1]._z);
  353.         removeFood(&food, p[1]);
  354.         displayFood(&food);
  355.     }
  356.  
  357.     if (!isFood(&food, p[1]))
  358.     {
  359.         printf("p[1] = {%d, %d, %d} is NOT food\n\n", p[1]._x, p[1]._y, p[1]._z);
  360.     }
  361.  
  362.     if (isFood(&food, p[4]))
  363.     {
  364.         printf("p[4] = {%d, %d, %d} is food\n", p[4]._x, p[4]._y, p[4]._z);
  365.         removeFood(&food, p[4]);
  366.         displayFood(&food);
  367.     }
  368.  
  369.     if (!isFood(&food, p[4]))
  370.     {
  371.         printf("p[4] = {%d, %d, %d} is NOT food\n\n", p[4]._x, p[4]._y, p[4]._z);
  372.     }
  373.  
  374.     if (isFood(&food, p[2]))
  375.     {
  376.         printf("p[2] = {%d, %d, %d} is food\n", p[2]._x, p[2]._y, p[2]._z);
  377.         removeFood(&food, p[2]);
  378.         displayFood(&food);
  379.     }
  380.  
  381.     if (!isFood(&food, p[2]))
  382.     {
  383.         printf("p[2] = {%d, %d, %d} is NOT food\n\n", p[2]._x, p[2]._y, p[2]._z);
  384.     }
  385.  
  386.     if (isFood(&food, p[0]))
  387.     {
  388.         printf("p[0] = {%d, %d, %d} is food\n", p[0]._x, p[0]._y, p[0]._z);
  389.         removeFood(&food, p[0]);
  390.         displayFood(&food);
  391.     }
  392.  
  393.     if (!isFood(&food, p[0]))
  394.     {
  395.         printf("p[0] = {%d, %d, %d} is NOT food\n\n", p[0]._x, p[0]._y, p[0]._z);
  396.     }
  397.     ////////////////////////////////////////////////////////////////////
  398.     printf("\nTest food functions end\n\n");
  399.     ////////////////////////////////////////////////////////////////////
  400. }
  401.  
  402. int main(void)
  403. {
  404.     struct snakeNode_S* snake = NULL;
  405.     struct foodNode_S* food = NULL;
  406.     struct obstacleNode_S* obstacles = NULL;
  407.  
  408.     struct point_S closestFood;
  409.  
  410.     char obstacleSign = '&';
  411.     //scanf("%c", &obstacleSign);
  412.    
  413.     ////////////////////////////////////////////////////////////////////
  414.     // tests start
  415.     ////////////////////////////////////////////////////////////////////
  416.    
  417.     testFoodFunctions();
  418.  
  419.     ////////////////////////////////////////////////////////////////////
  420.     // tests end
  421.     ////////////////////////////////////////////////////////////////////
  422.  
  423.     return 0;
  424. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement