贪吃蛇(链表指针)

来源:互联网 发布:问卷调查自动填写软件 编辑:程序博客网 时间:2024/04/29 06:13

#include

#include

#include

#include

#include "keyboard.h"



int width = 60;

int height = 22;


void drawInterface() {

    for (int i = 0; i < height; i += height - 1) {

        for (int j = 0; j < width; j++) {

            gotoxy(1+i, 1+j);

            printf("-\n");

        }

    }

    for (int i = 0; i < width; i += width - 1) {

        for (int j = 0; j < height - 2; j++) {

            gotoxy(2+j, 1+i);

            printf("|\n");

        }

    }

}

struct Food {

    int row;

    int col;

    

};

struct Node {

    int row;

    int col;

    struct Node *next;

};


void drawSnake(struct Node *tail) {

    while (tail != NULL) {

        gotoxy(tail->row, tail->col);

        printf("*\n");

        

        tail = tail->next;

    }

}

struct Node *head = NULL;

struct Node *tail = NULL;

int snakeHp = 3;


enum Direction {

    Up, Left, Down, Right

};

enum Direction dir = Right;


//栈区:函数开始运行时就去分配空间,函数结束时就去释放空间

//数据区:程序开始运行时就去分配空间,程序结束时就去释放空间

//堆区:你想申请空间就去申请分配,你想释放就去释放


void checkDirection() {

    

    int res = kbhit();

    if (res > 0) {

        char ch = getchar();

        if (ch == 'w') {

            if (dir == Down) {

                return;

            }

            dir = Up;

        }

        else if (ch == 'a') {

            if (dir == Right) {

                return;

            }

            dir = Left;

        }

        else if (ch == 's') {

            if (dir == Up) {

                return;

            }

            dir = Down;

        }

        else if (ch == 'd') {

            if (dir == Left) {

                return;

            }

            dir = Right;

        }

    }

    

    

}


void deleteTwoNode() {

    if (tail == NULL) {

        return;

    }

    struct Node *p = tail;

    struct Node *pnext = p->next;

    if (pnext == NULL) {

        gotoxy(p->row, p->col);

        printf(" \n");

        free(p);

        p = NULL;

        head = NULL;

        tail = NULL;

    }

    else {

        tail = pnext->next;

        gotoxy(p->row, p->col);

        printf(" \n");

        gotoxy(pnext->row, pnext->col);

        printf(" \n");

        free(pnext);

        pnext = NULL;

        free(p);

        p = NULL;

        if (tail == NULL) {

            head = NULL;

        }

    }

    

}


void reverseSnake() {

    if (tail == NULL) {

        return;

    }

    struct Node *p = tail;

    struct Node *pn = p->next;

    if (pn == NULL) {

        return;

    }

    struct Node *pnn = pn->next;

    p->next = NULL;

    

    while (pnn != NULL) {

        pn->next = p;

        p = pn;

        pn = pnn;

        pnn = pnn->next;

    }

    pn->next = p;

    

    head = tail;

    tail = pn;

    

    

}


void changeDirection() {

    if (dir == Left) {

        dir = Right;

    }

    else if (dir == Right) {

        dir = Left;

    }

    else if (dir == Up) {

        dir = Down;

    }

    else if (dir == Down) {

        dir = Up;

    }

}

void retainNodeBefore(struct Node *check) {

    struct Node *delete = tail;

    struct Node *deleteN = tail->next;

    while (delete != check) {

        gotoxy(delete->row, delete->col);

        printf(" \n");

        free(delete);

        delete = deleteN;

        deleteN = deleteN->next;

    }

    gotoxy(delete->row, delete->col);

    printf(" \n");

    tail = deleteN;

}

int moveSnake(struct Food *food) {

    static int fraction = 0;

    gotoxy(1165);

    printf("分数:%d\n", fraction);

    gotoxy(1265);

    printf("Hp:%d\n"snakeHp);

    if (tail == NULL && head == NULL) {

        return -1;

    }

    else {

        int oldRow = tail->row;

        int oldCol = tail->col;

        struct Node *p = tail;

        tail = tail->next;

        

        gotoxy(p->row, p->col);

        printf(" \n");

        if (tail == NULL) {

            tail = p;

        }

        head->next = p;

        p->next = NULL;

        

        switch (dir) {

            case Right:

                p->col = head->col + 1;

                p->row = head->row;

                break;

            case Left:

                p->col = head->col - 1;

                p->row = head->row;

                break;

            case Up:

                p->row = head->row - 1;

                p->col = head->col;

                break;

            case Down:

                p->row = head->row + 1;

                p->col = head->col;

                break;

                

            default:

                break;

        }

        

        

        head = p;

        

        

        gotoxy(p->row, p->col);

        printf("*\n");

        struct Node *check = tail;

        //蛇是否吃到食物

        if (head->row == food->row && head->col == food->col) {

            //产生新的食物

            fraction++;

            food->row = rand() % (height-3) + 2;

            food->col = rand() % (width-3) + 2;

            

            for (int i = 0; check != NULL; i++) {

                if (check->row == food->row && check->col == food->col) {

                    food->row = rand() % (height-3) + 2;

                    food->col = rand() % (width-3) + 2;

                    i = 0;

                    check = tail;

                }

                else {

                    check = check->next;

                }

            }

            gotoxy(food->row, food->col);

            printf("@\n");

            struct Node *pNode = (struct Node*)malloc(sizeof(struct Node));

            pNode->next = tail;

            pNode->row = oldRow;

            pNode->col = oldCol;

            tail = pNode;

            gotoxy(pNode->row, pNode->col);

            printf("*\n");

            

        }

        //判断Game Over

        if (head->col >= width || head->col <= 1 || head->row>= height || head->row <= 1 ) {

            if (snakeHp > 0) {

                //删去两节

                deleteTwoNode();

                reverseSnake();

                changeDirection();

                fraction = fraction/3;

                snakeHp--;

                if (tail == NULL && head == NULL) {

                    printf("Game Over!\n");

                    return -1;

                }

                else {

                    return 0;

                }

                

            }

            else {

                printf("Game Over!\n");

                return -1;

            }

            

        }

        check = tail;

        for (int i = 0; check != NULL; i++) {

            if (check == head) {

                break;

            }

            if (check->row == head->row && check->col == head->col) {

                retainNodeBefore(check);

                snakeHp--;

                fraction = fraction/3;

                if (snakeHp <= 0) {

                    printf("Game Over!\n");

                    return -1;

                }


                

            }

            check = check->next;

        }

    }

    

    return 0;

}

void produceSnake() {

    struct Node *node1 = (struct Node *)malloc(sizeof(structNode));

    node1->row = height/2;

    node1->col = 20;

    node1->next = NULL;

    struct Node *node2 = (struct Node *)malloc(sizeof(structNode));

    node2->row = height/2;

    node2->col = 19;

    node2->next = node1;

    struct Node *node3 = (struct Node *)malloc(sizeof(structNode));

    node3->row = height/2;

    node3->col = 18;

    node3->next = node2;

    struct Node *node4 = (struct Node *)malloc(sizeof(structNode));

    node4->row = height/2;

    node4->col = 17;

    node4->next = node3;


    tail = node4;

    head = node1;

}

int main(int argc, const char * argv[]) {

    

    system("clear screen");

    init_keyboard();

    srand((unsigned int)time(0));

    //画游戏界面

    drawInterface();

    //画蛇

    produceSnake();

    drawSnake(tail);

    

    //产生食物

    struct Food food;

    food.row = rand() % (height-3) + 2;

    food.col = rand() % (width-3) + 2;

    gotoxy(food.row, food.col);

    printf("@\n");

    //checkOnSnake(tail, food);

    //蛇不停移动


    while (1) {

        

        checkDirection();

        int res = moveSnake(&food);

        if (res < 0) {

            break;

        }

        

    }

    

    return 0;

}


转载自:http://blog.sina.com.cn/s/blog_5e3795060102vwyw.html

0 0
原创粉丝点击