📄 wuziqidlg.cpp
字号:
// wuziqiDlg.cpp : implementation file
//
#include "stdafx.h"
#include "wuziqi.h"
#include "BaseFederateAmbassador.h"
#include "stdlib.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define NUM_QIGE 15
#define WIDTH_QIGE 30
BaseFederateAmbassador fedAmb; //Federate大使
RTI::RTIambassador rtiAmb; //RTI大使
RTI::FederateHandle fedHd;
RTI::InteractionClassHandle ms_commTypeId;
RTI::ParameterHandle m_parameterIdMessage; // parameter handles
RTI::ParameterHandle m_parameterIdFederateName;
char myname[20];
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CWuziqiDlg dialog
CWuziqiDlg::CWuziqiDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWuziqiDlg::IDD, pParent),fedExecName("wuziqi")
{
//{{AFX_DATA_INIT(CWuziqiDlg)
m_Edityourname = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//
m_fedcount=0;
turnnum=0;
qizicolor=RGB(0,0,0);
turnflag=1; //每次发送交互和接受交互都要改变值
for(int i=0;i<NUM_QIGE;i++)
for(int j=0;j<NUM_QIGE;j++)
qipan[i][j]=RGB(255,255,255); //初始化为白色
for(i=0;i<5;i++) //初始化为黑色
fedmember[i].color=RGB(0,0,0);
//是否轮到自己下
}
void CWuziqiDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWuziqiDlg)
DDX_Control(pDX, IDC_COMBO_COLORSEL, m_colorsel);
DDX_Control(pDX, IDC_YOURNAME, m_yourname);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWuziqiDlg, CDialog)
//{{AFX_MSG_MAP(CWuziqiDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED(ID_CREAT, OnCreat)
ON_BN_CLICKED(ID_DETROY, OnDetroy)
ON_BN_CLICKED(ID_JOIN, OnJoin)
ON_BN_CLICKED(ID_RESIGN, OnResign)
ON_CBN_SELCHANGE(IDC_COMBO_COLORSEL, OnSelchangeComboColorsel)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWuziqiDlg message handlers
BOOL CWuziqiDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//初始化棋子颜色选择框
m_colorsel.AddString("黑色");
m_colorsel.AddString("蓝色");
m_colorsel.AddString("红色");
m_colorsel.AddString("黄色");
m_colorsel.SetCurSel(0);
// 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
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CWuziqiDlg::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 CWuziqiDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rect;
if (IsIconic())
{
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
{
CDC *pDC = GetDC();
CDC dcMem; // 与对话框dc兼容的内存dc
dcMem.CreateCompatibleDC(&dc); //创建与对话框dc兼容的内存dc
//CRect rcWindow;
//GetWindowRect(&rcWindow);
CPoint point;
point.x=WIDTH_QIGE;
point.y=WIDTH_QIGE;
drawqipan(pDC,point);
if(m_fedcount>0)
{
//根据数组画棋子
for(int i=0;i<NUM_QIGE;i++)
for(int j=0;j<NUM_QIGE;j++)
if(qipan[i][j]!=RGB(255,255,255)) //找到已经下了棋子的地方
{
CPoint point;
point.x=(i+1)*WIDTH_QIGE; //根据数组下表计算出坐标
point.y=(j+1)*WIDTH_QIGE;
rect.left = point.x-(WIDTH_QIGE/2);
rect.top = point.y-(WIDTH_QIGE/2);
rect.right = rect.left+WIDTH_QIGE;
rect.bottom = rect.top+WIDTH_QIGE;
CBrush m_brush(qipan[i][j]);
//CPen m_pen(PS_DOT ,3,RGB(255,255,255));
pDC->SelectObject(&m_brush);
//pDC->SelectObject(&m_pen);
pDC->Ellipse(&rect);
}
}
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CWuziqiDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CWuziqiDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CPaintDC dc(this);
CDC *pDC = GetDC();
CRect rect;
int tx,ty;
int cx,cy;
int flag=0;
char winmessage[200];
if(point.x<=(NUM_QIGE+1)*WIDTH_QIGE && point.x>=WIDTH_QIGE && point.y>=WIDTH_QIGE && point.y <=(NUM_QIGE+1)*WIDTH_QIGE && fedHd && turnflag)
{
tx=point.x%WIDTH_QIGE; //找出最近的下棋点
ty=point.y%WIDTH_QIGE;
if(tx<=(WIDTH_QIGE/2) && ty<=(WIDTH_QIGE/2))
{
point.x=point.x-tx;
point.y=point.y-ty;
}
else if(tx>(WIDTH_QIGE/2) && ty<=(WIDTH_QIGE/2))
{
point.x=point.x-tx+WIDTH_QIGE;
point.y=point.y-ty;
}
else if(tx<=(WIDTH_QIGE/2) && ty>(WIDTH_QIGE/2))
{
point.x=point.x-tx;
point.y=point.y-ty+WIDTH_QIGE;
}
else if(tx>(WIDTH_QIGE/2)&& ty>(WIDTH_QIGE/2))
{
point.x=point.x-tx+WIDTH_QIGE;
point.y=point.y-ty+WIDTH_QIGE;
}
cx=point.x/WIDTH_QIGE-1; //根据下子点计算出坐标
cy=point.y/WIDTH_QIGE-1;
if(qipan[cx][cy]==RGB(255,255,255)) //若该坐标处尚未下子,则跟新数组
{
qipan[cx][cy]=fedmember[0].color; //n_fedcolor 的获取在onselcolor
rect.left = point.x-(WIDTH_QIGE/2);
rect.top = point.y-(WIDTH_QIGE/2);
rect.right = rect.left+WIDTH_QIGE;
rect.bottom = rect.top+WIDTH_QIGE;
CBrush m_brush(qipan[cx][cy]);
//CPen m_pen(PS_DOT ,3,RGB(255,255,255));
pDC->SelectObject(&m_brush);
//pDC->SelectObject(&m_pen);
pDC->Ellipse(&rect);
turnflag=0; //标记下一步不是我下
flag=judgewin(cx,cy,qipan[cx][cy]);
if(flag)
{
strcpy(winmessage,fedmember[0].fedname);
strcat(winmessage," win!");
sendwinmsg(myname);
turnflag=0;
AfxMessageBox(winmessage);
}
//sedinteaction
CWuziqiDlg::coordinate tmp={0,0,0};
CWuziqiDlg::coordinate* qiziwzh;
qiziwzh=&tmp;
qiziwzh->x=point.x;
qiziwzh->y=point.y;
qiziwzh->color=fedmember[0].color;
structtostring(qiziwzh);
//AfxMessageBox(commmessage);
sendCommInteraction(commmessage); //发送交互
GetClientRect(rect); //GetClientRect取得窗口客户区(不包括非客户区)在客户区坐标系下的RECT坐标
InvalidateRect(rect); //窗口局部刷新
UpdateWindow();
}
}
CDialog::OnLButtonDown(nFlags, point);
}
void CWuziqiDlg::OnCreat()
{
// TODO: Add your control notification handler code here
try
{
//调用RTI中创建联盟的函数,第一个参数为所创建联盟的名字
//第二个参数为使用的fed文件的名称
rtiAmb.createFederationExecution(fedExecName, "wuziqi.fed" );
}
catch (RTI::Exception &e)
{
}
}
void CWuziqiDlg::OnDetroy()
{
// TODO: Add your control notification handler code here
try
{
rtiAmb.destroyFederationExecution(fedExecName);//调用RTI的销毁联盟函数,参数为联盟名
}
catch(RTI::Exception &e)
{
AfxMessageBox(e._name);
}
}
void CWuziqiDlg::OnJoin()
{
// TODO: Add your control notification handler code here
CRect rect;
fedAmb.setDlgPoint(this);
try
{
//调用RTI中加入联盟函数,第一个参数:成员名字
//第二个参数:加入的联盟的名字
//第三个参数:/********************************/
fedHd = rtiAmb.joinFederationExecution(myname,fedExecName,&fedAmb);
if(fedHd)
{
m_yourname.GetWindowText(myname,20);
strcpy(fedmember[m_fedcount++].fedname,myname);//将本盟员名字信息加入到盟员信息表中
GetClientRect(rect); //GetClientRect取得窗口客户区(不包括非客户区)在客户区坐标系下的RECT坐标
InvalidateRect(rect); //窗口局部刷新
UpdateWindow();
}
ms_commTypeId = rtiAmb.getInteractionClassHandle("Communication"); //获得交互类句柄
m_parameterIdMessage= rtiAmb.getParameterHandle( "Message",ms_commTypeId);//获得参数句柄
m_parameterIdFederateName=rtiAmb.getParameterHandle("FederateName",ms_commTypeId);//获得参数句柄
rtiAmb.publishInteractionClass(ms_commTypeId); //发布
rtiAmb.subscribeInteractionClass(ms_commTypeId); //订购
}
catch (RTI::Exception &e )
{
AfxMessageBox(e._name);
}
Sleep(1000);
sendFedName(myname);//盟员加入联盟后向联盟中的其他盟员发送自己的名字,使得其他成员发现其存在
Sleep(10);
}
void CWuziqiDlg::OnResign()
{
// TODO: Add your control notification handler code here
sendResignMsg(myname); //退出联盟前向其他盟员发送退出联盟消息
Sleep(500);
try
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -