C++簡單貪吃蛇實現

來源:互聯網
上載者:User

標籤:貪吃蛇

#include <iostream>#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include  <deque>#include <list>#define _SIZE_ 30#define _SET_ cout<<"\033[?25l\033[0m"static int flags = 0;typedef int Array[_SIZE_][_SIZE_]; using namespace std;struct Pos{    int x;    int y;    Pos(int X,int Y):x(X),y(Y){}};class Grial{    public:    Grial(Array a)    {        for(int i=0;i<_SIZE_;i++)        {            for(int j=0;j<_SIZE_;j++)            {                ar[i][j] = a[i][j];            }        }    }    void Init(Pos start)    {        int i = 0;        int j = 0;        char ch;    deque<Pos> Q;    list<Pos> Mlist;    ar[start.x][start.y]    = 1;    Q.push_front(start);    while(1)        {      system("stty raw -echo");//改變終端控制,      //輸入一個字元就立刻相應,將原來的\n作為結束的標誌,      //改為輸入一個字元就立即返回。        ch = getchar();        system("stty -raw echo");        //讓終端變回正常。        switch(ch)        {            case ‘w‘:                stateUp(Q);                     system("clear");            Printf();                    break;            case ‘a‘:                stateLeft(Q);                system("clear");                Printf();                    break;            case ‘d‘:                stateRight(Q);                system("clear");                Printf();                    break;              case ‘s‘:                stateDown(Q);                system("clear");                Printf();                    break;            default:                if(ch==‘0‘)                    exit(1);        }          GrialM(Mlist);            if(flags==0)            {            Pos pos = AdancePoint(Mlist);        ar[pos.x][pos.y] = 1;//隨機點。            flags=1;            }          sleep(0.675);         }    }bool IsIn(Pos pos,deque<Pos> &Q){    deque<Pos> :: iterator it=Q.begin();    while(it!=Q.end())    {        if(pos.x==it->x && pos.y==it->y)            return true;        it++;    }    return false;}void GrialM(list<Pos> &Mlist)//構造隨機鏈表.{    for(int i=0;i<_SIZE_;i++)    {        for(int j=0;j<_SIZE_;j++)        {            if(ar[i][j]==0)            Mlist.push_back(Pos(i,j));        }    }}Pos AdancePoint(list<Pos> &Mlist){    int n = Mlist.size();    list<Pos> :: iterator it = Mlist.begin();    advance(it,rand()%n);    return Pos(it->x,it->y);}void stateUp(deque<Pos> &Q)    {        Pos pos = Q.front();        int x ;        if(pos.x==0)        {   x = _SIZE_-1;}        else        {x = pos.x-1;}        int y = pos.y;    Pos result(x,y);    if(IsIn(result,Q))    {            exit(-1);    }        if(ar[x][y]==1)        {            Q.push_front(result);                   flags=0;            return ;        }        ar[x][y] = 1;        Q.push_front(result);       Pos ret = Q.back();        ar[ret.x][ret.y] = 0;        Q.pop_back();    }    void stateLeft(deque<Pos> &Q)    {        Pos pos = Q.front();        int x = pos.x;        int y;        if(pos.y==0)        {               y = _SIZE_-1;        }        else{y = pos.y-1;}        Pos result(x,y);        if(IsIn(result,Q))        {            exit(-1);        }        if(ar[x][y]==1)        {            Q.push_front(result);            flags=0;            return ;        }        ar[x][y] = 1;        Q.push_front(result);        Pos ret = Q.back();        ar[ret.x][ret.y] = 0;        Q.pop_back();    }    void stateRight(deque<Pos> &Q)    {        Pos pos = Q.front();        int x = pos.x;        int y ;        if(pos.y==_SIZE_-1)        {            y=0;        }           else {y=pos.y+1;}        Pos result(x,y);        if(IsIn(result,Q))        {            exit(-1);        }        if(ar[x][y]==1)        {            Q.push_front(result);            flags=0;            return ;        }        ar[x][y] = 1;        Q.push_front(result);        Pos ret = Q.back();        ar[ret.x][ret.y] = 0;        Q.pop_back();    }    void stateDown(deque<Pos> &Q)    {        Pos pos = Q.front();        int x ;        if(pos.x==_SIZE_-1)            {x=0;}        else {x = pos.x+1;}        int y = pos.y;        Pos result(x,y);        if(IsIn(result,Q))        {            exit(-1);        }        if(ar[x][y]==1)        {               Q.push_front(result);            flags=0;            return ;        }        ar[x][y] = 1;        Q.push_front(result);        Pos ret = Q.back();        ar[ret.x][ret.y] = 0;        Q.pop_back();    }    public:    void Printf()    {        _SET_;        for(int i=0;i<_SIZE_;i++)        {            for(int j=0;j<_SIZE_;j++)            {                if(ar[i][j]==1)                    {                    cout<<"\033[34m1\033[0m ";                    }                else                    cout<<ar[i][j]<<" ";            }            cout<<endl;        }    }    private:    Array ar;};int main(){    Array arr={0};    Grial gl(arr);    int x = rand()%10;    int y = rand()%10;    Pos start(x,y);    gl.Init(start);}

C++簡單貪吃蛇實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.