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

游戏

开发平台:

C/C++

  1. // Client_Game.cpp: implementation of the CClientGame 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 "Client_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. CClientGame::CClientGame()
  23. {
  24. }
  25. CClientGame::~CClientGame()
  26. {
  27. }
  28. //左边玩家(电脑)思考,并决定要出的牌;
  29. int CClientGame::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. pClientLink->Recv(Temp_Discarding,sizeof(CCard)*20);
  56. pClientLink->Recv(&Temp_Discarding_Counter,sizeof(int));
  57. pClientLink->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. for(int i=0;i<20;i++)
  71. {
  72. pLeftCards->m_cDiscarding[i] = Temp_Discarding[i];
  73. }
  74. pLeftCards->m_nDiscardingCounter = Temp_Discarding_Counter;
  75. //如果出牌数大于0;
  76. if( pLeftCards->m_cDiscardingType.m_nTypeNum == 4 )
  77. {
  78. m_nBombCounter++;//加分;
  79. }
  80. if( pLeftCards->m_cDiscardingType.m_nTypeNum != 0 )
  81. {
  82. pDrawCards->LeftDiscarding();
  83. ::RedrawGame();
  84. if( pLeftCards->m_nCardsCounter == 0 )
  85. {
  86. //打印结束信息;
  87. AccountScore(0);
  88. ::SetTimer(hWnd,0,0,NULL);   //开始游戏;
  89. return 0;
  90. }
  91. }
  92. else
  93. {
  94. //打印左边玩家不要信息;
  95. pDraw->TextGDI("过牌!-",250,200,RGB(255,0,0),400,"华文新魏",lpddsprimary);
  96. if( m_nCurrentLord == 0 )
  97. {
  98. m_nLordLock = 0;
  99. }
  100. Sleep(300);
  101. }
  102. return 1;
  103. }
  104. //中间(本机)玩家出牌;
  105. int CClientGame::CenterDoing()
  106. {
  107. if( m_nLordLock == 1 )
  108. {
  109. if( m_nRoundCounter == 3 )  //说明已经出了牌;
  110. {
  111. //把已经出的牌回退;
  112. if(pCenterCards->m_nDiscardingCounter != 0)
  113. pDrawCards->CenterBacking();
  114. if(pRightCards->m_nDiscardingCounter != 0)
  115. pDrawCards->RightBacking();
  116. if(pLeftCards->m_nDiscardingCounter != 0)
  117. pDrawCards->LeftBacking();
  118. m_nRoundCounter = 0;   //新的一圈;
  119. }
  120. m_nRoundCounter++;
  121. return -1;  //表示当前是地主,上次轮过牌,现在则不能出牌;
  122. }
  123. if( pLeftCards->m_cDiscardingType.m_nTypeNum == 0 &&
  124. pRightCards->m_cDiscardingType.m_nTypeNum == 0 )
  125. {
  126. m_nLordLock = -1;
  127. }
  128. //如果没有选择牌;
  129. if( pCardsMap->m_nUpCounter == 0 )
  130. {
  131. //当不是第一个出牌的时候才可以过牌;
  132. if( pLeftCards->m_cDiscardingType.m_nTypeNum != 0 || 
  133. pRightCards->m_cDiscardingType.m_nTypeNum != 0 )
  134. {
  135. pDraw->TextGDI("过牌!",470,400,RGB(255,0,0),400,"华文新魏",lpddsprimary);
  136. Sleep(300);
  137. if( m_nRoundCounter == 3 )  //说明已经出了牌;
  138. {
  139. //把已经出的牌回退;
  140. if(pCenterCards->m_nDiscardingCounter != 0)
  141. pDrawCards->CenterBacking();
  142. if(pRightCards->m_nDiscardingCounter != 0)
  143. pDrawCards->RightBacking();
  144. if(pLeftCards->m_nDiscardingCounter != 0)
  145. pDrawCards->LeftBacking();
  146. m_nRoundCounter = 0;   //新的一圈;
  147. }
  148. m_nRoundCounter++;
  149. if( m_nCurrentLord == 1 )
  150. {
  151. m_nLordLock = 1;
  152. }
  153. pCenterCards->m_cDiscardingType.m_nTypeNum = 0;
  154. pClientLink->Send(pCenterCards->m_cDiscarding,sizeof(CCard)*20);
  155. pClientLink->Send(&pCenterCards->m_nDiscardingCounter,sizeof(int));
  156. pClientLink->Send(&pCenterCards->m_cDiscardingType,sizeof(CCardsType));
  157. return 1;
  158. }
  159. else
  160. {
  161. return 0;
  162. }
  163. }
  164. pCenterCards->m_nChoosingCardsCounter = 0;
  165. //将要出的牌加入到m_cDiscarding[20]数组中,m_nDiscardingCounter计数增加;
  166. for(int i=0;i<pCenterCards->m_nCardsCounter;i++)
  167. {
  168. if(pCardsMap->Center0[i].m_nY == Center_y0 - Card_Up)
  169. {
  170. pCenterCards->m_cChoosingCards[pCenterCards->m_nChoosingCardsCounter] = pCenterCards->m_cCards[i];
  171. pCenterCards->m_nChoosingCardsCounter++;
  172. }
  173. }
  174. //判断出牌的合法性,
  175. if( pCenterCards->CheckChoosing() )
  176. {
  177. //大小判断;
  178. if( CompareCards() == 0 )
  179. {
  180. for(int j=0;j<pCenterCards->m_nCardsCounter;j++)
  181. {
  182. if(pCardsMap->Center0[j].m_nY == Center_y0 - Card_Up)
  183. {
  184. pCardsMap->Center0[j].m_nY += Card_Up;
  185. }
  186. }
  187. pCardsMap->m_nUpCounter = 0;  //提起牌数为0;
  188. ::RedrawGame();
  189. m_bButton = 0;   //延时锁;
  190. Sleep(400);
  191. m_bButton = 1;   //延时锁;
  192. return 0;
  193. }
  194. if( m_nRoundCounter == 3 )  //说明已经出了牌;
  195. {
  196. //把已经出的牌回退;
  197. if(pCenterCards->m_nDiscardingCounter != 0)
  198. pDrawCards->CenterBacking();
  199. if(pRightCards->m_nDiscardingCounter != 0)
  200. pDrawCards->RightBacking();
  201. if(pLeftCards->m_nDiscardingCounter != 0)
  202. pDrawCards->LeftBacking();
  203. m_nRoundCounter = 0;   //新的一圈;
  204. }
  205. m_nRoundCounter++;
  206. //将要出的牌加入到m_cDiscarding[20]数组中,m_nDiscardingCounter计数增加;
  207. for(int i=0;i<pCenterCards->m_nCardsCounter;i++)
  208. {
  209. if(pCardsMap->Center0[i].m_nY == Center_y0 - Card_Up)
  210. {
  211. pCenterCards->m_cDiscarding[pCenterCards->m_nDiscardingCounter] = pCenterCards->m_cCards[i];
  212. pCenterCards->m_nDiscardingCounter++;
  213. }
  214. }
  215. //发送给其他两个玩家;
  216. pClientLink->Send(pCenterCards->m_cDiscarding,sizeof(CCard)*20);
  217. pClientLink->Send(&pCenterCards->m_nDiscardingCounter,sizeof(int));
  218. pClientLink->Send(&pCenterCards->m_cDiscardingType,sizeof(CCardsType));
  219. if( pCenterCards->m_cDiscardingType.m_nTypeNum == 4 )
  220. {
  221. m_nBombCounter++;//加分;
  222. }
  223. pDrawCards->CenterDiscarding();      //本机玩家出牌;
  224. if( pCenterCards->m_nCardsCounter == 0 )
  225. {
  226. //打印结束信息;
  227. PlaySound(MAKEINTRESOURCE(IDR_WIN),AfxGetResourceHandle(),
  228. SND_ASYNC|SND_RESOURCE|SND_NODEFAULT );  //
  229. AccountScore(1);
  230. ::SetTimer(hWnd,0,0,NULL);   //开始游戏;
  231. return 2;
  232. }
  233. return 1;
  234. }
  235. else
  236. {
  237. for(int j=0;j<pCenterCards->m_nCardsCounter;j++)
  238. {
  239. if(pCardsMap->Center0[j].m_nY == Center_y0 - Card_Up)
  240. {
  241. pCardsMap->Center0[j].m_nY += Card_Up;
  242. }
  243. }
  244. pCardsMap->m_nUpCounter = 0;  //提起牌数为0;
  245. ::RedrawGame();
  246. m_bButton = 0;   //延时锁;
  247. Sleep(400);
  248. m_bButton = 1;   //延时锁;
  249. return 0;
  250. }
  251. return -1;
  252. }
  253. //右边玩家(电脑)思考,并决定要出的牌;
  254. int CClientGame::RightDoing()
  255. {
  256. if( m_nLordLock == 2 )
  257. {
  258. if( m_nRoundCounter == 3 )  //说明已经出了牌;
  259. {
  260. //把已经出的牌回退;
  261. if(pLeftCards->m_nDiscardingCounter != 0)
  262. pDrawCards->LeftBacking();
  263. if(pCenterCards->m_nDiscardingCounter != 0)
  264. pDrawCards->CenterBacking();
  265. if(pRightCards->m_nDiscardingCounter != 0)
  266. pDrawCards->RightBacking();
  267. m_nRoundCounter = 0;   //新的一圈;
  268. }
  269. m_nRoundCounter++;
  270. return -1;
  271. }
  272. if( pLeftCards->m_cDiscardingType.m_nTypeNum == 0 && 
  273. pCenterCards->m_cDiscardingType.m_nTypeNum == 0 )
  274. {
  275. m_nLordLock = -1;
  276. }
  277. //补丁,调整出牌显示;
  278. int Temp_Discarding_Counter = 0;  //刚出的牌的数量;
  279. CCard Temp_Discarding[20];    //刚出的牌;
  280. pClientLink->Recv(Temp_Discarding,sizeof(CCard)*20);
  281. pClientLink->Recv(&Temp_Discarding_Counter,sizeof(int));
  282. pClientLink->Recv(&pRightCards->m_cDiscardingType,sizeof(CCardsType));
  283. if( m_nRoundCounter == 3 )  //说明已经出了牌;
  284. {
  285. //把已经出的牌回退;
  286. if(pRightCards->m_nDiscardingCounter != 0)
  287. pDrawCards->RightBacking();
  288. if(pCenterCards->m_nDiscardingCounter != 0)
  289. pDrawCards->CenterBacking();
  290. if(pLeftCards->m_nDiscardingCounter != 0)
  291. pDrawCards->LeftBacking();
  292. m_nRoundCounter = 0;   //新的一圈;
  293. }
  294. m_nRoundCounter++;
  295. //更新数据;
  296. for(int i=0;i<20;i++)
  297. {
  298. pRightCards->m_cDiscarding[i] = Temp_Discarding[i];
  299. }
  300. pRightCards->m_nDiscardingCounter = Temp_Discarding_Counter;
  301. if( pRightCards->m_cDiscardingType.m_nTypeNum == 4 )
  302. {
  303. m_nBombCounter++;//加分;
  304. }
  305. if( pRightCards->m_cDiscardingType.m_nTypeNum != 0 )
  306. {
  307. pDrawCards->RightDiscarding();
  308. ::RedrawGame();
  309. if( pRightCards->m_nCardsCounter == 0 )
  310. {
  311. //打印结束信息;
  312. AccountScore(2);
  313. ::SetTimer(hWnd,0,0,NULL);   //开始游戏;
  314. return 0;
  315. }
  316. }
  317. else
  318. {
  319. if( m_nCurrentLord == 2 )
  320. {
  321. m_nLordLock = 2;
  322. }
  323. //打印右边玩家不要信息;
  324. pRightCards->m_cDiscardingType.m_nTypeNum = 0;
  325. pDraw->TextGDI("过牌!-",600,200,RGB(255,0,0),400,"华文新魏",lpddsprimary);
  326. Sleep(300);
  327. }
  328. return 1;
  329. }
  330. //客户端决定地主;
  331. int CClientGame::DecideLord()    
  332. {
  333. pClientLink->Recv(&m_nDefaultLord,sizeof(int));
  334. m_nCurrentLord = m_nDefaultLord;  //当前地主;
  335. pDrawItem->BringRandLord(m_nDefaultLord);
  336. //info_type = 0: "%s要当地主,本局地主是:%s"
  337. //info_type = 1: "%s不当地主,等待%s选择..."
  338. //info_type = 2: "按规则,本局地主是:%s"
  339. int i;
  340. int counter = 0;
  341. while(1)
  342. {
  343. if( m_nCurrentLord != 1 )
  344. {
  345. pClientLink->Recv(&i,sizeof(int));
  346. if( i == m_nCurrentLord )
  347. {
  348. pDrawItem->GameInfo(m_nCurrentLord,0);
  349. //跳出,准备开始游戏;
  350. break;
  351. }
  352. else if( i == -1 )
  353. {
  354. pDrawItem->GameInfo(m_nCurrentLord,1);
  355. pClientLink->Recv(&m_nCurrentLord,sizeof(int));
  356. }
  357. }
  358. else if( m_nCurrentLord == 1 )
  359. {
  360. pDrawItem->ChooseLord();
  361. while(1)
  362. {
  363. if( pInput->IsLButtonDown(m_cYES_RECT) )  //要
  364. {
  365. PlaySound(MAKEINTRESOURCE(IDR_CHOSE),AfxGetResourceHandle(),
  366. SND_ASYNC|SND_RESOURCE|SND_NODEFAULT );  
  367. pDrawItem->GameInfo(m_nCurrentLord,0);
  368. pClientLink->Send(&m_nCurrentLord,sizeof(int));
  369. //跳出,准备开始游戏;goto...
  370. goto out;
  371. }
  372. else if( pInput->IsLButtonDown(m_cNO_RECT) )  //不要
  373. {
  374. PlaySound(MAKEINTRESOURCE(IDR_CHOSE),AfxGetResourceHandle(),
  375. SND_ASYNC|SND_RESOURCE|SND_NODEFAULT );  //
  376. pDrawItem->GameInfo(m_nCurrentLord,1);
  377. i = -1;
  378. pClientLink->Send(&i,sizeof(int));
  379. pClientLink->Recv(&m_nCurrentLord,sizeof(int));
  380. break;
  381. }
  382. Sleep(50);   //产生一个延时;
  383. }//end while;
  384. }
  385. counter++;
  386. if( counter == 3 )
  387. {
  388. pDrawItem->GameInfo(m_nDefaultLord,2);
  389. m_nCurrentLord = m_nDefaultLord;
  390. //跳出,准备开始游戏;
  391. break;
  392. }
  393. }//end while;
  394. out:
  395. // Sleep(10000);
  396. return 1;
  397. }
  398. int CClientGame::Run()
  399. {
  400. //开始新的牌局;
  401. NewGame();    
  402. //显示局数;
  403. pDrawItem->GameCounter(m_nGameCounter);
  404. //接受服务器发牌;
  405. pClientLink->Recv(g_cAllCards,sizeof(CCard)*54);
  406. //发牌;
  407. pDrawCards->Dealing();   
  408. DecideLord();
  409. pDrawCards->DealingLord(m_nCurrentLord); //发地主牌;
  410. m_bOperate  = TRUE;
  411. //开始循环;
  412. if(m_nCurrentLord == 0)
  413. {
  414. LeftDoing();
  415. ::SetTimer(hWnd,20,10,NULL);
  416. }
  417. else if(m_nCurrentLord == 1)
  418. {
  419. ::SetTimer(hWnd,20,10,NULL);
  420. }
  421. else if(m_nCurrentLord == 2)
  422. {
  423. RightDoing();
  424. LeftDoing();
  425. ::SetTimer(hWnd,20,10,NULL);
  426. }
  427. return 1;
  428. }