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

📄 mainfrm.cpp

📁 这是一字棋的极小极大搜索
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "ticktacktoe.h"
#include "ticktacktoeDoc.h"
#include "ticktacktoeView.h"
#include "timelimit.h"

#include "MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_GAME_NEW_H2C, OnGameNewH2c)
	ON_COMMAND(ID_GAME_NEW_H2H, OnGameNewH2h)
	ON_COMMAND(ID_TOOL_UNDO, OnToolUndo)
	ON_COMMAND(ID_TOOL_REDO, OnToolRedo)
	ON_COMMAND(ID_TOOL_DIFFICULTY_EASY, OnToolDifficultyEasy)
	ON_COMMAND(ID_TOOL_DIFFICULTY_NORMAL, OnToolDifficultyNormal)
	ON_COMMAND(ID_TOOL_DIFFICULTY_HARD, OnToolDifficultyHard)

	ON_COMMAND(ID_TOOL_COMFIRST, OnToolComfirst)
	//}}AFX_MSG_MAP
	ON_COMMAND(ID_APP_ABOUT, &CMainFrame::OnAppAbout)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here

}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}
	if (!m_wndDlgBar.Create(this, IDR_MAINFRAME, 
		CBRS_ALIGN_TOP, AFX_IDW_DIALOGBAR))
	{
		TRACE0("Failed to create dialogbar\n");
		return -1;		// fail to create
	}

	if (!m_wndReBar.Create(this) ||
		!m_wndReBar.AddBar(&m_wndToolBar) ||
		!m_wndReBar.AddBar(&m_wndDlgBar))
	{
		TRACE0("Failed to create rebar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Remove this if you don't want tool tips
	m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
		CBRS_TOOLTIPS | CBRS_FLYBY);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	cs.style=WS_SYSMENU|WS_OVERLAPPED|WS_MINIMIZEBOX;
	cs.cx=400;
	cs.cy=440;
	cs.x=300;
	cs.y=200;

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


void CMainFrame::OnGameNewH2c() 
{
	// TODO: Add your command handler code here
	CTicktacktoeView *pView=(CTicktacktoeView *)GetActiveView();
	pView->htoc=true;
	pView->OnStart();
	m_wndStatusBar.SetPaneText(0,"人机对战",1);
	Invalidate();
}

void CMainFrame::OnGameNewH2h() 
{
	// TODO: Add your command handler code here
	CTicktacktoeView *pView=(CTicktacktoeView *)GetActiveView();
	pView->htoc=false;
	pView->OnStart();
	m_wndStatusBar.SetPaneText(0,"人人对战",1);
	Invalidate();
}

void CMainFrame::OnToolUndo() 
{
	// TODO: Add your command handler code here
	CTicktacktoeView *pView=(CTicktacktoeView *)GetActiveView();
	//如果step指示的在manual[][3]中的位置达到1(电脑先下),或0(人先下或人人对战)状态,则直接返回
	if(pView->step<=0) return;
	if(pView->computerFirst&&pView->step<=1) return;
	int i,j,k=0;
	pView->step--;
	//如果是人机对战,且manual中指示的棋步下棋者是人,则再退一步棋(即共退了两步棋)
	if(pView->htoc&&pView->manual[pView->step][0]!=-1) pView->step--;
	//按照manual[][3]中的数据,和step指示的位置,更新chessman[3][3]中信息,并重画界面
	for(i=0;i<pView->chessman.size();i++)
		for(j=0;j<pView->chessman[i].size();j++)
			pView->chessman[i][j]=0;
	for(i=0;i<pView->step;i++)
		pView->chessman[pView->manual[i][1]][pView->manual[i][2]]=
			pView->manual[i][0];
	pView->side=pView->manual[pView->step][0];
	pView->finished=0;
	Invalidate();
}

void CMainFrame::OnToolRedo() 
{
	// TODO: Add your command handler code here
	CTicktacktoeView *pView=(CTicktacktoeView *)GetActiveView();
	//当step指示位置到达manual[][3]的右端时,直接返回
	if(pView->step>=pView->manual.size()) return;
	int i,j,k=0;
	for(i=0;i<pView->chessman.size();i++)
		for(j=0;j<pView->chessman[i].size();j++)
			if(pView->chessman[i][j]==0) k++;
	pView->step++;
	//当人机对战时,若某些条件符合,则应再进一步(即共进了两步棋)
	if(pView->htoc&&pView->manual[pView->step-1][0]==-1
		&&k>=2&&pView->step<pView->manual.size()) pView->step++;
	//按照manual[][3]中的数据,和step指示的位置,更新chessman[3][3]中信息,并重画界面
	for(i=0;i<pView->chessman.size();i++)
		for(j=0;j<pView->chessman[i].size();j++)
			pView->chessman[i][j]=0;
	for(i=0;i<pView->step;i++)
		pView->chessman[pView->manual[i][1]][pView->manual[i][2]]=
			pView->manual[i][0];
	pView->side=-pView->manual[pView->step-1][0];
	if(pView->manual[pView->step-1][0]==1) pView->manTurn=true;
	else pView->manTurn=false;

	pView->Result();
	Invalidate();
}

void CMainFrame::OnToolDifficultyEasy() 
{
	// TODO: Add your command handler code here
	CTicktacktoeView *pView=(CTicktacktoeView *)GetActiveView();
	pView->myDepth=1;
	m_wndStatusBar.SetPaneText(0,"简单",1);
}

void CMainFrame::OnToolDifficultyNormal() 
{
	// TODO: Add your command handler code here
	CTicktacktoeView *pView=(CTicktacktoeView *)GetActiveView();
	pView->myDepth=3;	
	m_wndStatusBar.SetPaneText(0,"一般",1);
}

void CMainFrame::OnToolDifficultyHard() 
{
	// TODO: Add your command handler code here
	CTicktacktoeView *pView=(CTicktacktoeView *)GetActiveView();
	pView->myDepth=5;
	m_wndStatusBar.SetPaneText(0,"困难",1);
}

void CMainFrame::OnToolComfirst() 
{
	// TODO: Add your command handler code here
	CTicktacktoeView *pView=(CTicktacktoeView *)GetActiveView();
	if(pView->htoc)
	{
		pView->OnStart();

		srand((int)time(NULL));
		int i=rand()%3;
		int j=rand()%3;

		pView->chessman[i][j]=1;
		vector <int> tmpVec;
		tmpVec.push_back(1);
		tmpVec.push_back(i);
		tmpVec.push_back(j);
		pView->manual.push_back(tmpVec);
		pView->step++;
		tmpVec.clear();
		pView->manTurn=1;
		pView->computerFirst=true;

		CDC tmpDC,*pDC;
		pDC=GetDC();
		if(tmpDC.CreateCompatibleDC(pDC)==0)
		{
			AfxMessageBox("Fail to create DC!");
			return;
		}
		
		Invalidate();
	}
}


void CMainFrame::OnAppAbout()
{
	// TODO: 在此添加命令处理程序代码
	MessageBox("本实验由第八小组完成");
}

⌨️ 快捷键说明

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