Get the App
SLTechnology News&Howtos  ›  Development  › 

How to develop Chess Program based on C++ and MFC

Shulou Source: shulou.com Published: 2022-06-03 13:41:50 10月02日 Update

This article mainly introduces how to develop chess programs based on C++ and MFC. It is very detailed and has a certain reference value. Interested friends must finish reading it.

First, chessboard analysis

This is the chessboard I drew. The chessboard consists of 9 vertical lines and 10 horizontal lines. Here we set the interval between each line to be 50.

Second, the drawing process

1. Create a new MFC program in vs to remove the buttons and text automatically generated by the environment.

2. Open the * Dlg.cpp file, define a chessboard interval value in void CChessDlg::OnPaint () and the drawing device CDC * cd = CWnd::GetDC ()

Int nWid = 50 China CDC * cd = CWnd::GetDC ()

3. Draw the horizontal line of the chessboard

/ / draw a dash for (int I = 1; I

< 11; ++i){ cd->

MoveTo (nWid, I * nWid); cd- > LineTo (9 * nWid, I * nWid);}

4. Draw the vertical line of the chessboard

/ / draw a vertical bar for (int I = 1; I

< 10; ++i){  //绘制棋盘两侧线段 if(1 == i || 9 == i) { cd->

MoveTo (I * nWid, nWid); cd- > LineTo (I * nWid,10 * nWid);} else// draws vertical lines in the middle of the chessboard {cd- > MoveTo (I * nWid, nWid); cd- > LineTo (I * nWid,5 * nWid); cd- > MoveTo (I * nWid,6 * nWid); cd- > LineTo (I * nWid,10 * nWid);}}

5. Draw Nine Palace grids

/ / painting Nine Miyagi CD-> MoveTo (4 * nWid, nWid); cd- > LineTo (6 * nWid,3 * nWid); cd- > MoveTo (6 * nWid, nWid); cd- > LineTo (4 * nWid,3 * nWid); cd- > MoveTo (4 * nWid, 8 * nWid); cd- > LineTo (6 * nWid,10 * nWid); cd- > MoveTo (6 * nWid, 8 * nWid); cd- > LineTo (4 * nWid,10 * nWid)

At this point, our chessboard is drawn. Here is the complete code.

CDialogEx::OnPaint (); int nWid = 50; CDC * cd = CWnd::GetDC (); / / dash for (int I = 1; I

< 11; ++i) { cd->

MoveTo (nWid, I * nWid); cd- > LineTo (9 * nWid, I * nWid);} / / draw vertical bar for (int I = 1; I

< 10; ++i) { if(1 == i || 9 == i) { cd->

MoveTo (I * nWid, nWid); cd- > LineTo (I * nWid,10 * nWid);} else {cd- > MoveTo (I * nWid, nWid); cd- > LineTo (I * nWid,5 * nWid); cd- > MoveTo (I * nWid,6 * nWid); cd- > LineTo (I * nWid,10 * nWid);}} / / draw Jiugong cd- > MoveTo (4 * nWid, nWid); cd- > LineTo (6 * nWid,3 * nWid) Cd- > MoveTo (6 * nWid, nWid); cd- > LineTo (4 * nWid,3 * nWid); cd- > MoveTo (4 * nWid, 8 * nWid); cd- > LineTo (6 * nWid,10 * nWid); cd- > MoveTo (6 * nWid, 8 * nWid); cd- > LineTo (4 * nWid,10 * nWid); these are all the contents of the article "how to develop Chess programs based on C++ and MFC". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Chessboard vertical bar horizontal line program ninth house chess Clippers development content articles code value interest guys buddies buttons files text more environment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Linux Apple Xiaomi macOS