五子棋源代码(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=0int 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 (0010001000);
    CalcWindowRect (
&rect);

    SetWindowPos (NULL, 
00, 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 (000));
    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 (00255));
    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 (00255));
    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 ();
}

原创粉丝点击