Tony041010

Teris2(Semi-finished product)

Jun 17th, 2021 (edited)
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 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. void clockwise(){
  93.     int r[16]= { 12,8,4,0, 13, 9 , 5 , 1 , 14,10,6,2,15,11,7,3};
  94.     int i;
  95.     string t = bstr;
  96.     for(i = 0 ; i <=15 ; i++){
  97.         t[i] = bstr[r[i]];
  98.     }
  99.     for(i = 0 ; i <=15 ; i++){
  100.         bstr[i] = t[i];
  101.     }
  102. }
  103.  
  104. bool rotate(int f){
  105.     int i , j;
  106.     //清除上一步F
  107.     for( i=3 ; i>=0 ; i-- ){
  108.         for( j=3 ; j>=0 ; j-- ){
  109.             if(bstr[i*4+j] !='0'){
  110.                 map[f+i*10+j] = 0;
  111.             }
  112.         }
  113.     }
  114.     clockwise();
  115.    
  116.     //偵測碰撞(T點)
  117.     bool collision = false;
  118.     for( i=3 ; i>=0 ; i-- ){
  119.         for( j=3 ; j>=0 ; j--){
  120.             if(bstr[i*4+j] != '0'){
  121.                 if(map[f+i*10+j] != 0){
  122.                     collision = true;
  123.                 }
  124.             }
  125.         }
  126.     }
  127.    
  128.     if(collision == true){
  129.         clockwise();
  130.         clockwise();
  131.         clockwise();
  132.     }
  133.     int a = f;
  134.     for( i=3 ; i>=0 ; i-- ){
  135.         for( j=3 ; j>=0 ; j--){
  136.             if(bstr[i*4+j] !='0'){
  137.                 map[a+i*10+j] = bstr[i*4+j] -'0';
  138.             }
  139.         }
  140.     }
  141.     nowxy = a;
  142.     return not collision;
  143.    
  144. }
  145.  
  146. int main(void)
  147. {
  148.     int k;  //紀錄你按到哪一個按鍵
  149.    
  150.     init_map();
  151.     draw_map();
  152.    
  153.     cin >> bid;
  154.     bstr = block[bid];
  155.     draw_block();
  156.    
  157.     nowxy = 3;
  158.     move(3,3);
  159.     draw_map();
  160.     do{
  161.         k = _getch();
  162.         if( k == 'e' ){
  163.         // from nowxy to nowxy+10
  164.         move( nowxy , nowxy - 10 );
  165.         }
  166.         else if( k == 'd' ){
  167.             move( nowxy ,  nowxy + 10 );
  168.         }
  169.         else if( k == 's' ){
  170.             move( nowxy ,  nowxy - 1 );
  171.         }
  172.         else if( k == 'f' ){
  173.             move( nowxy ,  nowxy + 1 );
  174.         }
  175.         else if( k == 'q' ){
  176.             break;
  177.         }
  178.         else if( k == 'r' ){
  179.             /*clockwise();
  180.             draw_block();  第一題測試
  181.             continue; */
  182.             rotate(nowxy);
  183.             move(nowxy,nowxy);
  184.         }
  185.         draw_map();
  186.     }while(k!=27);
  187.    
  188.     return 0;
  189. }
Add Comment
Please, Sign In to add comment