FiveChessView.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:9k
源码类别:

游戏

开发平台:

Visual C++

  1. // FiveChessView.cpp : implementation of the CFiveChessView class
  2. //
  3. #include "stdafx.h"
  4. #include "FiveChess.h"
  5. #include "FiveChessDoc.h"
  6. #include "FiveChessView.h"
  7. #include "MainFrm.h"
  8. #include "ClientDlg.h"
  9. #include "ServerDlg.h"
  10. #include "afxsock.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. #define TOPDIS 10
  17. #define LEFTDIS 100 
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CFiveChessView
  20. IMPLEMENT_DYNCREATE(CFiveChessView, CView)
  21. BEGIN_MESSAGE_MAP(CFiveChessView, CView)
  22. //{{AFX_MSG_MAP(CFiveChessView)
  23. ON_WM_LBUTTONDOWN()
  24. //}}AFX_MSG_MAP
  25. // Standard printing commands
  26. ON_COMMAND(ID_SETSERVER, OnSetserver)
  27. ON_COMMAND(ID_SETCLIENT, OnSetclient)
  28. ON_EN_UPDATE(2, INPUTMESSAGE)
  29. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  30. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  31. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  32. END_MESSAGE_MAP()
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CFiveChessView construction/destruction
  35. CFiveChessView::CFiveChessView()
  36. {
  37. // TODO: add construction code here
  38. m_bIsClient=FALSE;
  39. m_bIsInit=FALSE;
  40. m_bOver=FALSE;
  41. m_bWin=FALSE;
  42. m_turn= 1;//服务器先走,黑棋
  43. }
  44. CFiveChessView::~CFiveChessView()
  45. {
  46. }
  47. BOOL CFiveChessView::PreCreateWindow(CREATESTRUCT& cs)
  48. {
  49. // TODO: Modify the Window class or styles here by modifying
  50. //  the CREATESTRUCT cs
  51. return CView::PreCreateWindow(cs);
  52. }
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CFiveChessView drawing
  55. void CFiveChessView::OnDraw(CDC* pDC)
  56. {
  57. CFiveChessDoc* pDoc = GetDocument();
  58. ASSERT_VALID(pDoc);
  59. CBrush back(RGB(125,125,20));
  60. pDC->SelectObject((CBrush*)&back);
  61. CRect rect;
  62. rect.left=LEFTDIS;rect.right=LEFTDIS+(LW-1)*30;
  63. rect.top=TOPDIS;rect.bottom=TOPDIS+(LW-1)*30;
  64. pDC->FillRect(&rect,&back);
  65. CPen pen(PS_SOLID,2,RGB(25,25,255));
  66. pDC->SelectObject((CPen*)&pen);
  67. int i,j;
  68. CPoint pos;
  69. for(i=0;i<LW;i++)//竖直的线
  70. {
  71. pos.x=LEFTDIS+30*i;
  72. pos.y=TOPDIS;
  73. pDC->MoveTo(pos);
  74. pos.y=TOPDIS+(LW-1)*30;
  75. pDC->LineTo(pos);
  76. }
  77. for(i=0;i<LW;i++)//水平的线
  78. {
  79. pos.x=LEFTDIS;
  80. pos.y=TOPDIS+30*i;
  81. pDC->MoveTo(pos);
  82. pos.x=LEFTDIS+(LW-1)*30;
  83. pDC->LineTo(pos);
  84. }
  85. CBrush whitebrush(RGB(255,255,255));
  86. CBrush blackbrush(RGB(0,0,0));
  87. for(i=0;i<LW;i++)
  88. for(j=0;j<LW;j++)
  89. {
  90. if(m_match.chessboard[i][j]==1)
  91. {
  92. pDC->SelectObject((CBrush*)&blackbrush);
  93. pDC->Ellipse(j*30+LEFTDIS-15,i*30+TOPDIS-15,j*30+LEFTDIS+15,i*30+TOPDIS+15);
  94. }
  95. else
  96. if(m_match.chessboard[i][j]==2)
  97. {
  98. pDC->SelectObject((CBrush*)&whitebrush);
  99. pDC->Ellipse(j*30+LEFTDIS-15,i*30+TOPDIS-15,j*30+LEFTDIS+15,i*30+TOPDIS+15);
  100. }
  101. }
  102. //星位的绘制
  103. CBrush starbrush(RGB(0,0,0));
  104. pDC->SelectObject((CBrush*)&starbrush);
  105. j=4;i=4;
  106. if(m_match.chessboard[i][j]==0)
  107. pDC->Ellipse(j*30+LEFTDIS-8,i*30+TOPDIS-8,j*30+LEFTDIS+8,i*30+TOPDIS+8);
  108. j=4;i=14;
  109. if(m_match.chessboard[i][j]==0)
  110. pDC->Ellipse(j*30+LEFTDIS-8,i*30+TOPDIS-8,j*30+LEFTDIS+8,i*30+TOPDIS+8);
  111. j=9;i=9;
  112. if(m_match.chessboard[i][j]==0)
  113. pDC->Ellipse(j*30+LEFTDIS-8,i*30+TOPDIS-8,j*30+LEFTDIS+8,i*30+TOPDIS+8);
  114. j=14;i=4;
  115. if(m_match.chessboard[i][j]==0)
  116. pDC->Ellipse(j*30+LEFTDIS-8,i*30+TOPDIS-8,j*30+LEFTDIS+8,i*30+TOPDIS+8);
  117. j=14;i=14;
  118. if(m_match.chessboard[i][j]==0)
  119. pDC->Ellipse(j*30+LEFTDIS-8,i*30+TOPDIS-8,j*30+LEFTDIS+8,i*30+TOPDIS+8);
  120. if(m_bOver==TRUE)
  121. {
  122. CBrush redbrush(RGB(255,0,0));
  123. pDC->SelectObject((CBrush*)&redbrush);
  124. for(int k=0;k<5;k++)
  125. {
  126. i=m_winpos[k][0];
  127. j=m_winpos[k][1];
  128. pDC->Ellipse(j*30+LEFTDIS-15,i*30+TOPDIS-15,j*30+LEFTDIS+15,i*30+TOPDIS+15);
  129. }
  130. }
  131. // TODO: add draw code for native data here
  132. }
  133. /////////////////////////////////////////////////////////////////////////////
  134. // CFiveChessView printing
  135. BOOL CFiveChessView::OnPreparePrinting(CPrintInfo* pInfo)
  136. {
  137. // default preparation
  138. return DoPreparePrinting(pInfo);
  139. }
  140. void CFiveChessView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  141. {
  142. // TODO: add extra initialization before printing
  143. }
  144. void CFiveChessView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  145. {
  146. // TODO: add cleanup after printing
  147. }
  148. /////////////////////////////////////////////////////////////////////////////
  149. // CFiveChessView diagnostics
  150. #ifdef _DEBUG
  151. void CFiveChessView::AssertValid() const
  152. {
  153. CView::AssertValid();
  154. }
  155. void CFiveChessView::Dump(CDumpContext& dc) const
  156. {
  157. CView::Dump(dc);
  158. }
  159. CFiveChessDoc* CFiveChessView::GetDocument() // non-debug version is inline
  160. {
  161. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFiveChessDoc)));
  162. return (CFiveChessDoc*)m_pDocument;
  163. }
  164. #endif //_DEBUG
  165. /////////////////////////////////////////////////////////////////////////////
  166. // CFiveChessView message handlers
  167. void CFiveChessView::OnSetserver() 
  168. {
  169. // TODO: Add your command handler code here
  170. CServerDlg dlg;
  171. if(dlg.DoModal()==IDOK)
  172. {
  173. m_bIsClient=FALSE;
  174. m_bIsInit=TRUE;
  175. m_port=dlg.m_iPort;
  176. m_ListenSocket.Init(m_port,this);
  177. }
  178. }
  179. void CFiveChessView::OnSetclient() 
  180. {
  181. // TODO: Add your command handler code here
  182. CClientDlg dlg;
  183. int ret=dlg.DoModal();
  184. if(ret==2000)
  185. {
  186. m_bIsClient=TRUE;
  187. m_bIsInit=TRUE;
  188. m_port=dlg.m_iPort;
  189. for(int i=0;i<4;i++)
  190. m_bIP[i]=dlg.m_bIP[i];
  191. m_ip.Format("%d.%d.%d.%d",dlg.m_bIP[0],dlg.m_bIP[1],dlg.m_bIP[2],dlg.m_bIP[3]);
  192. m_ClientSocket.Create();
  193. if(m_ClientSocket.Connect(LPCSTR(m_ip),m_port))
  194. {
  195. m_ClientSocket.Init(this);
  196. m_who=2; //客户端用白子
  197. AfxMessageBox("成功的连接到了服务器,可以开始游戏了");
  198. }
  199. else
  200. {
  201. m_ClientSocket.Close();
  202. AfxMessageBox("client connection failed");
  203. }
  204. }
  205. }
  206. void CFiveChessView::ProcessPendingAccept()
  207. {
  208. if(m_ListenSocket.Accept(m_ClientSocket)==FALSE)
  209. {
  210. AfxMessageBox("Server Listen Socket Error");
  211. return;
  212. }
  213. else
  214. {
  215. m_who=1;//服务器端用黑子
  216. m_ClientSocket.Init(this);
  217. m_outputedit.SetWindowText("有人进来了");
  218. AfxMessageBox("有人进来了");
  219. }
  220. }
  221. void CFiveChessView::GetLocalIP()
  222. {
  223. static BOOL first=TRUE;
  224. if(first==TRUE)
  225. {
  226. CClientSocket::GetLocalHostName(m_sLocalName);
  227. CClientSocket::GetIpAddress(LPCSTR(m_sLocalName),m_bIP[0],m_bIP[1],m_bIP[2],m_bIP[3]);
  228. m_ip.Format("%d.%d.%d.%d",m_bIP[0],m_bIP[1],m_bIP[2],m_bIP[3]);
  229. first=FALSE;
  230. }
  231. }
  232. void CFiveChessView::INPUTMESSAGE()
  233. {
  234. if(!m_bIsInit)
  235. {
  236. AfxMessageBox("No Connection ");
  237. return;
  238. }
  239. CString in;
  240. CMessg msg;
  241. m_inputedit.GetWindowText(in);
  242. if(in.GetLength()<1)
  243. {
  244. return;
  245. }
  246. if(in.GetAt(in.GetLength()-1)=='n')
  247. {
  248. in.TrimRight(" ");
  249. m_inputedit.SetWindowText("");
  250. if(in.GetLength()>2)
  251. {
  252. m_sMsgList+=in;
  253. m_outputedit.SetWindowText(m_sMsgList);
  254. m_iLineCurrentPos=m_outputedit.GetLineCount();
  255. m_outputedit.LineScroll(m_iLineCurrentPos);
  256. msg.m_strText=in;
  257. m_ClientSocket.SendMessage(&msg);
  258. }
  259. }
  260. }
  261. void CFiveChessView::OnInitialUpdate() 
  262. {
  263. CView::OnInitialUpdate();
  264. // TODO: Add your specialized code here and/or call the base class
  265. static bool oninitialupdatehaving=false;
  266. if(oninitialupdatehaving==false)
  267. {
  268. if(::AfxSocketInit()==FALSE)
  269. {
  270. AfxMessageBox("socket init error");
  271. }
  272. GetLocalIP();
  273. CRect rect;
  274. this->GetWindowRect(rect);
  275. m_outputedit.Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER|ES_READONLY,CRect(10,rect.bottom-200,rect.right-10,rect.bottom-140),this,1);
  276. m_inputedit.Create(ES_MULTILINE|WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER|ES_AUTOVSCROLL,CRect(10,rect.bottom-140,rect.right-10,rect.bottom-100),this,2);
  277. }
  278. }
  279. void CFiveChessView::OnLButtonDown(UINT nFlags, CPoint point) 
  280. {
  281. // TODO: Add your message handler code here and/or call default
  282. if(m_bOver==TRUE)
  283. {
  284. if(AfxMessageBox("你想重新开始游戏吗?",MB_YESNO)==IDYES)
  285. {
  286. //重新开始游戏
  287. m_bIsClient=FALSE;
  288. m_bIsInit=FALSE;
  289. m_turn=1;
  290. m_match.Clear();
  291. m_bWin=FALSE;
  292. m_bOver=FALSE;
  293. Invalidate(FALSE);
  294. return;
  295. }
  296. }
  297. if(m_turn==m_who)
  298. {
  299. m_POS[0]=(point.y-TOPDIS)/30; //x ,绘图坐标中的y
  300. if((point.y-TOPDIS)%30>15)
  301. m_POS[0]++;
  302. m_POS[1]=(point.x-LEFTDIS)/30; //y ,绘图坐标中的x
  303. if((point.x-LEFTDIS)%30>15)
  304. m_POS[1]++;
  305. if(m_POS[0]<0||m_POS[0]>=LW||m_POS[1]<0||m_POS[1]>=LW)
  306. {
  307. return;
  308. }
  309. if(m_match.CanDown(m_POS[0],m_POS[1],m_who)==TRUE)
  310. {
  311. Invalidate(FALSE);
  312. m_turn=m_who%2+1;
  313. CMessg msg;
  314. msg.m_turn=m_turn;
  315. msg.m_x=m_POS[0];
  316. msg.m_y=m_POS[1];
  317. m_ClientSocket.SendMessage(&msg);
  318. if(m_match.IsWin(m_who,m_winpos))
  319. {
  320. m_bWin=TRUE;
  321. m_bOver=TRUE;
  322. Invalidate(FALSE);
  323. AfxMessageBox("你赢了耶");
  324. m_sMsgList+="你赢了耶";
  325. m_outputedit.SetWindowText(m_sMsgList);
  326. }
  327. }
  328. }
  329. else
  330. {
  331. m_sMsgList+="不该你走棋";
  332. m_outputedit.SetWindowText(m_sMsgList);
  333. }
  334. CView::OnLButtonDown(nFlags, point);
  335. }