#include <iostream>#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include <deque>#include <list>#define _SIZE_#define _SET_ cout<< "\033[?25l\033[0m"Static intFlags =0;typedef intArray[_size_][_size_];using namespace STD;structpos{intXintY Pos (intXintY): X (x), Y (y) {}};classgrial{ Public: Grial (Array a) { for(intI=0; i<_size_;i++) { for(intj=0; j<_size_;j++) {ar[i][j] = a[i][j]; } } }voidInit (Pos start) {inti =0;intj =0;CharCh deque<Pos>Q; list<Pos>Mlist; AR[START.X][START.Y] =1; Q.push_front (start); while(1) {System ("Stty Raw-echo");//Change terminal control, //Enter a character immediately corresponding to the original \ n as the end of the flag, //Enter a character instead to return immediately. ch = getchar (); System"Stty-raw echo");//Let the terminal turn back to normal. 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;//Random point. flags=1; } Sleep (0.675); } }BOOLIsIn (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;}voidGrialm ( list<Pos>&mlist)//construct random linked list.{ for(intI=0; i<_size_;i++) { for(intj=0; j<_size_;j++) {if(ar[i][j]==0) Mlist.push_back (Pos (i,j)); }}}pos Adancepoint ( list<Pos>&mlist) {intn = mlist.size (); list<Pos>:: Iterator it = Mlist.begin (); Advance (It,rand ()%n);returnPos (it->x,it->y);}voidStateup ( deque<Pos>&Q) {pos pos = Q.front ();intx;if(pos.x==0) {x = _size_-1;}Else{x = pos.x-1;}inty = 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 (); }voidStateleft ( deque<Pos>&Q) {pos pos = Q.front ();intx = pos.x;intYif(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 (); }voidStateright ( deque<Pos>&Q) {pos pos = Q.front ();intx = pos.x;inty;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 (); }voidStatedown ( deque<Pos>&Q) {pos pos = Q.front ();intx;if(pos.x==_size_-1) {x=0;}Else{x = pos.x+1;}inty = 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:voidPrintf () {_set_; for(intI=0; i<_size_;i++) { for(intj=0; j<_size_;j++) {if(ar[i][j]==1) {cout<<"\033[34m1\033[0m"; }Else cout<<ar[i][j]<<" "; }cout<<endl; } }Private: Array ar;};intMain () {Array arr={0}; Grial GL (arr);intx = rand ()%Ten;inty = rand ()%Ten; Pos start (x, y); Gl. Init (start);
C + + Simple snake realization