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

📄 dcframewnd.cpp

📁 mfc设计编程
💻 CPP
字号:
// DCFrameWnd.cpp : implementation file
//

#include "stdafx.h"

#include "res.h"
#include "DCFrameWnd.h"
#include "DCDialog.h" 

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//==========================================================================
//全局量
/////////////////////////////////////////////////////////////////////////////
// DCFrameWnd
/////////////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNCREATE(DCFrameWnd, CFrameWnd)

DCFrameWnd::DCFrameWnd()
{
	LoadFrame(ID_MAIN,
					WS_OVERLAPPED     | \
					WS_CAPTION        | \
                    WS_SYSMENU        | \
                    WS_MINIMIZEBOX    );

	CRect tmp_win;
	CRect tmp_client;

	GetWindowRect(&tmp_win);
	GetClientRect(&tmp_client);

	int tmp_w=tmp_win.Width()-tmp_client.Width()+WIN_W;
	int tmp_h=tmp_win.Height()-tmp_client.Height()+WIN_H;

	MoveWindow(0,0,tmp_w,tmp_h);
	CenterWindow();
	//窗口居中

	mouse_x = 0;
	mouse_y = 0;
	tmp_input = 0;
	tmp_x = 0;
	tmp_y = 0;
}
//----------------------------------------------------------------------------
DCFrameWnd::~DCFrameWnd()
{
}
//////////////////////////////////////////////////////////////////////////////
//消息映射
//////////////////////////////////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(DCFrameWnd, CFrameWnd)
	//{{AFX_MSG_MAP(DCFrameWnd)
	ON_COMMAND(ID_MENU_ABOUT, OnMenuAbout)
	ON_COMMAND(ID_MENU_EXIT, OnMenuExit)
	ON_WM_PAINT()
	ON_COMMAND(ID_MENU_0, OnMenu0)
	ON_COMMAND(ID_MENU_1, OnMenu1)
	ON_COMMAND(ID_MENU_2, OnMenu2)
	ON_COMMAND(ID_MENU_3, OnMenu3)
	ON_COMMAND(ID_MENU_4, OnMenu4)
	ON_COMMAND(ID_MENU_5, OnMenu5)
	ON_COMMAND(ID_MENU_AI_FIRST, OnMenuAiFirst)
	ON_COMMAND(ID_MENU_HELP, OnMenuHelp)
	ON_COMMAND(ID_MENU_PLAYER_FIRST, OnMenuPlayerFirst)
	ON_COMMAND(ID_MENU_6, OnMenu6)
	ON_COMMAND(ID_MENU_7, OnMenu7)
	ON_COMMAND(ID_MENU_8, OnMenu8)
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DCFrameWnd message handlers
/////////////////////////////////////////////////////////////////////////////

//=====================================================================

void DCFrameWnd::OnMenuAbout() 
{
	DCDialog MyAbout;
	MyAbout.DoModal();// TODO: Add your command handler code here
	
}

void DCFrameWnd::OnMenuExit() 
{
	DestroyWindow();
	// TODO: Add your command handler code here
	
}

void DCFrameWnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	//------------------------------------
	//
	dc.MoveTo(0,WIN_H/3);
	dc.LineTo(WIN_W,WIN_H/3);

	dc.MoveTo(0,WIN_H/3*2);
	dc.LineTo(WIN_W,WIN_H/3*2);

	dc.MoveTo(WIN_W/3,0);
	dc.LineTo(WIN_W/3,WIN_H);

	dc.MoveTo(WIN_W/3*2,0);
	dc.LineTo(WIN_W/3*2,WIN_H);
	//----------------------------------------
 	/*****************************************************************************
	调试信息
	char tmp[256];
	
	sprintf(tmp,"%d,%d,%d,%d,%d,%d,%d,%d,%d-{%d}(%d,%d)[%d]-[%d,%d]",
		m_game.m_room[0],
		m_game.m_room[1],
		m_game.m_room[2],
		m_game.m_room[3],
		m_game.m_room[4],
		m_game.m_room[5],
		m_game.m_room[6],
		m_game.m_room[7],
		m_game.m_room[8],
		m_game.winner(),
		mouse_x,
		mouse_y,
		tmp_input,
		tmp_x,
		tmp_y
		);
	
	dc.TextOut(2,2,tmp);


	
	*****************************************************************************/
	//--------------------------------------------------
	//绘棋子
	int i,j;
	
	for (i=0;i<3;++i)
	{	
		for (j=0;j<3;++j)
		{
			if (m_game.m_room[i+j*3] == D_AI)
			{
				dc.MoveTo(i*(WIN_W/3),j*(WIN_W/3));
				dc.LineTo(i*(WIN_W/3)+(WIN_W/3),j*(WIN_W/3)+(WIN_W/3));
				
				dc.MoveTo(i*(WIN_W/3)+(WIN_W/3),j*(WIN_W/3));
				dc.LineTo(i*(WIN_W/3),j*(WIN_W/3)+(WIN_W/3));
			}

			if (m_game.m_room[i+j*3] == D_ME)
			{
				dc.Ellipse(i*(WIN_W/3),j*(WIN_W/3),(i+1)*(WIN_W/3),(j+1)*(WIN_W/3));

		

			}
			
		}
		
	}


	// TODO: Add your message handler code here
	
	// Do not call CFrameWnd::OnPaint() for painting messages
}

void DCFrameWnd::OnMenu0() 
{
	m_game.humanMove(0);
	m_game.computerMove();
	InvalidateRect(NULL,TRUE);

	
	// TODO: Add your command handler code here
	
}

void DCFrameWnd::OnMenu1() 
{

	m_game.humanMove(1);
	m_game.computerMove();
	InvalidateRect(NULL,TRUE);

	// TODO: Add your command handler code here
	
}

void DCFrameWnd::OnMenu2() 
{
	m_game.humanMove(2);
	m_game.computerMove();
	InvalidateRect(NULL,TRUE);
	// TODO: Add your command handler code here
	
}

void DCFrameWnd::OnMenu3() 
{
	m_game.humanMove(3);
	m_game.computerMove();
	InvalidateRect(NULL,TRUE);
	// TODO: Add your command handler code here
	
}

void DCFrameWnd::OnMenu4() 
{
	m_game.humanMove(4);
	m_game.computerMove();
	InvalidateRect(NULL,TRUE);
// TODO: Add your command handler code here
	
}

void DCFrameWnd::OnMenu5() 
{
	m_game.humanMove(5);
	m_game.computerMove();
	InvalidateRect(NULL,TRUE);
	// TODO: Add your command handler code here
	
}
void DCFrameWnd::OnMenu6() 
{
	m_game.humanMove(6);
	m_game.computerMove();
	InvalidateRect(NULL,TRUE);
	// TODO: Add your command handler code here
	
}

void DCFrameWnd::OnMenuAiFirst() 
{
	m_game.init();
	m_game.computerMove();
	InvalidateRect(NULL,TRUE);
	// TODO: Add your command handler code here
	
}

void DCFrameWnd::OnMenu7() 
{
	m_game.humanMove(7);
	m_game.computerMove();
	InvalidateRect(NULL,TRUE);
	// TODO: Add your command handler code here
	
}

void DCFrameWnd::OnMenu8() 
{
	m_game.humanMove(8);
	m_game.computerMove();
	InvalidateRect(NULL,TRUE);
	// TODO: Add your command handler code here
	
	
}

void DCFrameWnd::OnMenuHelp() 
{
	MessageBox("简单的井字棋,不要告诉我你不知道规则。","游戏规则");// TODO: Add your command handler code here
	
}

void DCFrameWnd::OnMenuPlayerFirst() 
{
	m_game.init();// TODO: Add your command handler code here
	InvalidateRect(NULL,TRUE);
	
}


void DCFrameWnd::OnLButtonDown(UINT nFlags, CPoint point) 
{


	if (m_game.winner()==D_AI)
	{
		MessageBox("电脑赢了,重来吧!","棼紫|DKink");
	}

	if (m_game.winner()==D_ME)
	{
		MessageBox("???不会吧,你赢了,作为开发者我没赢过!","棼紫|DKink");
	}
	
		if (m_game.winner()==D_TIE)
	{
		MessageBox("平局,不用担心,作为开发者我也没赢过!","棼紫|DKink");
	}


	mouse_x = point.x;
	mouse_y = point.y;

	tmp_x = point.x/(WIN_W/3);
	tmp_y = point.y/(WIN_H/3);

	tmp_input = tmp_y * 3 + tmp_x;

	if (( 0 > tmp_input ) || ( 8 < tmp_input))
	{
		return;

	}


	if (m_game.humanMove(tmp_input))
	{	
		m_game.computerMove();
	}

	InvalidateRect(NULL,TRUE);

	
	// TODO: Add your message handler code here and/or call default
	
	CFrameWnd::OnLButtonDown(nFlags, point);
}

⌨️ 快捷键说明

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