# Block'd Puzzle Game..

Discussion in 'C' started by Programming_Kills, Dec 1, 2010.

1. ### Programming_KillsNew Member

Joined:
Jun 14, 2010
Messages:
19
0
Trophy Points:
0
i am implimenting Block'd a puzzle game same as bubble breaker and i wrote a code for deleting group of adjacent blocks and after deleting making the board compact again.

my delete block function gets two arguments and that is the the position of block cliked and my delete function will detect the adjacent blocks of same colour recursively..
and my compact() function will compact the board again.

the problem is it is a user mode version i need a cpu mode plz if anyone can help me how to do so??

Code:
```int CSameGameBoard::DeleteBlocks(int row, int col)
{
//  Make sure that the row and column are valid
if(row < 0 || row >= m_nRows || col %lt; 0 || col >= m_nColumns)
return -1;
//  Can't delete background blocks
int nColor = m_arrBoard[row][col];
if(nColor == 0)
return -1;
//    First check if there are any of the adjacent sides
//  with the same color
int nCount = -1;
if((row - 1 >= 0 && m_arrBoard[row - 1][col] == nColor) ||
(row + 1 < m_nRows && m_arrBoard[row + 1][col] == nColor) ||
(col - 1 >= 0 && m_arrBoard[row][col - 1] == nColor) ||
(col + 1 < m_nColumns && m_arrBoard[row][col + 1] == nColor))
{
//  Then call the recursive function to eliminate all
//  other touching blocks with same color
m_arrBoard[row][col] = 0;
nCount = 1;
//  Recursive call for up
nCount +=
DeleteNeighborBlocks(row - 1, col, nColor, DIRECTION_DOWN);
//  Recursive call for down
nCount +=
DeleteNeighborBlocks(row + 1,col, nColor, DIRECTION_UP);
//  Recursive call for left
nCount +=
DeleteNeighborBlocks(row, col - 1, nColor, DIRECTION_RIGHT);
//  Recursive call for right
nCount +=
DeleteNeighborBlocks(row, col + 1, nColor, DIRECTION_LEFT);
//  Finally compact the board
CompactBoard();
//  Remove the count from the number remaining
m_nRemaining -= nCount;
}
//  Return the total number of pieces deleted
return nCount;
}

int CSameGameBoard::DeleteNeighborBlocks(int row, int col, int color,
Direction direction)
{
//  Check if it is on the board
if(row < 0 || row >= m_nRows || col %lt; 0 || col >= m_nColumns)
return 0;
//  Check if it has the same color
if(m_arrBoard[row][col] != color)
return 0;
int nCount = 1;
m_arrBoard[row][col] = 0;
//  If we weren't told to not go back up, check up
if(direction != DIRECTION_UP)
nCount +=
DeleteNeighborBlocks(row - 1, col, color, DIRECTION_DOWN);
//  If we weren't told to not go back down, check down
if(direction != DIRECTION_DOWN)
nCount +=
DeleteNeighborBlocks(row + 1, col, color, DIRECTION_UP);
//  If we weren't told to not go back left, check left
if(direction != DIRECTION_LEFT)
nCount +=
DeleteNeighborBlocks(row, col - 1, color, DIRECTION_RIGHT);
//  If we weren't told to not go back right, check right
if(direction != DIRECTION_RIGHT)
nCount +=
DeleteNeighborBlocks(row, col + 1, color, DIRECTION_LEFT);
//  Return the total number of pieces deleted
return nCount;
}

void CSameGameBoard::CompactBoard(void)
{
//  First move everything down
for(int col = 0; col < m_nColumns; col++)
{
int nNextEmptyRow = m_nRows - 1;
int nNextOccupiedRow = nNextEmptyRow;
while(nNextOccupiedRow >= 0 && nNextEmptyRow >= 0)
{
//  First find the next empty row
while(nNextEmptyRow >= 0 &&
m_arrBoard[nNextEmptyRow][col] != 0)
nNextEmptyRow--;
if(nNextEmptyRow >= 0)
{
//  Then find the next occupied row from the next empty row
nNextOccupiedRow = nNextEmptyRow - 1;
while(nNextOccupiedRow >= 0 &&
m_arrBoard[nNextOccupiedRow][col] == 0)
nNextOccupiedRow--;
if(nNextOccupiedRow >= 0)
{
//  Now move the block from occupied to empty
m_arrBoard[nNextEmptyRow][col] =
m_arrBoard[nNextOccupiedRow][col];
m_arrBoard[nNextOccupiedRow][col] = 0;
}
}
}
}
//  Then move everything from right to left
int nNextEmptyCol = 0;
int nNextOccupiedCol = nNextEmptyCol;
while(nNextEmptyCol < m_nColumns && nNextOccupiedCol %lt; m_nColumns)
{
//  First find the next empty column
while(nNextEmptyCol < m_nColumns &&
m_arrBoard[m_nRows - 1][nNextEmptyCol] != 0)
nNextEmptyCol++;
if(nNextEmptyCol < m_nColumns)
{
//  Then find the next column with something in it
nNextOccupiedCol = nNextEmptyCol + 1;
while(nNextOccupiedCol < m_nColumns &&
m_arrBoard[m_nRows - 1][nNextOccupiedCol] == 0)
nNextOccupiedCol++;
if(nNextOccupiedCol < m_nColumns)
{
//  Move entire column to the left
for(int row = 0; row < m_nRows; row++)
{
m_arrBoard[row][nNextEmptyCol] =
m_arrBoard[row][nNextOccupiedCol];
m_arrBoard[row][nNextOccupiedCol] = 0;
}
}
}
}
}```
you can see the game here.