c语言随机生成字母链表,C语言实现链表贪吃蛇
时间: 2023-07-10 18:36:17 浏览: 156
生成字母链表可以使用C语言的随机数生成函数rand(),然后将生成的数字转换成字母即可。以下是生成长度为n的字母链表的示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Node {
char data;
struct Node *next;
} Node;
Node *createList(int n) {
Node *head = NULL;
Node *tail = NULL;
srand(time(NULL));
for (int i = 0; i < n; i++) {
Node *node = (Node *) malloc(sizeof(Node));
node->data = rand() % 26 + 'a';
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
}
return head;
}
void printList(Node *head) {
while (head != NULL) {
printf("%c ", head->data);
head = head->next;
}
printf("\n");
}
int main() {
Node *list = createList(10);
printList(list);
return 0;
}
```
接下来是实现链表贪吃蛇的示例代码。链表贪吃蛇可以通过一个链表来表示蛇的身体,每个节点表示一个身体部位,头节点表示蛇头,尾节点表示蛇尾。蛇的运动可以通过在链表头部添加一个节点来实现。以下是示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 30
#define HEIGHT 20
typedef struct Point {
int x;
int y;
} Point;
typedef struct Node {
Point data;
struct Node *next;
} Node;
typedef enum Direction {
UP, DOWN, LEFT, RIGHT
} Direction;
Node *createSnake(Point head) {
Node *node = (Node *) malloc(sizeof(Node));
node->data = head;
node->next = NULL;
return node;
}
void printSnake(Node *snake) {
while (snake != NULL) {
printf("(%d,%d) ", snake->data.x, snake->data.y);
snake = snake->next;
}
printf("\n");
}
void drawMap(Node *snake, Point food) {
system("cls");
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1) {
printf("#");
} else if (i == food.y && j == food.x) {
printf("$");
} else {
int found = 0;
Node *p = snake;
while (p != NULL) {
if (p->data.x == j && p->data.y == i) {
printf("*");
found = 1;
break;
}
p = p->next;
}
if (!found) {
printf(" ");
}
}
}
printf("\n");
}
}
int isCollided(Node *snake) {
Node *p = snake->next;
while (p != NULL) {
if (p->data.x == snake->data.x && p->data.y == snake->data.y) {
return 1;
}
p = p->next;
}
return 0;
}
int isInMap(Point point) {
return point.x > 0 && point.x < WIDTH - 1 && point.y > 0 && point.y < HEIGHT - 1;
}
Point generateFood(Node *snake) {
Point food;
do {
food.x = rand() % (WIDTH - 2) + 1;
food.y = rand() % (HEIGHT - 2) + 1;
} while (!isInMap(food) || isCollided(snake));
return food;
}
void moveSnake(Node **snake, Direction direction, Point food, int *score) {
Point head = (*snake)->data;
switch (direction) {
case UP:
head.y--;
break;
case DOWN:
head.y++;
break;
case LEFT:
head.x--;
break;
case RIGHT:
head.x++;
break;
}
Node *node = (Node *) malloc(sizeof(Node));
node->data = head;
node->next = *snake;
*snake = node;
if (head.x == food.x && head.y == food.y) {
food = generateFood(*snake);
*score += 10;
} else {
Node *tail = *snake;
while (tail->next->next != NULL) {
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
drawMap(*snake, food);
}
int main() {
srand(time(NULL));
Point head = {WIDTH / 2, HEIGHT / 2};
Node *snake = createSnake(head);
Direction direction = RIGHT;
Point food = generateFood(snake);
int score = 0;
while (1) {
if (_kbhit()) {
char ch = _getch();
if (ch == 'w' && direction != DOWN) {
direction = UP;
} else if (ch == 's' && direction != UP) {
direction = DOWN;
} else if (ch == 'a' && direction != RIGHT) {
direction = LEFT;
} else if (ch == 'd' && direction != LEFT) {
direction = RIGHT;
}
}
moveSnake(&snake, direction, food, &score);
if (!isInMap(snake->data) || isCollided(snake)) {
printf("Game Over! Score: %d\n", score);
break;
}
Sleep(100);
}
return 0;
}
```
代码中使用了Windows系统的控制台函数和键盘输入函数,需要在Windows系统下运行。
阅读全文