OmokView.h
// OmokView.h : interface of the COmokView class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_OMOKVIEW_H__95EACAA5_FAEA_4766_A6B3_6C6245050A8B__INCLUDED_)
#define AFX_OMOKVIEW_H__95EACAA5_FAEA_4766_A6B3_6C6245050A8B__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class COmokView : public CView
{
private:
int row, col;
int omokBoard[19][19];
int number;
int count;
protected: // create from serialization only
COmokView();
DECLARE_DYNCREATE(COmokView)
// Attributes
public:
COmokDoc* GetDocument();
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(COmokView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL
// Implementation
public:
void CheckMove(int r, int c, int x, int y);
bool CheckOmok();
void init();
virtual ~COmokView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(COmokView)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in OmokView.cpp
inline COmokDoc* COmokView::GetDocument()
{ return (COmokDoc*)m_pDocument; }
#endif
/////////////////////////////////////////////////////////////////////////////
//Template:AFX INSERT LOCATION
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_OMOKVIEW_H__95EACAA5_FAEA_4766_A6B3_6C6245050A8B__INCLUDED_)
OmokView.cpp
// omokView.cpp : implementation of the COmokView class
//
#include "stdafx.h"
#include "omok.h"
#include "omokDoc.h"
#include "omokView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// COmokView
IMPLEMENT_DYNCREATE(COmokView, CView)
BEGIN_MESSAGE_MAP(COmokView, CView)
//{{AFX_MSG_MAP(COmokView)
ON_WM_LBUTTONDOWN()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// COmokView construction/destruction
COmokView::COmokView()
{
init();
}
void COmokView::init()
{
row = -1;
col = -1;
number=0;
for(int row=0;row<19;row++)
for(int col=0;col<19;col++)
omokBoard[row][col]=0;
}
COmokView::~COmokView()
{
}
BOOL COmokView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// COmokView drawing
void COmokView::OnDraw(CDC* pDC)
{
COmokDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CBrush white(RGB(255,255,255));
CBrush black(RGB(0,0,0,));
// 오목판 줄 긋기
for( int i = 20 ; i <= 560 ; i += 30 )
{
pDC->MoveTo(i, 20);
pDC->LineTo(i, 560);
pDC->MoveTo(20, i);
pDC->LineTo(560, i);
}
// 천원(天元), 화점(花點) 찍기
pDC->SelectObject(&black);
pDC->Ellipse(3*30+20-5,3*30+20-5,3*30+20+5,3*30+20+5);
pDC->Ellipse(9*30+20-5,3*30+20-5,9*30+20+5,3*30+20+5);
pDC->Ellipse(15*30+20-5,3*30+20-5,15*30+20+5,3*30+20+5);
pDC->Ellipse(3*30+20-5,9*30+20-5,3*30+20+5,9*30+20+5);
pDC->Ellipse(9*30+20-5,9*30+20-5,9*30+20+5,9*30+20+5);
pDC->Ellipse(15*30+20-5,9*30+20-5,15*30+20+5,9*30+20+5);
pDC->Ellipse(3*30+20-5,15*30+20-5,3*30+20+5,15*30+20+5);
pDC->Ellipse(9*30+20-5,15*30+20-5,9*30+20+5,15*30+20+5);
pDC->Ellipse(15*30+20-5,15*30+20-5,15*30+20+5,15*30+20+5);
// 돌 놓기
for(int row=0;row<19;row++)
for(int col=0;col<19;col++)
if(omokBoard[row][col]!=0)
{
if(omokBoard[row][col]%2==1)
pDC->SelectObject(&black);
else
pDC->SelectObject(&white);
pDC->Ellipse(row*30+10, col*30+10, row*30+30, col*30+30);
}
}
/////////////////////////////////////////////////////////////////////////////
// COmokView printing
BOOL COmokView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void COmokView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void COmokView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// COmokView diagnostics
#ifdef _DEBUG
void COmokView::AssertValid() const
{
CView::AssertValid();
}
void COmokView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
COmokDoc* COmokView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COmokDoc)));
return (COmokDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// COmokView message handlers
void COmokView::OnLButtonDown(UINT nFlags, CPoint point)
{
row=(point.x-5)/30;
col=(point.y-5)/30;
if((row>=0 && row<19) && (col>=0 && col<19) && omokBoard[row][col] == 0)
omokBoard[row][col] = ++number;
Invalidate(true);
if(CheckOmok())
{
if(number % 2 == 0)
MessageBox("흰 돌 승리!!");
else
MessageBox("검은돌 승리!!");
init();
Invalidate(true);
}
CView::OnLButtonDown(nFlags, point);
}
bool COmokView::CheckOmok()
{
// Left & Right
count = 0;
if (row > 0)
CheckMove(row, col, -1, 0);
if (row < 19)
CheckMove(row, col, 1, 0);
if (count == 4)
return true;
// LeftUpper & RightBottom
count = 0;
if (row > 0 && col > 0)
CheckMove(row, col, -1, -1);
if (row < 19 && col < 19)
CheckMove(row, col, 1, 1);
if (count == 4)
return true;
// Upper & Bottom
count = 0;
if (col > 0)
CheckMove(row, col, 0, -1);
if (col < 19)
CheckMove(row, col, 0, 1);
if (count == 4)
return true;
// RightUpper & LeftBottom
count = 0;
if (col > 0 && row < 19)
CheckMove(row, col, 1, -1);
if (col < 19 && row > 0)
CheckMove(row, col, -1, 1);
if (count == 4)
return true;
return false;
}
void COmokView::CheckMove(int r, int c, int x, int y)
{
r += x;
c += y;
if (omokBoard[r][c] != 0)
if (omokBoard[r][c] % 2 == omokBoard[row][col] % 2)
{
count++;
CheckMove(r,c, x, y);
}
}