⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eightnumberview.cpp

📁 目前开发人工只能的朋友一定会遇到bp算法中八进制的问题
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				   aa[i]=ab[j];
				   ab[j]=11;
				   break;
			   }
		   }
	   }
      //**start=aa[i];
	  /*str.Format("%d",aa[i]);
	  string +=str;
	  str.Format("%d",**start);
	  string +=str;
	  MessageBox(string);*/
   }
   for (i=0; i<9; i++)
   {
    str.Format("%d",aa[i]);
	string+=str;
   }
  int  z=1;
   for (i=0; i<3; i++)
	   for (j=0; j<3; j++)
	   {
		   start[i][j]=atoi(substr(string,z++,1));
	   }
    /*CString std="";
   for (i=0; i<3; i++)
	   for (j=0; j<3; j++)
   {
         str.Format("%d",start[i][j]);
	     std=std+"  "+str;
		 
	   
   }
  start=aa;
   for (i=0; i<3; i++)
     for (j=0; j<3; j++)
   {
	   str.Format("%d",**start);
	   std=std+"  "+str; 
   }
   MessageBox(std);*/
}

void CEightNumberView::OnButtonSteprun() 
{
	// TODO: Add your control notification handler code here
	int value;
	CString s;
	//pdc=GetDC();

	if(count<depth-1)
	{
		count++;
		si=output[count].si;
		sj=output[count].sj;
		ni=output[count+1].si;
		nj=output[count+1].sj;
		//ni=output[count].ni;
		//nj=output[count].nj;
		value=output[count].ch[ni][nj];
		/*CString str;
		str.Format("%d",value);
		//MessageBox(str);
		CString sting=str;
		str.Format("%d",si);
		sting+=str;
		str.Format("%d",sj);
		sting+=str;
		MessageBox(sting);*/
        drawd(sj,si,value);
        drawd(nj,ni,0);
		DrawGraphics();

		
		//start[si][sj]=value;
		//start[ni][nj]=0;
		//ItoBitmap();
		//DrawStation();
		//OnPaint();
		
		//drawd
		if(count==depth-1)
		{
			//m_RunToStop.EnableWindow(FALSE);
		    //m_SepRun.EnableWindow(FALSE);
			MessageBox("恭喜你,你已赢得这局,要重新开始,请单击‘开始游戏’","八数码游戏",MB_OK|MB_ICONINFORMATION);
			//m_cButtonStepRun.EnableWindow(FALSE);
		}
	}
  // MessageBox("This is me!");
}

void CEightNumberView::drawd(int x,int y,int a)
{
  	CDC *pDC=GetDC();
	CDC memdc1;
    UINT Mid;
	CBitmap bitmap;
  		 if (a==0) Mid=IDB_BITMAP9;
		   else if(a==1) Mid=IDB_BITMAP1;
		     else if(a==2) Mid=IDB_BITMAP2;
			   else if(a==3) Mid=IDB_BITMAP3;
			     else if(a==4) Mid=IDB_BITMAP4;
				   else if(a==5) Mid=IDB_BITMAP5;
				     else if(a==6) Mid=IDB_BITMAP6;
					   else if(a==7) Mid=IDB_BITMAP7;
					     else if(a==8) Mid=IDB_BITMAP8;

		bitmap.LoadBitmap(Mid);


	memdc1.CreateCompatibleDC(pDC);
	memdc1.SelectObject(bitmap);
	pDC->BitBlt((x)*cellwidth+20,(y)*cellheight+20,cellwidth,cellheight,&memdc1,(0)*cellwidth,(0)*cellheight,SRCCOPY);
	
	pDC->DeleteDC();
	//DeleteObject(map);
	return;
}

void CEightNumberView::OnButtonRun() 
{
	// TODO: Add your control notification handler code here
	int i,j;
	count=-1;
    for (i=0; i<3; i++)
       for (j=0; j<3; j++)
	   {
		   zerosite.ch[i][j]=start[i][j];
		   if (start[i][j]==0)
		   {
			   zerosite.si=j;
			   zerosite.sj=i;
			   ni=j;
			   nj=i;
		   }
	   }
	   for (i=0; i<3; i++)
		  for (j=0; j<3; j++)
		  {
			  drawd(i,j,start[j][i]);
		  }
    //m_ShowState.SetWindowText("正在进行初始化请耐心等待......");
	DrawGraphics();
    AutoPlay=!AutoPlay;
	if (AutoPlay)
	{
	   m_RunToStop.SetWindowText("停止自动运行");
       SetTimer(1,300,NULL);
	 }
	  else 
	  {
		  m_RunToStop.SetWindowText("自动运行");
		  KillTimer(1);
	  }
   
}

void CEightNumberView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	OnButtonSteprun();
	
	CFormView::OnTimer(nIDEvent);
}

void CEightNumberView::OnCancelMode() 
{
	CFormView::OnCancelMode();
	
	// TODO: Add your message handler code here
	
}

void CEightNumberView::OnButtonOnok() 
{
	// TODO: Add your control notification handler code here
	//CAboutDlg dlg;
	//dlg.DoModal();
	KillTimer(1);
    m_pThread=AfxBeginThread(MultiThread,0);
    m_pThread->SuspendThread ();

    CEditDlg dlg;
	dlg.DoModal();
	//if (dlg.)
	
	AfxGetMainWnd()->SendMessage(WM_CLOSE);
    
}

void CEightNumberView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int x,y;
	int value;
	x=point.x/cellwidth;
	y=point.y/cellheight;
	//CString str;
	//	str.Format("%d",abs(x-zerosite.si)+abs(y-zerosite.sj));
      //  MessageBox(str);
	if (abs(x-zerosite.si)+abs(y-zerosite.sj)==1)
	{
		if (x-zerosite.si==1)
		{
          value=zerosite.ch[y][x];
		  //CString str;
          //str.Format("%d",x-zerosite.si);
		  //MessageBox(str);
		  drawd(zerosite.si,zerosite.sj,value);
          drawd(x,y,0);
		  DrawGraphics();
		  
		  zerosite.ch[y][x]=0;
		  zerosite.ch[zerosite.sj][zerosite.si]=value;
		  zerosite.si=x;
		  
		}
		else if (x-zerosite.si==-1)
		{
          value=zerosite.ch[y][x];
		  drawd(zerosite.si,zerosite.sj,value);
          drawd(x,y,0);
		  DrawGraphics();
		  //CString str;
		  //str.Format("%d",value);
		  //MessageBox(str);
		  
		  zerosite.ch[zerosite.sj][zerosite.si]=value;
		  zerosite.ch[y][x]=0;
		  zerosite.si=x;
		}
        else if (y-zerosite.sj==1)
		{
          value=zerosite.ch[y][x];
		  drawd(zerosite.si,zerosite.sj,value);
          drawd(x,y,0);
		  DrawGraphics();
		  //CString str;
		  //str.Format("%d",value);
		  //MessageBox(str);
		  
		  zerosite.ch[zerosite.sj][zerosite.si]=value;
		  zerosite.ch[y][x]=0;
          zerosite.sj=y;
		}
	    else if (y-zerosite.sj==-1)
		{
          value=zerosite.ch[y][x];

		  drawd(zerosite.si,zerosite.sj,value);
          drawd(x,y,0);
		  DrawGraphics();
          //CString str;
		  // str.Format("%d",value);
		  //MessageBox(str);
		  
		  zerosite.ch[zerosite.sj][zerosite.si]=value;
		  zerosite.ch[y][x]=0;
          zerosite.sj=y;
		}
       

	}
	/*CString str,st="";
	for (int i=0; i<3; i++)
		for (int j=0; j<3; j++)
		{
          str.Format("%d",zerosite.ch[i][j]);
		  st+=str;
		}
		MessageBox(st);*/
	if (Goals(zerosite))
	{
        //m_RunToStop.EnableWindow(FALSE);
	//	m_SepRun.EnableWindow(FALSE);
		MessageBox("恭喜你,你已赢得这局,要重新开始,请单击‘开始游戏’","八数码游戏",MB_OK|MB_ICONINFORMATION);
	}

	CFormView::OnLButtonDown(nFlags, point);
}

void CEightNumberView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	int value;
	int x,y;
	switch(nChar)
	{
	case VK_RIGHT:
      if(zerosite.si>0 && zerosite.si<3)
		{
		  x=zerosite.si-1;
		  y=zerosite.sj;
		  value=zerosite.ch[y][x];
		  drawd(zerosite.si,zerosite.sj,value);
          drawd(x,y,0);
		  DrawGraphics();
		  //CString str;
		  //str.Format("%d",value);
		  //MessageBox(str);
		  
		  zerosite.ch[zerosite.sj][zerosite.si]=value;
		  zerosite.ch[y][x]=0;
		  zerosite.si=x;
		}
		break;
	case VK_LEFT:
		if(zerosite.si<2 && zerosite.si>-1)
		{
          x=zerosite.si+1;
		  y=zerosite.sj;
		  value=zerosite.ch[y][x];
		  drawd(zerosite.si,zerosite.sj,value);
          drawd(x,y,0);
		  DrawGraphics();
		  //CString str;
		  //str.Format("%d",value);
		  //MessageBox(str);
		  
		  zerosite.ch[zerosite.sj][zerosite.si]=value;
		  zerosite.ch[y][x]=0;
		  zerosite.si=x;
		}
		break;
	case VK_DOWN:
		if(zerosite.sj>0 && zerosite.sj<3)
		{
		  x=zerosite.si;
		  y=zerosite.sj-1;
		  value=zerosite.ch[y][x];

		  drawd(zerosite.si,zerosite.sj,value);
          drawd(x,y,0);
		  DrawGraphics();
          //CString str;
		  // str.Format("%d",value);
		  //MessageBox(str);
		  
		  zerosite.ch[zerosite.sj][zerosite.si]=value;
		  zerosite.ch[y][x]=0;
          zerosite.sj=y;	
		}
		break;
	case VK_UP:
		if(zerosite.sj<2 &&zerosite.sj>-1)
		{
          x=zerosite.si;
		  y=zerosite.sj+1;
		  value=zerosite.ch[y][x];
		  drawd(zerosite.si,zerosite.sj,value);
          drawd(x,y,0);
		  DrawGraphics();
		  //CString str;
		  //str.Format("%d",value);
		  //MessageBox(str);
		  
		  zerosite.ch[zerosite.sj][zerosite.si]=value;
		  zerosite.ch[y][x]=0;
          zerosite.sj=y;
		}
		break;
	}
	if (Goals(zerosite))
	{
		//m_RunToStop.EnableWindow(FALSE);
		//m_SepRun.EnableWindow(FALSE);
		MessageBox("恭喜你,你已赢得这局,要重新开始,请单击‘开始游戏’","八数码游戏",MB_OK|MB_ICONINFORMATION);
	}
	CFormView::OnKeyDown(nChar, nRepCnt, nFlags);
}

CString CEightNumberView::substr(CString st, int a, int b)
{
    int length=strlen(st);
	CString str;
	for (int i=a;i<=a+b-1;i++)
      str=str+st[i-1];
	return str;
}

void CEightNumberView::OnButtonStart() 
{
	// TODO: Add your control notification handler code here
	int i,j;
	depth=0;
	//open=1;
	//closed=0;
    if (!KillTimer(1)) KillTimer(1);
	count=-1;
	AutoPlay=false;
	
	UpdateData();
    GetRand();

	for (i=0; i<3; i++)
       for (j=0; j<3; j++)
	   {
		   zerosite.ch[i][j]=start[i][j];
		   if (start[i][j]==0)
		   {
			   zerosite.si=j;
			   zerosite.sj=i;
			   ni=j;
			   nj=i;
		   }
	   }
	   for (i=0; i<3; i++)
		  for (j=0; j<3; j++)
		  {
			  drawd(i,j,start[j][i]);
		  }
    m_ShowState.SetWindowText("正在进行初始化请耐心等待......");
	DrawGraphics();
	DrawStoA();
    //Run();
	m_pThread=AfxBeginThread(MultiThread,0);
	 if (ftot)
	 {
       m_SepRun.EnableWindow(TRUE);
	   m_RunToStop.EnableWindow(TRUE);
       m_ShowState.SetWindowText(String);   
	 }
	else
		{
			//MessageBox("非常遗憾此状态无解,你只能使用鼠标移动","初始化失败通知",MB_OK|MB_ICONINFORMATION);
           m_SepRun.EnableWindow(FALSE);
	       m_RunToStop.EnableWindow(FALSE);
		   
		   m_ShowState.SetWindowText("初始化失败!非常遗憾此状态无解\n\n   禁止使用自动运行和单步运行\n\n  只能使用鼠标或键盘方向键移动");
            //MessageBox("初始化失败!");
		}
  UpdateData(false);
    //Run();
	//ItoBitmap();
	//DrawStation();
	   //Invalidate();
   
    

}

void CEightNumberView::OnButtonExplain() 
{
	// TODO: Add your control notification handler code here
	CExplainGame dlg;
	dlg.DoModal();

}

void CEightNumberView::DrawStoA()
{
	int i,j;
	CDC *pdc;
    pdc=GetDC();
	CPen newPen,*oldPen;
	newPen.CreatePen(PS_SOLID,3,RGB(128,0,0));
	oldPen=pdc->SelectObject(&newPen);
	//初始状态
	for(i=0;i<=3;i++)
	{
		pdc->MoveTo(540+40,40+40*i);
		pdc->LineTo(540+160,40+40*i);
	}
	for(j=0;j<=3;j++)
	{
		pdc->MoveTo(540+40+40*j,40);
		pdc->LineTo(540+40+40*j,160);
	}
	//目标状态
	for(i=0;i<=3;i++)
	{
		pdc->MoveTo(540+240,40+40*i);
		pdc->LineTo(540+360,40+40*i);
	}
	for(j=0;j<=3;j++)
	{
		pdc->MoveTo(540+240+40*j,40);
		pdc->LineTo(540+240+40*j,160);
	}
	CFont newFont,*oldFont;
	newFont.CreateFont(20,20,0,0,700,FALSE,FALSE,FALSE,ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,PROOF_QUALITY,FF_ROMAN,"Times New Roman");
	oldFont=pdc->SelectObject(&newFont);
	pdc->SetBkColor(RGB(212,208,200));
	CString str;
	//显示目标状态
	for(i=0;i<3;i++)
		for(j=0;j<3;j++)
		{
			//str.Format("%i",goal[i][j]);
			//pdc->TextOut(540+240+40*j+10,40+40*i+10,str);
			str.Format("%i",start[i][j]);
			pdc->TextOut(540+40+40*j+10,40+40*i+10,str);
		}
	newPen.DeleteObject();
	newFont.DeleteObject();
	pdc->SelectObject(oldPen);
	pdc->SelectObject(oldFont);
}

BOOL CEightNumberView::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(pMsg->message==WM_KEYDOWN)
	{
		OnKeyDown((UINT)pMsg->wParam,(UINT)pMsg->lParam,(UINT)pMsg->lParam);
	}
	return CFormView::PreTranslateMessage(pMsg);
}

void CEightNumberView::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CFormView::CalcWindowRect(lpClientRect, nAdjustType);
}


UINT MultiThread(LPVOID param)
{
	CMainFrame *pFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd );
	CEightNumberView *pView=(CEightNumberView*)(pFrame->GetActiveView ());
	pView->Run();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -