?? table.cpp
字號:
#include "stdafx.h"
#include "Five.h"
#include "Table.h"
#include "Messages.h"
#include "Resource.h"
//////////////////////////////////////////////////////////////////////////
// 構(gòu)造函數(shù),初始化棋盤數(shù)據(jù)以及圖像數(shù)據(jù)
//////////////////////////////////////////////////////////////////////////
CTable::CTable()
{
// 初始化玩家姓名
TCHAR str[10];
CFiveApp *pApp = (CFiveApp *)AfxGetApp();
::GetPrivateProfileString( _T("Options"), _T("Name"), _T("Renjiu"), str, 15, pApp->m_szIni );
m_strMe = str;
// 初始化圖像列表
m_iml.Create( 24, 24, ILC_COLOR24 | ILC_MASK, 0, 2 );
// 載入黑、白棋子掩碼位圖
CBitmap bmpBlack, bmpWhite;
bmpBlack.LoadBitmap( IDB_BMP_BLACK );
m_iml.Add( &bmpBlack, 0xff00ff );
bmpWhite.LoadBitmap( IDB_BMP_WHITE );
m_iml.Add( &bmpWhite, 0xff00ff );
// 初始化游戲模式
m_pGame = NULL;
}
//////////////////////////////////////////////////////////////////////////
// 析構(gòu)函數(shù),釋放m_pGame指針
//////////////////////////////////////////////////////////////////////////
CTable::~CTable()
{
// 寫入玩家姓名
CFiveApp *pApp = (CFiveApp *)AfxGetApp();
::WritePrivateProfileString( _T("Options"), _T("Name"), m_strMe, pApp->m_szIni );
// 寫入戰(zhàn)績統(tǒng)計(jì)
TCHAR str[10];
wsprintf( str, _T("%d"), pApp->m_nWin );
::WritePrivateProfileString( _T("Stats"), _T("Win"), str, pApp->m_szIni );
wsprintf( str, _T("%d"), pApp->m_nDraw );
::WritePrivateProfileString( _T("Stats"), _T("Draw"), str, pApp->m_szIni );
wsprintf( str, _T("%d"), pApp->m_nLost );
::WritePrivateProfileString( _T("Stats"), _T("Lost"), str, pApp->m_szIni );
if ( NULL != m_pGame )
delete m_pGame;
}
//////////////////////////////////////////////////////////////////////////
// 在指定棋盤坐標(biāo)處繪制指定顏色的棋子
//////////////////////////////////////////////////////////////////////////
void CTable::Draw( int x, int y, int color )
{
POINT pt;
pt.x = 12 + 25 * x;
pt.y = 84 + 25 * y;
CDC *pDC = GetDC();
CPen pen;
pen.CreatePen( PS_SOLID, 1, 0xff );
pDC->SelectObject( &pen );
pDC->SetROP2( R2_NOTXORPEN );
m_iml.Draw( pDC, color, pt, ILD_TRANSPARENT );
STEP step;
// 利用R2_NOTXORPEN擦除先前畫出的矩形
if ( !m_pGame->m_StepList.empty() )
{
// 獲取最后一個(gè)點(diǎn)
step = *( m_pGame->m_StepList.begin() );
pDC->MoveTo( 11 + 25 * step.x, 83 + 25 * step.y );
pDC->LineTo( 36 + 25 * step.x, 83 + 25 * step.y );
pDC->LineTo( 36 + 25 * step.x, 108 + 25 * step.y );
pDC->LineTo( 11 + 25 * step.x, 108 + 25 * step.y );
pDC->LineTo( 11 + 25 * step.x, 83 + 25 * step.y );
}
// 更新最后落子坐標(biāo)數(shù)據(jù),畫新的矩形
step.color = color;
step.x = x;
step.y = y;
m_pGame->m_StepList.push_front( step );
pDC->MoveTo( 11 + 25 * step.x, 83 + 25 * step.y );
pDC->LineTo( 36 + 25 * step.x, 83 + 25 * step.y );
pDC->LineTo( 36 + 25 * step.x, 108 + 25 * step.y );
pDC->LineTo( 11 + 25 * step.x, 108 + 25 * step.y );
pDC->LineTo( 11 + 25 * step.x, 83 + 25 * step.y );
ReleaseDC( pDC );
}
//////////////////////////////////////////////////////////////////////////
// 清空棋盤
//////////////////////////////////////////////////////////////////////////
void CTable::Clear( BOOL bWait )
{
int x, y;
for ( y = 0; y < 15; y++ )
{
for ( x = 0; x < 15; x++ )
{
m_data[x][y] = -1;
}
}
// 設(shè)置等待標(biāo)志
m_bWait = bWait;
Invalidate();
// 刪除游戲
if ( m_pGame != NULL )
{
delete m_pGame;
m_pGame = NULL;
}
}
//////////////////////////////////////////////////////////////////////////
// 設(shè)置玩家顏色
//////////////////////////////////////////////////////////////////////////
void CTable::SetColor( int color )
{
m_color = color;
}
//////////////////////////////////////////////////////////////////////////
// 獲取玩家顏色
//////////////////////////////////////////////////////////////////////////
int CTable::GetColor() const
{
return m_color;
}
//////////////////////////////////////////////////////////////////////////
// 設(shè)置等待標(biāo)志,返回先前的等待標(biāo)志
//////////////////////////////////////////////////////////////////////////
BOOL CTable::SetWait( BOOL bWait )
{
m_bOldWait = m_bWait;
m_bWait = bWait;
return m_bOldWait;
}
//////////////////////////////////////////////////////////////////////////
// 設(shè)置棋盤數(shù)據(jù),并繪制棋子
//////////////////////////////////////////////////////////////////////////
void CTable::SetData( int x, int y, int color )
{
m_data[x][y] = color;
Draw( x, y, color );
}
//////////////////////////////////////////////////////////////////////////
// 判斷指定顏色是否勝利
//////////////////////////////////////////////////////////////////////////
BOOL CTable::Win( int color ) const
{
int x, y;
// 判斷橫向
for ( y = 0; y < 15; y++ )
{
for ( x = 0; x < 11; x++ )
{
if ( color == m_data[x][y] && color == m_data[x + 1][y] &&
color == m_data[x + 2][y] && color == m_data[x + 3][y] &&
color == m_data[x + 4][y] )
{
return TRUE;
}
}
}
// 判斷縱向
for ( y = 0; y < 11; y++ )
{
for ( x = 0; x < 15; x++ )
{
if ( color == m_data[x][y] && color == m_data[x][y + 1] &&
color == m_data[x][y + 2] && color == m_data[x][y + 3] &&
color == m_data[x][y + 4] )
{
return TRUE;
}
}
}
// 判斷“\”方向
for ( y = 0; y < 11; y++ )
{
for ( x = 0; x < 11; x++ )
{
if ( color == m_data[x][y] && color == m_data[x + 1][y + 1] &&
color == m_data[x + 2][y + 2] && color == m_data[x + 3][y + 3] &&
color == m_data[x + 4][y + 4] )
{
return TRUE;
}
}
}
// 判斷“/”方向
for ( y = 0; y < 11; y++ )
{
for ( x = 4; x < 15; x++ )
{
if ( color == m_data[x][y] && color == m_data[x - 1][y + 1] &&
color == m_data[x - 2][y + 2] && color == m_data[x - 3][y + 3] &&
color == m_data[x - 4][y + 4] )
{
return TRUE;
}
}
}
// 不滿足勝利條件
return FALSE;
}
//////////////////////////////////////////////////////////////////////////
// 發(fā)送再玩一次請求
//////////////////////////////////////////////////////////////////////////
void CTable::PlayAgain()
{
MSGSTRUCT msg;
msg.uMsg = MSG_PLAYAGAIN;
m_conn.Send( (LPCVOID)&msg, sizeof( MSGSTRUCT ) );
}
//////////////////////////////////////////////////////////////////////////
// 發(fā)送和棋請求
//////////////////////////////////////////////////////////////////////////
void CTable::DrawGame()
{
CDialog *pDlg = (CDialog *)AfxGetMainWnd();
// 使按鈕失效
pDlg->GetDlgItem( IDC_BTN_BACK )->EnableWindow( FALSE );
pDlg->GetDlgItem( IDC_BTN_HQ )->EnableWindow( FALSE );
pDlg->GetDlgItem( IDC_BTN_LOST )->EnableWindow( FALSE );
// 設(shè)置等待標(biāo)志
SetWait( TRUE );
MSGSTRUCT msg;
msg.uMsg = MSG_DRAW;
m_conn.Send( (LPCVOID)&msg, sizeof( MSGSTRUCT ) );
}
//////////////////////////////////////////////////////////////////////////
// 設(shè)置游戲模式
//////////////////////////////////////////////////////////////////////////
void CTable::SetGameMode( int nGameMode )
{
if ( 1 == nGameMode )
m_pGame = new COneGame( this );
else
m_pGame = new CTwoGame( this );
m_pGame->Init();
}
//////////////////////////////////////////////////////////////////////////
// 悔棋
//////////////////////////////////////////////////////////////////////////
void CTable::Back()
{
m_pGame->Back();
}
//////////////////////////////////////////////////////////////////////////
// 處理對方落子后的工作
//////////////////////////////////////////////////////////////////////////
void CTable::Over()
{
// 判斷對方是否勝利
if ( Win( 1 - m_color ) )
{
CFiveApp *pApp = (CFiveApp *)AfxGetApp();
pApp->m_nLost++;
CDialog *pDlg = (CDialog *)GetParent();
PlaySound( MAKEINTRESOURCE( IDR_WAVE_LOST ), NULL, SND_RESOURCE | SND_SYNC );
pDlg->MessageBox( _T("您輸了,不過不要灰心,失敗乃成功之母哦!"), _T("失敗"), MB_ICONINFORMATION );
pDlg->GetDlgItem( IDC_BTN_HQ )->EnableWindow( FALSE );
pDlg->GetDlgItem( IDC_BTN_BACK )->EnableWindow( FALSE );
pDlg->GetDlgItem( IDC_BTN_LOST )->EnableWindow( FALSE );
// 如果是網(wǎng)絡(luò)對戰(zhàn),則生效“重玩”
if ( m_bConnected )
{
pDlg->GetMenu()->EnableMenuItem( ID_MENU_PLAYAGAIN, MF_ENABLED | MF_BYCOMMAND );
}
return;
}
m_bWait = FALSE;
}
//////////////////////////////////////////////////////////////////////////
// 設(shè)置菜單狀態(tài)(主要為網(wǎng)絡(luò)對戰(zhàn)準(zhǔn)備)
//////////////////////////////////////////////////////////////////////////
void CTable::SetMenuState( BOOL bEnable )
{
UINT uEnable, uDisable;
if ( bEnable )
{
uEnable = MF_ENABLED;
uDisable = MF_GRAYED | MF_DISABLED;
}
else
{
uEnable = MF_GRAYED | MF_DISABLED;
uDisable = MF_ENABLED;
}
CMenu *pMenu = GetParent()->GetMenu();
pMenu->GetSubMenu( 0 )->EnableMenuItem( 0, uEnable | MF_BYPOSITION );
pMenu->EnableMenuItem( ID_MENU_SERVER, uEnable );
pMenu->EnableMenuItem( ID_MENU_CLIENT, uEnable );
pMenu->EnableMenuItem( ID_MENU_LEAVE, uDisable );
pMenu->EnableMenuItem( ID_MENU_PLAYAGAIN, uEnable );
}
//////////////////////////////////////////////////////////////////////////
// 接受連接
//////////////////////////////////////////////////////////////////////////
void CTable::Accept( int nGameMode )
{
if ( 2 == nGameMode )
{
m_sock.Accept( m_conn );
}
m_bConnected = TRUE;
SetColor( 0 );
Clear( FALSE );
SetGameMode( nGameMode );
}
//////////////////////////////////////////////////////////////////////////
// 主動(dòng)連接
//////////////////////////////////////////////////////////////////////////
void CTable::Connect( int nGameMode )
{
SetColor( 1 );
Clear( TRUE );
SetGameMode( nGameMode );
}
//////////////////////////////////////////////////////////////////////////
// 發(fā)送聊天消息
//////////////////////////////////////////////////////////////////////////
void CTable::Chat( LPCTSTR lpszMsg )
{
MSGSTRUCT msg;
msg.uMsg = MSG_CHAT;
lstrcpy( msg.szMsg, lpszMsg );
m_conn.Send( (LPCVOID)&msg, sizeof( MSGSTRUCT ) );
}
//////////////////////////////////////////////////////////////////////////
// 發(fā)送認(rèn)輸消息
//////////////////////////////////////////////////////////////////////////
void CTable::GiveUp()
{
CFiveApp *pApp = (CFiveApp *)AfxGetApp();
pApp->m_nLost++;
CDialog *pDlg = (CDialog *)AfxGetMainWnd();
// 使按鈕失效
pDlg->GetDlgItem( IDC_BTN_BACK )->EnableWindow( FALSE );
pDlg->GetDlgItem( IDC_BTN_HQ )->EnableWindow( FALSE );
pDlg->GetDlgItem( IDC_BTN_LOST )->EnableWindow( FALSE );
// 修改等待狀態(tài)
SetWait( TRUE );
// 生效菜單項(xiàng)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -