关于链表和数组问题
在链表中存储的有效数据是一个笑脸,怎么让它输出时是呈现在一个二维数组中呢?
回复 2楼 林月儿
就是比如说:贪吃蛇身体的是一个个笑脸组成,分别存储在各个结点中,我怎么让它在程序运行的界面上输出来?界面上不是一个二维数组吗,怎么让链表的内容呈现在数组上呢?
#include<stdio.h> //哥哥,这是我仿照你写的。。其他的没毛病,可是我的按键检测函数好像不管用。。。怎么回事??? #include<windows.h> #include<conio.h> #include<time.h> #include<stdlib.h> #include<string.h> struct Snake { int x; int y; struct Snake * pnext; }; struct Food { int x; int y; }; struct Direction { char direction; }; char ch = 1; char tn = 3; int stop = 0; char direction; struct Food * food; struct Direction * headir; struct Snake * head,* tail; //建立头指针 struct Snake * p; //遍历指针 void ZHUYE(); //绘制主界面函数 void HideCursor(); //隐藏光标函数 void gotoxy(int x,int y); //移动光标函数 void creat_snake(); //初始化蛇身函数 void draw_snake(); //绘制蛇身 void creat_food(); //产生食物函数 void creat_node(); //添加头结点函数 void shanchu_tail(); //删除尾结点函数 void gameover(); //游戏结束判断函数 int selfpan(); //判断是否咬到自身 void snake_move(); //蛇身移动函数 void keyjiance(); //按键检测函数 void main() { ZHUYE(); //主界面 while(!stop) { keyjiance(); snake_move(); draw_snake(); Sleep(500); gameover(); } } void ZHUYE() //绘制主页面函数 { HideCursor(); int i,j; for(i = 0; i < 40; i++) { gotoxy(i,0); printf("-"); gotoxy(i,14); printf("-"); } for(j = 0; j < 15; j++) { gotoxy(0,j); printf("|"); gotoxy(39,j); printf("|"); } gotoxy(12,7); printf("按任意键开始!"); _getch(); gotoxy(12,7); printf(" "); creat_snake(); creat_food(); } void HideCursor() //隐藏光标 { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void gotoxy(int x,int y) //移动光标 { HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE); COORD loc={x,y}; SetConsoleCursorPosition(hOut,loc); } void creat_snake() //蛇身初始化 { int i; struct Snake * tail; tail = (struct Snake *)malloc(sizeof(struct Snake)); headir = (struct Direction *)malloc(sizeof(struct Direction)); food = (struct Food *)malloc(sizeof(struct Food)); if(tail == NULL) { exit (-1); } tail -> x = 10; tail -> y = 6; tail -> pnext = NULL; headir -> direction = 'd'; for(i = 1; i <= 4; i++) { head = (struct Snake *)malloc(sizeof(struct Snake)); head -> pnext = tail; head -> x = 10 + i; head -> y = 6; tail = head; } head = tail; draw_snake(); } void draw_snake() //绘制蛇身结点函数 { struct Snake * temp; temp = head; while(temp != NULL) { gotoxy(temp -> x,temp -> y); printf("%c",ch); temp = temp -> pnext; } } void creat_food() { struct Snake * temp; temp = head; srand(time(NULL)); food -> x = rand()%38+1; food -> y = rand()%13+1; while(temp != NULL) { if(food -> x == temp -> x&&food -> y == temp -> y) { free(food); creat_food(); } temp = temp -> pnext; } gotoxy(food -> x,food -> y); printf("%c",tn); } void creat_node(int x,int y) //增添头结点 { struct Snake * p2; p2 = (struct Snake *)malloc(sizeof(struct Snake)); p2 -> pnext = head; p2 -> x = x; p2 -> y = y; head = p2; gameover(); } void shanchu_tail() //删除尾结点函数定义 { struct Snake * temp; p = head; while(p -> pnext != NULL) { temp = p; p = p -> pnext; } gotoxy(p -> x,p -> y); printf(" "); temp -> pnext = NULL; free(p); } int selfpan() //是否咬到自身 { struct Snake * temp; temp = head -> pnext; while(temp != NULL) { if(temp -> x == head -> x&& temp -> y == head -> y) return 1; temp = temp -> pnext; } } void gameover() //判断游戏结束 { struct Snake * temp; temp = head -> pnext; if(selfpan() == 1) { stop = 1; system("cls"); gotoxy(18,7); printf("游戏结束!"); gotoxy(8,18); printf(" "); } if(head -> x <1 || head -> x > 38 || head -> y <1 || head -> y >13) { stop = 1; system("cls"); gotoxy(18,7); printf("游戏结束!"); gotoxy(8,18); printf(" "); } } void snake_move() //蛇身移动函数定义 { if(headir -> direction == 'w') { if(food -> x == head -> x && food -> y == head -> y) { gotoxy(food -> x,food -> y); printf(" "); creat_node(head -> x,head -> y -1); creat_food(); } else { creat_node(head -> x,head -> y -1); shanchu_tail(); } } if(headir -> direction == 's') { if(food -> x == head -> x && food -> y == head -> y) { gotoxy(food -> x,food -> y); printf(" "); creat_node(head -> x,head -> y + 1); creat_food(); } else { creat_node(head -> x,head -> y + 1); shanchu_tail(); } } if(headir -> direction == 'd') { if(food -> x == head -> x && food -> y == head -> y) { gotoxy(food -> x,food -> y); printf(" "); creat_node(head -> x + 1,head -> y); creat_food(); } else { creat_node(head -> x + 1,head -> y); shanchu_tail(); } } if(headir -> direction == 'a') { if(food -> x == head -> x && food -> y == head -> y) { gotoxy(food -> x,food -> y); printf(" "); creat_node(head -> x -1,head -> y); creat_food(); } else { creat_node(head -> x - 1,head -> y); shanchu_tail(); } } } void keyjiance() //按键检测函数定义 { char key; if(_kbhit()) { key = getch(); switch(key) { case 'w' : if(headir -> direction == 's') break; else headir -> direction == 'w';break; case 's' : if(headir -> direction == 'w') break; else headir -> direction == 's';break; case 'd' : if(headir -> direction == 'a') break; else headir -> direction == 'd';break; case 'a' : if(headir -> direction == 'd') break; else headir -> direction == 'a';break; default : break; } } }