/** * Validate table check if it has empty or full column and rows. */ bool Table::validate( ) { int i, x; // must be invalidated to validate if (!validated) { i = x = 0; // check rows while (i < height && !x) { x = lines[i]->empty( ) | lines[i]->full( ); i++; } i = 0; // check columns while (i < width && !x) { x = emptyColumn(i) | fullColumn(i); i++; } // validate if (!x) validated = true; } return validated; }
/** * Set value in row and column. */ int Table::setValue(int value, int row, int col) { int pos; int ret = -1; if (validated && row >= 0 && row < height) { // was the value added if (!lines[row]->setValue(value, col)) { // check if not violates table if ((value ? fullRow(row) : emptyRow(row)) || (value ? fullColumn(col) : emptyColumn(col)) || duplicatedRow(row, &pos)) lines[row]->setValue((value ? 0 : 1), col); else ret = 0; // no error } } return ret; }
void gameStart(int mode) { bool success;//是否下落成功;若失败,则说明沉底 int i, j, k, t = 0; int cmd = 0; int timeLeft[TOTAL_PLAYER+1] = {0, 1000, 1000}; Block tpBlk[TOTAL_PLAYER+1]; gameInit();//初始化游戏 while(1) { for(i = 1; i <= TOTAL_PLAYER; i++) { t++;//计时器增加 setorigin(0, 0); //设置原点 recoverBk(0, g_player[i].preview.y + 4 * BLOCK_SIZE, 160, 30); recoverBk(0, 480 - 30, 160, 30); //重绘预览区 printScore(t);//显示得分 drawStageLine();//绘制边界线 FlushBatchDraw();//执行绘图 if(g_player[i].dropped == true) { saveState(i, g_player[i].nowBlk);//保存方块的存在状态 if(mode == CLEAR && g_player[1+(i==1)].score >= 100)gameOver(i); if(reachTop(i)) { gameOver(i); continue; }//判断游戏结束 k = fullColumn(i); if(k > 0) { g_player[i].score += k * k; if(mode == NORMAL && g_player[i].score % 10 == 0)newColumn(1 + (i == 1)); }//计分与消行 tpBlk[i] = g_player[i].preBlk; //备份预览方块 g_player[i].nowBlk = preToNow(g_player[i].preBlk);//previewBlock变为player[1].nowBlk g_player[i].preBlk = createBlk(); //产生新的预览方块 refreshBlk(i, tpBlk[i], g_player[i].preBlk, PREVIEW); //绘制新的previewBlock g_player[i].dropped = false; } getCmd(&cmd);//获得命令 timeLeft[i] -= 10; for (j = 1; j <= TOTAL_PLAYER; j++) { tpBlk[j] = g_player[j].nowBlk; success = DispatchCommand(j, cmd); if(success)refreshBlk(j, tpBlk[j], g_player[j].nowBlk); else continue; }//在一个玩家的循环中要同时处理两个玩家 if(timeLeft[i] <= 0) { if(mode == NORMAL)timeLeft[i] = INIT_DELAY - (int)(2.5 * g_player[1+(i==1)].score); else timeLeft[i] = (int)(INIT_DELAY - (t / 1000.0)); //速度规则 if(timeLeft[i] < MIN_DELAY)timeLeft[i] = 3 * MIN_DELAY; tpBlk[i] = g_player[i].nowBlk; if(i == 1) success = DispatchCommand(1, CMD_DOWN); else success = DispatchCommand(2, CMD_DOWN2); if(success)refreshBlk(i, tpBlk[i], g_player[i].nowBlk); } } } getch(); closegraph(); }