首页 > 代码库 > 贪吃蛇

贪吃蛇

 

  1 #define WIDTH 100
  2 #define HEIGHT 100
  3 
  4 typedef enum Direct {
  5     UP,
  6     DOWN,
  7     LEFT,
  8     RIGHT,
  9     NO_DIRECT
 10 }Direct;
 11 
 12 typedef struct Point {
 13     int x;
 14     int y;
 15     Point *next;
 16 }Point;
 17 
 18 typedef struct Snake {
 19     Point *head;
 20     Point *tail;
 21     int length;
 22     Direct direct;
 23 }Snake;
 24 
 25 const int width = WIDTH;
 26 const int height = HEIGHT;
 27 
 28 Point* NewPoint()
 29 {
 30     Point *np = (Point*)malloc(sizeof(Point));
 31     if(np != NULL)
 32     {
 33         memset(np, 0, sizeof(Point));
 34     }
 35     return np;
 36 }
 37 
 38 Point* NewPointWithParam(int x, int y)
 39 {
 40     Point *np = NewPoint();
 41     if(np != NULL)
 42     {
 43         np->x = x;
 44         np->y = y;
 45     }
 46     return np;
 47 }
 48 
 49 Direct ForwardOneStep(const Snake *snake, const Point *food)
 50 {
 51     if(snake == NULL || food == NULL) return NO_DIRECT;
 52     Direct dir = NO_DIRECT;
 53     Point *head = snake->head;
 54     if(head == NULL) return NO_DIRECT;
 55     
 56     switch(snake->direct)
 57     {
 58         case UP:
 59             if(head->x == 0) dir = RIGHT;
 60             else if(head->x == width-2) dir = LEFT;
 61             break;
 62         case DOWN:
 63             if(head->x == width - 1)
 64             {
 65                 if(head->y == height - 1) dir = LEFT;
 66                 else dir = DOWN;
 67             }
 68             break;
 69         case LEFT:
 70             if(head->x == 0) dir == UP;
 71             else dir = LEFT;
 72             break;
 73         case RIGHT:
 74             if(head->y == 0 && head->x == width - 1) dir = DOWN;
 75             else if(head->y != 0 && head->x == width - 2) dir = UP;
 76             else dir = RIGHT;
 77             break;
 78         default:
 79             break;
 80     }
 81 
 82     return dir;
 83 }
 84 
 85 Point *CreateOneFood(const Snake *snake)
 86 {    
 87     if(snake == NULL) return NULL;
 88     
 89     static int area[WIDTH][HEIGHT];
 90     Point *np = NULL;
 91     Point *food = NULL;
 92     int unusedNum = width*height - snake->length;
 93     int randPos;
 94     int x, y;
 95     int foodX = -1, foodY = -1;
 96     memset(area, 0, sizeof(area));
 97     np = snake->head;
 98     while(np != NULL)
 99     {
100         area[np->x][np->y] = 1;
101     }
102     randPos = rand() % unusedNum;
103     for(x=0; x<width; x++)
104     {
105         if(randPos < 0) break;
106         for(y=0; y<height; y++)
107         {
108             if(area[x][y] == 0) randPos--;
109             if(randPos < 0)
110             {
111                 foodX = x;
112                 foodY = y;
113                 break;
114             }
115         }
116     }
117 
118     if((foodX >= 0 && foodX < width) && (foodY >= 0 && foodY < height))
119     {
120         food = NewPointWithParam(foodX, foodY);
121     }
122     
123     return food;
124 }

 

贪吃蛇