#include <afxwin.h>
#include "TicTac.h"
CMyApp myApp;
/**//////////////////////////////////////////////////////////////////////////
// CMyApp member functions
BOOL CMyApp::InitInstance ()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow ();
return TRUE;
}
/**//////////////////////////////////////////////////////////////////////////
// CMainWindow message map and member functions
BEGIN_MESSAGE_MAP (CMainWindow, CWnd)
ON_WM_PAINT ()
ON_WM_LBUTTONDOWN ()
ON_WM_LBUTTONDBLCLK ()
ON_WM_RBUTTONDOWN ()
END_MESSAGE_MAP ()
CMainWindow::CMainWindow ()
{
//初始化棋子的位置
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
m_rcSquares[i][j]=CRect( j*50 , i*50 , (j+1)*50 , (i+1)*50 ) ;
}
}
m_nNextChar = EX;
for(int k=0;k<20;k++)
::ZeroMemory (m_nGameGrid[k], 20 * sizeof (int));
//
// Register a WNDCLASS.
//
CString strWndClass = AfxRegisterWndClass (
CS_DBLCLKS, // Class style
AfxGetApp ()->LoadStandardCursor (IDC_ARROW), // Class cursor
(HBRUSH) (COLOR_3DFACE + 1), // Background brush
AfxGetApp ()->LoadStandardIcon (IDI_WINLOGO) // Class icon
);
//
// Create a window.
//
CreateEx (0, strWndClass, _T ("Tic-Tac-Toe"),
WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL);
//
// Size the window.
//
CRect rect (0, 0, 1000, 1000);
CalcWindowRect (&rect);
SetWindowPos (NULL, 0, 0, rect.Width (), rect.Height (),
SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW);
}
void CMainWindow::PostNcDestroy ()
{
delete this;
}
void CMainWindow::OnPaint ()
{
CPaintDC dc (this);
DrawBoard (&dc);
}
void CMainWindow::OnLButtonDown (UINT nFlags, CPoint point)
{
//
// Do nothing if it's O's turn, if the click occurred outside the
// tic-tac-toe grid, or if a nonempty square was clicked.
//
if (m_nNextChar != EX)
return;
com nPos = GetRectID (point);
if ((nPos.m== -1) || (m_nGameGrid[nPos.m][nPos.n] != 0))
return;
//
// Add an X to the game grid and toggle m_nNextChar.
//
m_nGameGrid[nPos.m][nPos.n] = EX;
m_nNextChar = OH;
//
// Draw an X on the screen and see if either player has won.
//
CClientDC dc (this);
DrawX (&dc, nPos);
CheckForGameOver ();
}
void CMainWindow::OnRButtonDown (UINT nFlags, CPoint point)
{
//
// Do nothing if it's X's turn, if the click occurred outside the
// tic-tac-toe grid, or if a nonempty square was clicked.
//
if (m_nNextChar != OH)
return;
com nPos = GetRectID (point);
if ( (nPos.m== -1 && nPos.n==-1) || (m_nGameGrid[nPos.m][nPos.n] != 0))
return;
//
// Add an O to the game grid and toggle m_nNextChar.
//
m_nGameGrid[nPos.m][nPos.n] = OH;
m_nNextChar = EX;
//
// Draw an O on the screen and see if either player has won.
//
CClientDC dc (this);
DrawO (&dc, nPos);
CheckForGameOver ();
}
void CMainWindow::OnLButtonDblClk (UINT nFlags, CPoint point)
{
//
// Reset the game if one of the thick black lines defining the game
// grid is double-clicked with the left mouse button.
//
ResetGame ();
}
com CMainWindow::GetRectID (CPoint& point)
{
for (int i=0; i<20; i++)
for(int j=0;j<20;j++)
{
if (m_rcSquares[i][j].PtInRect (point))
return com(i,j);
}
return com(-1,-1);
}
void CMainWindow::DrawBoard (CDC* pDC)
{
CPen pen (PS_SOLID, 1, RGB (0, 0, 0));
CPen* pOldPen = pDC->SelectObject (&pen);
for(int j = 25 ; j <= 1000 ; j += 50 )
{
pDC->MoveTo(j,0);
pDC->LineTo(j,1000);
}
for(int k = 25 ; k <= 1000 ; k += 50 )
{
pDC->MoveTo(0,k);
pDC->LineTo(1000,k);
}
for(int i=0;i<20;i++)
for(int j=0;j<20;j++)
{
if (m_nGameGrid[i][j] == EX)
{
com A(i,j);
DrawX (pDC, A);
}
else if (m_nGameGrid[i][j] == OH)
{
com B(i,j);
DrawO (pDC, B);
}
}
pDC->SelectObject (pOldPen);
}
void CMainWindow::DrawX (CDC* pDC, com & nPos)
{
CPen pen (PS_SOLID, 1, RGB (0, 0, 255));
CPen* pOldPen = pDC->SelectObject (&pen);
pDC->SelectStockObject (HOLLOW_BRUSH);
CRect rect = m_rcSquares[nPos.m][nPos.n];
// rect.DeflateRect (16, 16);
pDC->Ellipse (rect);
pDC->SelectObject (pOldPen);
}
void CMainWindow::DrawO (CDC* pDC, const com & nPos)
{
CPen pen (PS_SOLID, 1, RGB (0, 0, 255));
CPen* pOldPen = pDC->SelectObject (&pen);
pDC->SelectStockObject (BLACK_BRUSH);
CRect rect = m_rcSquares[nPos.m][nPos.n];
// rect.DeflateRect (16, 16);
pDC->Ellipse (rect);
pDC->SelectObject (pOldPen);
}
void CMainWindow::CheckForGameOver ()
{
int nWinner;
//
// If the grid contains three consecutive Xs or Os, declare a winner
// and start a new game.
//
if (nWinner = IsWinner ()) {
CString string = (nWinner == EX) ?
_T ("X wins!") : _T ("O wins!");
MessageBox (string, _T ("Game Over"), MB_ICONEXCLAMATION | MB_OK);
ResetGame ();
}
//
// If the grid is full, declare a draw and start a new game.
//
else if (IsDraw ()) {
MessageBox (_T ("It's a draw!"), _T ("Game Over"),
MB_ICONEXCLAMATION | MB_OK);
ResetGame ();
}
}
int CMainWindow::IsWinner ()
{
int retrust=0;
int i=0;
int j=0;
int m,n;
//水平位置是否有5个棋子连在一起
while(i<20)
{
while(j<20)
{
if(j+5>=20)
break;
if( m_nGameGrid[i][j]==EX && m_nGameGrid[i][j+1]==EX &&
m_nGameGrid[i][j+2]==EX && m_nGameGrid[i][j+3]==EX &&
m_nGameGrid[i][j+4]==EX )
return retrust=EX;
if( m_nGameGrid[i][j]==OH && m_nGameGrid[i][j+1]==OH &&
m_nGameGrid[i][j+2]==OH && m_nGameGrid[i][j+3]==OH &&
m_nGameGrid[i][j+4]==OH )
return retrust=OH;
j++;
}
j=0;
i++;
}
//垂直位置是否有5个棋子连在一起
i=j=0;
while(j<20)
{
while(i<20)
{
if(i+5>=20)
break;
if( m_nGameGrid[i][j]==EX && m_nGameGrid[i+1][j]==EX &&
m_nGameGrid[i+2][j]==EX && m_nGameGrid[i+3][j]==EX &&
m_nGameGrid[i+4][j]==EX )
return retrust=EX;
if( m_nGameGrid[i][j]==OH && m_nGameGrid[i+1][j]==OH &&
m_nGameGrid[i+2][j]==OH && m_nGameGrid[i+3][j]==OH &&
m_nGameGrid[i+4][j]==OH )
return retrust=OH;
i++;
}
j++;
i=0;
}
//左上到右下斜线是否有5个棋子连在一起
i=j=0;
while(i<20)
{
while(j<20)
{
m=i;
n=j;
if(m+4>=20 ||n+4>=20)
break;
while(m<20 && n<20)
{
if( m_nGameGrid[m][n]==EX && m_nGameGrid[m+1][n+1]==EX &&
m_nGameGrid[m+2][n+2]==EX && m_nGameGrid[m+3][n+3]==EX &&
m_nGameGrid[m+4][n+4]==EX )
return retrust=EX;
if( m_nGameGrid[m][n]==OH && m_nGameGrid[m+1][n+1]==OH &&
m_nGameGrid[m+2][n+2]==OH && m_nGameGrid[m+3][n+3]==OH &&
m_nGameGrid[m+4][n+4]==OH