Tony041010

Teris3(finished)

Jun 17th, 2021 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 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. int newbid;
  14. string newbstr;
  15. //第一題,地圖繪製,0用白色方塊顯示,其餘用黑色方塊
  16. void draw_map() {
  17.     system("cls");  //cls means clear screen
  18.     for(int i = 0 ; i < 200 ; i++ ){
  19.         if( map[i] == 0 ){
  20.             cout << "□";
  21.         }
  22.         else{
  23.             cout << "■" ;
  24.         }
  25.         if(i % 10 == 9){
  26.             cout << endl;
  27.         }
  28.     }
  29. }
  30.  
  31. void init_map() {
  32.     for(int i = 0 ; i < 200 ; i++){
  33.         if( i%10 == 0 || (i-9) % 10 == 0 || i > 190 ){
  34.             map[i] = 1;
  35.         }
  36.     }
  37. }
  38.  
  39. void draw_block(){
  40.     //system("cls");
  41.     for(int i = 0 ; i < 16 ; i++){
  42.         if(newbstr[i] =='0'){
  43.             cout << "□";
  44.         }
  45.         else{
  46.             cout << "■";
  47.         }
  48.         if(i % 4 == 3){
  49.             cout << endl;
  50.         }
  51.     }
  52. }
  53.  
  54. bool move( int f , int t ){
  55.     int i , j;
  56.    
  57.     //清除上一步F
  58.     for( i = 3 ; i>=0 ; i-- ){
  59.         for( j = 3; j >= 0 ; j-- ){
  60.             if(bstr[i*4+j] !='0'){
  61.                 map[f+i*10+j] = 0;
  62.             }
  63.         }
  64.     }
  65.     //偵測碰撞(T點)
  66.     bool collision = false;
  67.     for( i = 3 ; i>=0 ; i-- ){
  68.         for( j = 3; j >= 0 ; j-- ){
  69.             if(bstr[i*4+j] != '0'){
  70.                 if(map[t+i*10+j] != 0){
  71.                     collision = true;
  72.                 }
  73.             }
  74.         }
  75.     }
  76.     int a;
  77.     if(collision == false){
  78.         a = t;
  79.     }
  80.     else{
  81.         a = f;
  82.     }
  83.     for(i = 3 ; i >= 0 ; i--){
  84.         for(j = 3 ; j >= 0 ; j--){
  85.             if(bstr[i*4+j] !='0'){
  86.                 map[a+i*10+j] = bstr[i*4+j] - '0';
  87.             }
  88.         }
  89.     }
  90.     nowxy = a;
  91.     return not collision;
  92. }
  93.  
  94. void clockwise(){
  95.     int r[16]= { 12,8,4,0, 13, 9 , 5 , 1 , 14,10,6,2,15,11,7,3};
  96.     int i;
  97.     string t = bstr;
  98.     for(i = 0 ; i <=15 ; i++){
  99.         t[i] = bstr[r[i]];
  100.     }
  101.     for(i = 0 ; i <=15 ; i++){
  102.         bstr[i] = t[i];
  103.     }
  104. }
  105.  
  106. bool rotate(int f){
  107.     int i , j;
  108.     //清除上一步F
  109.     for( i=3 ; i>=0 ; i-- ){
  110.         for( j=3 ; j>=0 ; j-- ){
  111.             if(bstr[i*4+j] !='0'){
  112.                 map[f+i*10+j] = 0;
  113.             }
  114.         }
  115.     }
  116.     clockwise();
  117.    
  118.     //偵測碰撞(T點)
  119.     bool collision = false;
  120.     for( i=3 ; i>=0 ; i-- ){
  121.         for( j=3 ; j>=0 ; j--){
  122.             if(bstr[i*4+j] != '0'){
  123.                 if(map[f+i*10+j] != 0){
  124.                     collision = true;
  125.                 }
  126.             }
  127.         }
  128.     }
  129.    
  130.     if(collision == true){
  131.         clockwise();
  132.         clockwise();
  133.         clockwise();
  134.     }
  135.     int a = f;
  136.     for( i=3 ; i>=0 ; i-- ){
  137.         for( j=3 ; j>=0 ; j--){
  138.             if(bstr[i*4+j] !='0'){
  139.                 map[a+i*10+j] = bstr[i*4+j] -'0';
  140.             }
  141.         }
  142.     }
  143.     nowxy = a;
  144.     return not collision;
  145.    
  146. }
  147.  
  148. bool check(int n){
  149.     if(n<190){
  150.         int x = n/10;
  151.         for(int i = 0 ;i<10 ; i++){
  152.             if(map[i+10*x]==0){
  153.                 return false;
  154.             }
  155.         }
  156.        
  157.         for(int i = x ; i >0 ; i--){
  158.             for(int j = 1 ; j<9 ; j++){
  159.                 map[i*10+j] = map[(i-1)*10+j];
  160.             }
  161.         }
  162.     }
  163. }
  164.  
  165.  
  166. int main(void)
  167. {
  168.     int k;  //紀錄你按到哪一個按鍵
  169.    
  170.     init_map();
  171.     draw_map();
  172.    
  173.     //cin >> bid;
  174.     bid = rand() % 7;
  175.     bstr = block[bid];
  176.     newbid = rand()%7;
  177.     newbstr = block[newbid];
  178.    
  179.     nowxy = 3;
  180.     move(3,3);
  181.     draw_map();
  182.     do{
  183.         Sleep(500);
  184.         if(!move(nowxy,nowxy+10)){
  185.            
  186.             check(nowxy);
  187.             check(nowxy+10);
  188.             check(nowxy+20);
  189.             check(nowxy+30);
  190.            
  191.             if(nowxy<10){
  192.                 cout << "Game Over";
  193.                 break;
  194.             }
  195.             else{
  196.                 bid = newbid;
  197.                 bstr = newbstr;
  198.                 newbid = rand()%7;
  199.                 newbstr = block[newbid];
  200.                
  201.                 nowxy=3;
  202.                 move(3,3);
  203.             }
  204.         }
  205.         if(kbhit()){
  206.             k = _getch();
  207.             if( k == 'e' ){
  208.             // from nowxy to nowxy+10
  209.                 rotate(nowxy);
  210.                 move(nowxy,nowxy);
  211.             }
  212.             else if( k == 'd' ){
  213.                 move( nowxy ,  nowxy + 10 );
  214.             }
  215.             else if( k == 's' ){
  216.                 move( nowxy ,  nowxy - 1 );
  217.             }
  218.             else if( k == 'f' ){
  219.                 move( nowxy ,  nowxy + 1 );
  220.             }
  221.             else if( k == 'q' ){
  222.                 break;
  223.             }
  224.         }
  225.         draw_map();
  226.         draw_block();
  227.     }while(k!=27);
  228.    
  229.     return 0;
  230. }
  231.  
Add Comment
Please, Sign In to add comment