Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <time.h>
- #include <windows.h>
- using namespace std;
- int map[200]={0};
- vector<string> block{"0000001001110000", "0000001101100000", "0000011001100000", "0000111100000000", "0000011000110000", "0000011100010000", "0000011101000000"}; //7種方塊
- int bid; //方塊編號
- string bstr; //方塊內容
- int nowxy; //現在位置
- int newbid;
- string newbstr;
- //第一題,地圖繪製,0用白色方塊顯示,其餘用黑色方塊
- void draw_map() {
- system("cls"); //cls means clear screen
- for(int i = 0 ; i < 200 ; i++ ){
- if( map[i] == 0 ){
- cout << "□";
- }
- else{
- cout << "■" ;
- }
- if(i % 10 == 9){
- cout << endl;
- }
- }
- }
- void init_map() {
- for(int i = 0 ; i < 200 ; i++){
- if( i%10 == 0 || (i-9) % 10 == 0 || i > 190 ){
- map[i] = 1;
- }
- }
- }
- void draw_block(){
- //system("cls");
- for(int i = 0 ; i < 16 ; i++){
- if(newbstr[i] =='0'){
- cout << "□";
- }
- else{
- cout << "■";
- }
- if(i % 4 == 3){
- cout << endl;
- }
- }
- }
- bool move( int f , int t ){
- int i , j;
- //清除上一步F
- for( i = 3 ; i>=0 ; i-- ){
- for( j = 3; j >= 0 ; j-- ){
- if(bstr[i*4+j] !='0'){
- map[f+i*10+j] = 0;
- }
- }
- }
- //偵測碰撞(T點)
- bool collision = false;
- for( i = 3 ; i>=0 ; i-- ){
- for( j = 3; j >= 0 ; j-- ){
- if(bstr[i*4+j] != '0'){
- if(map[t+i*10+j] != 0){
- collision = true;
- }
- }
- }
- }
- int a;
- if(collision == false){
- a = t;
- }
- else{
- a = f;
- }
- for(i = 3 ; i >= 0 ; i--){
- for(j = 3 ; j >= 0 ; j--){
- if(bstr[i*4+j] !='0'){
- map[a+i*10+j] = bstr[i*4+j] - '0';
- }
- }
- }
- nowxy = a;
- return not collision;
- }
- void clockwise(){
- int r[16]= { 12,8,4,0, 13, 9 , 5 , 1 , 14,10,6,2,15,11,7,3};
- int i;
- string t = bstr;
- for(i = 0 ; i <=15 ; i++){
- t[i] = bstr[r[i]];
- }
- for(i = 0 ; i <=15 ; i++){
- bstr[i] = t[i];
- }
- }
- bool rotate(int f){
- int i , j;
- //清除上一步F
- for( i=3 ; i>=0 ; i-- ){
- for( j=3 ; j>=0 ; j-- ){
- if(bstr[i*4+j] !='0'){
- map[f+i*10+j] = 0;
- }
- }
- }
- clockwise();
- //偵測碰撞(T點)
- bool collision = false;
- for( i=3 ; i>=0 ; i-- ){
- for( j=3 ; j>=0 ; j--){
- if(bstr[i*4+j] != '0'){
- if(map[f+i*10+j] != 0){
- collision = true;
- }
- }
- }
- }
- if(collision == true){
- clockwise();
- clockwise();
- clockwise();
- }
- int a = f;
- for( i=3 ; i>=0 ; i-- ){
- for( j=3 ; j>=0 ; j--){
- if(bstr[i*4+j] !='0'){
- map[a+i*10+j] = bstr[i*4+j] -'0';
- }
- }
- }
- nowxy = a;
- return not collision;
- }
- bool check(int n){
- if(n<190){
- int x = n/10;
- for(int i = 0 ;i<10 ; i++){
- if(map[i+10*x]==0){
- return false;
- }
- }
- for(int i = x ; i >0 ; i--){
- for(int j = 1 ; j<9 ; j++){
- map[i*10+j] = map[(i-1)*10+j];
- }
- }
- }
- }
- int main(void)
- {
- int k; //紀錄你按到哪一個按鍵
- init_map();
- draw_map();
- //cin >> bid;
- bid = rand() % 7;
- bstr = block[bid];
- newbid = rand()%7;
- newbstr = block[newbid];
- nowxy = 3;
- move(3,3);
- draw_map();
- do{
- Sleep(500);
- if(!move(nowxy,nowxy+10)){
- check(nowxy);
- check(nowxy+10);
- check(nowxy+20);
- check(nowxy+30);
- if(nowxy<10){
- cout << "Game Over";
- break;
- }
- else{
- bid = newbid;
- bstr = newbstr;
- newbid = rand()%7;
- newbstr = block[newbid];
- nowxy=3;
- move(3,3);
- }
- }
- if(kbhit()){
- k = _getch();
- if( k == 'e' ){
- // from nowxy to nowxy+10
- rotate(nowxy);
- move(nowxy,nowxy);
- }
- else if( k == 'd' ){
- move( nowxy , nowxy + 10 );
- }
- else if( k == 's' ){
- move( nowxy , nowxy - 1 );
- }
- else if( k == 'f' ){
- move( nowxy , nowxy + 1 );
- }
- else if( k == 'q' ){
- break;
- }
- }
- draw_map();
- draw_block();
- }while(k!=27);
- return 0;
- }
Add Comment
Please, Sign In to add comment