请版主删除此贴

类别:VC语言 点击:0 评论:0 推荐:

 

// fivestarDlg.cpp : implementation file
/////////////////////////////////////////////////////////////////////////////
file://Disigned by jucee              /////           
file://If you have problem can connect me by e_mial:[email protected]  /////
//  or Oicq:16578734 or visit my homepage http://jucee.top263.net /////
////////////////////////////////////////////////////////////////  /////
/***************************************************************************/
#include "stdafx.h"
#include "fivestar.h"
#include "fivestarDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
 CAboutDlg();

// Dialog Data
 file://{{AFX_DATA(CAboutDlg)
 enum { IDD = IDD_ABOUTBOX };
 file://}}AFX_DATA

 // ClassWizard generated virtual function overrides
 file://{{AFX_VIRTUAL(CAboutDlg)
 protected:
 virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
 file://}}AFX_VIRTUAL

// Implementation
protected:
 file://{{AFX_MSG(CAboutDlg)
 file://}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
 file://{{AFX_DATA_INIT(CAboutDlg)
 file://}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 file://{{AFX_DATA_MAP(CAboutDlg)
 file://}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
 file://{{AFX_MSG_MAP(CAboutDlg)
 file://}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFivestarDlg dialog

CFivestarDlg::CFivestarDlg(CWnd* pParent /*=NULL*/)
 : CDialog(CFivestarDlg::IDD, pParent)
{
 file://{{AFX_DATA_INIT(CFivestarDlg)
 file://}}AFX_DATA_INIT
 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFivestarDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 file://{{AFX_DATA_MAP(CFivestarDlg)
 file://}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFivestarDlg, CDialog)
 file://{{AFX_MSG_MAP(CFivestarDlg)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_COMMAND(IDM_ABOUT, OnAbout)
 ON_COMMAND(IDM_START, OnStart)
 ON_COMMAND(IDM_END, OnEnd)
 ON_COMMAND(IDM_LOAD, OnLoad)
 ON_COMMAND(IDM_SAVE, OnSave)
 ON_WM_LBUTTONDOWN()
 ON_UPDATE_COMMAND_UI(IDM_START, OnUpdateStart)
 ON_BN_CLICKED(IDC_BNSTART, OnBnstart)
 ON_BN_CLICKED(IDC_BNSAVE, OnBnsave)
 ON_BN_CLICKED(IDC_BNLOAD, OnBnload)
 ON_BN_CLICKED(IDC_BNRESTART, OnBnrestart)
 ON_COMMAND(IDM_EXIT, OnExit)
 file://}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFivestarDlg message handlers

BOOL CFivestarDlg::OnInitDialog()
{
 CDialog::OnInitDialog();

 // Add "About..." menu item to system menu.

 // IDM_ABOUTBOX must be in the system command range.
 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
 ASSERT(IDM_ABOUTBOX < 0xF000);

 CMenu* pSysMenu = GetSystemMenu(FALSE);
 if (pSysMenu != NULL)
 {
  CString strAboutMenu;
  strAboutMenu.LoadString(IDS_ABOUTBOX);
  if (!strAboutMenu.IsEmpty())
  {
   pSysMenu->AppendMenu(MF_SEPARATOR);
   pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  }
 }

 // Set the icon for this dialog.  The framework does this automatically
 //  when the application's main window is not a dialog
 SetIcon(m_hIcon, TRUE);   // Set big icon
 SetIcon(m_hIcon, FALSE);  // Set small icon
 END=false;
 // TODO: Add extra initialization here
 SetPointValueNull();
 bStart=false;
 
 return TRUE;  // return TRUE  unless you set the focus to a control
}

void CFivestarDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
 {
  CAboutDlg dlgAbout;
  dlgAbout.DoModal();
 }
 else
 {
  CDialog::OnSysCommand(nID, lParam);
 }
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFivestarDlg::OnPaint()
{
 
 CPaintDC dc(this); // device context for painting
 CRect clientrect;
 GetClientRect(&clientrect);
 lenX=lenY=16;
 int i=0;
 CBrush brush;
 CBrush *hOldBrush;
 brush.CreateSolidBrush(RGB(255,255,0));
 hOldBrush=dc.SelectObject(&brush);
 dc.SetBkMode(TRANSPARENT);
 dc.SetBkColor(RGB(0,0,0));
 for(i=0;i<30;i++){//draw h_line
     dc.MoveTo(lenX,lenY+i*lenY);
  dc.LineTo(30*lenX,lenY+i*lenY);
 }
 for(i=0;i<30;i++){//draw v_line
  dc.MoveTo(lenX+i*lenX,lenY);
  dc.LineTo(lenX+i*lenX,30*lenY);
 }
 dc.SelectObject(hOldBrush);
 brush.DeleteObject();
 init();
 
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFivestarDlg::OnQueryDragIcon()
{
 return (HCURSOR) m_hIcon;
}

void CFivestarDlg::OnAbout()
{
 // TODO: Add your command handler code here
 CAboutDlg dlg;
 dlg.DoModal();
}


void CFivestarDlg::GetMaxValuePoint(int* i,int* j,int t)
{
 int max=0;
 *i=0;
 *j=0;
 for(int m=0;m<30;m++){
  for(int n=0;n<30;n++){
   if((Matrix[m][n].value>max)&&(Matrix[m][n].type==t))
   {
    max=Matrix[m][n].value;
    *i=m;
    *j=n;
   }
  }
 }
}
void CFivestarDlg::AddValue()
//////////////////////////////////////////////////////////
/*              i-1,j-1  i-1,j  i-1,j+1
    i,j-1  i,j   i,j+1
    i+1,j-1  i+1,j  i+1,j+1
*/
///////////////////////////////////////////////////////////
{
 int count,m,n;
 for(int i=0;i<30;i++)
  for(int j=0;j<30;j++){
   int t;
   if(!Matrix[i][j].type){
   for(t=Matrix[i][j-1].type,m=i,n=j-1,count=0;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,n--);
    GiveScort(count,i,j,t);
    for(t=Matrix[i][j+1].type,m=i,n=j+1,count=0;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,n++);
    GiveScort(count,i,j,t);
     
    for(t=Matrix[i-1][j-1].type,m=i-1,n=j-1,count=0;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,m--,n--);
    GiveScort(count,i,j,t);
    for(t=Matrix[i+1][j+1].type,m=i+1,n=j+1,count=0;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,m++,n++);
    GiveScort(count,i,j,t);
    
    for(t=Matrix[i-1][j].type,m=i-1,n=j,count=0;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,m--);
    GiveScort(count,i,j,t);
    for(t=Matrix[i+1][j].type,m=i+1,n=j,count=0;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,m++);
    GiveScort(count,i,j,t);
    
    for(t=Matrix[i-1][j+1].type,m=i-1,n=j+1,count=0;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,m--,n++);
    GiveScort(count,i,j,t);
    for(t=Matrix[i+1][j-1].type,m=i+1,n=j-1,count=0;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,m++,n--);
    GiveScort(count,i,j,t);
    ////////////////////////////////////////
    for(t=Matrix[i][j-1].type,m=i,n=j-1,count=0;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,n--);
    if(Matrix[i][j-1].type!=Matrix[i][j+1].type)goto next1;
    for(m=i,n=j+1;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,++n);
    GiveScort(count,i,j,t);

next1:   for(t=Matrix[i+1][j].type,m=i+1,n=j,count=0;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,m++);
    if(Matrix[i-1][j].type!=Matrix[i+1][j].type)goto next2;
    for(m=i-1,n=j;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,m--);
    GiveScort(count,i,j,t);
     
next2:   for(t=Matrix[i-1][j-1].type,m=i-1,n=j-1,count=0;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,m--,n--);
    if(Matrix[i+1][j+1].type!=Matrix[i-1][j-1].type)goto next3;
    for(m=i+1,n=j+1;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,m++,n++);
    GiveScort(count,i,j,t);
    
    
next3:   for(t=Matrix[i-1][j+1].type,m=i-1,n=j+1,count=0;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,m--,n++);
    if(Matrix[i-1][j+1].type!=Matrix[i+1][j-1].type)count=0;
    for(m=i+1,n=j-1;t&&Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;count++,m++,n--);
    GiveScort(count,i,j,t);
    


   }
  }
}


void CFivestarDlg::SetPointValueNull()
{
 for(int i=0;i<30;i++)
 {
  for(int j=0;j<30;j++){
   Matrix[i][j].value=0;
  }
 }
}
void CFivestarDlg::SetPointTypeNull()
{
 for(int i=0;i<30;i++)
 {
  for(int j=0;j<30;j++){
   Matrix[i][j].type=0;
  }
 }
}
bool CFivestarDlg::AssertWin(int t)
{
 for(int i=0;i<30;i++)
 {
  for(int j=0;j<30;j++)
  {
   if(Matrix[i][j].type==t){
    for(int m=i+1,n=j+1,count=1;Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;m++,n++,count++);
     if(count>=5)return true;
    for(m=i,n=j+1,count=1;Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;n++,count++);
     if(count>=5)return true;
    for(m=i+1,n=j,count=1;Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;m++,count++);
     if(count>=5)return true;
    for(m=i+1,n=j-1,count=1;Matrix[m][n].type==t&&m>=0&&n>=0&&m<30&&n<30;m++,n--,count++);
     if(count>=5)return true;
   }
  }
 }
return false;
}
bool CFivestarDlg::BlackGoStep(CPoint point)
{
 bool Result=false;
 CRect Rect(0,0,30*lenX,30*lenY);
 if(Rect.PtInRect(point))
 {
  CClientDC dc(this);
  dc.SetROP2(R2_COPYPEN);
  CPen NullPen;
  CPen* pOldPen;
  int x,y;
  NullPen.CreatePen(PS_NULL,0,NULL,0,NULL);
  pOldPen=(CPen*)dc.SelectObject(NullPen);
  CPoint tempPoint1,tempPoint2,validPoint;
  tempPoint1.x=(x=point.x/lenX)*lenX;
  tempPoint1.y=(y=point.y/lenY)*lenY;
  x--;y--;
  tempPoint2.x=tempPoint1.x+lenX/2;
  tempPoint2.y=tempPoint1.y+lenY/2;
  validPoint.x=(point.x>tempPoint2.x)?
    (x++,tempPoint2.x):(tempPoint1.x-lenX/2);
  validPoint.y=(point.y>tempPoint2.y)?(y++,tempPoint2.y):tempPoint1.y-lenY/2;
  if(!Matrix[x][y].type){
  dc.Ellipse(validPoint.x,validPoint.y,validPoint.x+lenX,validPoint.y+lenY);
  Matrix[x][y].type=1;
  Result=true;
  }
  dc.SelectObject(pOldPen);
  NullPen.DeleteObject();
 }
 return(Result);
}
void CFivestarDlg::RedGoStep()
{
 int i,j;
 CClientDC dc(this);
 CBrush RedBrush(RGB(255,0,0));
 HBRUSH pOldBrush=(HBRUSH)dc.SelectObject(&RedBrush);
 GetMaxValuePoint(&i,&j,0);
 Matrix[i][j].type=2;
 dc.Ellipse(lenX+i*lenX-lenX/2,
  lenY+j*lenY-lenY/2,
  lenX+i*lenX-lenX/2+lenX,
  lenY+j*lenY-lenY/2+lenY);
 Matrix[i][j].type=2;
 dc.SelectObject(pOldBrush);
 RedBrush.DeleteObject();


}
void CFivestarDlg::EndGame()
{ Invalidate();  
 UpdateWindow();
 SetPointValueNull();
 SetPointTypeNull();
 END=true;
}

void CFivestarDlg::OnStart()
{
 // TODO: Add your command handler code here
 EndGame();
 init();
 END=false;
}

void CFivestarDlg::OnEnd()
{
 // TODO: Add your command handler code here
 EndGame();
 Invalidate();
 init();
 END=false;
}

void CFivestarDlg::Serialize(CArchive& ar)
{
 if (ar.IsStoring())
 { // storing code
  for(int i=0;i<30;i++){
   for(int j=0;j<30;j++){
   Matrix[i][j].Serialize(ar);
   }
  }
 }
 else
 { // loading code
  for(int i=0;i<30;i++){
   for(int j=0;j<30;j++){
    Matrix[i][j].Serialize(ar);
   }
  }
 }
}

void CFivestarDlg::OnLoad()
{
 // TODO: Add your command handler code here
 CFile file("save.res",CFile::modeRead);
 CArchive ar(&file,CArchive::load);
 Serialize(ar);
 Invalidate();
 init();
 END=false;
}

void CFivestarDlg::OnSave()
{
 // TODO: Add your command handler code here
 CFile file("save.res",CFile::modeReadWrite);
 CArchive ar(&file,CArchive::store);
 Serialize(ar);
 init();
}
void CFivestarDlg::init()
{
 CBrush RedBrush(RGB(255,0,0));
 CBrush* pOldBrush;
 CClientDC dc(this);
 for(int i=0;i<30;i++)
 {
  for(int j=0;j<30;j++)
  {
   if(Matrix[i][j].type==2){
    pOldBrush=dc.SelectObject(&RedBrush);
    dc.Ellipse(lenX+i*lenX-lenX/2,
      lenY+j*lenY-lenY/2,
      lenX+i*lenX-lenX/2+lenX,
      lenY+j*lenY-lenY/2+lenY);
    dc.SelectObject(pOldBrush);
   }
   else if(Matrix[i][j].type==1){
    dc.Ellipse(lenX+i*lenX-lenX/2,
      lenY+j*lenY-lenY/2,
      lenX+i*lenX-lenX/2+lenX,
      lenY+j*lenY-lenY/2+lenY);
   }
  
  }
 }
 RedBrush.DeleteObject();
}

 


void CFivestarDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 if(END)return;
 if(!BlackGoStep(point))return;
 if(AssertWin(1)){
  MessageBox("You Win!!",AfxGetAppName()); 
  EndGame();
  return;
 }
 SetPointValueNull();
 AddValue();
 RedGoStep();
 if(AssertWin(2)){
  MessageBox("You Lost!!",AfxGetAppName()); 
  EndGame();
  return;
 }
 CDialog::OnLButtonDown(nFlags, point);
}

void CFivestarDlg::OnUpdateStart(CCmdUI* pCmdUI)
{
 // TODO: Add your command update UI handler code here
 pCmdUI->SetCheck(!bStart);
}


void CFivestarDlg::OnBnstart()
{
 // TODO: Add your control notification handler code here
 OnStart();
}

void CFivestarDlg::OnBnsave()
{
 // TODO: Add your control notification handler code here
 OnSave();
}

void CFivestarDlg::OnBnload()
{
 // TODO: Add your control notification handler code here
 OnLoad();
}

void CFivestarDlg::OnBnrestart()
{
 // TODO: Add your control notification handler code here
 OnEnd();
}

void CFivestarDlg::OnExit()
{
 // TODO: Add your command handler code here
 PostQuitMessage(WM_QUIT);
}
void CFivestarDlg::GiveScort(int n,int i,int j,int t)
{
 if(t==2){//t==2
  switch(n)
  {
   case 1:
    Matrix[i][j].value+=20;break;
   case 2:
    Matrix[i][j].value+=100;break;
   case 3:
    Matrix[i][j].value+=1000;break;
   case 4:
    Matrix[i][j].value+=20000;break;
   default:
    break;
   }
 }
 else{//t=1
  switch(n)
  {
  case 1:
   Matrix[i][j].value+=10;break;
  case 2:
   Matrix[i][j].value+=90;break;
  case 3:
   Matrix[i][j].value+=2000;break;
  case 4:
   Matrix[i][j].value+=10000;break;
  default:
   break;

  }
 }
}

 

本文地址:http://com.8s8s.com/it/it3192.htm