Tony041010

數字拼盤1(手動輸入3*3盤面)

Jun 17th, 2021 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 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. int map[10]={0};  //一開始10格都設為0 ,但是捨棄第0格,只用map[1]~map[9]九宮格
  9. vector<string> bign{"0","1","2","3","4","5","6","7","8","9"};  //用來輸出的全形數字,因為空白格 □是全形,這樣格子才不會歪掉
  10. int p;//目前位置
  11. int k;//目前按鍵
  12. int s;//目前移動數
  13. string puzzle=" 153247860";//範例測資
  14.  
  15. //第一題,初始化,把範例測資安排到對應的格子裡
  16. void init() {
  17.     cin>>puzzle;
  18.     puzzle = " " + puzzle;
  19.     int i;
  20.     for( i = 1 ; i <=9 ; i++ ){
  21.         map[i] = puzzle[i] - '0';
  22.         if( puzzle[i] == '0' ){
  23.             p = i;
  24.         }
  25.     }
  26.     s = 0;
  27. }
  28.  
  29. //第一題,九宮格繪製,0用白色方塊顯示,其餘用全形數字
  30. void draw() {
  31.     system("cls");  //cls means clear screen
  32.     cout << "steps:" << s << endl;
  33.     for(int i = 1 ; i <= 9 ; i++ ){
  34.         if( map[i] == 0 ){
  35.             cout << "□";
  36.         }
  37.         else{
  38.             cout << bign[ map[i] ];
  39.         }
  40.         if( i % 3 == 0 ){
  41.             cout << endl;
  42.         }
  43.     }
  44. }
  45.  
  46. int main(void)
  47. {
  48.     init();
  49.     draw();
  50.     while(1){
  51.         if( kbhit() ){
  52.             k = _getch();
  53.             if(k == 'q'){
  54.                 break;
  55.              }
  56.             else if( k == 'e'){
  57.                 if( p < 7 ){
  58.                     int x = p + 3 ;
  59.                     int y = map[x];
  60.                     map[x] = map[p];
  61.                     map[p] = y;
  62.                     p = x;
  63.                 }
  64.             }
  65.             else if( k == 'd'){
  66.                 if( p > 3 ){
  67.                     int x = p - 3 ;
  68.                     int y = map[x];
  69.                     map[x] = map[p];
  70.                     map[p] = y;
  71.                     p = x;
  72.                 }
  73.             }
  74.             else if( k == 's'){
  75.                 if( p % 3 != 0 ){
  76.                     int x = p + 1 ;
  77.                     int y = map[x];
  78.                     map[x] = map[p];
  79.                     map[p] = y;
  80.                     p = x;
  81.                 }
  82.             }
  83.             else if( k == 'f'){
  84.                 if( p % 3 != 1 ){
  85.                     int x = p - 1 ;
  86.                     int y = map[x];
  87.                     map[x] = map[p];
  88.                     map[p] = y;
  89.                     p = x;
  90.                 }
  91.             }
  92.             if( k == 'e' || k == 'd' || k == 's' || k == 'f' ){
  93.                 s++;
  94.             }
  95.             bool finished = true;
  96.             for( int i = 1 ; i <= 8 ; i++ ){
  97.                 if( map[i] != i ){
  98.                     finished = false;
  99.                     break;
  100.                 }
  101.             }
  102.             if( finished ){
  103.                 draw();
  104.                 cout<<"you win!\n";
  105.                 break;
  106.             }
  107.             draw();
  108.          }
  109.      }
  110.     return 0;
  111. }
  112.  
Add Comment
Please, Sign In to add comment