In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how C++ realizes Chinese chess". In daily operation, I believe many people have doubts about how C++ can realize Chinese chess. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about how C++ can realize Chinese chess! Next, please follow the editor to study!
Enum Pieces / / Chess {NONE =-1, soldier, elephant, scholar, general, pawn, pawn, horse, minister, officer, gun, soldier, BEGIN, END,}; / / assign enum Pieces redChess [] = {horse, soldier, elephant, soldier, general, soldier, pawn} to id Enum Pieces blackChess [] = {horse, horse, phase, officer, gun, soldier}; / / convert to the string const char* ChessName [] = {"horse", "soldier", "elephant", "soldier", "general", "horse", "pawn", "horse", "horse", "official", "gun", "gun", "soldier"} when drawing
Next up is our old friend structure.
/ / the attribute of each piece struct Chess {enum Pieces id; / / piece name DWORD type; / / piece type, red? Black? Short x; short y; bool isRiver; / / whether you have crossed the river}; / / the game map struct Chess map [ROW] [COL]; struct State {int begr; int begc; int endr; int endc; int state;} state = {- 1Jing Lok 1m Lok 1m begin}
In our initialization function, we must think of the logic in it.
/ / initialize data void init () {/ / traversal map for (size_t I = 0; I
< ROW; i++) { size_t temp = 0; for (size_t k = 0; k < COL; k++) { map[i][k].id = NONE; //先把棋子置为没有 if (i map[row][col].x + 30 && msg.y >Map [row] [col] .y + 30) {row++; col++;} / / printf ("(% d% d)\ n", row, col) If (state.state = = BEGIN) {state.begr = row; state.begc = col; state.state = END } else if (state.state = = END) {state.endr = row; state.endc = col; state.state = BEGIN } chessMove ();}
The key point, the movement function of chess pieces, and the rules of the game are reflected here.
/ / move chess piece void chessMove () {printf ("beg (% d% d) end (% d% d)\ n", state.begr, state.begc, state.endr, state.endc); bool canMove = false / / under what circumstances can a chess piece be moved if (! (state.begr = = state.endr & & state.begc = = state.endc) & & / / the same piece is not clicked. The endringing characters must be legal map [state.begr] [state.begc]. Id! = NONE// cannot move without chess pieces / * & & map [state.begr] [state.begc] .type! = map [state.endr] [state.endc] .type * / / cannot eat its own {switch (map [state.begr] [state.begc] .id) {case pieces: Case blocks: if (state.begr = = state.endr | | state.begc = = state.endc) {/ / whether there is an obstacle between the start point and the end point if (hasBlock (& state)) {canMove = true }} break; case horse: case horse: break; case image: case phase: break Case: case: break; case will: case gun: break; case gun: case gun: break Case pawn: case soldier: break; default: break;} if (canMove) {printf ("canMove\ n") Map [state.endr] [state.endc]. Id = map [state.begr] [state.begc] .id; map [state.begr] [state.begc]. Id = NONE; map [state.endr] [state.endc]. IsRiver = map [state.begr] [state.begc] .isRiver Map [state.endr] [state.endc] .type = map [state.begr] [state.begc] .type;}
Finally, there is our main function, which is called to get the project running.
Int main () {/ / create graphics window initgraph (740,820); / / set background mode setbkmode (TRANSPARENT); / / paste chessboard IMAGE img_board; loadimage (& img_board, ". / res/ChessBoard.png"); init (); / / A pair of buffered drawings to prevent splash screen BeginBatchDraw () While (true) {cleardevice (); putimage (0,0, & img_board); draw (); mouseEvent (); FlushBatchDraw ();} EndBatchDraw (); getchar (); return 0 At this point, the study of "how C++ can realize Chinese Chess" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.