Tony041010

2048(completed)

Jun 17th, 2021 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.82 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(){ //初始化規格跟一開始的2個數字
  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. //push f、s、e、d : 當玩家執行上 (e)下 (d)左 (s)右 (f)鍵時,數字就會照2048的規則跑
  55. void push_f(){
  56.     for(int i = 3;i <=15 ; i +=4){
  57.         for(int j = i-1 ; j>=i-3 ; j-- ){
  58.             int k = j;
  59.             while(1){
  60.                 //如果右邊的數字為0,就跟它對調。調著調著就會把數字全部往右移
  61.                 if(map[k+1]==0){
  62.                     int m_f = map[k+1];
  63.                     map[k+1] = map[k];
  64.                     map[k] = m_f;
  65.                    
  66.                 }
  67.                 //如果數字一樣就加起來
  68.                 else if(map[k+1]==map[k]){
  69.                     map[k+1]= map[k+1]*2;
  70.                     map[k]=0;
  71.                 }
  72.                 k=k+1;
  73.                 if(k ==i)break;
  74.             }
  75.         }
  76.     }
  77. }
  78.  
  79. push_s(){
  80.     for(int i = 0;i <=12 ; i +=4){
  81.         for(int j = i+1 ; j<=i+3 ; j++ ){
  82.             int k = j;
  83.             while(1){
  84.                 //如果左邊的數字為0,就跟它對調。調著調著就會把數字全部往左移
  85.                 if(map[k-1]==0){
  86.                     int m_s = map[k-1];
  87.                     map[k-1] = map[k];
  88.                     map[k] = m_s;
  89.                    
  90.                 }
  91.                 //如果數字一樣就加起來
  92.                 else if(map[k-1]==map[k]){
  93.                     map[k-1]= map[k-1]*2;
  94.                     map[k]=0;
  95.                 }
  96.                 k=k-1;
  97.                 if(k ==i)break;
  98.             }
  99.         }
  100.     }
  101. }
  102.  
  103. void push_e(){
  104.     for(int i = 0;i <=3 ; i ++){
  105.         for(int j = i+4 ; j<=i+12 ; j+=4 ){
  106.             int k = j;
  107.             while(1){
  108.                 //如果上面的數字為0,就跟它對調。調著調著就會把數字全部往上移
  109.                 if(map[k-4]==0){
  110.                     int m_e = map[k-4];
  111.                     map[k-4] = map[k];
  112.                     map[k] = m_e;
  113.                    
  114.                 }
  115.                 //如果數字一樣就加起來
  116.                 else if(map[k-4]==map[k]){
  117.                     map[k-4]= map[k-4]*2;
  118.                     map[k]=0;
  119.                 }
  120.                 k=k-4;
  121.                 if(k ==i)break;
  122.             }
  123.         }
  124.     }
  125. }
  126. void push_d(){
  127.     for(int i = 12 ; i <=15 ; i ++){
  128.         for(int j = i-4 ; j>=i-12 ; j-=4 ){
  129.             int k = j;
  130.             while(1){
  131.                 //如果下面的數字為0,就跟它對調。調著調著就會把數字全部往下移
  132.                 if(map[k+4]==0){
  133.                     int m_d = map[k+4];
  134.                     map[k+4] = map[k];
  135.                     map[k] = m_d;
  136.                    
  137.                 }
  138.                 //如果數字一樣就加起來
  139.                 else if(map[k+4]==map[k]){
  140.                     map[k+4]= map[k+4]*2;
  141.                     map[k]=0;
  142.                 }
  143.                 k=k+4;
  144.                 if(k ==i)break;
  145.             }
  146.         }
  147.     }
  148. }
  149.  
  150. int main(){
  151.     init();
  152.     draw();
  153.     while(1){
  154.         if( kbhit() ){
  155.             k = _getch();
  156.             if( k == 'q' ){
  157.                 draw();
  158.                 break;
  159.             }
  160.             if( k == 'f' ){
  161.                 push_f();
  162.                 random();
  163.                 step++;
  164.             }
  165.             if( k == 's' ){
  166.                 push_s();
  167.                 random();
  168.                 step++;
  169.             }
  170.             if( k =='e' ){
  171.                 push_e();
  172.                 random();
  173.                 step++;
  174.             }
  175.             if( k == 'd' ){
  176.                 push_d();
  177.                 random();
  178.                 step++;
  179.             }
  180.             bool finished = false;
  181.             int lefted = 16;
  182.             for( int i = 0 ; i <= 15 ; i++ ){
  183.                 if( map[i] == 16 ){
  184.                     finished = true;
  185.                 }
  186.                 if( map[i] != 0 ){
  187.                     lefted--;
  188.                 }
  189.             }
  190.             if( finished ){
  191.                 draw();
  192.                 cout << "congras, mission completed!";
  193.                 break;
  194.             }
  195.             else if( lefted == 0 ){
  196.                 draw();
  197.                 cout << "sorry, you failed!";
  198.             }
  199.             else{
  200.                 draw();
  201.                 Sleep(100);
  202.             }
  203.         }
  204.     }
  205. }
Add Comment
Please, Sign In to add comment