Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- ///////////////////////////////////////////////////////////////
- // structures
- ///////////////////////////////////////////////////////////////
- struct point_S
- {
- short _x;
- short _y;
- short _z;
- };
- struct snakeNode_S
- {
- struct snakeNode_S* _next;
- struct point_S _point;
- };
- struct foodNode_S
- {
- struct foodNode_S* _next;
- struct point_S _point;
- short _value; // could be 1,2,3,4
- int _distance;
- };
- struct obstacleNode_S
- {
- struct obstacleNode_S* _next;
- struct point_S _point;
- };
- ///////////////////////////////////////////////////////////////
- // global variables
- ///////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////
- // functions declarations
- ///////////////////////////////////////////////////////////////
- int pointEqualPoint(struct point_S a, struct point_S b);
- struct point_S pointPlusPoint(struct point_S a, struct point_S b);
- int point2pointDist(struct point_S a, struct point_S b);
- int snake2foodDist(const struct snakeNode_S** snake, const struct foodNode_S* foodNode);
- void updateFoodDist(const struct snakeNode_S** snake, struct foodNode_S** food, struct point_S* closestFood);
- void addFood(struct foodNode_S** food, struct point_S p, short val);
- int isFood(struct foodNode_S** food, struct point_S p);
- void removeFood(struct foodNode_S** food, struct point_S p);
- void addObstacle(struct obstacleNode_S** obstacle, struct point_S p);
- int isObstacle(struct obstacleNode_S** obstacle, struct point_S p);
- void initSnake(struct snakeNode_S** snake, struct point_S p1, struct point_S p2);
- void feedSnake(struct snakeNode_S** snake, const struct foodNode_S** food, struct point_S p); //not finished
- void displaySnake(const struct snakeNode_S* snake);
- void displayObstacle(const struct obstacleNode_S* obstacle, const char* obstacleSign);
- void displayFood(const struct foodNode_S** food);
- void moveSnake(struct point_S p, struct snakeNode_S** snake);
- void makeMove(char direction,
- struct snakeNode_S** snake,
- struct onstacleNode_S** obstacle,
- struct foodNode_S** food);
- ///////////////////////////////////////////////////////////////
- // functions definitions
- ///////////////////////////////////////////////////////////////
- int pointEqualPoint(struct point_S a, struct point_S b)
- {
- if ((a._x == b._x) && (a._y == b._y) && (a._z == b._z))
- return 1;
- else
- return 0;
- }
- struct point_S pointPlusPoint(struct point_S a, struct point_S b)
- {
- struct point_S result;
- result._x = a._x + b._x;
- result._x = a._y + b._y;
- result._x = a._z + b._z;
- return result;
- }
- int point2pointDist(struct point_S a, struct point_S b)
- {
- 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);
- }
- int snake2foodDist(const struct snakeNode_S** snake, const struct foodNode_S* food)
- {
- return point2pointDist((*snake)->_point, food->_point);
- }
- void updateFoodDist(const struct snakeNode_S** snake, struct foodNode_S** food, struct point_S* closestFood)
- {
- int minDist;
- struct point_S snakeHead = (*snake)->_point;
- struct foodNode_S* temp = *food;
- temp->_value = point2pointDist(snakeHead, temp->_point);
- minDist = temp->_value;
- temp = temp->_next;
- *closestFood = temp->_point;
- while (temp->_next != NULL)
- {
- temp->_value = point2pointDist(snakeHead, temp->_point);
- if (minDist > temp->_value)
- {
- minDist = temp->_value;
- *closestFood = temp->_point;
- }
- }
- }
- void addFood(struct foodNode_S** food, struct point_S p, short val)
- {
- struct foodNode_S* temp = (struct foodNode_S*)malloc(sizeof(struct foodNode_S));
- temp->_distance = 0;
- temp->_point = p;
- temp->_value = val;
- temp->_next = (*food);
- (*food) = temp;
- }
- int isFood(struct foodNode_S** food, struct point_S p)
- {
- struct foodNode_S* temp = *food;
- while (temp != NULL)
- {
- if (pointEqualPoint(temp->_point, p))
- return 1;
- temp = temp->_next;
- }
- return 0;
- }
- void removeFood(struct foodNode_S** food, struct point_S p)
- {
- struct foodNode_S* actual = *food;
- struct foodNode_S* prev = NULL;
- struct foodNode_S* next = (*food)->_next;
- while (actual->_next != NULL)
- {
- if (pointEqualPoint(actual->_point, p))
- {
- if ((prev == NULL) && (next == NULL))
- {
- free(actual);
- food = NULL;
- }
- if (prev == NULL)
- {
- (*food) = next;
- free(actual);
- }
- else if (next == NULL)
- {
- free(actual);
- prev->_next = NULL;
- }
- else
- {
- prev->_next = next;
- free(actual);
- }
- break;
- }
- prev = actual;
- actual = actual->_next;
- next = actual->_next; // now next can be NULL
- }
- }
- void addObstacle(struct obstacleNode_S** obstacle, struct point_S p)
- {
- struct obstacleNode_S* temp = malloc(sizeof(struct obstacleNode_S));
- temp->_point = p;
- temp->_next = *obstacle;
- obstacle = &temp;
- }
- int isObstacle(struct obstacleNode_S** obstacle, struct point_S p)
- {
- struct obstacleNode_S* temp = *obstacle;
- while (temp->_next != NULL)
- {
- if (pointEqualPoint(temp->_point, p))
- return 1;
- temp = temp->_next;
- }
- return 0;
- }
- void initSnake(struct snakeNode_S** snake, struct point_S p1, struct point_S p2)
- {
- struct snakeNode_S* s1 = malloc(sizeof(struct snakeNode_S));
- struct snakeNode_S* s2 = malloc(sizeof(struct snakeNode_S));
- s1->_point = p1;
- s1->_next = s2;
- s2->_point = p2;
- s2->_next = NULL;
- *snake = s1;
- }
- void feedSnake(struct snakeNode_S** snake, const struct foodNode_S** food, struct point_S p)
- {
- struct snakeNode_S* temp = *snake;
- while (temp->_next->_next != NULL)
- temp = temp->_next;
- //TODO...
- }
- void displaySnake(const struct snakeNode_S* snake)
- {
- struct snakeNode_S* temp = snake;
- while (temp != NULL)
- {
- printf("^:%d%;%d%;%d|", temp->_point._x, temp->_point._y, temp->_point._z);
- temp = temp->_next;
- }
- printf("\n");
- }
- void displayObstacle(const struct obstacleNode_S* obstacle, const char* obstacleSign)
- {
- struct obstacleNode_S* temp = obstacle;
- while (temp != NULL)
- {
- printf("%c:%d%;%d%;%d", *obstacleSign, temp->_point._x, temp->_point._y, temp->_point._z);
- temp = temp->_next;
- printf("\n");
- }
- }
- void displayFood(const struct foodNode_S** food)
- {
- struct foodNode_S* temp = (*food);
- while (temp != NULL) {
- char c;
- if (temp->_value == 1) c = '@';
- else if (temp->_value == 2) c = '%';
- else if (temp->_value == 3) c = '$';
- else if (temp->_value == 4) c = '*';
- printf("%c%:%d;%d%;%d",c, temp->_point._x, temp->_point._y, temp->_point._z);
- temp = temp->_next;
- printf("\n");
- }
- }
- void moveSnake(struct point_S p, struct snakeNode_S** snake)
- {
- struct snakeNode_S* newHead = malloc(sizeof(struct snakeNode_S));
- newHead->_point = p;
- newHead->_next = *snake;
- snake = &newHead;
- while ((*snake)->_next->_next != NULL)
- (*snake) = (*snake)->_next;
- free((*snake)->_next->_next);
- (*snake)->_next = NULL;
- snake = &newHead;
- }
- void makeMove(char direction,
- struct snakeNode_S** snake,
- struct onstacleNode_S** obstacle,
- struct foodNode_S** food)
- {
- struct point_S p = (*snake)->_point;
- // possible directions E(+x) S(+y) D(+z) W(-x) N(-y) U(-z)
- switch (direction)
- {
- case 'E':
- p._x++;
- break;
- case 'S':
- p._y++;
- break;
- case 'D':
- p._z++;
- break;
- case 'W':
- p._x--;
- break;
- case 'N':
- p._y--;
- break;
- case 'U':
- p._z--;
- break;
- default:
- printf("ERROR: unknown direction!\n");
- }
- if(isFood(food, p))
- feedSnake(snake, food, p);
- else
- moveSnake(p, snake);
- }
- ///////////////////////////////////////////////////////////////
- // test functions
- ///////////////////////////////////////////////////////////////
- void testFoodFunctions()
- {
- struct point_S p[5];
- p[0]._x = 0; p[0]._y = 0; p[0]._z = 0;
- p[1]._x = 1; p[1]._y = 1; p[1]._z = 1;
- p[2]._x = 2; p[2]._y = 2; p[2]._z = 2;
- p[3]._x = 1; p[3]._y = 2; p[3]._z = 1;
- p[4]._x = 1; p[4]._y = 2; p[4]._z = 0;
- struct foodNode_S* food = NULL;
- ////////////////////////////////////////////////////////////////////
- printf("\nTest food functions start\n\n");
- ////////////////////////////////////////////////////////////////////
- int i;
- for (i = 0; i < 5; i++)
- {
- addFood(&food, p[i], i % 4 + 1);
- }
- displayFood(&food);
- if (isFood(&food, p[3]))
- {
- printf("p[3] = {%d, %d, %d} is food\n", p[3]._x, p[3]._y, p[3]._z);
- removeFood(&food, p[3]);
- displayFood(&food);
- }
- if (!isFood(&food, p[3]))
- {
- printf("p[3] = {%d, %d, %d} is NOT food\n\n", p[3]._x, p[3]._y, p[3]._z);
- }
- if (isFood(&food, p[1]))
- {
- printf("p[1] = {%d, %d, %d} is food\n", p[1]._x, p[1]._y, p[1]._z);
- removeFood(&food, p[1]);
- displayFood(&food);
- }
- if (!isFood(&food, p[1]))
- {
- printf("p[1] = {%d, %d, %d} is NOT food\n\n", p[1]._x, p[1]._y, p[1]._z);
- }
- if (isFood(&food, p[4]))
- {
- printf("p[4] = {%d, %d, %d} is food\n", p[4]._x, p[4]._y, p[4]._z);
- removeFood(&food, p[4]);
- displayFood(&food);
- }
- if (!isFood(&food, p[4]))
- {
- printf("p[4] = {%d, %d, %d} is NOT food\n\n", p[4]._x, p[4]._y, p[4]._z);
- }
- if (isFood(&food, p[2]))
- {
- printf("p[2] = {%d, %d, %d} is food\n", p[2]._x, p[2]._y, p[2]._z);
- removeFood(&food, p[2]);
- displayFood(&food);
- }
- if (!isFood(&food, p[2]))
- {
- printf("p[2] = {%d, %d, %d} is NOT food\n\n", p[2]._x, p[2]._y, p[2]._z);
- }
- if (isFood(&food, p[0]))
- {
- printf("p[0] = {%d, %d, %d} is food\n", p[0]._x, p[0]._y, p[0]._z);
- removeFood(&food, p[0]);
- displayFood(&food);
- }
- if (!isFood(&food, p[0]))
- {
- printf("p[0] = {%d, %d, %d} is NOT food\n\n", p[0]._x, p[0]._y, p[0]._z);
- }
- ////////////////////////////////////////////////////////////////////
- printf("\nTest food functions end\n\n");
- ////////////////////////////////////////////////////////////////////
- }
- int main(void)
- {
- struct snakeNode_S* snake = NULL;
- struct foodNode_S* food = NULL;
- struct obstacleNode_S* obstacles = NULL;
- struct point_S closestFood;
- char obstacleSign = '&';
- //scanf("%c", &obstacleSign);
- ////////////////////////////////////////////////////////////////////
- // tests start
- ////////////////////////////////////////////////////////////////////
- testFoodFunctions();
- ////////////////////////////////////////////////////////////////////
- // tests end
- ////////////////////////////////////////////////////////////////////
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement