?? server_game.cpp
字號(hào):
// Server_Game.cpp: implementation of the CServerGame class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "斗地主.h"
#include "Game.h"
#include "Link.h"
#include "My_DirectInput.h"
#include "my_directdraw.h"
#include "Playing_Cards.h"
#include "Draw_Item_Engine.h"
#include "Draw_Cards_Engine.h"
#include "Server_Game.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CServerGame::CServerGame()
{
}
CServerGame::~CServerGame()
{
}
//左邊玩家思考,并決定要出的牌;
int CServerGame::LeftDoing()
{
if( m_nLordLock == 0 )
{
if( m_nRoundCounter == 3 ) //說明已經(jīng)出了牌;
{
//把已經(jīng)出的牌回退;
if(pLeftCards->m_nDiscardingCounter != 0)
pDrawCards->LeftBacking();
if(pCenterCards->m_nDiscardingCounter != 0)
pDrawCards->CenterBacking();
if(pRightCards->m_nDiscardingCounter != 0)
pDrawCards->RightBacking();
m_nRoundCounter = 0; //新的一圈;
}
m_nRoundCounter++;
return -1;
}
if( pRightCards->m_cDiscardingType.m_nTypeNum == 0 &&
pCenterCards->m_cDiscardingType.m_nTypeNum == 0 )
{
m_nLordLock = -1;
}
//補(bǔ)丁,調(diào)整出牌顯示;
int Temp_Discarding_Counter = 0; //剛出的牌的數(shù)量;
CCard Temp_Discarding[20];//剛出的牌;
pServerLink2->Recv(Temp_Discarding,sizeof(CCard)*20);
pServerLink2->Recv(&Temp_Discarding_Counter,sizeof(int));
pServerLink2->Recv(&pLeftCards->m_cDiscardingType,sizeof(CCardsType));
if( m_nRoundCounter == 3 ) //說明已經(jīng)出了牌;
{
//把已經(jīng)出的牌回退;
if(pLeftCards->m_nDiscardingCounter != 0)
pDrawCards->LeftBacking();
if(pCenterCards->m_nDiscardingCounter != 0)
pDrawCards->CenterBacking();
if(pRightCards->m_nDiscardingCounter != 0)
pDrawCards->RightBacking();
m_nRoundCounter = 0; //新的一圈;
}
m_nRoundCounter++;
//更新數(shù)據(jù);
for(int i=0;i<20;i++)
{
pLeftCards->m_cDiscarding[i] = Temp_Discarding[i];
}
pLeftCards->m_nDiscardingCounter = Temp_Discarding_Counter;
pServerLink1->Send(pLeftCards->m_cDiscarding,sizeof(CCard)*20);
pServerLink1->Send(&pLeftCards->m_nDiscardingCounter,sizeof(int));
pServerLink1->Send(&pLeftCards->m_cDiscardingType,sizeof(CCardsType));
if( pLeftCards->m_cDiscardingType.m_nTypeNum == 4 )
{
m_nBombCounter++;//加分;
}
if(pLeftCards->m_cDiscardingType.m_nTypeNum != 0)
{
pDrawCards->LeftDiscarding();
::RedrawGame();
if( pLeftCards->m_nCardsCounter == 0 )
{
//打印結(jié)束信息;
AccountScore(0);
::SetTimer(hWnd,0,0,NULL); //開始游戲;
return 0;
}
}
else
{
//顯示不要;
pDraw->TextGDI("過牌!-",250,200,RGB(255,0,0),400,"華文新魏",lpddsprimary);
if( m_nCurrentLord == 0 )
{
m_nLordLock = 0;
}
Sleep(300);
}
return 1;
}
//中間(本機(jī))玩家出牌;
int CServerGame::CenterDoing()
{
if( m_nLordLock == 1 )
{
if( m_nRoundCounter == 3 ) //說明已經(jīng)出了牌;
{
//把已經(jīng)出的牌回退;
if(pCenterCards->m_nDiscardingCounter != 0)
pDrawCards->CenterBacking();
if(pRightCards->m_nDiscardingCounter != 0)
pDrawCards->RightBacking();
if(pLeftCards->m_nDiscardingCounter != 0)
pDrawCards->LeftBacking();
m_nRoundCounter = 0; //新的一圈;
}
m_nRoundCounter++;
return -1; //表示當(dāng)前是地主,上次輪過牌,現(xiàn)在則不能出牌;
}
//如果沒有選擇牌;
if( pCardsMap->m_nUpCounter == 0 )
{
//當(dāng)不是第一個(gè)出牌的時(shí)候才可以過牌;
if( pLeftCards->m_cDiscardingType.m_nTypeNum != 0 ||
pRightCards->m_cDiscardingType.m_nTypeNum != 0 )
{
if( m_nRoundCounter == 3 ) //說明已經(jīng)出了牌;
{
//把已經(jīng)出的牌回退;
if(pCenterCards->m_nDiscardingCounter != 0)
pDrawCards->CenterBacking();
if(pRightCards->m_nDiscardingCounter != 0)
pDrawCards->RightBacking();
if(pLeftCards->m_nDiscardingCounter != 0)
pDrawCards->LeftBacking();
m_nRoundCounter = 0; //新的一圈;
}
m_nRoundCounter++;
pCenterCards->m_cDiscardingType.m_nTypeNum = 0; //將出的牌值設(shè)為空;
pServerLink1->Send(pCenterCards->m_cDiscarding,sizeof(CCard)*20);
pServerLink1->Send(&pCenterCards->m_nDiscardingCounter,sizeof(int));
pServerLink1->Send(&pCenterCards->m_cDiscardingType,sizeof(CCardsType));
pServerLink2->Send(pCenterCards->m_cDiscarding,sizeof(CCard)*20);
pServerLink2->Send(&pCenterCards->m_nDiscardingCounter,sizeof(int));
pServerLink2->Send(&pCenterCards->m_cDiscardingType,sizeof(CCardsType));
if( m_nCurrentLord == 1 )
{
m_nLordLock = 1;
}
pDraw->TextGDI("過牌!",470,400,RGB(255,0,0),400,"華文新魏",lpddsprimary);
Sleep(500);
return 1;
}
else
{
return 0;
}
}
pCenterCards->m_nChoosingCardsCounter = 0;
//將要出的牌加入到m_cDiscarding[20]數(shù)組中,m_nDiscardingCounter計(jì)數(shù)增加;
for(int i=0;i<pCenterCards->m_nCardsCounter;i++)
{
if(pCardsMap->Center0[i].m_nY== Center_y0 - Card_Up)
{
pCenterCards->m_cChoosingCards[pCenterCards->m_nChoosingCardsCounter] = pCenterCards->m_cCards[i];
pCenterCards->m_nChoosingCardsCounter++;
}
}
//判斷出牌的合法性,
if( pCenterCards->CheckChoosing() )
{
//大小判斷;
if( CompareCards() == 0 )
{
for(int j=0;j<pCenterCards->m_nCardsCounter;j++)
{
if(pCardsMap->Center0[j].m_nY== Center_y0 - Card_Up)
{
pCardsMap->Center0[j].m_nY+= Card_Up;
}
}
pCardsMap->m_nUpCounter = 0; //提起牌數(shù)為0;
::RedrawGame();
m_bButton = 0; //延時(shí)鎖;
Sleep(400);
m_bButton = 1; //延時(shí)鎖;
return 0;
}
if( m_nRoundCounter == 3 ) //說明已經(jīng)出了牌;
{
//把已經(jīng)出的牌回退;
if(pCenterCards->m_nDiscardingCounter != 0)
pDrawCards->CenterBacking();
if(pRightCards->m_nDiscardingCounter != 0)
pDrawCards->RightBacking();
if(pLeftCards->m_nDiscardingCounter != 0)
pDrawCards->LeftBacking();
m_nRoundCounter = 0; //新的一圈;
}
m_nRoundCounter++;
//將要出的牌加入到m_cDiscarding[20]數(shù)組中,m_nDiscardingCounter計(jì)數(shù)增加;
for(int i=0;i<pCenterCards->m_nCardsCounter;i++)
{
if(pCardsMap->Center0[i].m_nY== Center_y0 - Card_Up)
{
pCenterCards->m_cDiscarding[pCenterCards->m_nDiscardingCounter] = pCenterCards->m_cCards[i];
pCenterCards->m_nDiscardingCounter++;
}
}
if( pCenterCards->m_cDiscardingType.m_nTypeNum == 4 )
{
m_nBombCounter++;//加分;
}
//發(fā)送給其他兩個(gè)玩家;
pServerLink1->Send(pCenterCards->m_cDiscarding,sizeof(CCard)*20);
pServerLink1->Send(&pCenterCards->m_nDiscardingCounter,sizeof(int));
pServerLink1->Send(&pCenterCards->m_cDiscardingType,sizeof(CCardsType));
pServerLink2->Send(pCenterCards->m_cDiscarding,sizeof(CCard)*20);
pServerLink2->Send(&pCenterCards->m_nDiscardingCounter,sizeof(int));
pServerLink2->Send(&pCenterCards->m_cDiscardingType,sizeof(CCardsType));
pDrawCards->CenterDiscarding(); //本機(jī)玩家出牌;
::RedrawGame();
if( pCenterCards->m_nCardsCounter == 0 )
{
//打印結(jié)束信息;
PlaySound(MAKEINTRESOURCE(IDR_WIN),AfxGetResourceHandle(),
SND_ASYNC|SND_RESOURCE|SND_NODEFAULT ); //
AccountScore(1);
::SetTimer(hWnd,0,0,NULL); //開始游戲;
return 2;
}
}
else
{
for(int j=0;j<pCenterCards->m_nCardsCounter;j++)
{
if(pCardsMap->Center0[j].m_nY== Center_y0 - Card_Up)
{
pCardsMap->Center0[j].m_nY+= Card_Up;
}
}
pCardsMap->m_nUpCounter = 0; //提起牌數(shù)為0;
::RedrawGame();
m_bButton = 0; //延時(shí)鎖;
Sleep(400);
m_bButton = 1; //延時(shí)鎖;
return 0;
}
return 1;
}
//右邊玩家思考,并決定要出的牌;
int CServerGame::RightDoing()
{
if( m_nLordLock == 2 )
{
if( m_nRoundCounter == 3 ) //說明已經(jīng)出了牌;
{
//把已經(jīng)出的牌回退;
if(pLeftCards->m_nDiscardingCounter != 0)
pDrawCards->LeftBacking();
if(pCenterCards->m_nDiscardingCounter != 0)
pDrawCards->CenterBacking();
if(pRightCards->m_nDiscardingCounter != 0)
pDrawCards->RightBacking();
m_nRoundCounter = 0; //新的一圈;
}
m_nRoundCounter++;
return -1;
}
if( pLeftCards->m_cDiscardingType.m_nTypeNum == 0 &&
pCenterCards->m_cDiscardingType.m_nTypeNum == 0 )
{
m_nLordLock = -1;
}
//補(bǔ)丁,調(diào)整出牌顯示;
int Temp_Discarding_Counter = 0; //剛出的牌的數(shù)量;
CCard Temp_Discarding[20]; //剛出的牌;
pServerLink1->Recv(Temp_Discarding,sizeof(CCard)*20);
pServerLink1->Recv(&Temp_Discarding_Counter,sizeof(int));
pServerLink1->Recv(&pRightCards->m_cDiscardingType,sizeof(CCardsType));
if( m_nRoundCounter == 3 ) //說明已經(jīng)出了牌;
{
//把已經(jīng)出的牌回退;
if(pRightCards->m_nDiscardingCounter != 0)
pDrawCards->RightBacking();
if(pCenterCards->m_nDiscardingCounter != 0)
pDrawCards->CenterBacking();
if(pLeftCards->m_nDiscardingCounter != 0)
pDrawCards->LeftBacking();
m_nRoundCounter = 0; //新的一圈;
}
m_nRoundCounter++;
//更新數(shù)據(jù);
for(int i=0;i<20;i++)
{
pRightCards->m_cDiscarding[i] = Temp_Discarding[i];
}
pRightCards->m_nDiscardingCounter = Temp_Discarding_Counter;
pServerLink2->Send(pRightCards->m_cDiscarding,sizeof(CCard)*20);
pServerLink2->Send(&pRightCards->m_nDiscardingCounter,sizeof(int));
pServerLink2->Send(&pRightCards->m_cDiscardingType,sizeof(CCardsType));
if( pRightCards->m_cDiscardingType.m_nTypeNum == 4 )
{
m_nBombCounter++;//加分;
}
if(pRightCards->m_cDiscardingType.m_nTypeNum != 0)
{
pDrawCards->RightDiscarding();
::RedrawGame();
if( pRightCards->m_nCardsCounter == 0 )
{
//打印結(jié)束信息;
AccountScore(2);
::SetTimer(hWnd,0,0,NULL); //開始游戲;
return 0;
}
}
else
{
if( m_nCurrentLord == 2 )
{
m_nLordLock = 2;
}
pRightCards->m_cDiscardingType.m_nTypeNum = 0;
pDraw->TextGDI("過牌!-",600,200,RGB(255,0,0),400,"華文新魏",lpddsprimary);
Sleep(300);
}
return 1;
}
//傳送牌數(shù)據(jù)結(jié)構(gòu)給兩個(gè)客戶;
int CServerGame::SentCards()
{
CCard All_Send_Cards[54];
All_Send_Cards[51] = g_cAllCards[51];
All_Send_Cards[52] = g_cAllCards[52];
All_Send_Cards[53] = g_cAllCards[53];
int i;
for(i=0;i<17;i++)
{
All_Send_Cards[i*3] = g_cAllCards[i*3+1];
All_Send_Cards[i*3+1] = g_cAllCards[i*3+2];
All_Send_Cards[i*3+2] = g_cAllCards[i*3];
}
pServerLink1->Send(All_Send_Cards,sizeof(CCard)*54);
for(i=0;i<17;i++)
{
All_Send_Cards[i*3] = g_cAllCards[i*3+2];
All_Send_Cards[i*3+1] = g_cAllCards[i*3];
All_Send_Cards[i*3+2] = g_cAllCards[i*3+1];
}
pServerLink2->Send(All_Send_Cards,sizeof(CCard)*54);
return 1;
}
//服務(wù)器決定地主;
int CServerGame::DecideLord()
{
m_nDefaultLord = rand()%3;
m_nCurrentLord = m_nDefaultLord;
int i;
switch( m_nCurrentLord )
{
case 0:
i = 2;
pServerLink1->Send(&i,sizeof(int));
i = 1;
pServerLink2->Send(&i,sizeof(int));
break;
case 1:
i = 0;
pServerLink1->Send(&i,sizeof(int));
i = 2;
pServerLink2->Send(&i,sizeof(int));
break;
case 2:
i = 1;
pServerLink1->Send(&i,sizeof(int));
i = 0;
pServerLink2->Send(&i,sizeof(int));
break;
}
pDrawItem->BringRandLord(m_nDefaultLord);
int counter = 0;
while(1)
{
if( m_nCurrentLord == 1 )
{
pDrawItem->ChooseLord();
while(1)
{
if( pInput->IsLButtonDown(m_cYES_RECT) ) //要
{
PlaySound(MAKEINTRESOURCE(IDR_CHOSE),AfxGetResourceHandle(),
SND_ASYNC|SND_RESOURCE|SND_NODEFAULT ); //
pDrawItem->GameInfo(m_nCurrentLord,0);
i = 0;
pServerLink1->Send(&i,sizeof(int));
i = 2;
pServerLink2->Send(&i,sizeof(int));
//跳出,準(zhǔn)備開始游戲;goto...
goto out;
}
else if( pInput->IsLButtonDown(m_cNO_RECT) ) //不要
{
PlaySound(MAKEINTRESOURCE(IDR_CHOSE),AfxGetResourceHandle(),
SND_ASYNC|SND_RESOURCE|SND_NODEFAULT ); //
pDrawItem->GameInfo(m_nCurrentLord,1);
i = -1;
pServerLink1->Send(&i,sizeof(int));
pServerLink2->Send(&i,sizeof(int));
i = 1;
pServerLink1->Send(&i,sizeof(int));
i = 0;
pServerLink2->Send(&i,sizeof(int));
m_nCurrentLord = 2; //自己更新
break;
}
Sleep(50); //產(chǎn)生一個(gè)延時(shí);
}//end while;
}
else if( m_nCurrentLord != 1 )
{
if( m_nCurrentLord == 2 )
{
pServerLink1->Recv(&i,sizeof(int));
if( i == 1 )
{
pDrawItem->GameInfo(m_nCurrentLord,0);
i = 0;
pServerLink2->Send(&i,sizeof(int));
//跳出,準(zhǔn)備開始游戲;
break;
}
else if( i == -1 )
{
pDrawItem->GameInfo(m_nCurrentLord,1);
i = -1;
pServerLink2->Send(&i,sizeof(int));
m_nCurrentLord = 0; //自己更新;
//更新當(dāng)前地主;
i = 2;
pServerLink1->Send(&i,sizeof(int));
i = 1;
pServerLink2->Send(&i,sizeof(int));
}
}
else if( m_nCurrentLord == 0 )
{
pServerLink2->Recv(&i,sizeof(int));
if( i == 1 )
{
pDrawItem->GameInfo(m_nCurrentLord,0);
i = 2;
pServerLink1->Send(&i,sizeof(int));
//跳出,準(zhǔn)備開始游戲;
break;
}
else if( i == -1 )
{
pDrawItem->GameInfo(m_nCurrentLord,1);
i = -1;
pServerLink1->Send(&i,sizeof(int));
m_nCurrentLord = 1; //自己更新;
//更新當(dāng)前地主;
i = 0;
pServerLink1->Send(&i,sizeof(int));
i = 2;
pServerLink2->Send(&i,sizeof(int));
}
}
}
counter++;
if( counter == 3 )
{
pDrawItem->GameInfo(m_nDefaultLord,2);
m_nCurrentLord = m_nDefaultLord;
//跳出,準(zhǔn)備開始游戲;
break;
}
}
out:
// Sleep(10000);
return 1;
}
//開始游戲
int CServerGame::Run()
{
//開始新的牌局;
NewGame();
//顯示局?jǐn)?shù);
pDrawItem->GameCounter(m_nGameCounter);
//洗牌;
ShuffleCards();
//傳送牌數(shù)據(jù)結(jié)構(gòu)給兩個(gè)客戶;
SentCards();
pDrawCards->Dealing(); //發(fā)牌;
DecideLord(); //決定地主;
pDrawCards->DealingLord(m_nCurrentLord); //發(fā)地主牌;
m_bOperate = TRUE;
//開始循環(huán);
if(m_nCurrentLord == 0)
{
LeftDoing();
::SetTimer(hWnd,20,10,NULL);
}
else if(m_nCurrentLord == 1)
{
::SetTimer(hWnd,20,10,NULL);
}
else if(m_nCurrentLord == 2)
{
RightDoing();
LeftDoing();
::SetTimer(hWnd,20,10,NULL);
}
return 1;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -