Tony041010

Teris1(coding,not finished)

Jun 17th, 2021 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 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[200]={0};  
  9. vector<string> block{"0000001001110000", "0000001101100000", "0000011001100000", "0000111100000000", "0000011000110000", "0000011100010000", "0000011101000000"}; //7種方塊
  10. int bid;  //方塊編號
  11. string bstr;  //方塊內容
  12. int nowxy;  //現在位置
  13. //第一題,地圖繪製,0用白色方塊顯示,其餘用黑色方塊
  14. void draw_map() {
  15.     system("cls");  //cls means clear screen
  16.     for(int i = 0 ; i < 200 ; i++ ){
  17.         if( map[i] == 0 ){
  18.             cout << "□";
  19.         }
  20.         else{
  21.             cout << "■" ;
  22.         }
  23.         if(i % 10 == 9){
  24.             cout << endl;
  25.         }
  26.     }
  27. }
  28.  
  29. void init_map() {
  30.     for(int i = 0 ; i < 200 ; i++){
  31.         if( i%10 == 0 || (i-9) % 10 == 0 || i > 190 ){
  32.             map[i] = 1;
  33.         }
  34.     }
  35. }
  36.  
  37. void draw_block(){
  38.     //system("cls");
  39.     for(int i = 0 ; i < 16 ; i++){
  40.         if(bstr[i] =='0'){
  41.             cout << "□";
  42.         }
  43.         else{
  44.             cout << "■";
  45.         }
  46.         if(i % 4 == 3){
  47.             cout << endl;
  48.         }
  49.     }
  50. }
  51.  
  52. bool move( int f , int t ){
  53.     int i , j;
  54.    
  55.     //清除上一步F
  56.     for( i = 3 ; i>=0 ; i-- ){
  57.         for( j = 3; j >= 0 ; j-- ){
  58.             if(bstr[i*4+j] !='0'){
  59.                 map[f+i*10+j] = 0;
  60.             }
  61.         }
  62.     }
  63.     //偵測碰撞(T點)
  64.     bool collision = false;
  65.     for( i = 3 ; i>=0 ; i-- ){
  66.         for( j = 3; j >= 0 ; j-- ){
  67.             if(bstr[i*4+j] != '0'){
  68.                 if(map[t+i*10+j] != 0){
  69.                     collision = true;
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     int a;
  75.     if(collision == false){
  76.         a = t;
  77.     }
  78.     else{
  79.         a = f;
  80.     }
  81.     for(i = 3 ; i >= 0 ; i--){
  82.         for(j = 3 ; j >= 0 ; j--){
  83.             if(bstr[i*4+j] !='0'){
  84.                 map[a+i*10+j] = bstr[i*4+j] - '0';
  85.             }
  86.         }
  87.     }
  88.     nowxy = a;
  89.     return not collision;
  90. }
  91.  
  92. int main(void)
  93. {
  94.     int k;  //紀錄你按到哪一個按鍵
  95.    
  96.     init_map();
  97.     draw_map();
  98.    
  99.     cin >> bid;
  100.     bstr = block[bid];
  101.     draw_block();
  102.    
  103.     nowxy = 3;
  104.     move(3,3);
  105.     draw_map();
  106.     do{
  107.         k = _getch();
  108.         if( k == 'e' ){
  109.         // from nowxy to nowxy+10
  110.         move( nowxy , nowxy - 10 );
  111.         }
  112.         else if( k == 'd' ){
  113.             move( nowxy ,  nowxy + 10 );
  114.         }
  115.         else if( k == 's' ){
  116.             move( nowxy ,  nowxy - 1 );
  117.         }
  118.         else if( k == 'f' ){
  119.             move( nowxy ,  nowxy + 1 );
  120.         }
  121.         else if( k == 'q' ){
  122.             break;
  123.         }
  124.         draw_map();
  125.     }while(k!=27);
  126.    
  127.     return 0;
  128. }
Add Comment
Please, Sign In to add comment