Advertisement
Tony041010

2048(not completed)

Jun 17th, 2021 (edited)
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <conio.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <time.h>
  6. #include <windows.h>
  7. using namespace std;
  8.  
  9. int map[16] = {0};
  10. int step = 0;
  11. char k;
  12.  
  13. void draw(){
  14.     system( "cls" );
  15.     cout << "steps:" << step << "\n";
  16.     for( int i = 0 ; i < 16 ; i++ ){
  17.         if( map[i] == 0 ){
  18.             cout << "\t";
  19.         }
  20.         else{
  21.             cout << map[i] << "\t";
  22.         }
  23.         if( i % 4 == 3 ){
  24.             cout << "\n";
  25.         }
  26.     }  
  27. }
  28.  
  29. void random(){
  30.     int x = 0;
  31.     while( x < 1 ){
  32.         int index = rand() % 16;
  33.         int number = rand() % 5;  
  34.         if( number ==2 || number == 4 ){
  35.             if(map[index] ==0){
  36.                 map[ index ] = number;
  37.                 x+=1;
  38.             }
  39.         }
  40.     }
  41. }
  42. void init(){
  43.     int x = 0;
  44.     while( x < 3 ){
  45.         int index = rand() % 16;
  46.         int number = rand() % 5;  
  47.         if( number ==2 || number == 4 ){
  48.             map[ index ] = number;
  49.             x+=1;
  50.         }
  51.     }
  52. }
  53.  
  54. void push_f(){
  55.     for(int i = 3;i <=15 ; i +=4){
  56.         for(int j = i-1 ; j>=i-3 ; j-- ){
  57.             int k = j;
  58.             while(1){
  59.                 if(map[k+1]==0){
  60.                     int m_f = map[k+1];
  61.                     map[k+1] = map[k];
  62.                     map[k] = m_f;
  63.                    
  64.                 }
  65.                 else if(map[k+1]==map[k]){
  66.                     map[k+1]= map[k+1]*2;
  67.                     map[k]=0;
  68.                 }
  69.                 k=k+1;
  70.                 if(k ==i)break;
  71.             }
  72.         }
  73.     }
  74. }
  75.  
  76. push_s(){
  77.     for(int i = 0;i <=12 ; i +=4){
  78.         for(int j = i+1 ; j<=i+3 ; j++ ){
  79.             int k = j;
  80.             while(1){
  81.                 if(map[k-1]==0){
  82.                     int m_s = map[k-1];
  83.                     map[k-1] = map[k];
  84.                     map[k] = m_s;
  85.                    
  86.                 }
  87.                 else if(map[k-1]==map[k]){
  88.                     map[k-1]= map[k-1]*2;
  89.                     map[k]=0;
  90.                 }
  91.                 k=k-1;
  92.                 if(k ==i)break;
  93.             }
  94.         }
  95.     }
  96. }
  97.  
  98. void push_e(){
  99.     for(int i = 0;i <=3 ; i ++){
  100.         for(int j = i+4 ; j<=i+12 ; j+=4 ){
  101.             int k = j;
  102.             while(1){
  103.                 if(map[k-4]==0){
  104.                     int m_e = map[k-4];
  105.                     map[k-4] = map[k];
  106.                     map[k] = m_e;
  107.                    
  108.                 }
  109.                 else if(map[k-4]==map[k]){
  110.                     map[k-4]= map[k-4]*2;
  111.                     map[k]=0;
  112.                 }
  113.                 k=k-4;
  114.                 if(k ==i)break;
  115.             }
  116.         }
  117.     }
  118. }
  119. void push_d(){
  120.     for(int i = 12 ; i <=15 ; i ++){
  121.         for(int j = i-4 ; j>=i-12 ; j-=4 ){
  122.             int k = j;
  123.             while(1){
  124.                 if(map[k+4]==0){
  125.                     int m_d = map[k+4];
  126.                     map[k+4] = map[k];
  127.                     map[k] = m_d;
  128.                    
  129.                 }
  130.                 else if(map[k+4]==map[k]){
  131.                     map[k+4]= map[k+4]*2;
  132.                     map[k]=0;
  133.                 }
  134.                 k=k+4;
  135.                 if(k ==i)break;
  136.             }
  137.         }
  138.     }
  139. }
  140.  
  141. int main(){
  142.     init();
  143.     draw();
  144.     while(1){
  145.         if( kbhit() ){
  146.             k = _getch();
  147.             if( k == 'q' ){
  148.                 draw();
  149.                 break;
  150.             }
  151.             if( k == 'f' ){
  152.                 push_f();
  153.                 random();
  154.                 step++;
  155.             }
  156.             if( k == 's' ){
  157.                 push_s();
  158.                 random();
  159.                 step++;
  160.             }
  161.             if( k =='e' ){
  162.                 push_e();
  163.                 random();
  164.                 step++;
  165.             }
  166.             if( k == 'd' ){
  167.                 push_d();
  168.                 random();
  169.                 step++;
  170.             }
  171.         }
  172.         draw();
  173.         Sleep(100);
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement