📄 cball.cpp
字号:
// CBall.cpp: implementation of the CCBall class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "LineColor.h"
#include "CBall.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCBall::CCBall()
{
memset(&m_nClearX, NULL, sizeof(m_nClearX));
}
CCBall::~CCBall()
{
}
void CCBall::Init()
{
memset(&m_nData, NULL, sizeof(m_nData));
}
void CCBall::SetActiveBall(int nX, int nY)
{
m_nCurX = nX;
m_nCurY = nY;
}
void CCBall::SwapActiveBall(int nX, int nY)
{
m_nCurX = nX;
m_nCurY = nY;
bMoveFlag = TRUE;
}
int CCBall::GetRandomPos()
{
int nX = 0;
int nY = 0;
while(1)
{
int nCount = 0;
for (int y=0; y<MAX_GRIDY; y++)
{
for (int x=0; x<MAX_GRIDX; x++)
{
if (m_nData[y][x] == 0)
{
nCount++;
}
}
}
if (nCount <= 3)
{
for (int y=0; y<MAX_GRIDY; y++)
{
for (int x=0; x<MAX_GRIDX; x++)
{
if (m_nData[y][x] == 0)
{
return y*10 + x;
}
}
}
return -1;
}
DWORD dwTime = ::GetTickCount();
//Sleep(1);
srand((unsigned)dwTime);
nX = rand()%MAX_GRIDX;
nY = rand()%MAX_GRIDY;
if (m_nData[nY][nX] == 0)
{
m_nData[nY][nX] = -1;
break;
}
}
return nY*10 + nX;
}
int CCBall::GetRandomColor()
{
int nCount = 0;
for (int y=0; y<MAX_GRIDY; y++)
{
for (int x=0; x<MAX_GRIDX; x++)
{
if (m_nData[y][x] == 0)
{
nCount++;
}
}
}
if (nCount < 2)
{
return -1;
}
DWORD dwTime = ::GetTickCount();
Sleep(1);
srand((unsigned)dwTime);
return rand()%MAX_COLOR + 1;
}
BOOL CCBall::IsExistBall(int nX, int nY)
{
if (m_nData[nY][nX] == 0)
{
return FALSE;
}
else
{
return TRUE;
}
}
void CCBall::MoveBall(int nX, int nY)
{
m_nData[nY][nX] = m_nData[m_nCurY][m_nCurX];
m_nData[m_nCurY][m_nCurX] = 0;
m_nCurX = -1;
m_nCurY = -1;
}
int CCBall::GetPreBallColor(int nItem)
{
return m_nPreColor[nItem];
}
int CCBall::GetBallColor(int nX, int nY)
{
return m_nData[nY][nX];
}
int CCBall::GetActiveBall()
{
return m_nCurY*10 + m_nCurX;
}
BOOL CCBall::IsCanMoveBall(int nX, int nY)
{
SetMaze();
for (int i=UP; i<=RIGHT; i++)
{
if (GetPath(m_nCurX, m_nCurY, nX, nY, i))
{
return true;
}
}
return false;
}
bool CCBall::CheckCanClear()
{
int nCount = 0;
int nClear = 0;
bool bFlag = false;
bool bck1=true, bck2=true, bck3=true, bck4=true;
memset(m_nClearX, NULL, sizeof(m_nClearX));
for (int nColor=1; nColor<=MAX_COLOR; nColor++)
{
int nRecordX[9];
int nRecordY[9];
//横向
if (bck1)
{
for (int y=0; y<9; y++)
{
for (int x=0; x<9; x++)
{
nCount = 0;
memset(nRecordX, 0, sizeof(nRecordX));
memset(nRecordY, 0, sizeof(nRecordX));
if (m_nData[y][x] == nColor)
{
//统计连续的个数
for (int i=x; i<9; i++)
{
if (m_nData[y][i] != nColor)
{
break;
}
else
{
nRecordX[nCount] = i;
nRecordY[nCount] = y;
nCount++;
}
}
if (nCount < 5)
{
nCount = 0;
memset(nRecordX, 0, sizeof(nRecordX));
memset(nRecordY, 0, sizeof(nRecordX));
}
else
{
bFlag = true;
bck1 = false;
for (int j=0; j<nCount; j++)
{
m_nClearX[nRecordY[j]][nRecordX[j]] = nColor;
}
goto out1;
}
}
}
}
}
out1:
//纵向
if (bck2)
{
for (int x=0; x<9; x++)
{
for (int y=0; y<9; y++)
{
nCount = 0;
memset(nRecordX, 0, 9*sizeof(int));
memset(nRecordY, 0, 9*sizeof(int));
if (m_nData[y][x] == nColor)
{
for (int i=y; i<9; i++)
{
if (m_nData[i][x] != nColor)
{
break;
}
else
{
nRecordX[nCount] = x;
nRecordY[nCount] = i;
nCount++;
}
}
if (nCount < 5)
{
nCount = 0;
memset(nRecordX, 0, 9*sizeof(int));
memset(nRecordY, 0, 9*sizeof(int));
}
else
{
bFlag = true;
bck2 = false;
for (int j=0; j<nCount; j++)
{
m_nClearX[nRecordY[j]][nRecordX[j]] = nColor;
}
goto out2;
}
}
}
}
}
out2:
//左斜
if (bck3)
{
//大循环是开始Y=0, X=4, X++直到为8; Y=8, X=0, X++直到为4;
//小循环是开始X--, Y下标++; 改变后, X下标++, Y下标--
int x, y;
for (y=0, x=4; x<9; x++)
{
nCount = 0;
memset(nRecordX, 0, 9*sizeof(int));
memset(nRecordY, 0, 9*sizeof(int));
int j = y;
for (int i=x; i>=0; i--, j++)
{
if (m_nData[j][i] == nColor)
{
nRecordX[nCount] = i;
nRecordY[nCount] = j;
nCount++;
}
else
if (nCount < 5)
{
nCount = 0;
memset(nRecordX, 0, 9*sizeof(int));
memset(nRecordY, 0, 9*sizeof(int));
}
else
{
bFlag = true;
bck3 = false;
for (int k=0; k<nCount; k++)
{
m_nClearX[nRecordY[k]][nRecordX[k]] = nColor;
}
TRACE("左斜1\n");
goto out3;
}
}
if (nCount < 5)
{
nCount = 0;
memset(nRecordX, 0, 9*sizeof(int));
memset(nRecordY, 0, 9*sizeof(int));
}
else
{
bFlag = true;
bck3 = false;
for (int k=0; k<nCount; k++)
{
m_nClearX[nRecordY[k]][nRecordX[k]] = nColor;
}
TRACE("左斜1\n");
goto out3;
}
}
//大循环是开始Y=0, X=4, X++直到为8; Y=8, X=0, X++直到为4;
//小循环是开始X--, Y下标++; 改变后, X下标++, Y下标--
for (y=8, x=0; x<=4; x++)
{
nCount = 0;
memset(nRecordX, 0, 9*sizeof(int));
memset(nRecordY, 0, 9*sizeof(int));
int j = x;
for (int i=y; i>=0; i--, j++)
{
if (m_nData[i][j] == nColor)
{
nRecordX[nCount] = j;
nRecordY[nCount] = i;
nCount++;
continue;
}
else
if (nCount < 5)
{
nCount = 0;
memset(nRecordX, 0, 9*sizeof(int));
memset(nRecordY, 0, 9*sizeof(int));
}
else
{
bFlag = true;
bck3 = false;
for (int k=0; k<nCount; k++)
{
m_nClearX[nRecordY[k]][nRecordX[k]] = nColor;
}
TRACE("左斜2\n");
goto out3;
}
}
if (nCount < 5)
{
nCount = 0;
memset(nRecordX, 0, 9*sizeof(int));
memset(nRecordY, 0, 9*sizeof(int));
}
else
{
bFlag = true;
bck3 = false;
for (int k=0; k<nCount; k++)
{
m_nClearX[nRecordY[k]][nRecordX[k]] = nColor;
}
TRACE("左斜1\n");
goto out3;
}
}
}
out3:
//右斜
if (bck4)
{
//大循环是开始Y=0, X=4, X--直到为0; Y=8, X=4, X++直到为8;
//小循环是开始X++, Y下标++, 改变后, X下标--, Y下标--
int x, y;
for (y=0, x=4; x>=0; x--)
{
nCount = 0;
memset(nRecordX, 0, 9*sizeof(int));
memset(nRecordY, 0, 9*sizeof(int));
int j = y;
for (int i=x; i<9; i++, j++)
{
if (m_nData[j][i] == nColor)
{
nRecordX[nCount] = i;
nRecordY[nCount] = j;
nCount++;
}
else
if (nCount < 5)
{
nCount = 0;
memset(nRecordX, 0, 9*sizeof(int));
memset(nRecordY, 0, 9*sizeof(int));
}
else
{
bFlag = true;
bck4 = false;
for (int k=0; k<nCount; k++)
{
m_nClearX[nRecordY[k]][nRecordX[k]] = nColor;
}
TRACE("右斜1\n");
goto out4;
}
}
if (nCount < 5)
{
nCount = 0;
memset(nRecordX, 0, 9*sizeof(int));
memset(nRecordY, 0, 9*sizeof(int));
}
else
{
bFlag = true;
bck4 = false;
for (int k=0; k<nCount; k++)
{
m_nClearX[nRecordY[k]][nRecordX[k]] = nColor;
}
TRACE("右斜1\n");
goto out4;
}
}
//大循环是开始Y=0, X=4, X++直到为8; Y=8, X=0, X++直到为4;
//小循环是开始X--, Y下标++; 改变后, X下标--, Y下标--
for (y=8, x=4; x<=8; x++)
{
nCount = 0;
memset(nRecordX, 0, 9*sizeof(int));
memset(nRecordY, 0, 9*sizeof(int));
int j = y;
for (int i=x; i>=0; i--, j--)
{
if (m_nData[j][i] == nColor)
{
nRecordX[nCount] = i;
nRecordY[nCount] = j;
nCount++;
}
else
if (nCount < 5)
{
nCount = 0;
memset(nRecordX, 0, 9*sizeof(int));
memset(nRecordY, 0, 9*sizeof(int));
}
else
{
bFlag = true;
bck4 = false;
for (int k=0; k<nCount; k++)
{
m_nClearX[nRecordY[k]][nRecordX[k]] = nColor;
}
TRACE("右斜2\n");
goto out4;
}
}
if (nCount < 5)
{
nCount = 0;
memset(nRecordX, 0, 9*sizeof(int));
memset(nRecordY, 0, 9*sizeof(int));
}
else
{
bFlag = true;
bck3 = false;
for (int k=0; k<nCount; k++)
{
m_nClearX[nRecordY[k]][nRecordX[k]] = nColor;
}
TRACE("右斜2\n");
goto out4;
}
}
}
}
out4:
if (bFlag)
{
for (int x=0; x<9; x++)
{
for (int y=0; y<9; y++)
{
if (m_nClearX[y][x] != 0)
{
m_nData[y][x] = 0;
}
}
}
}
return bFlag;
}
void CCBall::SetMaze()
{
memset(&m_mark, NULL, sizeof(m_mark));
memset(&m_maze, NULL, sizeof(m_maze));
m_move[UP].x = 0;
m_move[UP].y = -1;
m_move[DOWN].x = 0;
m_move[DOWN].y = 1;
m_move[LEFT].x = -1;
m_move[LEFT].y = 0;
m_move[RIGHT].x = 1;
m_move[RIGHT].y = 0;
for (int y=0; y<9; y++)
{
for (int x=0; x<9; x++)
{
if (m_nData[y][x] == 0)
{
m_maze[y+1][x+1] = 1;
}
}
}
}
bool CCBall::GetPath(int xstart, int ystart, int xend, int yend, int dir)
{
int x0 = xstart + 1;
int y0 = ystart + 1;
int x1 = xend + 1;
int y1 = yend + 1;
m_mark[y0][x0] = 1;
SeqStack<ITEM> st(11 * 11);
// SeqStack<ITEM> s(11 * 11);
ITEM tmpParent, tmpChild;
tmpParent.x = x0;
tmpParent.y = y0;
tmpParent.dir = UP;
st.Push(tmpParent);
int nStep = 0;
while (!st.IsEmpty())
{
for (int dir=UP; dir<=RIGHT; dir++)
{
tmpChild.x = tmpParent.x + m_move[dir].x;
tmpChild.y = tmpParent.y + m_move[dir].y;
tmpChild.dir = dir;
st.Push(tmpChild);
if (m_mark[tmpChild.y][tmpChild.x] == 1 ||
m_maze[tmpChild.y][tmpChild.x] == 0)
{
st.Pop();
continue;
}
if (tmpChild.x == x1 && tmpChild.y == y1)
{
/* FILE *fp = fopen("1.txt", "w");
if (fp != NULL)
{
ITEM tmp;
while (!s.IsEmpty())
{
tmp = s.Pop();
fprintf(fp, "%d %d %d\n",
tmp.x, tmp.y, tmp.dir);
}
}
fclose(fp);
*/
ClearMaze();
return true;
}
nStep++;
// s.Push(tmpChild);
m_mark[tmpChild.y][tmpChild.x] = 1;
}
tmpParent = st.Pop();
}
ClearMaze();
return false;
}
void CCBall::ClearMaze()
{
memset(&m_mark, NULL, sizeof(m_mark));
memset(&m_maze, NULL, sizeof(m_maze));
}
void CCBall::GrowPreBall(int nNum)
{
for (int i=0; i<nNum; i++)
{
m_nPreColor[i] = GetRandomColor();
}
}
int CCBall::GetFirstColor()
{
return GetRandomColor();
}
//第一次
bool CCBall::GrowBallForFirst(int &x, int &y, int &color)
{
int nRet = GetRandomPos();
if (nRet == -1)
{
return false;
}
x = nRet%10;
y = nRet/10;
color = GetRandomColor();
if (color == -1)
{
return false;
}
m_nData[y][x] = color;
m_nCurX = -1;
m_nCurY = -1;
return true;
}
bool CCBall::GrowBall(int &x, int &y, int &color, int index)
{
int nRet = GetRandomPos();
if (nRet == -1)
{
return false;
}
x = nRet % 10;
y = nRet / 10;
color = m_nPreColor[index];
m_nData[y][x] = color;
m_nCurX = -1;
m_nCurY = -1;
return true;
}
bool CCBall::IsOver()
{
int nCount = 0;
for (int y=0; y<MAX_GRIDY; y++)
{
for (int x=0; x<MAX_GRIDX; x++)
{
if (m_nData[y][x] == 0)
{
nCount++;
}
}
}
if (nCount == 0)
{
return false;
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -