C++貪喫蛇

效果極好…

#include<iostream>  
#include<cstdlib>//該函數主要可以提供一些函數與符號常量  
#include<algorithm>//提供大量基於迭代器的非成員模版函數  
#include<conio.h>//其中定義了通過控制檯進行數據輸入和數據輸出的函數  
#include<time.h>//日期和時間頭文件  
#include<windows.h>  
#define ML 100     
using namespace std;  
class Snake    //聲明一個類Snake  
{  
public:  
    int head,tail,body[200],length;  
};  
Snake T;              //定義一個對象T  
int map[100][100];      //定義一個整形的二維數組做標記用  
char maze[100][100];    //根據map數組的不同數值顯示不同符號,以此繪圖  
void init()         //初始化函數  
{  
    T.head=3;  
    T.tail=0;  
//  T.length=0;  
    memset(T.body,0,sizeof(T.body));  
}  
int card[800];  
char order;     //接收指令  
int getnum()  
{  
    static int n=0;  
    n++;  
    n=n%800;  
    return card[n];  
}  

void up()  
{  
    int sum,i;  
    sum=T.body[(T.head-1+ML)%ML]-100;  
    if(map[sum/100][sum%100]==1)  
    {  
        T.length++;  
        T.body[T.head++]=sum;  
        T.head%=ML;  
        map[sum/100][sum%100]=0;  
        for(i=T.tail;i!=T.head;)  
        {  
            sum=T.body[i];  
            map[sum/100][sum%100]=-1;  
            i++;  
            i%=ML;  
        }  
        while(1)  
        {  
            sum=getnum();  
            if(map[sum/100][sum%100]==0)  
            {  
                map[sum/100][sum%100]=1;  
                break;  
            }  
        }  
        for(i=T.tail;i!=T.head;)  
        {  
            sum=T.body[i];  
            map[sum/100][sum%100]=0;  
            i++;  
            i%=ML;  
        }  
    }  
    else  
    {  
        T.body[T.head++]=sum;  
        T.head%=ML;  
        T.tail=(++T.tail)%ML;  
    }  
}  
void down()  
{  
    int sum,i;  
    sum=T.body[(T.head-1+ML)%ML]+100;  
    if(map[sum/100][sum%100]==1)  
    {  
        T.length++;  
        T.body[T.head++]=sum;  
        T.head%=ML;  
        map[sum/100][sum%100]=0;  
        for(i=T.tail;i!=T.head;)  
        {  
            sum=T.body[i];  
            map[sum/100][sum%100]=-1;  
            i++;  
            i%=ML;  
        }  
        while(1)  
        {  
            sum=getnum();  
            if(map[sum/100][sum%100]==0)  
            {  
                map[sum/100][sum%100]=1;  
                break;  
            }  
        }  
        for(i=T.tail;i!=T.head;)  
        {  
            sum=T.body[i];  
            map[sum/100][sum%100]=0;  
            i++;  
            i%=ML;  
        }  
    }  
    else  
    {  
        T.body[T.head++]=sum;  
        T.head%=ML;  
        T.tail=(++T.tail)%ML;  
    }  
}  
void right()  
{  
    int sum,i;  
    sum=T.body[(T.head-1+ML)%ML]+1;  
    if(map[sum/100][sum%100]==1)  
    {  
        T.length++;  
        T.body[T.head++]=sum;  
        T.head%=ML;  
        map[sum/100][sum%100]=0;  
        for(i=T.tail;i!=T.head;)  
        {  
            sum=T.body[i];  
            map[sum/100][sum%100]=-1;  
            i++;  
            i%=ML;  
        }  
        while(1)  
        {  
            sum=getnum();  
            if(map[sum/100][sum%100]==0)  
            {  
                map[sum/100][sum%100]=1;  
                break;  
            }  
        }  
        for(i=T.tail;i!=T.head;)  
        {  
            sum=T.body[i];  
            map[sum/100][sum%100]=0;  
            i++;  
            i%=ML;  
        }  
    }  
    else  
    {  
        T.body[T.head++]=sum;  
        T.head%=ML;  
        T.tail=(++T.tail)%ML;  
    }  
}  
void left()  
{  
    int sum,i;  
    sum=T.body[(T.head-1+ML)%ML]-1;  
    if(map[sum/100][sum%100]==1)  
    {  
        T.length++;  
        T.body[T.head++]=sum;  
        T.head%=ML;  
        map[sum/100][sum%100]=0;  
        for(i=T.tail;i!=T.head;)  
        {  
            sum=T.body[i];  
            map[sum/100][sum%100]=-1;  
            i++;  
            i%=ML;  
        }  
        while(1)  
        {  
            sum=getnum();  
            if(map[sum/100][sum%100]==0)  
            {  
                map[sum/100][sum%100]=1;  
                break;  
            }  
        }  
        for(i=T.tail;i!=T.head;)  
        {  
            sum=T.body[i];  
            map[sum/100][sum%100]=0;  
            i++;  
            i%=ML;  
        }  
    }  
    else  
    {  
        T.body[T.head++]=sum;  
        T.head%=ML;  
        T.tail=(++T.tail)%ML;  
    }  
}  
int main()  
{  
    memset(map,0,sizeof(map));  
    memset(maze,' ',sizeof(maze));  
    int i,j,sum=0,k=0,temp;  
    for(i=1;i<20;i++)  
        for(j=1;j<40;j++)  
        {  
            card[k++]=i*100+j;  
        }  
    srand(time(0));  
    temp=rand()%10+1;  
    while(temp--)  
    random_shuffle(card,card+19*39);  
    for(i=0;i<=40;i++)  
        map[i][0]=map[20][i]=map[i][40]=map[0][i]=-1;  
    init();  
    T.length=1;  
    T.body[T.head++]=101;  
    T.head%=ML;  

    sum=getnum();  
    map[sum/100][sum%100]=1;  

    char jud='d';  
    int TM=300,start;  
    while(1)  
    {     
        (TM>100)?(TM=300-T.length*60):(TM=50);  
        start=clock();  
        //利用臨時無窮循環制作刷屏時間  
        while(clock()-start<=TM && !kbhit())           //kbhit() 檢查當前是否有鍵盤輸入,若有則返回一個非0值,否則返回0  
        {  
            ;  
        }  
        if(kbhit()&&(order=getch(),order=='w'||order=='s'||order=='a'||order=='d'))  
        {  

            sum=T.body[(T.head-1+ML)%ML];  
            system("CLS");  
            for(i=T.tail;i!=T.head;)  
            {  
                temp=T.body[i];  
                map[temp/100][temp%100]=-1;  
                i++;  
                i%=ML;  
            }  
            if(order=='w')  
            {  
                jud=order;  
                //cout<<"-->w\n";  
                if(map[sum/100-1][sum%100]!=-1)  
                {  
                    for(i=T.tail;i!=T.head;)  
                    {  
                        temp=T.body[i];  
                        map[temp/100][temp%100]=0;  
                        i++;  
                        i%=ML;  
                    }  
                    up();  
                }  
                else  
                {  
                    system("CLS");  
                    cout<<"============================================"<<endl;  
                    cout<<"       很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;  
                    cout<<"============================================"<<endl;  
                    break;  
                }  

            }  
            else if(order=='a')  
            {  
                jud=order;  
                //cout<<"-->a\n";  
                if(map[sum/100][sum%100-1]!=-1)  
                {  
                    for(i=T.tail;i!=T.head;)  
                    {  
                        temp=T.body[i];  
                        map[temp/100][temp%100]=0;  
                        i++;  
                        i%=ML;  
                    }  
                    left();  
                }  
                else  
                {  
                    system("CLS");  
                    cout<<"============================================"<<endl;  
                    cout<<"       很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;  
                    cout<<"============================================"<<endl;  
                    break;  
                }  
            }  
            else if (order=='s')  
            {  
                jud=order;  
                //cout<<"-->s\n";  
                if(map[sum/100+1][sum%100]!=-1)  
                {  
                    for(i=T.tail;i!=T.head;)  
                    {  
                        temp=T.body[i];  
                        map[temp/100][temp%100]=0;  
                        i++;  
                        i%=ML;  
                    }  
                    down();  
                }  
                else  
                {  
                    system("CLS");  
                    cout<<"============================================"<<endl;  
                    cout<<"       很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;  
                    cout<<"============================================"<<endl;  
                    break;  
                }  
            }  
            else if(order=='d')  
            {  
                jud=order;  
                //cout<<"-->d\n";  
                if(map[sum/100][sum%100+1]!=-1)  
                {  
                    for(i=T.tail;i!=T.head;)  
                    {  
                        temp=T.body[i];  
                        map[temp/100][temp%100]=0;  
                        i++;  
                        i%=ML;  
                    }  
                    right();  
                }  
                else  
                {  
                    system("CLS");  
                    cout<<"============================================"<<endl;  
                    cout<<"       很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;  
                    cout<<"============================================"<<endl;  
                    break;  
                }  
            }  
            memset(maze,' ',sizeof(maze));  
            for(i=0;i<=20;i++)  
                for(j=0;j<=40;j++)  
                {  
                    if(map[i][j]==-1)  
                        maze[i][j]='#';  
                    else if(map[i][j]==1)  
                        maze[i][j]='o';  
                }  
            for(i=T.tail;i!=T.head;)  
            {  
                sum=T.body[i];  
                maze[sum/100][sum%100]='*';  
                ++i;  
                i%=ML;  
            }  
            sum=T.body[(i-1+ML)%ML];  
            maze[sum/100][sum%100]='@';  
            cout<<"遊戲操作:  ↑:w   ↓:s   ←:a   →:d"<<endl;  
            cout<<"score: "<<T.length*10-10<<endl;  
            for(i=0;i<=20;i++)  
            {  
                for(j=0;j<=40;j++)  
                {  
                    cout<<maze[i][j];  
                }  
                cout<<endl;  
            }  
        }  
        else  
        {  
                sum=T.body[(T.head-1+ML)%ML];  
                system("CLS");  
                for(i=T.tail;i!=T.head;)  
                {  
                    temp=T.body[i];  
                    map[temp/100][temp%100]=-1;  
                    i++;  
                    i%=ML;  
                }  
                if(jud=='w')  
                {  
                //cout<<"-->w\n";  
                if(map[sum/100-1][sum%100]!=-1)  
                {  
                    for(i=T.tail;i!=T.head;)  
                    {  
                        temp=T.body[i];  
                        map[temp/100][temp%100]=0;  
                        i++;  
                        i%=ML;  
                    }  
                    up();  
                }  
                else  
                {  
                    system("CLS");  
                    cout<<"============================================"<<endl;  
                    cout<<"       很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;  
                    cout<<"============================================"<<endl;  
                    break;  
                }  

            }  
            else if(jud=='a')  
            {  
                //cout<<"-->a\n";  
                if(map[sum/100][sum%100-1]!=-1)  
                {  
                    for(i=T.tail;i!=T.head;)  
                    {  
                        temp=T.body[i];  
                        map[temp/100][temp%100]=0;  
                        i++;  
                        i%=ML;  
                    }  
                    left();  
                }  
                else  
                {  
                    system("CLS");  
                    cout<<"============================================"<<endl;  
                    cout<<"       很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;  
                    cout<<"============================================"<<endl;  
                    break;  
                }  
            }                                                         
            else if (jud=='s')  
            {  
                //cout<<"-->s\n";  
                if(map[sum/100+1][sum%100]!=-1)  
                {  
                    for(i=T.tail;i!=T.head;)  
                    {  
                        temp=T.body[i];  
                        map[temp/100][temp%100]=0;  
                        i++;  
                        i%=ML;  
                    }  
                    down();  
                }  
                else  
                {  
                    system("CLS");  
                    cout<<"============================================"<<endl;  
                    cout<<"       很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;  
                    cout<<"============================================"<<endl;  
                    break;  
                }  
            }  
            else if(jud=='d')  
            {  
                //cout<<"-->d\n";  
                if(map[sum/100][sum%100+1]!=-1)  
                {  
                    for(i=T.tail;i!=T.head;)  
                    {  
                        temp=T.body[i];  
                        map[temp/100][temp%100]=0;  
                        i++;  
                        i%=ML;  
                    }  
                    right();  
                }  
                else  
                {  
                    system("CLS");  
                    cout<<"============================================"<<endl;  
                    cout<<"       很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;  
                    cout<<"============================================"<<endl;  
                    break;  
                }  
            }  

            if(T.length>=20){  
                    system("CLS");  
                    cout<<"============================================"<<endl;  
                    cout<<"                 恭喜,過關!!! "<<endl;  
                    cout<<"============================================"<<endl;  
                    break;  
            }  

            memset(maze,' ',sizeof(maze));  
            for(i=0;i<=20;i++)  
                for(j=0;j<=40;j++)  
                {  
                    if(map[i][j]==-1)  
                        maze[i][j]='#';  
                    else if(map[i][j]==1)  
                        maze[i][j]='o';  
                }  
            for(i=T.tail;i!=T.head;)  
            {  
                sum=T.body[i];  
                maze[sum/100][sum%100]='*';  
                ++i;  
                i%=ML;  
            }  
            sum=T.body[(i-1+ML)%ML];  
            maze[sum/100][sum%100]='@';  
            cout<<"遊戲操作:  ↑:w   ↓:s   ←:a   →:d"<<endl;  
            cout<<"score: "<<T.length*10-10<<endl;  
            for(i=0;i<=20;i++)  
            {  
                for(j=0;j<=40;j++)  
                {  
                    cout<<maze[i][j];  
                }  
                cout<<endl;  
            }  
        }  

    }  
    system("pause");  
    return 0;  
}  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章