| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1666 人关注过本帖
标题:C++贪食蛇,求大神给下详细注释
只看楼主 加入收藏
小虫笨
Rank: 2
等 级:论坛游民
威 望:3
帖 子:8
专家分:10
注 册:2018-5-30
收藏
 问题点数:0 回复次数:3 
C++贪食蛇,求大神给下详细注释
程序代码:
#include <iostream>
#include <conio.h>
#include <ctime>
#include <windows.h>
using namespace std;
const int ROW = 20;
const int line = 35;
class snake;
class Node
{
private:
    Node() :x(0), y(0), llink(NULL), next(NULL){}
    Node *llink, *next;
    int x, y;
    friend class snake;
};
class snake
{
public:
    snake();
    ~snake();
    void move();
    void chioce_key(char);
    void output();
private:
    Node *head, *tail;
    int game[ROW][line];
    enum { UP, DOWN, RIGHT, LEFT }point;
    int food_x, food_y;
    void get_food();
    void gotoxy(int, int);
    void add_head(int, int);
    void delete_tail();
};
snake::snake() :food_x(0), food_y(0), head(NULL), tail(NULL), point(RIGHT)
{
    int i, j;
    for (i = 0; i<ROW; ++i)
    {
        for (j = 0; j<line; ++j)
        {
            game[i][0] = game[i][line - 1] = 1;
            game[0][j] = game[ROW - 1][j] = 1;
        }
    }
    head = tail;
    add_head(6, 4);
    add_head(6, 5);
    add_head(6, 6);
    get_food();
}
snake::~snake()
{
    while (head)
    {
        Node *temp = head;
        head = head->next;
        delete temp;
    }
}
void snake::add_head(int a, int b)
{
    Node *temp = new Node;
    temp->x = a;
    temp->y = b;
    if (head == NULL)
    {
        head = tail = temp;
    }
    else
    {
        head->llink = temp;
        temp->next = head;
        head = head->llink;
    }
    game[a][b] = 1;
}
void snake::delete_tail()
{
    Node *temp = tail;
    game[tail->x][tail->y] = 0;
    tail = tail->llink;
    tail->next = NULL;
    delete temp;
}
void snake::chioce_key(char key)
{
    switch (key)
    {
    case 'w':
    case 'W':point = UP; break;
    case 'a':
    case 'A':point = LEFT; break;
    case 'd':
    case 'D':point = RIGHT; break;
    case 's':
    case 'S':point = DOWN; break;
    }
}
void snake::output()
{
    int i, j;
    gotoxy(0, 0);
    for (i = 0; i<ROW; ++i)
    {
        for (j = 0; j<line; ++j)
        {
            if (game[i][j] == 1)
            {
                cout << "* ";
            }
            else
            {
                cout << "  ";
            }
        }
        cout << endl;
    }
}
void snake::get_food()
{
    srand(unsigned(time(NULL)));
    do
    {
        food_x = rand() % 18 + 1;
        food_y = rand() % 18 + 1;
    } while (game[food_x][food_y] == 1);
    game[food_x][food_y] = 1;
}
void snake::move()
{
    int a = head->x;
    int b = head->y;
    switch (point)
    {
    case UP:--a; break;
    case DOWN:++a; break;
    case LEFT:--b; break;
    case RIGHT:++b; break;
    }
    if (game[a][b] == 1 && food_x != a&&food_y != b)
    {
        output();
        cout << "失败了" << endl;
        this->~snake();
        exit(0);
    }
    if (food_x == a&&food_y == b)
    {
        add_head(a, b);
        get_food();
        return;
    }
    add_head(a, b);
    delete_tail();
}
void snake::gotoxy(int x, int y)
{
    HANDLE hOutput;
    COORD loc;
    loc.X = x;
    loc.Y = y;
    hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOutput, loc);
}
int main()
{
    snake s;
    s.output();
    cout << "操作方法:W ↑ S ↓ A ← D →\n";
    while (true)
    {
        char keydown = _getch();
        s.chioce_key(keydown);
        while (!_kbhit())
        {
            s.move();
            s.output();
            Sleep(100);
        }
    }
    return 0;
}
搜索更多相关主题的帖子: int Node void head case 
2018-06-22 22:24
小虫笨
Rank: 2
等 级:论坛游民
威 望:3
帖 子:8
专家分:10
注 册:2018-5-30
收藏
得分:0 
大神们给写个注释呀。。老师给的简单源码。完全看不懂呀。
2018-06-24 21:00
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
一行都不懂?

剑栈风樯各苦辛,别时冰雪到时春
2018-06-24 22:43
小虫笨
Rank: 2
等 级:论坛游民
威 望:3
帖 子:8
专家分:10
注 册:2018-5-30
收藏
得分:0 
回复 3楼 林月儿
只认得构造函数,析构函数。其他不懂
2018-06-27 19:19
快速回复:C++贪食蛇,求大神给下详细注释
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.050358 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved