五子棋源代码(VC++6.0)
来源:互联网 发布:修改nginx 404页面 编辑:程序博客网 时间:2024/05/18 01:26
//作者:廖福平
//毕业院校:江西师范大学
联系方式:sms91@126.com
一、头文件
//TicTac.h
#define EX 1
#define OH 2
class com
...{
public:
com(int i=0, int j=0):m(i),n(j)
...{
};
com& operator=(const com & rv)
...{
m=rv.m;
n=rv.n;
return *this;
};
int m;
int n;
}; //为了保存的棋子的坐标
class CMyApp : public CWinApp
...{
public:
virtual BOOL InitInstance ();
};
class CMainWindow : public CWnd
...{
protected:
CRect m_rcSquares[20][20]; // Grid coordinates
int m_nGameGrid[20][20]; // Grid contents
int m_nNextChar; // Next character (EX or OH)
com GetRectID (CPoint & point);
void DrawBoard (CDC* pDC);
void DrawX (CDC* pDC, com & nPos);
void DrawO (CDC* pDC, const com & nPos);
void ResetGame ();
void CheckForGameOver ();
int IsWinner ();
BOOL IsDraw ();
public:
CMainWindow ();
protected:
virtual void PostNcDestroy ();
afx_msg void OnPaint ();
afx_msg void OnLButtonDown (UINT nFlags, CPoint point);
afx_msg void OnLButtonDblClk (UINT nFlags, CPoint point);
afx_msg void OnRButtonDown (UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP ()
};
#define EX 1
#define OH 2
class com
...{
public:
com(int i=0, int j=0):m(i),n(j)
...{
};
com& operator=(const com & rv)
...{
m=rv.m;
n=rv.n;
return *this;
};
int m;
int n;
}; //为了保存的棋子的坐标
class CMyApp : public CWinApp
...{
public:
virtual BOOL InitInstance ();
};
class CMainWindow : public CWnd
...{
protected:
CRect m_rcSquares[20][20]; // Grid coordinates
int m_nGameGrid[20][20]; // Grid contents
int m_nNextChar; // Next character (EX or OH)
com GetRectID (CPoint & point);
void DrawBoard (CDC* pDC);
void DrawX (CDC* pDC, com & nPos);
void DrawO (CDC* pDC, const com & nPos);
void ResetGame ();
void CheckForGameOver ();
int IsWinner ();
BOOL IsDraw ();
public:
CMainWindow ();
protected:
virtual void PostNcDestroy ();
afx_msg void OnPaint ();
afx_msg void OnLButtonDown (UINT nFlags, CPoint point);
afx_msg void OnLButtonDblClk (UINT nFlags, CPoint point);
afx_msg void OnRButtonDown (UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP ()
};
二、CPP文件
#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 )
return retrust=OH;
m++; n++;
}
j++;
}
i++;
j=0;
}
//左下到右上斜线是否有5个棋子连在一起
i=19;
j=0;
while(i>=0)
...{
while(j<20)
...{
m=i;
n=j;
if(m-4<0 ||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 )
return retrust=OH;
m--; n++;
}
j++;
}
j=0;
i--;
}
return retrust;
}
BOOL CMainWindow::IsDraw ()
...{
BOOL retrust=TRUE;
for(int i=0;i<20;i++)
for(int j=0;j<20;j++)
...{
if (m_nGameGrid[i][j] == 0)
retrust=FALSE;
}
return retrust;
}
void CMainWindow::ResetGame ()
...{
m_nNextChar = EX;
for(int i=0;i<20;i++)
::ZeroMemory (m_nGameGrid[i], 20 * sizeof (int));
Invalidate ();
}
#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 )
return retrust=OH;
m++; n++;
}
j++;
}
i++;
j=0;
}
//左下到右上斜线是否有5个棋子连在一起
i=19;
j=0;
while(i>=0)
...{
while(j<20)
...{
m=i;
n=j;
if(m-4<0 ||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 )
return retrust=OH;
m--; n++;
}
j++;
}
j=0;
i--;
}
return retrust;
}
BOOL CMainWindow::IsDraw ()
...{
BOOL retrust=TRUE;
for(int i=0;i<20;i++)
for(int j=0;j<20;j++)
...{
if (m_nGameGrid[i][j] == 0)
retrust=FALSE;
}
return retrust;
}
void CMainWindow::ResetGame ()
...{
m_nNextChar = EX;
for(int i=0;i<20;i++)
::ZeroMemory (m_nGameGrid[i], 20 * sizeof (int));
Invalidate ();
}
- 五子棋源代码(VC++6.0)
- VC++ 五子棋
- 五子棋的源代码
- 五子棋游戏源代码
- 我的五子棋源代码
- java简单五子棋源代码
- java 五子棋源代码+注释
- java 五子棋 源代码
- 初学VC之五子棋
- 用opencv和vc++6.0开发的五子棋游戏软件
- java网络五子棋的源代码
- 用JAVA做五子棋源代码
- c++版五子棋 源代码,2009
- java图形界面五子棋源代码共享
- C语言五子棋游戏源代码
- VC源代码
- java网络五子棋的源代码(一)
- java网络五子棋的源代码(二)
- WCDMA缩略语 & 几个容易混淆的概念 & 关于掉话原因分析
- 怎样使用JSP发送email?(一)
- 网络socket编程指南
- ACM培训第三周总结
- 我和我女朋友之四
- 五子棋源代码(VC++6.0)
- Java开发的环境配置
- 需要看的文章
- Asp.net页面之间传递参数的几种方法
- 好久没写blog了,最近干了些啥?
- “劣质”键盘惹的祸?
- Interface-接口
- 如何设计一个大型的AJAX应用程序
- GIS的坐标系统(转载)