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

📄 chessdlg.cpp

📁 下棋
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// chessDlg.cpp : implementation file
//

#include "stdafx.h"
#include "chess.h"
#include "chessDlg.h"

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

#include "serversocket.h"
#include "clientsocket.h"
#include "seldialog.h"
//#include "
#define ROW 21
#define COLUMN 16
#define SOCKETPORT 6080
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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

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

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

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

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChessDlg dialog

CChessDlg::CChessDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CChessDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChessDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bReady=FALSE;
	m_pServerSocket=NULL;
	m_pClientSocket=NULL;
	m_bConnected=FALSE;

	
}

void CChessDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChessDlg)
	DDX_Control(pDX, IDC_SEND_MESSAGE, m_sendMessage);
	DDX_Control(pDX, IDC_REC_MESSAGE, m_recMessage);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CChessDlg, CDialog)
	//{{AFX_MSG_MAP(CChessDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_LBUTTONUP()
	ON_WM_TIMER()
	ON_COMMAND(ID_GAME_NEW, OnGameNew)
	ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout)
	ON_COMMAND(ID_GAME_EXIT, OnGameExit)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_WM_CHAR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChessDlg message handlers

BOOL CChessDlg::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
	
	MoveWindow(100,100,ROW*34+10,COLUMN*34+80);
	SetWindowText("网络五子棋");
	DrawCross();
//	if(m_bReady)
	SetTimer(1,500,NULL);

    ShowSelDlg();
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CChessDlg::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 CChessDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		DrawCross();
		CDialog::OnPaint();
	}
}

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

void CChessDlg::DrawCross()
{
	CRect rect;
    GetWindowRect(&rect);
	GetClientRect(&rect);
	CDC*pDC;
	pDC=GetDC();
	CPen penNew(PS_SOLID,1,RGB(0,0,0)),*ppenOld;
	ppenOld=pDC->SelectObject(&penNew);
	int iX,iY;
	iX=iY=32;
	for(int i=0;i<ROW;i++)
	{//draw |
	   pDC->MoveTo(25+iX*i,85+0);
	   pDC->LineTo(25+iX*i,85+iY*(COLUMN-1));
	}
	for(int j=0;j<COLUMN;j++)
	{
	   pDC->MoveTo(25+0,85+iY*j);
	   pDC->LineTo(25+iX*(ROW-1),85+iY*j);
	}


	pDC->SelectObject(ppenOld);
	CBrush brush1(RGB(0,0,0)),*pBrushOld,brush2(RGB(255,255,255));
//	if(m_bServer)
//    	pBrushOld=pDC->SelectObject(&brush1);
//	else
//    	pBrushOld=pDC->SelectObject(&brush2);

	POSITION pos;
	pos=m_whitePosList.GetHeadPosition();
	while(pos)
	{
    	pBrushOld=pDC->SelectObject(&brush1);
		int posx,posy;
		void *temp;
		temp=m_whitePosList.GetNext(pos);
		posx=(int)temp>>16&0x0000ffff;
		posy=(int)temp&0x0000ffff;
		pDC->Ellipse(posx-16,posy-16,posx+16,posy+16);
		pDC->SelectObject(pBrushOld);
	}
	pos=m_blackPosList.GetHeadPosition();
	while(pos)
	{
    	pBrushOld=pDC->SelectObject(&brush2);
		int posx,posy;
		void *temp;
		temp=m_whitePosList.GetNext(pos);
		posx=(int)temp>>16&0x0000ffff;
		posy=(int)temp&0x0000ffff;
		pDC->Ellipse(posx-16,posy-16,posx+16,posy+16);
    	pDC->SelectObject(pBrushOld);
	}
//	Invalidate();
}

void CChessDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if(!m_bReady)
		return;
	if(point.x<25||point.x>665||point.y<85||point.y>565)
		return ;
	int posx,posy,i,j;
	if((i=(point.x-25)%32)<=16)
		posx=point.x-i;
	else
		posx=point.x+32-i;
	if((j=(point.y-85)%32)<=16)
		posy=point.y-j;
	else 
		posy=point.y+32-j;
	i=posx<<16|posy;
	POSITION pos;
	pos=m_whitePosList.GetHeadPosition();
	while(pos)
	{
		if(i==(int)m_whitePosList.GetNext(pos))
			return;
	}
	pos=m_blackPosList.GetHeadPosition();
	while(pos)
	{
		if(i==(int)m_blackPosList.GetNext(pos))
			return;
	}
	if(m_bServer)
	{
		m_whitePosList.AddTail((void*)i);
		m_pServerSocket->m_pClient->Send(CHESS,(void*)&i,4);
		if(IsFive(TRUE,posx,posy))
			m_pServerSocket->m_pClient->Send(MESSAGE,"哈哈,我已经赢了,要不要再来一把?",34),
			m_recMessage.SetWindowText("恭喜大侠,您已经赢了!");
		m_bReady=FALSE;
	}
	else
	{
		m_blackPosList.AddTail((void*)i);
		m_pClientSocket->Send(CHESS,(void*)&i,4);
	    if(IsFive(FALSE,posx,posy))
			m_pClientSocket->Send(MESSAGE,"哈哈,我已经赢了,要不要再来一把?",34),
			m_recMessage.SetWindowText("恭喜大侠,您已经赢了!");
		m_bReady=FALSE;
	}
	OnPaint();
	CDialog::OnLButtonUp(nFlags, point);
}

void CChessDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default

	if(!m_bServer && !m_bConnected && m_pClientSocket)
	{
		m_bServer=FALSE;
		if(!m_pClientSocket->Connect(m_strIP,6080))
			m_recMessage.SetWindowText("对方服务器没有启动,请等待!"),m_bConnected=FALSE;
		else
			m_bConnected=TRUE;
	}

	if(!m_bReady)
		return ;

    static BOOL i=TRUE;
	i=!i;
	if(m_whitePosList.GetCount()==0)
		return;
	CDC *pDC=GetDC();
    CBrush brush1(RGB(200,200,200)),brush2(RGB(0,0,0)),brush3(RGB(255,255,255)),*pBrushOld;
	if(i)
    	pBrushOld=pDC->SelectObject(&brush1);
	else 
		if(m_bServer)
			pBrushOld=pDC->SelectObject(&brush3);
		else
			pBrushOld=pDC->SelectObject(&brush2);
 	int posx,posy;
	void *temp;
	if(!m_bServer&&m_whitePosList.GetCount())
		temp=m_whitePosList.GetTail();
	else
		if(m_blackPosList.GetCount())
			temp=m_blackPosList.GetTail();
	posx=(int)temp>>16&0x0000ffff;
	posy=(int)temp&0x0000ffff;
//	pBrushOld=pDC->SelectObject(&brush);
   	pDC->Ellipse(posx-16,posy-16,posx+16,posy+16);
	pDC->SelectObject(pBrushOld);

   CDialog::OnTimer(nIDEvent);
}

void CChessDlg::OnGameNew() 
{
	m_bReady=FALSE;
	m_bConnected=FALSE;
	m_whitePosList.RemoveAll();
	m_blackPosList.RemoveAll();

⌨️ 快捷键说明

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