SHARE
TWEET

Tiny twos

a guest Jun 29th, 2015 1,057 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream> // Tiny-twos by @eigenbom
  2. #include<ctime>    // A minimal threes-like
  3. #include<cstdlib>  // > cl/gcc twos.cpp
  4. #include<conio.h>  // > twos.exe
  5. using namespace std;using i=int;using ci=const i;ci W=4;ci W2=W*W;i h,j,k,l,a,g,m,c,x,y,b[W2];void main(){srand(time(NULL));b[rand()%W]=1;for(;;){system("cls");y=(c==72)?1:(c==80)?-1:0;x=(c==77)?1:(c==75)?-1:0;g=x||y;m=!g||!c;while(g){g=0;for(k=0;k<W2;k++){h=(x<0||y<0)?(W2-1-k):k;j=h/W;l=x>0?(h+1):(x<0?(h-1):(y>0?(h-W):(h+W)));a=b[h]&&((x>0&&h%W!=W-1)||(x<0&&h%W!=0)||(y>0&&j%W!=0)||(y<0&&j%W!=W-1));if(a&&b[h]==b[l]){b[h]=0;b[l]<<=1;g=m=1;}else if(a&&!b[l]){b[l]=b[h];b[h]=0;g=m=1;}}}h=0;for(k=0;k<W2;k++){j=k/W;if(!h&&!b[k]&&((x>0&&k%W==0)||(x<0&&k%W==W-1)||(y>0&&j%W==W-1)||(y<0&&j%W==0))){b[k]=rand()%3;m=1;h=b[k];}printf("%3d%c",b[k],(k%W==W-1)?'\n':' ');}if (!m||(c=_getch())==27){j=0;for(i k:b)j+=k;printf("Score: %d",j);break;}}}
RAW Paste Data
Top