📄 othelloview.cpp
字号:
// OthelloView.cpp : implementation of the COthelloView class
//
#include "stdafx.h"
#include "Othello.h"
#include "OthelloDoc.h"
#include "OthelloView.h"
#include "WinDlg.h"
#include <mmsystem.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// COthelloView
IMPLEMENT_DYNCREATE(COthelloView, CView)
BEGIN_MESSAGE_MAP(COthelloView, CView)
//{{AFX_MSG_MAP(COthelloView)
ON_COMMAND(ID_GAME_NEW, OnGameNew)
ON_WM_LBUTTONUP()
ON_WM_TIMER()
ON_COMMAND(ID_GAME_FIRST, OnGameFirst)
ON_COMMAND(ID_GAME_SECOND, OnGameSecond)
ON_COMMAND(ID_GAME_DOUBLE, OnGameDouble)
ON_UPDATE_COMMAND_UI(ID_GAME_FIRST, OnUpdateGameFirst)
ON_UPDATE_COMMAND_UI(ID_GAME_SECOND, OnUpdateGameSecond)
ON_COMMAND(ID_FUNCTION_HINT, OnFunctionHint)
ON_UPDATE_COMMAND_UI(ID_FUNCTION_HINT, OnUpdateFunctionHint)
ON_COMMAND(ID_FUNCTION_REDO, OnFunctionRedo)
ON_COMMAND(ID_FUNCTION_UNDO, OnFunctionUndo)
ON_COMMAND(ID_FUNCTION_DEPTH9, OnFunctionDepth9)
ON_UPDATE_COMMAND_UI(ID_FUNCTION_DEPTH9, OnUpdateFunctionDepth9)
ON_COMMAND(ID_FUNCTION_DEPTH8, OnFunctionDepth8)
ON_UPDATE_COMMAND_UI(ID_FUNCTION_DEPTH8, OnUpdateFunctionDepth8)
ON_COMMAND(ID_FUNCTION_DEPTH7, OnFunctionDepth7)
ON_UPDATE_COMMAND_UI(ID_FUNCTION_DEPTH7, OnUpdateFunctionDepth7)
ON_COMMAND(ID_FUNCTION_DEPTH6, OnFunctionDepth6)
ON_UPDATE_COMMAND_UI(ID_FUNCTION_DEPTH6, OnUpdateFunctionDepth6)
ON_COMMAND(ID_FUNCTION_DEPTH5, OnFunctionDepth5)
ON_UPDATE_COMMAND_UI(ID_FUNCTION_DEPTH5, OnUpdateFunctionDepth5)
ON_COMMAND(ID_FUNCTION_DEPTH4, OnFunctionDepth4)
ON_UPDATE_COMMAND_UI(ID_FUNCTION_DEPTH4, OnUpdateFunctionDepth4)
ON_COMMAND(ID_FUNCTION_DEPTH3, OnFunctionDepth3)
ON_UPDATE_COMMAND_UI(ID_FUNCTION_DEPTH3, OnUpdateFunctionDepth3)
ON_COMMAND(ID_FUNCTION_DEPTH2, OnFunctionDepth2)
ON_UPDATE_COMMAND_UI(ID_FUNCTION_DEPTH2, OnUpdateFunctionDepth2)
ON_COMMAND(ID_FUNCTION_DEPTH1, OnFunctionDepth1)
ON_UPDATE_COMMAND_UI(ID_FUNCTION_DEPTH1, OnUpdateFunctionDepth1)
ON_COMMAND(ID_FUNCTION_STABLE, OnFunctionStable)
ON_UPDATE_COMMAND_UI(ID_FUNCTION_STABLE, OnUpdateFunctionStable)
ON_COMMAND(ID_FUNCTION_ACTION, OnFunctionAction)
ON_UPDATE_COMMAND_UI(ID_FUNCTION_ACTION, OnUpdateFunctionAction)
ON_COMMAND(ID_HELP_HELP, OnHelpHelp)
ON_COMMAND(ID_FUNCTION_MUSIC, OnFunctionMusic)
ON_UPDATE_COMMAND_UI(ID_FUNCTION_MUSIC, OnUpdateFunctionMusic)
ON_COMMAND(ID_GAME_DISPLAY, OnGameDisplay)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COthelloView construction/destruction
COthelloView::COthelloView()
{
// TODO: add construction code here
m_Change = 0;
timer = 0;
m_Music = false;
mciOpen.lpstrDeviceType=0;
mciOpen.lpstrElementName="./res/music.mp3";
mciSendCommand(NULL,MCI_OPEN, MCI_OPEN_ELEMENT,(DWORD)&mciOpen);
}
COthelloView::~COthelloView()
{
mciSendCommand(mciOpen.wDeviceID,MCI_CLOSE,NULL,NULL);
}
BOOL COthelloView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// COthelloView drawing
void COthelloView::OnDraw(CDC* pDC)
{
COthelloDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if(timer ==0) {
timer = 1;
SetTimer(1,500,NULL);
}
CRect r;
GetClientRect(&r);
m_Origin = r.bottom/10;
pDoc->m_Judge->Draw(pDC,r.bottom,m_Change);
}
/////////////////////////////////////////////////////////////////////////////
// COthelloView printing
BOOL COthelloView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void COthelloView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void COthelloView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// COthelloView diagnostics
#ifdef _DEBUG
void COthelloView::AssertValid() const
{
CView::AssertValid();
}
void COthelloView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
COthelloDoc* COthelloView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COthelloDoc)));
return (COthelloDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// COthelloView message handlers
void COthelloView::OnGameNew()
{
// TODO: Add your command handler code here
// if(timer==1) {
// KillTimer(1);
// timer = 0;
// }
COthelloDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(m_Change==0 && pDoc->m_Judge->m_GoAI==false){
KillTimer(2);
PlaySound("MouseClick",NULL,SND_ASYNC);
pDoc->m_Judge->Initiate();
Invalidate(0);
}
// if(timer==0) {
// SetTimer(1,500,NULL);
// timer = 1;
// }
Invalidate(0);
}
void COthelloView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
COthelloDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int x,y;
if( (point.x-m_Origin)>0 && (point.y-m_Origin)>0) {
x = (point.x-m_Origin)/m_Origin;
y = (point.y-m_Origin)/m_Origin;
if(x>=0 && x<8 && y>=0 && y<8) {
if(pDoc->m_Judge->m_GoHuman) {
pDoc->m_Judge->m_GoHuman = false;
pDoc->m_Judge->m_Player->Drop(x,y);
pDoc->m_Judge->Play();
SetTimer(2,100,NULL);
}
else {
if(pDoc->m_Judge->m_Gameover) {
AfxMessageBox("单击游戏,开始新游戏 ");
}
}
}
}
CView::OnLButtonUp(nFlags, point);
}
void COthelloView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
COthelloDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
switch(nIDEvent) {
case 1:
if(pDoc->m_Judge->m_GoAI) {
pDoc->m_Judge->m_GoAI = false;
pDoc->m_Judge->Play();
SetTimer(2,100,NULL);
}
break;
case 2:
if(m_Change>7){
KillTimer(2);
pDoc->m_Judge->m_Change = false;
m_Change = 0;
pDoc->m_Judge->GoOn();
Invalidate(0);
break;
}
if(pDoc->m_Judge->m_Change) {
m_Change++;
Invalidate(0);
}
break;
case 3:
if(m_Music) {
MCI_PLAY_PARMS mciPlay;
mciPlay.dwFrom = 0;
mciSendCommand(mciOpen.wDeviceID,MCI_PLAY,MCI_NOTIFY | MCI_FROM,
(DWORD)&mciPlay);
}
else {
KillTimer(3);
}
break;
case 4:
if(m_Change>7){
m_Change = 0;
Invalidate(0);
if(!pDoc->m_Judge->Display()) {
KillTimer(4);
pDoc->m_Judge->m_Gameover = true;
break;
}
}
else {
m_Change++;
Invalidate(0);
}
break;
}
CView::OnTimer(nIDEvent);
}
/*
void COthelloView::OnOptionA2a()
{
// TODO: Add your command handler code here
COthelloDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDoc->m_Judge->m_Model = A2A;
}
*/
void COthelloView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
// TODO: Add your specialized code here and/or call the base class
Invalidate(0);
}
void COthelloView::OnGameFirst()
{
// TODO: Add your command handler code here
COthelloDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDoc->m_Judge->m_Model = H2A;
pDoc->m_Judge->Initiate();
Invalidate(0);
}
void COthelloView::OnGameSecond()
{
// TODO: Add your command handler code here
COthelloDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDoc->m_Judge->m_Model = A2H;
pDoc->m_Judge->Initiate();
Invalidate(0);
}
void COthelloView::OnGameDouble()
{
// TODO: Add your command handler code here
COthelloDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDoc->m_Judge->m_Model = H2H;
pDoc->m_Judge->Initiate();
Invalidate(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -