2 weixin 36153180 weixin_36153180 于 2016.09.16 17:21 提问

俄罗斯方块C++程序,遇到死循环的情况,求助

#ifndef RB_H

#define RB_H

//长条形
const int A1=1;
const int A1_1=8;
//田字
const int A2=2;
//z字
const int A3=3;
const int A3_1=9;
//反z字
const int A4=4;
const int A4_1=10;
//T字
const int A5=5;
const int A5_1=11;
const int A5_2=12;
const int A5_3=13;
//L形
const int A6=6;
const int A6_1=14;
const int A6_2=15;
const int A6_3=16;
const int A6_4=17;
//反L形
const int A7=7;
const int A7_1=18;
const int A7_2=19;
const int A7_3=20;
const int A7_4=21;

class RB
{
private:
int score;
int level;
int ID;
int hotPointx;
int hotPointy;
int top;
public:
RB()
{
score=0;
level=1;
top=30;

}

// void GetHotPoint();
void DrawMap();
void DrawBlock(int x,int y,int id);
void DeleteBlock(int x,int y,int id);
int Rotate(int id);
int Judge(int x,int y,int id);
void UpdateMap(int id);
void RunGame();
};

#endif

#include"RB.h"
#include
using namespace std;
#include
#include

int Block[19][8]=
{
{0,0,0,1,0,2,0,3},//一字
{-1,1,0,1,1,1,2,1},

{0,0,1,0,1,1,0,1},//田形

{0,0,0,1,1,1,1,2},//z形    
{0,1,1,1,1,0,2,0},

{0,2,0,1,1,1,1,0},//反z形
{0,0,1,0,1,1,2,1},

{0,0,1,0,2,0,1,1},//T形
{1,0,0,1,1,1,2,1},
{1,0,0,1,1,1,1,2},
{0,0,0,1,0,2,1,1},

{0,0,0,1,0,2,1,2},//L形
{0,0,0,1,1,0,2,0},
{0,0,1,0,1,1,1,2},
{2,0,0,1,1,1,2,1},

{1,0,1,1,1,2,1,2},//反L形
{0,0,0,1,1,1,2,1},
{0,0,0,1,0,2,1,0},
{0,0,1,0,2,0,2,1}

};
int height[19]={1,4,2,2,3,2,3,3,3,3,3,3,2,3,2,3,2,3,2};
int map[12][24]={0};

void setCurPos(int i, int j, int color = 1 | 2 | 4)//

{

HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出句柄

SetConsoleTextAttribute(out, color);// //设置颜色

COORD pos = {2*i, j };

SetConsoleCursorPosition(out, pos);// //设置位置

}

//旋转方块
int RB::Rotate(int ID)
{

switch(ID)
{
case 0:ID=1;break;
case 1:ID=0;break;

case 2:ID=2;break;

case 3:ID=4;break;
case 4:ID=3;break;

case 5:ID=6;break;

case 6:ID=5;break;

case 7:ID=8;break;
case 8:ID=9;break;
case 9:ID=10;break;
case 10:ID=7;break;

case 11:ID=12; break;
case 12:ID=13;break;
case 13:ID=14;break;
case 14:ID=15;break;

case 15:ID=16; break;
case 16:ID=17;break;
case 17:ID=18;break;
case 18:ID=15;break;

}
return ID;
}

//画出砖块
void RB::DrawBlock(int x,int y,int id)
{

int nx=0,ny=0;
for(int i=0;i<4;i++)
{
nx=x+Block[id][2*i];
ny=y+Block[id][2*i+1];

  setCurPos(nx+1,ny+1);
  cout<<"■";

}
}
//消除砖块
void RB::DeleteBlock(int x,int y,int id)
{
int nx=0,ny=0;
for(int i=0;i<4;i++)
{
nx=x+Block[id][2*i];
ny=y+Block[id][2*i+1];
setCurPos(nx+1,ny+1);
cout<<" ";
}
}
//画出游戏的边框
void RB::DrawMap()
{
for(int i=0;i<21;i++)
{
setCurPos(i,0);
cout<<"■";
setCurPos(i,25);
cout<<"■";
}
for(int j=1;j<25;j++)
{
setCurPos(0,j);
cout<<"■";
setCurPos(13,j);
cout<<"■";
setCurPos(20,j);
cout<<"■";
}
for( i=14;i<21;i++)
{
setCurPos(i,7);
cout<<"■";
}

}

int RB::Judge(int x,int y,int id)
{int nx=0,ny=0;
for(int i=0;i {
nx=x+Block[id][2*i];
ny=y+Block[id][1+2*i];
if(nx>0||nx0||ny return 1;
}
return 0;
}
//更新画面
void RB::UpdateMap(int id)
{
int flag=0;
int clear=0;
int nx=0;int ny=0;
// for(int i=0;i // {
// nx=hotPointx+Block[id][i*2];
// ny=hotPointy+Block[id][2*i+1];
// map[nx][ny]=1;
// }
if(hotPointy top=hotPointy;
for(int j=hotPointy;j {
flag=0;
for(int i=0;i {
if(map[i][j]==0)
flag=0;
break;
}
if(flag==1)
{
for(int k = j; k >= top; k--)
{
if (k == 0) //消除 到了最顶行

{

for (int i = 1; i < 13; i++)
{

map[i][k] = 0;

setCurPos(i,k + 1);

cout << " ";

}

}

else

{

for (int i = 0; i < 12; i++)

{

map[i][k] = map[i][k - 1];

setCurPos(i, k + 1); //因为有两条图形框边,游戏运行过程中的坐标系相对于 真正的坐标系是x、y都加 1 的

if (map[i][k] == 0) //被删除行的 上一行是空的,所以这一行也是空的

cout << " ";

else //上一行不是空的

{

int tempColor = map[i][k];

setCurPos(i + 1, k + 1, tempColor);

cout << "■";

}

}

}

}
top++; // //消除成功,最高点下降一个

clear++;

score += clear * 100;

    }
}

}
void RB::RunGame()
{

DrawMap();

int curBlock=0;
int nextBlock=0;
int Count=0;
int i=0;
char x=' ';
hotPointx=6;hotPointy=0;
curBlock=rand()%7;
nextBlock=rand()%7;
DrawBlock(hotPointx,hotPointy,curBlock);
DrawBlock(15,2,nextBlock);
Count = 10000 - 1000 * level;  
while(1)
{

    if(i>Count)
    {
        if(Judge(hotPointx,hotPointy+1,curBlock)==0)
        {
            //落到底一瞬间发生了什么
            UpdateMap(curBlock);
            DeleteBlock(15,2,nextBlock);

            curBlock=nextBlock;
            hotPointx=6;hotPointy=0;

            nextBlock=rand()%7;
            DrawBlock(hotPointx,hotPointy,curBlock);
            DrawBlock(15,2,nextBlock);
            if (Judge(hotPointx, hotPointy, curBlock) == 0)      //无法绘制当前图形  
            {  
                system("cls");  
                cout << "游戏结束!!!最终得分为:" << score << endl;  
                system("pause");  
                exit(0);  
            }
        }
        else
        {
            DeleteBlock(hotPointx,hotPointy,nextBlock);
            hotPointy++;
            DrawBlock(hotPointx,hotPointy,nextBlock);
        }
    }

    if(_kbhit())
    {
        x=_getch();
        if(x=='a'||x=='A')
        {

            if(Judge(hotPointx-1,hotPointy,curBlock))
            {
                DeleteBlock(hotPointx,hotPointy,curBlock);
                hotPointx-=1;
                DrawBlock(hotPointx,hotPointy,curBlock);
            }
        }
        else if(x=='s'||x=='S')
        {
            if(Judge(hotPointx,hotPointy+1,curBlock))
            {
                DeleteBlock(hotPointx,hotPointy,curBlock);
                hotPointy+=1;
                DrawBlock(hotPointx,hotPointy,curBlock);
            }
        }
        else if(x=='d'||x=='D')
        {
            if(Judge(hotPointx+1,hotPointy,curBlock))
            {
                DeleteBlock(hotPointx,hotPointy,curBlock);
                hotPointx+=1;
                DrawBlock(hotPointx,hotPointy,curBlock);
            }
        }
        else if(x=='w'||x=='W')
        {
            int temp = curBlock;  
            curBlock = Rotate(curBlock);  
            if (Judge(hotPointx, hotPointy, curBlock))  
            {  
                DeleteBlock(hotPointx, hotPointy, temp);  
                DrawBlock(hotPointx, hotPointy, curBlock);  
            }  
            else  
                curBlock = temp; 
        }
        while(_kbhit())
        {
            _getch();   
        }

    }
    i++;

}

}

void main()
{

RB r1;
r1.RunGame();

}

2个回答

WANGBIN890615
WANGBIN890615   2016.09.18 18:01
已采纳

代码我就不看了, 遇到死循环,一个是他的循环开始的地方,一个是终止的地方,一个是变化量,看你这情况,
肯定是变化量的地方出了问题,没达到终止条件,你可以这么测试,让变量更快的达到终止条件,如果可以证明终止条件没问题,变化量出了问题,另一个是修改变化量,测试是不是循环条件出了问题。找到问题了,自然就找到解决方案了。

weixin_36153180
weixin_36153180 多谢,我试试
大约一年之前 回复
u012930316
u012930316   2016.09.18 18:10

你这代码这么多...能说明下到底是哪个地方死循环了么.....

weixin_36153180
weixin_36153180 运行的时候一直在窗口中画方格,停不下来。
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片