吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 7345|回复: 21
收起左侧

[C&C++ 原创] C++ 贪吃蛇源码分享【动态空间+链表】

  [复制链接]
yep96 发表于 2016-10-10 21:29
本帖最后由 yep96 于 2016-11-4 21:04 编辑

这是上个学期的大作业,源码除了方向键控制外没有其他参考,应该和网上其他的源码区别挺大的吧。可以参考一下,有什么建议在好不过的了
[Asm] 纯文本查看 复制代码
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<conio.h>
#include <windows.h>
using namespace std;
int eat_food = 0;
float score = 0;
short int grade = 0;
short int timeout = 700;
int step = 0;
short int derect=77;//////////上下左右 asc

class SNAKE
{
        short int x;
        short int y;
        SNAKE* Qian;
        SNAKE* Hou;
public:
        SNAKE(){x = 0; y = 0; Qian = NULL; Hou = NULL;}
        SNAKE(int a, int b){x = a; y =b; Qian = NULL; Hou = NULL;}
        void change(int a ,int b){if(a!=-1)x = a; if(b!=-1)y = b;}
        short int getx(){return x;}
        short int gety(){return y;}
        SNAKE* qian(){return Qian;}
        SNAKE* qian(SNAKE* a){Qian=a;return Qian;}
        SNAKE* hou(){return Hou;}
        SNAKE* hou(SNAKE* a){Hou=a;return Hou;}
};

class FOOD
{
public:
        short int x;
        short int y;
};
FOOD food;

SNAKE* end;//尾巴地址
SNAKE* head;///头地址

bool move(int,int,short int **);
bool randfood(int, int , short int &,short int &,short int **);
int main()
{
        cout << "\n\t贪吃蛇\n\n\n\t\t Powered by xxxxxxx(差点忘了删)";
        short int i,j, a, b;
        cout<<"请输入行数:";
        cin>>a; cin.sync(); cin.clear();
        cout<<"请输入列数:";
        cin>>b; cin.sync(); cin.clear();
        a+=2;b+=2;
        short int **point;
        point = new short int* [a]; //////-1 墙     0 空     1 身子        2 食物       3头
        for(i=0; i<a ;++i)
                point[i]=new short int[b];

        for(i = 1; i < a-1; ++i)
                for(j = 1; j < b-1; ++j)
                        point[i][j] = 0;

        for(i = 0; i < a; ++i)
                point[i][b-1]=point[i][0]=-1;
        for(i = 0; i < b; ++i)
                point[0][i]=point[a-1][i]=-1;

        cout << " 游戏马上开始,请按任意键继续\n PS:可随时以cmd选择模式暂停\n\n";
        system("pause"); 

        end = new SNAKE;
        SNAKE* save_end = end;
        for(i = 1; i < (a-2)*(b-2); ++i)
        {
                head = new SNAKE;        
                end -> qian(head);
                head -> hou(end);
                end = head;
        }

        head -> qian(save_end);
        save_end -> hou(head);
        head=head->qian();/////////////形成a*b的环状双向链表,已验证

        end->change(a/2, b/2-1);
        head->change(a/2, b/2);
        point[a/2][b/2-1]=1;
        point[a/2][b/2]=3;

        randfood(a, b, food.x,food.y,point);
        while(move(a,b,point));///传值边界含外框

        for(i=0;i<a;++i)
                delete[] point[i];
        delete[] point;
        SNAKE* temp = head;
        for(i = 0; i < (a-2)*(b-2); ++i)
        {
                temp = head -> qian();
                delete head;
                head = temp;
        }////////////delete

        system("pause");
		return 0;
}


bool randfood(int M, int N, short int &x,short int &y,short int **point)///////false时为全满,赢
{
        int i,j;
        srand(unsigned(time(0)));
        x=rand()%(M-2)+1;
        y=rand()%(N-2)+1;
        i=x;
        j=y;
        for(++x;;++x)
        {
                if(x==M-1)
                        x-=M-2;
                for(++y;;++y)
                {
                        if(y==N-1)
                                y-=N-2;
                        if(point[x][y]==0)
                        {
                                point[x][y]=2;
                                return true;
                        }
                        if(j==y)        break;                
                }
                if(i==x)        return false;
        }
}


bool move(int M, int N, short int **point)
{
        score=eat_food*10+step*0.1;
        grade=score/100+1;
        switch(grade)
        {
                case 2 : if(timeout==700){timeout=550;system("cls");cout<<"\n\n\n\n\t\t恭喜升级!!\n\n";system("pause");}break;
                case 4 : if(timeout==550){timeout=400;system("cls");cout<<"\n\n\n\n\t\t恭喜升级!!\n\n";system("pause");}break;
                case 7 : if(timeout==400){timeout=300;system("cls");cout<<"\n\n\n\n\t\t恭喜升级!!\n\n";system("pause");}break;
                case 11: if(timeout==300){timeout=200;system("cls");cout<<"\n\n\n\n\t\t恭喜升级!!\n\n";system("pause");}break;
        }

        system("cls");
        cout<<" 步数:"<<++step<<"   吃了:"<<eat_food<<"   分数:"<<(int)score<<"   等级:"<<grade<<"   时间间隔"<<timeout<<"ms"<<endl;
        int i,j,time_1;
        for(i=0;i<M;++i)
        {
                cout<<'\t';
                for(j=0;j<N;++j)
                {
                        if(point[i][j]==0)
                                cout<<"  ";
                        if(point[i][j]==1)
                                cout<<"▉";
                        if(point[i][j]==-1)
                                cout<<"▓";
                        if(point[i][j]==3)
                                cout<<"◆";
                        if(point[i][j]==2)
                                cout<<"●";
                }
                cout<<endl;
        }

        int temp,key;
        time_1=clock();
        while(clock()-time_1<=timeout)
        {
                key=kbhit();/////////////////键盘输入时返回1
                Sleep(50);
                if(key!=0 && getch()==224)
                {

                        temp=getch();
                        if((temp==80&&head->getx()+1!=head->hou()->getx())||(temp==72&&head->getx()-1!=head->hou()->getx())||(temp==75&&head->gety()-1!=head->hou()->gety())||(temp==77&&head->gety()+1!=head->hou()->gety()))
                        {        derect=temp;        break;}
                }
        }
        switch(derect)
        {
                case 80 : head->qian()->change(head->getx()+1, head->gety()); break;//xia
                case 75 : head->qian()->change(head->getx(), head->gety()-1); break;//zuo
                case 77 : head->qian()->change(head->getx(), head->gety()+1); break;//you
                case 72 : head->qian()->change(head->getx()-1, head->gety()); break;//shang
        }
        head = head ->qian();
        if(point[(int)head->getx()][(int)head->gety()]!=2)
        {
                point[end->getx()][end->gety()]=0;
                end = end ->qian();
        }
        else
        {
                ++eat_food;
                if(randfood(M, N, food.x,food.y,point)==0)
                {
                        cout<<"\n\n\t\tYou Win!!!\n\n";//////////////赢了
                        system("color 1e");
                        return false;
                }
        }

        if(point[head->getx()][head->gety()]==1||point[head->getx()][head->gety()]==-1)
        {
                cout<<"\n\n\t\tGame Over!\n\n";/////////////////输了
                system("color 1C");
                return false;        
        }
        else
        {
                point[head->getx()][head->gety()]=3;
                point[head->hou()->getx()][head->hou()->gety()]=1;
                return true;
        }
}
[i][b][i][i][i][i][i][i][i][i][i][i][i]




免费评分

参与人数 3热心值 +3 收起 理由
zxlk1996 + 1 热心回复!
loooooooong + 1 用心讨论,共获提升!
普通用户 + 1 求看回帖链表怎么做

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| yep96 发表于 2016-10-10 22:44
普通用户 发表于 2016-10-10 22:00
这个链表怎么做啊- -

1.jpg 2.jpg 3.jpg 4.jpg
我的教材,用的是类模板,可以参考一下。我上面贪吃蛇的链表老师应该不会喜欢。。
哪里卡着我可以帮忙看看。全部写出来,,我很懒的
 楼主| yep96 发表于 2016-10-10 22:29
普通用户 发表于 2016-10-10 22:00
这个链表怎么做啊- -

....都不想再写代码了
我的链表代码是自己编的,可能和老师教的听不一样的
一般来说,习惯于用class List和class Node。List用于创建、添加、修改和删除等操作,Node作为储存空间,添加List为friend,当然也可以用各种函数传参,但太过麻烦
王雷轰 发表于 2016-10-10 21:47
不苦小和尚 发表于 2016-10-10 21:59
学习一下面向对象的思想
普通用户 发表于 2016-10-10 22:00
mmexport1476107840916.jpg 这个链表怎么做啊- -
me乄too 发表于 2016-10-10 22:13
我试了一下  运行不了啊   45行出错了
 楼主| yep96 发表于 2016-10-10 22:20
me乄too 发表于 2016-10-10 22:13
我试了一下  运行不了啊   45行出错了

我习惯于void main()
但是更正规的是int main(),而且g++编译不认void main()
 楼主| yep96 发表于 2016-10-10 22:21
me乄too 发表于 2016-10-10 22:13
我试了一下  运行不了啊   45行出错了

你用的是linux?不对,Linux没有windows.h头文件。。。
撒旦asd 发表于 2016-10-10 22:26
想知道楼主编c++用的什么编译器呀?
 楼主| yep96 发表于 2016-10-10 22:30
撒旦asd 发表于 2016-10-10 22:26
想知道楼主编c++用的什么编译器呀?

VS 2015 ent。我记得vc6.0好像也能编译
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-25 14:01

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表