?? shuductrl.cpp
字號:
int l = GameRect.left;
int t = GameRect.top;
CRect drawRect;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(MarkData[i][j] == 1)
{
drawRect.left = l + j * w;
drawRect.right = drawRect.left + w;
drawRect.top = t + i * h;
drawRect.bottom = drawRect.top + h;
drawRect.DeflateRect(1,1,1,1);
pDC->FillSolidRect(&drawRect,RGB(210,210,210));
}
}
}
}
void CShuDuCtrl::DrawSelectNumber(CDC *pDC)
{
if(!GameStarted)return;
if(beDragging)return;
int w = PIC_WIDTH+16;
int h = PIC_HEIGHT+16;
int l = GameRect.left;
int t = GameRect.top;
if(TargetRow>=0 && TargetRow <9 && TargetCol>=0 && TargetCol<9 && m_GameData[TargetRow][TargetCol]!=0)
{
int num = m_GameData[TargetRow][TargetCol];
//搜索所有當前數字
CRect rect;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(m_GameData[i][j] == num)
{
rect.left = l + j * w;
rect.right = rect.left + w;
rect.top = t + i * h;
rect.bottom = rect.top + h;
rect.DeflateRect(1,1,1,1);
pDC->FillSolidRect(&rect,RGB(110,240,240));
}
}
}
}
else if(TrackNumber >0)
{
COLORREF NumColor = RGB(235,0,0);
COLORREF AreaColor = RGB(150,150,150);
CRect rect;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
BOOL have = FALSE;
rect.left = l + j * w;
rect.right = rect.left + w;
rect.top = t + i * h;
rect.bottom = rect.top + h;
rect.DeflateRect(1,1,1,1);
if(m_GameData[i][j] == TrackNumber)
pDC->FillSolidRect(&rect,NumColor);
else if(m_GameData[i][j] > 0)
{
pDC->FillSolidRect(&rect,AreaColor);
}
else //empty
{
for(int m=0;m<9;m++)
{
if(m_GameData[i][m] == TrackNumber)
{
have = TRUE;
}
if(m_GameData[m][j] == TrackNumber)
{
have = TRUE;
}
}
int sx = (j/3)*3;
int sy = (i/3)*3;
//按九宮搜索
for(m=sy;m<sy+3;m++)
{
for(int n=sx;n<sx+3;n++)
{
if(m_GameData[m][n] == TrackNumber)
{
have = TRUE;
}
}
}
if(have)
{
pDC->FillSolidRect(&rect,AreaColor);
}
}//else
}//for
}//for
}//else if
}
//畫可用數字
void CShuDuCtrl::DrawAviNumbers(CDC* pDC)
{
if(!GameStarted)return;
int w = PIC_WIDTH+16;
int h = PIC_HEIGHT+16;
int l = GameRect.left;
int t = GameRect.top;
CRect drawRect;
char TextBuffer[12];
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(m_GameData[i][j] == 0)
{
drawRect.SetRect(l+j*w, t+i*h,l+(j+1)*w, t+(i+1)*h);
strcpy(TextBuffer,"123456789");
for(int k=0;k<9;k++)
{
int y = m_GameData[k][j];
int x = m_GameData[i][k];
if(x!=0)TextBuffer[x-1] = ' ';
if(y!=0)TextBuffer[y-1] = ' ';
}
int sx = (i/3)*3;
int sy = (j/3)*3;
for(k = sx; k < sx+3; k++)
{
for(int m = sy; m<sy+3; m++)
{
if(m_GameData[k][m] !=0)
{
TextBuffer[m_GameData[k][m] -1] = ' ';
}
}
}
for(k=0;k<9;k++)
{
if(TextBuffer[k] == ' ')
{
BOOL beFind = FALSE;
for(int m=k+1;m<9;m++)
{
if(TextBuffer[m] != ' ')
{
beFind = TRUE;
break;
}
}
if(!beFind)
{
TextBuffer[k] = 0;
break;
}
else
{
TextBuffer[k] = TextBuffer[m];
TextBuffer[m] = ' ';
}
}
}//for(k
//DrawTextResult
pDC->DrawText(TextBuffer,k,&drawRect,DT_CENTER | DT_VCENTER | DT_WORDBREAK);
}
}//for(int j
}//for(int i
}
//畫已經放置好的數字圖片
void CShuDuCtrl::DrawGameNumbers(CDC* pDC)
{
if(!GameStarted)return;
int w = PIC_WIDTH+16;
int h = PIC_HEIGHT+16;
int l = GameRect.left;
int t = GameRect.top;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
int n = m_GameData[i][j];
if(n != 0)
{
CPoint pp(l+j*w+8,t+i*h+8);
GamePicList.Draw(pDC,n-1,pp,ILD_NORMAL);
}
}
}
}
//畫托拽圖片和目標網格
void CShuDuCtrl::DrawDragNumber(CDC* pDC)
{
int w = PIC_WIDTH+16;
int h = PIC_HEIGHT+16;
int l = GameRect.left;
int t = GameRect.top;
if(beDragging)
{
if(TargetRow>=0 && TargetRow <9 && TargetCol>=0 && TargetCol<9 && m_GameData[TargetRow][TargetCol]==0)
{
CPen pen(PS_SOLID,1,RGB(0,0,127));
CPen* pOld = pDC->SelectObject(&pen);
pDC->MoveTo(l + TargetCol*w+1,t+TargetRow*h+1);
pDC->LineTo(l + TargetCol*w+1,t+TargetRow*h+h-1);
pDC->LineTo(l + TargetCol*w+w-1,t+TargetRow*h+h-1);
pDC->LineTo(l + TargetCol*w+w-1,t+TargetRow*h+1);
pDC->LineTo(l + TargetCol*w+1,t+TargetRow*h+1);
pDC->SelectObject(pOld);
}
CPoint pt( MousePos.x - ptDragHot.x, MousePos.y - ptDragHot.y);
BOOL result = GamePicList.Draw(pDC,DragNumber-1, pt,ILD_NORMAL);
}
}
void CShuDuCtrl::DeleteSomeNumbers()
{
for(int i=0;i<9;i++) NumberLeft[i] = 0;
srand( GetTickCount() );
int SumDelete;
if(GameLevel == 0) SumDelete = EASYLEVEL_NUM;
else if( GameLevel == 1)
SumDelete = NORMALLEVEL_NUM;
else
SumDelete = HARDLEVEL_NUM;
int sum = 0;
for(;;)
{
int r = rand()%9;
int c = rand()%9;
int num = m_GameData[r][c];
if(num != 0)
{
m_GameData[r][c] = 0;
MarkData[r][c] = 0;
NumberLeft[num-1]++;
sum++;
}
if(sum>=SumDelete)break;
}
}
BEGIN_MESSAGE_MAP(CShuDuCtrl, CStatic)
//{{AFX_MSG_MAP(CShuDuCtrl)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_ERASEBKGND()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CShuDuCtrl message handlers
//只允許從剩余數字中向數獨矩陣托放,方向的用“撤銷”按鈕實現
void CShuDuCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
POINT ptCursor;
GetCursorPos(&ptCursor);
ScreenToClient(&ptCursor);
CRect picRect;
for(int i=0;i<9;i++)
{
picRect.left = NumberLeftRect.left + 8;
picRect.right = picRect.left + PIC_WIDTH;
picRect.top = NumberLeftRect.top + i*(PIC_HEIGHT+16)+14;
picRect.bottom = picRect.top + PIC_HEIGHT;
if(picRect.PtInRect(ptCursor) && NumberLeft[i]>0)
{
CRect nr;
nr.left = NumberLeftRect.left;
nr.top = NumberLeftRect.top+i*(PIC_HEIGHT+16);
nr.right= NumberLeftRect.right;
nr.bottom = nr.top + PIC_HEIGHT+16;
NumberLeft[i]--;
DragNumber = i+1;
beDragging = TRUE;
ptDragHot.x = ptCursor.x - picRect.left;
ptDragHot.y = ptCursor.y - picRect.top;
MousePos = ptCursor;
Invalidate();
break;
}
}
}
void CShuDuCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
if(beDragging)
{
if(TargetRow>=0 && TargetRow <9 && TargetCol>=0 && TargetCol<9 && m_GameData[TargetRow][TargetCol]==0)
{
m_GameData[TargetRow][TargetCol] = DragNumber;
//保存操作
Step[CurStep].row = TargetRow;
Step[CurStep].col = TargetCol;
Step[CurStep].number = DragNumber;
CurStep++;
//檢查是否游戲結束
BOOL Finished = TRUE;
for(int i=0;i<9;i++)
{
if(NumberLeft[i]>0) Finished = FALSE;
}
if(Finished && !CheckErrors(&memDC))
{
GameCompleted = TRUE; //FINISH THIS GAME
CWnd* pParent = GetParent();
if(pParent != NULL)
{
pParent->PostMessage(WM_GAMECOMPLETED);
}
}
else
NeedCheck = TRUE;
}
else
{
NumberLeft[DragNumber-1]++;
}
beDragging = FALSE;
DragNumber = 0;
Invalidate();
}
}
void CShuDuCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
int NewRow,NewCol;
int NewTrackNum;
if(GameStarted)
{
POINT ptCursor;
GetCursorPos(&ptCursor);
ScreenToClient(&ptCursor);
//計算目標格坐標
if(GameRect.PtInRect(ptCursor))
{
NewRow = (ptCursor.y - GameRect.top) / (PIC_HEIGHT+16);
NewCol = (ptCursor.x - GameRect.left) / (PIC_WIDTH+16);
}
else
{
NewRow = -1; NewCol = -1;
}
if(NumberLeftRect.PtInRect(ptCursor))
{
NewTrackNum = (ptCursor.y - NumberLeftRect.top) / (PIC_HEIGHT+16) + 1;
}
else NewTrackNum = -1;
MousePos.x = ptCursor.x;
MousePos.y = ptCursor.y;
if(beDragging)
{
Invalidate();
}
if( NewRow!=TargetRow || NewCol!=TargetCol )
{
TargetRow = NewRow;
TargetCol = NewCol;
Invalidate();
}
if(NewTrackNum != TrackNumber)
{
TrackNumber = NewTrackNum;
Invalidate();
}
}
}
BOOL CShuDuCtrl::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CShuDuCtrl::OnPaint()
{
CPaintDC dc(this);
if(!beInitialized)
Initialize();
DrawGameBitmap();
// Do not call CStatic::OnPaint() for painting messages
}
void CShuDuCtrl::DrawGameBitmap()
{
//準備位圖
PrepareBitmap();
CDC *pDC = GetDC();
pDC->BitBlt(0,0,rectClient.Width(),rectClient.Height(),&memDC,0,0,SRCCOPY);
ReleaseDC(pDC);
}
void CShuDuCtrl::UnDo()
{
if(CurStep<=0)return;
if(!GameStarted || GameCompleted)return;
int Row = Step[CurStep-1].row;
int Col = Step[CurStep-1].col;
ASSERT(Step[CurStep-1].number == m_GameData[Row][Col]);
m_GameData[Row][Col] = 0;
NumberLeft[Step[CurStep-1].number-1]++;
CurStep--;
Invalidate();
}
void CShuDuCtrl::SetGameLevel(int NewLevel)
{
if(GameStarted)
{
GameStarted = FALSE;
}
InitGameData();
GameLevel = NewLevel;
if(GameLevel <0)GameLevel =0;
if(GameLevel >2)GameLevel =2;
Invalidate();
}
void CShuDuCtrl::InitGameData()
{
memset(m_GameData,0,sizeof(SHUDUMETRIX));
memset(MarkData,0,sizeof(SHUDUMETRIX));
for(int i=0;i<9;i++) NumberLeft[i] = 0;
beDragging = FALSE;
CurStep = 0;
NeedCheck = FALSE;
GameStarted = FALSE;
GameCompleted = FALSE;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -