gameob.cpp
上传用户:loctite114
上传日期:2007-01-03
资源大小:49k
文件大小:8k
源码类别:

棋牌游戏

开发平台:

Visual C++

  1. // Copyright (c) 1999 Lee Patterson
  2. // leepatterson@home.com
  3. #include <winsock.h>
  4. #include <stdio.h>
  5. #include <llist.h>
  6. #include "blockingsocket.h"
  7. #include "gameob.h"
  8. //**********
  9. //*
  10. //* PERSON INSTANCE OBJECT
  11. //*
  12. //**********************
  13. CPersonInstance::CPersonInstance()
  14. {
  15. m_color=WHITE;
  16. m_location=CHESS;
  17. m_channel=0; 
  18. }
  19. CPersonInstance::~CPersonInstance()
  20. {
  21. }
  22. //**********
  23. //*
  24. //* BOARD SPACE OBJECT
  25. //*
  26. //**********************
  27. BoardSpace::BoardSpace()
  28. {
  29. static int icount=0;
  30. m_id=icount++;
  31. m_type=EMPTY;
  32. }
  33. //**********
  34. //*
  35. //* BOARD GAME OBJECT
  36. //*
  37. //**********************
  38. BoardGame::BoardGame()
  39. {
  40. m_white=NULL;
  41. m_black=NULL;
  42. m_nBoardType=BTYPE_UNDEFINED;
  43. memset(m_gamename,0,sizeof m_gamename);
  44. }
  45. void BoardGame::AddPerson(CPersonInstance* pInstance)
  46. {
  47. m_connections.AddTail(pInstance);
  48. }
  49. void BoardGame::RemovePerson(CPersonInstance* pInstance)
  50. {
  51. CLList* plist=m_connections.FindItem(pInstance);
  52. if(plist)
  53. m_connections.RemoveItem(plist);
  54. }
  55. int BoardGame::Left(int v)
  56. {
  57. return (v&0xf0)>>4;
  58. }
  59. int BoardGame::Right(int v)
  60. {
  61. return v&0xf;
  62. }
  63. //**********
  64. //*
  65. //* CHESS BOARD OBJECT
  66. //*
  67. //**********************
  68. ChessBoard::ChessBoard()
  69. {
  70. FillBoard();
  71. }
  72. bool ChessBoard::ValidMove(int from0,int from1,int to0,int to1)
  73. {
  74. return true; //all moves are cool
  75. }
  76. void ChessBoard::Move(int from0,int from1,int to0,int to1)
  77. {
  78. m_spaces[to1-'0'][to0-'0'].m_type=m_spaces[from1-'0'][from0-'0'].m_type;
  79. m_spaces[from1-'0'][from0-'0'].m_type=BoardSpace::EMPTY;
  80. }
  81. void ChessBoard::FillBoard()
  82. {
  83. int blackbackrow[]={BoardSpace::BROOK,BoardSpace::BKNIGHT,BoardSpace::BBISHOP,BoardSpace::BQUEEN,BoardSpace::BKING,BoardSpace::BBISHOP,BoardSpace::BKNIGHT,BoardSpace::BROOK};
  84. int whitebackrow[]={BoardSpace::WROOK,BoardSpace::WKNIGHT,BoardSpace::WBISHOP,BoardSpace::WQUEEN,BoardSpace::WKING,BoardSpace::WBISHOP,BoardSpace::WKNIGHT,BoardSpace::WROOK};
  85. int i;
  86. int j;
  87. //fill back rows
  88. for(i=0; i<8; i++)
  89. {
  90. m_spaces[0][i].m_type=blackbackrow[i];
  91. m_spaces[7][i].m_type=whitebackrow[i];
  92. }
  93. //fill pawn row
  94. for(i=0; i<8; i++)
  95. {
  96. m_spaces[1][i].m_type=BoardSpace::BPAWN;
  97. m_spaces[6][i].m_type=BoardSpace::WPAWN;
  98. }
  99. //fill in the rest of the empty spaces
  100. for(i=2; i<6; i++)
  101. {
  102. for(j=0; j<8; j++)
  103. m_spaces[i][j].m_type=BoardSpace::EMPTY;
  104. }
  105. }
  106. void ChessBoard::Draw()
  107. {
  108. CPersonInstance* pInstance;
  109. CLList* plist=m_connections.GetHead();
  110. while(plist)
  111. {
  112. pInstance=(CPersonInstance*)plist->GetItem();
  113. DrawBoard(pInstance);
  114. plist=plist->GetNext();
  115. }
  116. }
  117. void ChessBoard::DrawBoard(CPersonInstance* pInstance)
  118. {
  119. char buffer[80]; //buffer to store a line
  120. char* p=buffer;
  121. for(int y=0; y<8; y++)
  122. {
  123. sprintf(buffer,"%d ",y); 
  124. p=buffer+strlen(buffer);
  125. for(int x=0; x<8; x++)
  126. {
  127. *(p++)=GetPiece(m_spaces[y][x].m_type);
  128. *(p++)=' ';
  129. }
  130. *p='';
  131. pInstance->Print(buffer);
  132. }
  133. pInstance->Print("  0 1 2 3 4 5 6 7");
  134. }
  135. char ChessBoard::GetPiece(int type)
  136. {
  137. char id;
  138. switch(type)
  139. {
  140. case BoardSpace::EMPTY:
  141. id='.';
  142. break;
  143. case BoardSpace::WPAWN:
  144. id='p';
  145. break;
  146. case BoardSpace::WBISHOP:
  147. id='b';
  148. break;
  149. case BoardSpace::WKNIGHT:
  150. id='n';
  151. break;
  152. case BoardSpace::WROOK:
  153. id='r';
  154. break;
  155. case BoardSpace::WQUEEN:
  156. id='q';
  157. break;
  158. case BoardSpace::WKING:
  159. id='k';
  160. break;
  161. case BoardSpace::BPAWN:
  162. id='P';
  163. break;
  164. case BoardSpace::BBISHOP:
  165. id='B';
  166. break;
  167. case BoardSpace::BKNIGHT:
  168. id='N';
  169. break;
  170. case BoardSpace::BROOK:
  171. id='R';
  172. break;
  173. case BoardSpace::BQUEEN:
  174. id='Q';
  175. break;
  176. case BoardSpace::BKING:
  177. id='K';
  178. break;
  179. default:
  180. id='?';
  181. break;
  182. }
  183. return id;
  184. }
  185. //**********
  186. //*
  187. //* CHESS GAME OBJECT
  188. //*
  189. //**********************
  190. ChessGameOb::ChessGameOb(CPersonInstance* pInstanceWhite,const char* pGameName)
  191. {
  192. m_white = pInstanceWhite;
  193. strcpy(m_gamename,pGameName);
  194. m_connections.AddTail(pInstanceWhite);
  195. m_black=NULL;
  196. }
  197. ChessGameOb::~ChessGameOb()
  198. {
  199. m_white->Cleanup();
  200. m_black->Cleanup();
  201. delete m_white;
  202. delete m_black;
  203. }
  204. void ChessGameOb::AddWatcher(CPersonInstance* pInstance)
  205. {
  206. m_connections.AddTail(pInstance);
  207. }
  208. void ChessGameOb::AddBlack(CPersonInstance* pInstance)
  209. {
  210. m_black = pInstance;
  211. m_connections.AddTail(pInstance);
  212. }
  213. bool ChessGameOb::HasAllPlayers()
  214. {
  215. if(m_white && m_black)
  216. return true;
  217. return false;
  218. }
  219. //**********
  220. //*
  221. //* CHECKER BOARD OBJECT
  222. //*
  223. //**********************
  224. CheckerBoard::CheckerBoard()
  225. {
  226. FillBoard();
  227. }
  228. bool CheckerBoard::ValidMove(int from0,int from1,int to0,int to1)
  229. {
  230. return true; //all moves are cool
  231. }
  232. void CheckerBoard::Move(int from0,int from1,int to0,int to1)
  233. {
  234. m_spaces[to1-'0'][to0-'0'].m_type=m_spaces[from1-'0'][from0-'0'].m_type;
  235. m_spaces[from1-'0'][from0-'0'].m_type=BoardSpace::EMPTY;
  236. }
  237. void CheckerBoard::FillBoard()
  238. {
  239. int rows[]=
  240. {
  241. BoardSpace::WSQUARE,BoardSpace::BCHECKER,BoardSpace::WSQUARE,BoardSpace::BCHECKER,BoardSpace::WSQUARE,BoardSpace::BCHECKER,BoardSpace::WSQUARE,BoardSpace::BCHECKER,
  242. BoardSpace::BCHECKER,BoardSpace::WSQUARE,BoardSpace::BCHECKER,BoardSpace::WSQUARE,BoardSpace::BCHECKER,BoardSpace::WSQUARE,BoardSpace::BCHECKER,BoardSpace::WSQUARE,
  243. BoardSpace::WSQUARE,BoardSpace::BCHECKER,BoardSpace::WSQUARE,BoardSpace::BCHECKER,BoardSpace::WSQUARE,BoardSpace::BCHECKER,BoardSpace::WSQUARE,BoardSpace::BCHECKER,
  244. BoardSpace::BSQUARE,BoardSpace::WSQUARE,BoardSpace::BSQUARE,BoardSpace::WSQUARE,BoardSpace::BSQUARE,BoardSpace::WSQUARE,BoardSpace::BSQUARE,BoardSpace::WSQUARE,
  245. BoardSpace::WSQUARE,BoardSpace::BSQUARE,BoardSpace::WSQUARE,BoardSpace::BSQUARE,BoardSpace::WSQUARE,BoardSpace::BSQUARE,BoardSpace::WSQUARE,BoardSpace::BSQUARE,
  246. BoardSpace::WCHECKER,BoardSpace::WSQUARE,BoardSpace::WCHECKER,BoardSpace::WSQUARE,BoardSpace::WCHECKER,BoardSpace::WSQUARE,BoardSpace::WCHECKER,BoardSpace::WSQUARE,
  247. BoardSpace::WSQUARE,BoardSpace::WCHECKER,BoardSpace::WSQUARE,BoardSpace::WCHECKER,BoardSpace::WSQUARE,BoardSpace::WCHECKER,BoardSpace::WSQUARE,BoardSpace::WCHECKER,
  248. BoardSpace::WCHECKER,BoardSpace::WSQUARE,BoardSpace::WCHECKER,BoardSpace::WSQUARE,BoardSpace::WCHECKER,BoardSpace::WSQUARE,BoardSpace::WCHECKER,BoardSpace::WSQUARE,
  249. };
  250. int irow=0;
  251. for(int y=0; y<8; y++)
  252. for(int x=0; x<8; x++)
  253. m_spaces[y][x].m_type=rows[irow++];
  254. }
  255. void CheckerBoard::Draw()
  256. {
  257. CPersonInstance* pInstance;
  258. CLList* plist=m_connections.GetHead();
  259. while(plist)
  260. {
  261. pInstance=(CPersonInstance*)plist->GetItem();
  262. DrawBoard(pInstance);
  263. plist=plist->GetNext();
  264. }
  265. }
  266. void CheckerBoard::DrawBoard(CPersonInstance* pInstance)
  267. {
  268. char buffer[80]; //buffer to store a line
  269. char* p=buffer;
  270. pInstance->Print("    0   1   2   3   4   5   6   7");
  271. for(int y=0; y<8; y++)
  272. {
  273. pInstance->Print("   --- --- --- --- --- --- --- ---");
  274. sprintf(buffer,"%d |",y); 
  275. p=buffer+strlen(buffer);
  276. for(int x=0; x<8; x++)
  277. {
  278. *(p++)=' ';
  279. *(p++)=GetPiece(m_spaces[y][x].m_type);
  280. *(p++)=' ';
  281. *(p++)='|';
  282. }
  283. *(p++)=' ';
  284. *(p++)='0'+y;
  285. *p='';
  286. pInstance->Print(buffer);
  287. }
  288. pInstance->Print("   --- --- --- --- --- --- --- ---");
  289. pInstance->Print("    0   1   2   3   4   5   6   7");
  290. }
  291. char CheckerBoard::GetPiece(int type)
  292. {
  293. char id;
  294. switch(type)
  295. {
  296. case BoardSpace::EMPTYCHECKER:
  297. id=' ';
  298. break;
  299. case BoardSpace::WSQUARE:
  300. id=' ';
  301. break;
  302. case BoardSpace::BSQUARE:
  303. id=' ';
  304. break;
  305. case BoardSpace::WCHECKER :
  306. id='W';
  307. break;
  308. case BoardSpace::BCHECKER:
  309. id='b';
  310. break;
  311. default: id='?'; break;
  312. }
  313. return id;
  314. }
  315. //**********
  316. //*
  317. //* CHECKER GAME OBJECT
  318. //*
  319. //**********************
  320. CheckerGameOb::CheckerGameOb(CPersonInstance* pInstanceWhite,const char* pGameName)
  321. {
  322. m_white = pInstanceWhite;
  323. strcpy(m_gamename,pGameName);
  324. m_connections.AddTail(pInstanceWhite);
  325. m_black=NULL;
  326. }
  327. CheckerGameOb::~CheckerGameOb()
  328. {
  329. m_white->Cleanup();
  330. m_black->Cleanup();
  331. delete m_white;
  332. delete m_black;
  333. }
  334. void CheckerGameOb::AddWatcher(CPersonInstance* pInstance)
  335. {
  336. m_connections.AddTail(pInstance);
  337. }
  338. void CheckerGameOb::AddBlack(CPersonInstance* pInstance)
  339. {
  340. m_black = pInstance;
  341. m_connections.AddTail(pInstance);
  342. }
  343. bool CheckerGameOb::HasAllPlayers()
  344. {
  345. if(m_white && m_black)
  346. return true;
  347. return false;
  348. }