谁帮忙解释下这段c++代码
俄罗斯方块代码。帮我解释下我做记号的那段代码,谢谢#include <iostream>
#include <ctime>
using namespace std;
#define ROW 10
#define COL 10
//-------------------------- 声明函数-----------------
void bDown();//向下走
void bleft();//向左走
void bRight();//向右走
void bBian();//变形
void cRanShape();//初始化随机方块
void arrView();//显示界面
void fenshu();//计算分数
//----------定义方块的结构------------------
struct tagShape
{
int x;//当前方块的横坐标
int y;//当前方块的纵坐标
int rB;//随机大变化;
int rS;//随机小变化;
int iKey;//判断是否可以初始化;
int iX;//方块的行数
int iY;//方块的列数
int iOver;//游戏结束
int ifen;//当前得分
}tShape;
tagShape & T_Shape = tShape;//定义结构引用
//------------------定义全局变量-----------------------------------------
int g_arrInfo[ROW][COL] = {0}; //定义环境二维数组全局变量
int i = 0 , j = 0;//用于FOR循环的临时变量;
int g_arrBox[7][4][5][4] = //---------定义方块变化四维数组全局变量,共七次大变化,四次小变化,每次变化四四格
{
//---------竖条变化开始(0)----------;
{
{
{1,1,1,1},
{0,0,0,0}
},
{
{1,0,0,0},
{1,0,0,0},
{1,0,0,0},
{1,0,0,0},
{0,0,0,0}
},
{
{1,1,1,1},
{0,0,0,0}
},
{
{1,0,0,0},
{1,0,0,0},
{1,0,0,0},
{1,0,0,0},
{0,0,0,0}
}
},
//---------竖条变化结束----------;
//----------右弯变化开始(1)----------
{
{
{1,1,1,0},
{1,0,0,0},
{0,0,0,0}
},
{
{1,1,0,0},
{0,1,0,0},
{0,1,0,0},
{0,0,0,0}
},
{
{0,0,1,0},
{1,1,1,0},
{0,0,0,0}
},
{
{1,0,0,0},
{1,0,0,0},
{1,1,0,0},
{0,0,0,0}
}
},
//----------右弯变化结束----------
//-----------左弯变化开始(2)---------;
{
{
{1,1,1,0},
{0,0,1,0},
{0,0,0,0}
},
{
{0,1,0,0},
{0,1,0,0},
{1,1,0,0},
{0,0,0,0}
},
{
{1,0,0,0},
{1,1,1,0},
{0,0,0,0}
},
{
{1,1,0,0},
{1,0,0,0},
{1,0,0,0},
{0,0,0,0}
}
},
//----------左弯变化结束----------
//--------土变化开始(3)--------------
{
{
{1,1,1,0},
{0,1,0,0},
{0,0,0,0}
},
{
{0,1,0,0},
{1,1,0,0},
{0,1,0,0},
{0,0,0,0}
},
{
{0,1,0,0},
{1,1,1,0},
{0,0,0,0}
},
{
{1,0,0,0},
{1,1,0,0},
{1,0,0,0},
{0,0,0,0}
}
},
//-----------土变化结束------------
//-----------左闪变化开始(4)------------
{
{
{1,1,0,0},
{0,1,1,0},
{0,0,0,0}
},
{
{0,1,0,0},
{1,1,0,0},
{1,0,0,0},
{0,0,0,0}
},
{
{1,1,0,0},
{0,1,1,0},
{0,0,0,0}
},
{
{0,1,0,0},
{1,1,0,0},
{1,0,0,0},
{0,0,0,0}
}
},
//-----------左闪变化结束------------
//-----------右闪变化开始(5)------------
{
{
{0,1,1,0},
{1,1,0,0},
{0,0,0,0}
},
{
{1,0,0,0},
{1,1,0,0},
{0,1,0,0},
{0,0,0,0}
},
{
{0,1,1,0},
{1,1,0,0},
{0,0,0,0}
},
{
{1,0,0,0},
{1,1,0,0},
{0,1,0,0},
{0,0,0,0}
}
},
//-----------右闪变化结束------------
//-----------田变化开始(6)------------
{
{
{1,1,0,0},
{1,1,0,0},
{0,0,0,0}
},
{
{1,1,0,0},
{1,1,0,0},
{0,0,0,0}
},
{
{1,1,0,0},
{1,1,0,0},
{0,0,0,0}
},
{
{1,1,0,0},
{1,1,0,0},
{0,0,0,0}
}
}
};
//---------结束定义方块变化四维数组全局变量-----------------------
int main()
{
cRanShape(); //先随机一个方块
int iFangxiang = 0;//输入方向
T_ = 0;//游戏结束初始化为0,为1时则结束游戏
T_Shape.ifen = 0;//当前得分初始化为0
enum {Left=1,Right,Down,Bianxing};
while (1)
{
switch (iFangxiang)
{
case Left:
bleft();
break;
case Right:
bRight();
break;
case Down:
bDown();
break;
case Bianxing:
bBian();
break;
}
if (T_Shape.iKey) //如果到达最后就重新生成方块,并计算得分;
{
fenshu();
cRanShape();
}
arrView();
if (T_ == 1)//游戏结束退出循环
{
cout << "-------------------------游戏结束,总得分:" << T_Shape.ifen <<"--------------------------" << endl;
break;
}
cout << "当前得分:" << T_Shape.ifen << " " << "请输入方向(1左2右3下4变形):";
cin >> iFangxiang;
}
return 0;
}
void cRanShape()//初始化随机方块
{
int iCenter = COL / 2;
srand( time(0) );
T_Shape.rB = rand() % 7;//随机方块7次大变化
T_Shape.rS = rand() % 4;//随机方块4次小变化
int iTmp = 0;
T_Shape.iKey = 0;//初始化0,如果为1就可以执行下次循环
T_Shape.x = 0; //方块现在坐标位置的横坐标
T_Shape.y = iCenter - 2; //方块现在坐标位置的纵坐标
for (i = 0; i < T_Shape.iX; ++i)//游戏结束条件
{
for (j = 0; j < T_Shape.iY; ++j)
{
if (g_arrBox[T_Shape.rB][T_Shape.rS][i][j] == 1 && g_arrInfo[T_Shape.x + i][T_Shape.y + j] == 1)
{
T_ = 1;
break;
}
}
}
//初始化方块大小为零
T_Shape.iX = 0;
T_Shape.iY = 0;
for (i = 0; i < 4; ++i) //方块赋值并计算方块的大小
{
for (j = 0; j < 4; ++j)
{
if (g_arrInfo[i][j + iCenter - 2] == 0)//如果空间为0则赋值
{
g_arrInfo[i][j + iCenter - 2] = g_arrBox[T_Shape.rB][T_Shape.rS][i][j];
}
if (g_arrBox[T_Shape.rB][T_Shape.rS][i][j] == 1)//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!从这里开始都是做什么的?我看不出现在是什么状态
{
iTmp = 1;
}
if (g_arrBox[T_Shape.rB][T_Shape.rS][i][j] == 1 && g_arrBox[T_Shape.rB][T_Shape.rS][i + 1][j] == 0)//计算方块列数
{
T_Shape.iY += 1;
}
}
if (iTmp == 1)//只要该行有一个方块为1,则为一行,临时变量ITMP清零
{
T_Shape.iX += 1;
iTmp = 0;
}
}
if (T_Shape.iY == 0)//如果是竖条就加一
{
T_Shape.iY = 1;
}
}
void arrView() //显示界面
{
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
if (g_arrInfo[i][j])
cout << "■";
else
cout << "□";
}
cout << endl;
}
}
//---------------------------向下走开始--------------------------------
void bDown()
{
for (i = 0; i < T_Shape.iX; ++i)//先判断能不能向下走--
{
for (j = 0; j < T_Shape.iY; ++j)
{
if (g_arrBox[T_Shape.rB][T_Shape.rS][i][j] == 1 && g_arrBox[T_Shape.rB][T_Shape.rS][i + 1][j] == 0 && g_arrInfo[T_Shape.x + i + 1][T_Shape.y + j] == 1 || T_Shape.x + T_Shape.iX == ROW)
{
T_Shape.iKey = 1;
}
}
}
if (T_Shape.iKey == 0) //如果可以向下走就开始执行
{
for (i = T_Shape.x + T_Shape.iX ; i > 0 ; --i)
{
for (j = T_Shape.y; j < T_Shape.y + T_Shape.iY; ++j)
{
if (g_arrInfo[i][j] == 0 && g_arrInfo[i - 1][j] == 1 )
{
g_arrInfo[i][j] = 1;
g_arrInfo[i - 1][j] =0;
}
}
}
T_Shape.x += 1; //往下走加一
}
}
//---------------------------向下走结束--------------------------------
//------------------------向左走开始-----------------------
void bleft()
{
int iTmp = 0;
for (i = 0; i < T_Shape.iX; ++i)//先判断能不能向左走--
{
for (j = 0; j < T_Shape.iY; ++j)
{
if (g_arrBox[T_Shape.rB][T_Shape.rS][i][j] == 1 && g_arrBox[T_Shape.rB][T_Shape.rS][i][j - 1] == 0 && g_arrInfo[T_Shape.x + i][T_Shape.y + j - 1] == 1 || T_Shape.y == 0)
{
iTmp = 1;
}
}
}
if (iTmp == 0)
{
for (i = T_Shape.x; i < T_Shape.x + T_Shape.iX; ++i)
{
for (j = T_Shape.y; j < T_Shape.y + T_Shape.iY; ++j)
{
if (g_arrInfo[i][j - 1] == 0 && g_arrInfo[i][j] == 1)
{
g_arrInfo[i][j - 1] = g_arrInfo[i][j];
g_arrInfo[i][j] = 0;
}
}
}
T_Shape.y -= 1;
}
}
//----------------------------------------向左走结束------------------------------------------
//---------------------------向右走开始------------------------------------------
void bRight()
{
int iTmp = 0;
for (i = 0; i < T_Shape.iX; ++i)//先判断能不能向左走--
{
for (j = 0; j < T_Shape.iY; ++j)
{
if (g_arrBox[T_Shape.rB][T_Shape.rS][i][j] == 1 && g_arrBox[T_Shape.rB][T_Shape.rS][i][j + 1] == 0 && g_arrInfo[T_Shape.x + i][T_Shape.y + j + 1] == 1 || T_Shape.y + T_Shape.iY == COL)
{
iTmp = 1;
}
}
}
if (iTmp == 0)
{
for (i = T_Shape.x; i < T_Shape.x + T_Shape.iX; ++i)
{
for (j = T_Shape.y + T_Shape.iY; j > T_Shape.y; --j)
{
if (g_arrInfo[i][j] == 0 && g_arrInfo[i][j - 1] == 1)
{
g_arrInfo[i][j] = g_arrInfo[i][j - 1];
g_arrInfo[i][j - 1] = 0;
}
}
}
T_Shape.y += 1;
}
}
//--------------向右走结束----------------------------
//-------------------------------------变形开始--------------------------------------------------------------
void bBian()
{
for (i = 0; i < T_Shape.iX; ++i)//先清零,再计算
{
for (j = 0; j < T_Shape.iY; ++j)
{
if (g_arrBox[T_Shape.rB][T_Shape.rS][i][j] == 1 )
{
g_arrInfo[T_Shape.x + i][T_Shape.y + j] = 0;
}
}
}
//-----设置一个临时变量用来判断当前环境可否变形,每变一次形就将小类加一,如果大于三自动赋零------------
int iRs = T_Shape.rS;
iRs += 1;
if (iRs > 3)
{
iRs = 0;
}
int iTmp = 0 , iOk = 0;
int iX = 0; //定义临时方块大小变量ix,iy,用来测当前方块可否变形
int iY = 0;
//--------计算方块大小开始-------------
for (i = 0; i < 4; ++i)
{
for (j = 0; j < 4; ++j)
{
if (g_arrBox[T_Shape.rB][iRs][i][j] == 1)//计算方块行数
{
iTmp = 1;
}
if (g_arrBox[T_Shape.rB][iRs][i][j] == 1 && g_arrBox[T_Shape.rB][iRs][i + 1][j] == 0)//计算方块列数
{
iY += 1;
}
}
if (iTmp == 1)
{
iX += 1;
iTmp = 0;
}
}
if (iY == 0)//如果是竖条就加一
{
iY = 1;
}
//--------计算方块大小结束-------------
//-----------再来计算当前状态可否变化-------------------------
for (i = 0; i < iX; ++i)
{
for (j = 0; j < iY; ++j)
{
if (g_arrBox[T_Shape.rB][iRs][i][j] == 1 && g_arrInfo[T_Shape.x + i][T_Shape.y + j] == 1 || T_Shape.x + iX >= ROW || T_Shape.y + iY >= COL)
{
iOk = 1;
}
}
}
//-----------------计算结束-----------------------------
if (iOk == 0)//达到条件就变形,否则还原
{
for (i = 0; i < iX; ++i)
{
for (j = 0; j < iY; ++j)
{
if (g_arrBox[T_Shape.rB][iRs][i][j] == 1)
{
g_arrInfo[T_Shape.x + i][T_Shape.y + j] = 1;
}
}
}
T_Shape.rS = iRs; T_Shape.iX = iX; T_Shape.iY = iY;
}
else
{
for (i = 0; i < T_Shape.iX; ++i)//先清零,再计算
{
for (j = 0; j < T_Shape.iY; ++j)
{
if (g_arrBox[T_Shape.rB][T_Shape.rS][i][j] == 1 )
{
g_arrInfo[T_Shape.x + i][T_Shape.y + j] = 1;
}
}
}
}
}
//--------------------计算分数开始--------------------------------------------------------------------------
void fenshu()
{
int iTmp = 0 , iTmpFen = 1;
int k,l;//临时的小变量
for (i = T_Shape.x; i < T_Shape.x + T_Shape.iX; ++i)
{
for (j = 0; j < COL; ++j)
{
if (g_arrInfo[i][j] == 0)//只要该行有一个为0,则不能得分
{
iTmp = 1;
}
}
if (iTmp == 1)
{
iTmp = 0;//如果不能得分则清零重新循环
}
else
{
iTmpFen *= 2;//得分(消一行加2分,两行4分,三行8分,四行16分)
for (k = i; k > 0; --k)
{
for (l = 0; l < COL; ++l)//去掉消行的格数
{
g_arrInfo[k][l] = g_arrInfo[k - 1][l];
g_arrInfo[k - 1][l] = 0;
//cout << k << " , " << j << endl;
}
}
}
}
T_Shape.ifen += iTmpFen;
}
[ 本帖最后由 qqbwz 于 2009-12-31 18:34 编辑 ]