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

📄 mfcpodlg.cpp

📁 java game uploading now
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MfcPoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MfcPo.h"
#include "MfcPoDlg.h"
#include "OAI.h"
#include "GameType.h"

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

//XA = board size x, XB = board size y
#define XA 332
#define XB 332
us	undos;

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CMfcPoDlg dialog

CMfcPoDlg::CMfcPoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMfcPoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMfcPoDlg)
	m_info = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}

void CMfcPoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMfcPoDlg)
	DDX_Control(pDX, IDC_LIST1, m_hislist);
	DDX_Text(pDX, IDC_INFO, m_info);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMfcPoDlg, CDialog)
	//{{AFX_MSG_MAP(CMfcPoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_LBUTTONDOWN()
	ON_BN_CLICKED(IDNEWGAME, OnNewgame)
	ON_BN_CLICKED(IDUndoMove, OnUndoMove)
	ON_COMMAND(ID_FILE_EXIT, OnFileExit)
	ON_COMMAND(ID_FILE_NEWGAME, OnFileNewgame)
	ON_COMMAND(ID_FILE_SAVEGame, OnFILESAVEGame)
	ON_COMMAND(ID_FILE_LOADGAME, OnFileLoadgame)
	ON_COMMAND(ID_HELP_SUGGESTMOVE, OnHelpSuggestmove)
	ON_COMMAND(ID_HELP_UNDOMOVE, OnHelpUndomove)
	ON_COMMAND(ID_EVALUATE_BOARDEVALUATION, OnEvaluateBoardevaluation)
	ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout)
	ON_WM_CREATE()
	ON_COMMAND(ID_VIEW_MIRRORXY, OnViewMirrorxy)
	ON_COMMAND(ID_VIEW_MIRRORX, OnViewMirrorx)
	ON_COMMAND(ID_VIEW_MIRRORY, OnViewMirrory)
	ON_COMMAND(ID_VIEW_ROTATECLOCKWISE, OnViewRotateclockwise)
	ON_COMMAND(ID_VIEW_ROTATEANTICLOCKWISE, OnViewRotateanticlockwise)
	ON_COMMAND(ID_OPTIONS_AISETTING, OnOptionsAisetting)
	ON_WM_DESTROY()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMfcPoDlg message handlers

BOOL CMfcPoDlg::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

	//define stone location, used to determine mouse location
	int x1,y1=0;
	for (int y=16;y<317;y+=38){
		x1=0;
		for (int x=16;x<317;x+=38){
			mvstr[x1][y1].top= y+5;
			mvstr[x1][y1].bottom=y+33;
			mvstr[x1][y1].left=x+5;
			mvstr[x1][y1].right=x+33;
			ai.board[x1][y1]=EMPTY;		//empty
			x1++;
		}
	y1++;
	}
	ai.new_board();

	undos.no_indos = 0;
	CGameType gametype;
	if (gametype.DoModal()==IDOK){
		ai.to_play = 1;	//black 
		if (gametype.m_select)
			oneplayer = 0;
		else{
			oneplayer = 1;
			if (gametype.m_move){
				ai.board[4][4] = BLACK;
				switch (GetTickCount()%2){
				case 0:
					ai.board[5][4] = BLACK;
					m_hislist.AddString(MoveDisplay(BLACK,5,4));
					break;
				case 1:
					ai.board[4][5] = BLACK;
					m_hislist.AddString(MoveDisplay(BLACK,4,5));
				}
				ai.to_play = -1;	//white turn
			}
		}

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

void CMfcPoDlg::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 CMfcPoDlg::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
	{
		CPaintDC dc(this);
		CClientDC cdc(this);
		cdc.BitBlt(0,0,XA,XB,&m_bgdc,0,0,SRCCOPY);
		for(int x=0;x<8;x++){
			for (int y=0;y<8;y++){
				if (ai.board[x][y]== WHITE)
					DrawW(&cdc,x,y);
				else if (ai.board[x][y]== BLACK)
					DrawB(&cdc,x,y);
				else if (ai.is_legal(ai.to_play,x,y))
					DrawV(&cdc,x,y);	//draw valid moves label

			}
		}
		CDialog::OnPaint();
	}
}

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

//draw black stone
void CMfcPoDlg::DrawB(CClientDC *pdc, int x, int y)
{
	CPen OSize(PS_SOLID,3, RGB(0,0,0));
	CBrush lBlack(RGB(0,0,0));

	CPen *lOldPen;
	CBrush *lOldBrush;

	lOldPen = pdc->SelectObject(&OSize);
	lOldBrush = pdc->SelectObject(&lBlack);

	pdc->Ellipse(mvstr[x][y].left, mvstr[x][y].top, mvstr[x][y].right, mvstr[x][y].bottom);

	pdc->SelectObject(lOldPen);
	pdc->SelectObject(lOldBrush);
}

//draw white stone
void CMfcPoDlg::DrawW(CClientDC *pdc, int x, int y)
{
	CPen OSize(PS_SOLID,3, RGB(255,255,255));
	CBrush lBlack(RGB(255,255,255));

	CPen *lOldPen;
	CBrush *lOldBrush;

	lOldPen = pdc->SelectObject(&OSize);
	lOldBrush = pdc->SelectObject(&lBlack);

	pdc->Ellipse(mvstr[x][y].left, mvstr[x][y].top, mvstr[x][y].right, mvstr[x][y].bottom);

	pdc->SelectObject(lOldPen);
	pdc->SelectObject(lOldBrush);
}

//draw valid moves label
void CMfcPoDlg::DrawV(CClientDC *pdc, int x, int y)
{
	CPen OSize(PS_SOLID,3, RGB(200,0,0));
	CBrush lBlack(RGB(200,0,0));

	CPen *lOldPen;
	CBrush *lOldBrush;

	lOldPen = pdc->SelectObject(&OSize);
	lOldBrush = pdc->SelectObject(&lBlack);

	pdc->Ellipse(mvstr[x][y].left+12, mvstr[x][y].top+12, mvstr[x][y].right-12, mvstr[x][y].bottom-12);

	pdc->SelectObject(lOldPen);
	pdc->SelectObject(lOldBrush);

}

//convert mouse position -> board position
BOOL CMfcPoDlg::CheckPos(CPoint point, int &x, int &y)
{
	int xc,yc;
	for (xc=0; xc<8; xc++){
		for (yc=0; yc<8;yc++){
		if ((point.x>mvstr[xc][yc].left)&&(point.x<mvstr[xc][yc].right))
			if ((point.y>mvstr[xc][yc].top)&&(point.y<mvstr[xc][yc].bottom))
			{
				x = xc;
				y = yc;
				return TRUE;
			}
		}
	}
	return FALSE;

}

//when user mouse click
void CMfcPoDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	int xmv, ymv;
	
	if (nFlags!=MK_LBUTTON){
		CDialog::OnLButtonDown(nFlags, point);
		return;
	}

	//return if mouse position is outside the board
	if (!CheckPos(point, xmv, ymv)){
		CDialog::OnLButtonDown(nFlags, point);
		return;
	}

if (ai.is_legal(ai.to_play, xmv,ymv)){
	AddUndo();
	DoMove(xmv,ymv,ai.to_play);
	m_hislist.AddString(MoveDisplay(ai.to_play,xmv,ymv));
	m_hislist.SetCurSel(m_hislist.GetCount()-1);
	ai.to_play = -ai.to_play;	//change turn

	if (CheckWin()){
		CDialog::OnLButtonDown(nFlags, point);
		return;
		}
	else if (oneplayer)
		AIMove(ai.to_play);
	}
else{
	if (ai.anyvalidmove(ai.to_play)){		//any valid move?
		CDialog::OnLButtonDown(nFlags, point);
		return;
	}
	else if (ai.anyvalidmove(-ai.to_play)){	//any opponents move?
		MessageBox("Press OK to skip turn!",(ai.to_play==BLACK)?"BLACK":"WHITE",MB_OK|MB_ICONINFORMATION);
		ai.to_play = - ai.to_play;
		if (oneplayer)
			AIMove(ai.to_play);
	}
	else		//opponents also have no move, so end game
		ShowScore();
	
	}	
	Invalidate();
	CDialog::OnLButtonDown(nFlags, point);
}

//draw stone
void CMfcPoDlg::DrawBW(int x, int y, int type)
{
	CClientDC sdc(this);
	if (type==BLACK)
		DrawB(&sdc,x,y);
	if (type==WHITE)
		DrawW(&sdc,x,y);
}

//new game
void CMfcPoDlg::OnNewgame() 
{
	ai.new_board();
	ClearUndo();
	m_hislist.ResetContent();
	ai.to_play=1;			//black turn

	CGameType gametype;
	if (gametype.DoModal()==IDOK){
		if (gametype.m_select)
			oneplayer = 0;
		else{
			oneplayer = 1;
			if (gametype.m_move){
				ai.board[4][4] = BLACK;
				ai.to_play = -ai.to_play;
				switch (GetTickCount()%2){
				case 0:
					ai.board[5][4] = BLACK;
					m_hislist.AddString(MoveDisplay(BLACK,5,4));
					break;
				case 1:
					ai.board[4][5] = BLACK;
					m_hislist.AddString(MoveDisplay(BLACK,4,5));
				}
			}
		}

	}
	Invalidate();
	
}

//computer move
void CMfcPoDlg::AIMove(int color)
{
	CCmdTarget::BeginWaitCursor();
	int score, x, y, time, nodes;
	if (ai.move(color, score, x, y, time, nodes)){
		ai.play_move(color,x,y);
		m_hislist.AddString(MoveDisplay(color,x,y));
		m_hislist.SetCurSel(m_hislist.GetCount()-1);
		ShowResult(x,y,score,time,nodes);
		CheckWin();
		ai.to_play = -ai.to_play;	//change turn

	}
	else{
		if (score==END)
			ShowScore();
		else if (score==SKIP){
			MessageBox("Press OK to skip turn!",(ai.to_play==BLACK)?"BLACK":"WHITE",MB_OK|MB_ICONINFORMATION);
			ai.to_play = -ai.to_play;
		}
	}
	EndWaitCursor();
}

//check winning condition
BOOL CMfcPoDlg::CheckWin()
{
	if(ai.anyvalidmove(ai.to_play))	//do we still have moves?
		return FALSE;
	if(ai.anyvalidmove(-ai.to_play))//do opponents still have moves?
		return FALSE;

	ShowScore();
	return TRUE;
}

//do move
void CMfcPoDlg::DoMove(int xmv, int ymv, int color)
{
	ai.play_move(color,xmv,ymv);

⌨️ 快捷键说明

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