Server_Game.cpp
上传用户:may_xy
上传日期:2007-08-09
资源大小:1519k
文件大小:14k
源码类别:

游戏

开发平台:

C/C++

  1. // Server_Game.cpp: implementation of the CServerGame class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "斗地主.h"
  6. #include "Game.h"
  7. #include "Link.h"
  8. #include "My_DirectInput.h"
  9. #include "my_directdraw.h"
  10. #include "Playing_Cards.h"
  11. #include "Draw_Item_Engine.h"
  12. #include "Draw_Cards_Engine.h"
  13. #include "Server_Game.h"
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[]=__FILE__;
  17. #define new DEBUG_NEW
  18. #endif
  19. //////////////////////////////////////////////////////////////////////
  20. // Construction/Destruction
  21. //////////////////////////////////////////////////////////////////////
  22. CServerGame::CServerGame()
  23. {
  24. }
  25. CServerGame::~CServerGame()
  26. {
  27. }
  28. //左边玩家思考,并决定要出的牌;
  29. int CServerGame::LeftDoing()
  30. {
  31. if( m_nLordLock == 0 )
  32. {
  33. if( m_nRoundCounter == 3 )  //说明已经出了牌;
  34. {
  35. //把已经出的牌回退;
  36. if(pLeftCards->m_nDiscardingCounter != 0)
  37. pDrawCards->LeftBacking();
  38. if(pCenterCards->m_nDiscardingCounter != 0)
  39. pDrawCards->CenterBacking();
  40. if(pRightCards->m_nDiscardingCounter != 0)
  41. pDrawCards->RightBacking();
  42. m_nRoundCounter = 0;   //新的一圈;
  43. }
  44. m_nRoundCounter++;
  45. return -1;
  46. }
  47. if( pRightCards->m_cDiscardingType.m_nTypeNum == 0 && 
  48. pCenterCards->m_cDiscardingType.m_nTypeNum == 0 )
  49. {
  50. m_nLordLock = -1;
  51. }
  52. //补丁,调整出牌显示;
  53. int Temp_Discarding_Counter = 0;  //刚出的牌的数量;
  54. CCard Temp_Discarding[20];//刚出的牌;
  55. pServerLink2->Recv(Temp_Discarding,sizeof(CCard)*20);
  56. pServerLink2->Recv(&Temp_Discarding_Counter,sizeof(int));
  57. pServerLink2->Recv(&pLeftCards->m_cDiscardingType,sizeof(CCardsType));
  58. if( m_nRoundCounter == 3 )  //说明已经出了牌;
  59. {
  60. //把已经出的牌回退;
  61. if(pLeftCards->m_nDiscardingCounter != 0)
  62. pDrawCards->LeftBacking();
  63. if(pCenterCards->m_nDiscardingCounter != 0)
  64. pDrawCards->CenterBacking();
  65. if(pRightCards->m_nDiscardingCounter != 0)
  66. pDrawCards->RightBacking();
  67. m_nRoundCounter = 0;   //新的一圈;
  68. }
  69. m_nRoundCounter++;
  70. //更新数据;
  71. for(int i=0;i<20;i++)
  72. {
  73. pLeftCards->m_cDiscarding[i] = Temp_Discarding[i];
  74. }
  75. pLeftCards->m_nDiscardingCounter = Temp_Discarding_Counter;
  76. pServerLink1->Send(pLeftCards->m_cDiscarding,sizeof(CCard)*20);
  77. pServerLink1->Send(&pLeftCards->m_nDiscardingCounter,sizeof(int));
  78. pServerLink1->Send(&pLeftCards->m_cDiscardingType,sizeof(CCardsType));
  79. if( pLeftCards->m_cDiscardingType.m_nTypeNum == 4 )
  80. {
  81. m_nBombCounter++;//加分;
  82. }
  83. if(pLeftCards->m_cDiscardingType.m_nTypeNum != 0)
  84. {
  85. pDrawCards->LeftDiscarding();
  86. ::RedrawGame();
  87. if( pLeftCards->m_nCardsCounter == 0 )
  88. {
  89. //打印结束信息;
  90. AccountScore(0);
  91. ::SetTimer(hWnd,0,0,NULL);   //开始游戏;
  92. return 0;
  93. }
  94. }
  95. else
  96. {
  97. //显示不要;
  98. pDraw->TextGDI("过牌!-",250,200,RGB(255,0,0),400,"华文新魏",lpddsprimary);
  99. if( m_nCurrentLord == 0 )
  100. {
  101. m_nLordLock = 0;
  102. }
  103. Sleep(300);
  104. }
  105. return 1;
  106. }
  107. //中间(本机)玩家出牌;
  108. int CServerGame::CenterDoing()
  109. {
  110. if( m_nLordLock == 1 )
  111. {
  112. if( m_nRoundCounter == 3 )  //说明已经出了牌;
  113. {
  114. //把已经出的牌回退;
  115. if(pCenterCards->m_nDiscardingCounter != 0)
  116. pDrawCards->CenterBacking();
  117. if(pRightCards->m_nDiscardingCounter != 0)
  118. pDrawCards->RightBacking();
  119. if(pLeftCards->m_nDiscardingCounter != 0)
  120. pDrawCards->LeftBacking();
  121. m_nRoundCounter = 0;   //新的一圈;
  122. }
  123. m_nRoundCounter++;
  124. return -1;  //表示当前是地主,上次轮过牌,现在则不能出牌;
  125. }
  126. //如果没有选择牌;
  127. if( pCardsMap->m_nUpCounter == 0 )
  128. {
  129. //当不是第一个出牌的时候才可以过牌;
  130. if( pLeftCards->m_cDiscardingType.m_nTypeNum != 0 || 
  131. pRightCards->m_cDiscardingType.m_nTypeNum != 0 )
  132. {
  133. if( m_nRoundCounter == 3 )  //说明已经出了牌;
  134. {
  135. //把已经出的牌回退;
  136. if(pCenterCards->m_nDiscardingCounter != 0)
  137. pDrawCards->CenterBacking();
  138. if(pRightCards->m_nDiscardingCounter != 0)
  139. pDrawCards->RightBacking();
  140. if(pLeftCards->m_nDiscardingCounter != 0)
  141. pDrawCards->LeftBacking();
  142. m_nRoundCounter = 0;   //新的一圈;
  143. }
  144. m_nRoundCounter++;
  145. pCenterCards->m_cDiscardingType.m_nTypeNum = 0; //将出的牌值设为空;
  146. pServerLink1->Send(pCenterCards->m_cDiscarding,sizeof(CCard)*20);
  147. pServerLink1->Send(&pCenterCards->m_nDiscardingCounter,sizeof(int));
  148. pServerLink1->Send(&pCenterCards->m_cDiscardingType,sizeof(CCardsType));
  149. pServerLink2->Send(pCenterCards->m_cDiscarding,sizeof(CCard)*20);
  150. pServerLink2->Send(&pCenterCards->m_nDiscardingCounter,sizeof(int));
  151. pServerLink2->Send(&pCenterCards->m_cDiscardingType,sizeof(CCardsType));
  152. if( m_nCurrentLord == 1 )
  153. {
  154. m_nLordLock = 1;
  155. }
  156. pDraw->TextGDI("过牌!",470,400,RGB(255,0,0),400,"华文新魏",lpddsprimary);
  157. Sleep(500);
  158. return 1;
  159. }
  160. else
  161. {
  162. return 0;
  163. }
  164. }
  165. pCenterCards->m_nChoosingCardsCounter = 0;
  166. //将要出的牌加入到m_cDiscarding[20]数组中,m_nDiscardingCounter计数增加;
  167. for(int i=0;i<pCenterCards->m_nCardsCounter;i++)
  168. {
  169. if(pCardsMap->Center0[i].m_nY== Center_y0 - Card_Up)
  170. {
  171. pCenterCards->m_cChoosingCards[pCenterCards->m_nChoosingCardsCounter] = pCenterCards->m_cCards[i];
  172. pCenterCards->m_nChoosingCardsCounter++;
  173. }
  174. }
  175. //判断出牌的合法性,
  176. if( pCenterCards->CheckChoosing() )
  177. {
  178. //大小判断;
  179. if( CompareCards() == 0 )
  180. {
  181. for(int j=0;j<pCenterCards->m_nCardsCounter;j++)
  182. {
  183. if(pCardsMap->Center0[j].m_nY== Center_y0 - Card_Up)
  184. {
  185. pCardsMap->Center0[j].m_nY+= Card_Up;
  186. }
  187. }
  188. pCardsMap->m_nUpCounter = 0;  //提起牌数为0;
  189. ::RedrawGame();
  190. m_bButton = 0;   //延时锁;
  191. Sleep(400);
  192. m_bButton = 1;   //延时锁;
  193. return 0;
  194. }
  195. if( m_nRoundCounter == 3 )  //说明已经出了牌;
  196. {
  197. //把已经出的牌回退;
  198. if(pCenterCards->m_nDiscardingCounter != 0)
  199. pDrawCards->CenterBacking();
  200. if(pRightCards->m_nDiscardingCounter != 0)
  201. pDrawCards->RightBacking();
  202. if(pLeftCards->m_nDiscardingCounter != 0)
  203. pDrawCards->LeftBacking();
  204. m_nRoundCounter = 0;   //新的一圈;
  205. }
  206. m_nRoundCounter++;
  207. //将要出的牌加入到m_cDiscarding[20]数组中,m_nDiscardingCounter计数增加;
  208. for(int i=0;i<pCenterCards->m_nCardsCounter;i++)
  209. {
  210. if(pCardsMap->Center0[i].m_nY== Center_y0 - Card_Up)
  211. {
  212. pCenterCards->m_cDiscarding[pCenterCards->m_nDiscardingCounter] = pCenterCards->m_cCards[i];
  213. pCenterCards->m_nDiscardingCounter++;
  214. }
  215. }
  216. if( pCenterCards->m_cDiscardingType.m_nTypeNum == 4 )
  217. {
  218. m_nBombCounter++;//加分;
  219. }
  220. //发送给其他两个玩家;
  221. pServerLink1->Send(pCenterCards->m_cDiscarding,sizeof(CCard)*20);
  222. pServerLink1->Send(&pCenterCards->m_nDiscardingCounter,sizeof(int));
  223. pServerLink1->Send(&pCenterCards->m_cDiscardingType,sizeof(CCardsType));
  224. pServerLink2->Send(pCenterCards->m_cDiscarding,sizeof(CCard)*20);
  225. pServerLink2->Send(&pCenterCards->m_nDiscardingCounter,sizeof(int));
  226. pServerLink2->Send(&pCenterCards->m_cDiscardingType,sizeof(CCardsType));
  227. pDrawCards->CenterDiscarding();  //本机玩家出牌;
  228. ::RedrawGame();
  229. if( pCenterCards->m_nCardsCounter == 0 )
  230. {
  231. //打印结束信息;
  232. PlaySound(MAKEINTRESOURCE(IDR_WIN),AfxGetResourceHandle(),
  233. SND_ASYNC|SND_RESOURCE|SND_NODEFAULT );  //
  234. AccountScore(1);
  235. ::SetTimer(hWnd,0,0,NULL);   //开始游戏;
  236. return 2;
  237. }
  238. }
  239. else
  240. {
  241. for(int j=0;j<pCenterCards->m_nCardsCounter;j++)
  242. {
  243. if(pCardsMap->Center0[j].m_nY== Center_y0 - Card_Up)
  244. {
  245. pCardsMap->Center0[j].m_nY+= Card_Up;
  246. }
  247. }
  248. pCardsMap->m_nUpCounter = 0;  //提起牌数为0;
  249. ::RedrawGame();
  250. m_bButton = 0;   //延时锁;
  251. Sleep(400);
  252. m_bButton = 1;   //延时锁;
  253. return 0;
  254. }
  255. return 1;
  256. }
  257. //右边玩家思考,并决定要出的牌;
  258. int CServerGame::RightDoing()
  259. {
  260. if( m_nLordLock == 2 )
  261. {
  262. if( m_nRoundCounter == 3 )  //说明已经出了牌;
  263. {
  264. //把已经出的牌回退;
  265. if(pLeftCards->m_nDiscardingCounter != 0)
  266. pDrawCards->LeftBacking();
  267. if(pCenterCards->m_nDiscardingCounter != 0)
  268. pDrawCards->CenterBacking();
  269. if(pRightCards->m_nDiscardingCounter != 0)
  270. pDrawCards->RightBacking();
  271. m_nRoundCounter = 0;   //新的一圈;
  272. }
  273. m_nRoundCounter++;
  274. return -1;
  275. }
  276. if( pLeftCards->m_cDiscardingType.m_nTypeNum == 0 && 
  277. pCenterCards->m_cDiscardingType.m_nTypeNum == 0 )
  278. {
  279. m_nLordLock = -1;
  280. }
  281. //补丁,调整出牌显示;
  282. int Temp_Discarding_Counter = 0;  //刚出的牌的数量;
  283. CCard Temp_Discarding[20];   //刚出的牌;
  284. pServerLink1->Recv(Temp_Discarding,sizeof(CCard)*20);
  285. pServerLink1->Recv(&Temp_Discarding_Counter,sizeof(int));
  286. pServerLink1->Recv(&pRightCards->m_cDiscardingType,sizeof(CCardsType));
  287. if( m_nRoundCounter == 3 )  //说明已经出了牌;
  288. {
  289. //把已经出的牌回退;
  290. if(pRightCards->m_nDiscardingCounter != 0)
  291. pDrawCards->RightBacking();
  292. if(pCenterCards->m_nDiscardingCounter != 0)
  293. pDrawCards->CenterBacking();
  294. if(pLeftCards->m_nDiscardingCounter != 0)
  295. pDrawCards->LeftBacking();
  296. m_nRoundCounter = 0;   //新的一圈;
  297. }
  298. m_nRoundCounter++;
  299. //更新数据;
  300. for(int i=0;i<20;i++)
  301. {
  302. pRightCards->m_cDiscarding[i] = Temp_Discarding[i];
  303. }
  304. pRightCards->m_nDiscardingCounter = Temp_Discarding_Counter;
  305. pServerLink2->Send(pRightCards->m_cDiscarding,sizeof(CCard)*20);
  306. pServerLink2->Send(&pRightCards->m_nDiscardingCounter,sizeof(int));
  307. pServerLink2->Send(&pRightCards->m_cDiscardingType,sizeof(CCardsType));
  308. if( pRightCards->m_cDiscardingType.m_nTypeNum == 4 )
  309. {
  310. m_nBombCounter++;//加分;
  311. }
  312. if(pRightCards->m_cDiscardingType.m_nTypeNum != 0)
  313. {
  314. pDrawCards->RightDiscarding();
  315. ::RedrawGame();
  316. if( pRightCards->m_nCardsCounter == 0 )
  317. {
  318. //打印结束信息;
  319. AccountScore(2);
  320. ::SetTimer(hWnd,0,0,NULL);   //开始游戏;
  321. return 0;
  322. }
  323. }
  324. else
  325. {
  326. if( m_nCurrentLord == 2 )
  327. {
  328. m_nLordLock = 2;
  329. }
  330. pRightCards->m_cDiscardingType.m_nTypeNum = 0;
  331. pDraw->TextGDI("过牌!-",600,200,RGB(255,0,0),400,"华文新魏",lpddsprimary);
  332. Sleep(300);
  333. }
  334. return 1;
  335. }
  336. //传送牌数据结构给两个客户;
  337. int CServerGame::SentCards()
  338. {
  339. CCard All_Send_Cards[54];
  340. All_Send_Cards[51] = g_cAllCards[51];
  341. All_Send_Cards[52] = g_cAllCards[52];
  342. All_Send_Cards[53] = g_cAllCards[53];
  343. int i;
  344. for(i=0;i<17;i++)
  345. {
  346. All_Send_Cards[i*3]   = g_cAllCards[i*3+1];
  347. All_Send_Cards[i*3+1] = g_cAllCards[i*3+2];
  348. All_Send_Cards[i*3+2] = g_cAllCards[i*3];
  349. }
  350. pServerLink1->Send(All_Send_Cards,sizeof(CCard)*54);
  351. for(i=0;i<17;i++)
  352. {
  353. All_Send_Cards[i*3]   = g_cAllCards[i*3+2];
  354. All_Send_Cards[i*3+1] = g_cAllCards[i*3];
  355. All_Send_Cards[i*3+2] = g_cAllCards[i*3+1];
  356. }
  357. pServerLink2->Send(All_Send_Cards,sizeof(CCard)*54);
  358. return 1;
  359. }
  360. //服务器决定地主;
  361. int CServerGame::DecideLord()    
  362. {
  363. m_nDefaultLord = rand()%3;
  364. m_nCurrentLord = m_nDefaultLord;
  365. int i;
  366. switch( m_nCurrentLord )
  367. {
  368. case 0:
  369. i = 2;
  370. pServerLink1->Send(&i,sizeof(int));
  371. i = 1;
  372. pServerLink2->Send(&i,sizeof(int));
  373. break;
  374. case 1:
  375. i = 0;
  376. pServerLink1->Send(&i,sizeof(int));
  377. i = 2;
  378. pServerLink2->Send(&i,sizeof(int));
  379. break;
  380. case 2:
  381. i = 1;
  382. pServerLink1->Send(&i,sizeof(int));
  383. i = 0;
  384. pServerLink2->Send(&i,sizeof(int));
  385. break;
  386. }
  387. pDrawItem->BringRandLord(m_nDefaultLord);
  388. int counter = 0;
  389. while(1)
  390. {
  391. if( m_nCurrentLord == 1 )
  392. {
  393. pDrawItem->ChooseLord();
  394. while(1)
  395. {
  396. if( pInput->IsLButtonDown(m_cYES_RECT) )  //要
  397. {
  398. PlaySound(MAKEINTRESOURCE(IDR_CHOSE),AfxGetResourceHandle(),
  399. SND_ASYNC|SND_RESOURCE|SND_NODEFAULT );  //
  400. pDrawItem->GameInfo(m_nCurrentLord,0);
  401. i = 0;
  402. pServerLink1->Send(&i,sizeof(int));
  403. i = 2;
  404. pServerLink2->Send(&i,sizeof(int));
  405. //跳出,准备开始游戏;goto...
  406. goto out;
  407. }
  408. else if( pInput->IsLButtonDown(m_cNO_RECT) )  //不要
  409. {
  410. PlaySound(MAKEINTRESOURCE(IDR_CHOSE),AfxGetResourceHandle(),
  411. SND_ASYNC|SND_RESOURCE|SND_NODEFAULT );  //
  412. pDrawItem->GameInfo(m_nCurrentLord,1);
  413. i = -1;
  414. pServerLink1->Send(&i,sizeof(int));
  415. pServerLink2->Send(&i,sizeof(int));
  416. i = 1;
  417. pServerLink1->Send(&i,sizeof(int));
  418. i = 0;
  419. pServerLink2->Send(&i,sizeof(int));
  420. m_nCurrentLord = 2;   //自己更新
  421. break;
  422. }
  423. Sleep(50);   //产生一个延时;
  424. }//end while;
  425. }
  426. else if( m_nCurrentLord != 1 )
  427. {
  428. if( m_nCurrentLord == 2 )
  429. {
  430. pServerLink1->Recv(&i,sizeof(int));
  431. if( i == 1 )
  432. {
  433. pDrawItem->GameInfo(m_nCurrentLord,0);
  434. i = 0;
  435. pServerLink2->Send(&i,sizeof(int));
  436. //跳出,准备开始游戏;
  437. break;
  438. }
  439. else if( i == -1 )
  440. {
  441. pDrawItem->GameInfo(m_nCurrentLord,1);
  442. i = -1;
  443. pServerLink2->Send(&i,sizeof(int));
  444. m_nCurrentLord = 0;  //自己更新;
  445. //更新当前地主;
  446. i = 2;
  447. pServerLink1->Send(&i,sizeof(int));
  448. i = 1;
  449. pServerLink2->Send(&i,sizeof(int));
  450. }
  451. }
  452. else if( m_nCurrentLord == 0 )
  453. {
  454. pServerLink2->Recv(&i,sizeof(int));
  455. if( i == 1 )
  456. {
  457. pDrawItem->GameInfo(m_nCurrentLord,0);
  458. i = 2;
  459. pServerLink1->Send(&i,sizeof(int));
  460. //跳出,准备开始游戏;
  461. break;
  462. }
  463. else if( i == -1 )
  464. {
  465. pDrawItem->GameInfo(m_nCurrentLord,1);
  466. i = -1;
  467. pServerLink1->Send(&i,sizeof(int));
  468. m_nCurrentLord = 1;  //自己更新;
  469. //更新当前地主;
  470. i = 0;
  471. pServerLink1->Send(&i,sizeof(int));
  472. i = 2;
  473. pServerLink2->Send(&i,sizeof(int));
  474. }
  475. }
  476. }
  477. counter++;
  478. if( counter == 3 )
  479. {
  480. pDrawItem->GameInfo(m_nDefaultLord,2);
  481. m_nCurrentLord = m_nDefaultLord;
  482. //跳出,准备开始游戏;
  483. break;
  484. }
  485. }
  486. out:
  487. // Sleep(10000);
  488. return 1;
  489. }
  490. //开始游戏
  491. int CServerGame::Run()
  492. {
  493. //开始新的牌局;
  494. NewGame();    
  495. //显示局数;
  496. pDrawItem->GameCounter(m_nGameCounter);  
  497. //洗牌;
  498. ShuffleCards();
  499. //传送牌数据结构给两个客户;
  500. SentCards();
  501. pDrawCards->Dealing();   //发牌;
  502. DecideLord();    //决定地主;
  503. pDrawCards->DealingLord(m_nCurrentLord); //发地主牌;
  504. m_bOperate  = TRUE;
  505. //开始循环;
  506. if(m_nCurrentLord == 0)
  507. {
  508. LeftDoing();
  509. ::SetTimer(hWnd,20,10,NULL);
  510. }
  511. else if(m_nCurrentLord == 1)
  512. {
  513. ::SetTimer(hWnd,20,10,NULL);
  514. }
  515. else if(m_nCurrentLord == 2)
  516. {
  517. RightDoing();
  518. LeftDoing();
  519. ::SetTimer(hWnd,20,10,NULL);
  520. }
  521. return 1;
  522. }