编程之美1.17――俄罗斯方块游戏(二)

2014-11-24 11:53:44 · 作者: · 浏览: 44
unt];
// 统计洞的个数
if (remlineCount)
clearLines(curchess, offsetY, remline);
int holeCount = calcHoles(curchess, offsetX, offsetY) -
calcHoles(chess, offsetX, offsetY);
score -= holeCount*4;
// 位置过高则扣分
if (holeCount > 5) score -= 15;
if (offsetY-remlineCount < VERT_LEN*3/5)
score -= VERT_LEN*3/5-(offsetY-remlineCount);
return score;
}

void output(unsigned char *curchess)
{
for (int j=0; j {
for (int i=0; i cout << curchess[j*HORIZ_LEN+i] << " ";
cout << endl;
}
}

int main()
{
srand(time(0));
int i, j, k, n, m;
// 初始化积木块
for (i=0; i for (j=0; j {
unsigned char l[BLOCK_AREA];
for (k=0; k scanf("%d",&l[k]);
blockSet[i][j].init(l);
}
// 初始化棋盘
for (i=0; i scanf("%d",&chess[i]);
// 显示前TOP_STEP步
int offsetX, offsetY;
unsigned char tmpchess[CHESS_AREA];
for (n=TOP_STEP; n>=0; n--)
{
output(chess);
calcHeight(chess); // 为每一步计算一次height数组,避免计算offsetY时过多的重复计算
int bind = rand()%BLOCK_SIZE; // 积木块的序号
int maxScore = -inf;
for (j=0; j {
const Block& b = blockSet[bind][j]; // 得到当前的积木块
for (offsetX=-b.minCol; offsetX {
// 计算从offsetX列落下,所落在棋盘的位置offsetY
offsetY = calcBottomOffsetY(b, offsetX);
if (offsetY<0) continue;
memcpy(tmpchess, chess, CHESS_AREA);
pasteTo(tmpchess, b, offsetX, offsetY); // 在棋盘中添加积木块
int curScore = calcScore(tmpchess, offsetX, offsetY); // 计算当前情况下的得分
if (curScore > maxScore)
{
maxScore = curScore; www.2cto.com
memcpy(nextChess, tmpchess, CHESS_AREA);
}
}
}
memcpy(chess, nextChess, CHESS_AREA);
}
}


作者:linyunzju