Tony041010

數字拼盤2(可以玩,並且自行決定拼盤的維度,如4*4等等)

Jun 17th, 2021 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 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. vector<string> bign{"0","1","2","3","4","5","6","7","8","9"};  //用來輸出的全形數字,因為空白格 □是全形,這樣格子才不會歪掉
  9. const int length = 4;
  10. int map[length*length + 1]={0};  //一開始10格都設為0 ,但是捨棄第0格,只用map[1]~map[9]九宮格
  11. int p;//目前位置
  12. int k;//目前按鍵
  13. int s;//目前移動數
  14.  
  15. void draw(){
  16.     system("cls");  //cls means clear screen
  17.     cout << "steps:" << s << endl;
  18.     for(int i = 1 ; i <= (length * length) ; i++ ){
  19.         //cout << "de";
  20.         if( map[i] == 0 ){
  21.             cout << "□" << " ";
  22.         }
  23.         else if( map[i] >=1 && map[i] <=9 ){
  24.             cout << bign[ map[i] ] << " ";
  25.         }
  26.         else{
  27.             cout << map[i] << " ";
  28.         }
  29.         if( i % length == 0 ){
  30.             cout << endl;
  31.         }
  32.     }
  33. }
  34.  
  35. void init(){
  36.     int i , j ;
  37.     for( i = 1 ; i <= ((length*length) -1) ; i++ ){
  38.         map[i] = i;
  39.         if (i ==((length*length) -1)){
  40.             cout << "de";
  41.         }
  42.     }
  43.     map[i] = 0;
  44.    
  45.     /*while(1){
  46.         for( i = 1 ; i <= (length*length -1) ; i ++ ){
  47.             int x = rand()%(length*length -1)+1;
  48.             int y = map[x];
  49.             map[x] = map[i];
  50.             map[i] = y;
  51.         }
  52.        
  53.         //第二題 逆數對之運算
  54.         int a = 0;
  55.         for( i = 1 ; i <= (length*length -2) ; i++ ){
  56.             for( j = i + 1 ; j <= (length*length -1) ; j ++ ){
  57.                 if( map[i] > map[j] ){
  58.                     a++;
  59.                 }
  60.             }
  61.         }
  62.         if( a % 2 == 0 ){
  63.             break; 
  64.         }
  65.         cout << "no solution , try next\n";
  66.         draw();
  67.     } */
  68.     p = length*length;
  69.     s = 0;
  70. }
  71.  
  72.  
  73.  
  74. int main(){
  75.     init();
  76.     draw();
  77.     while(1){
  78.         if( kbhit() ){
  79.             k = _getch();
  80.             if(k == 'q'){
  81.                 break;
  82.              }
  83.             else if( k == 'e'){
  84.                 if( p < (length*length -length) ){
  85.                     int x = p + length ;
  86.                     int y = map[x];
  87.                     map[x] = map[p];
  88.                     map[p] = y;
  89.                     p = x;
  90.                 }
  91.             }
  92.             else if( k == 'd'){
  93.                 if( p > length ){
  94.                     int x = p - length ;
  95.                     int y = map[x];
  96.                     map[x] = map[p];
  97.                     map[p] = y;
  98.                     p = x;
  99.                 }
  100.             }
  101.             else if( k == 's'){
  102.                 if( p % length != 0 ){
  103.                     int x = p + 1 ;
  104.                     int y = map[x];
  105.                     map[x] = map[p];
  106.                     map[p] = y;
  107.                     p = x;
  108.                 }
  109.             }
  110.             else if( k == 'f'){
  111.                 if( p % length != 1 ){
  112.                     int x = p - 1 ;
  113.                     int y = map[x];
  114.                     map[x] = map[p];
  115.                     map[p] = y;
  116.                     p = x;
  117.                 }
  118.             }
  119.             if( k == 'e' || k == 'd' || k == 's' || k == 'f' ){
  120.                 s++;
  121.             }
  122.             bool finished = true;
  123.             for( int i = 1 ; i <= (length*length -1) ; i++ ){
  124.                 if( map[i] != i ){
  125.                     finished = false;
  126.                     break;
  127.                 }
  128.             }
  129.             if( finished ){
  130.                 cout<<"you win!\n";
  131.                 break;
  132.             }
  133.             draw();
  134.          }
  135.      }
  136.     return 0;
  137. }
Add Comment
Please, Sign In to add comment