In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to achieve backgammon Mini Game with C language". In daily operation, I believe many people have doubts about how to realize backgammon Mini Game with C language. 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 language to achieve backgammon Mini Game". Next, please follow the editor to study!
① game effect
There is a 3-to-3 chessboard
Every time after playing chess (computer and player), the chessboard will update the data.
There will be a menu prompting relevant commands to do relevant operations.
The way to play chess is to enter coordinates.
At the end of each game, you will be asked if you want to continue.
Logic Construction of ② main function
The most important thing for the implementation of any project is to build the logical structure, and the realization of each function is subdivided by function (modularization).
So we need to build logic, so what is the logical structure?
First of all, it will jump out of the prompt menu, the content is 0 to exit the game, 1 to start / continue the game, other keys prompt error, and re-enter
Press the prompt after 0 to "quit the game successfully"
Press 1 to start the game
# include int main () {int input = 0; do {remind (); / / prompt menu scanf ("% d", & input); / / start entering the command switch (input) {case 0: printf ("quit the game"); break; case 1: playgame (); / / start the game printf ("whether to continue the game"); break Default: printf ("command error, please reselect"); break;}} while (input); return 0;} ③ game module (playgame) logic building
The previous logical structure has been built, and what we need to implement is the playgame () module.
So what is his functional logic?
Chessboard contents are stored in a two-dimensional array
Initialize the chessboard, ie there are no descendants on the chessboard. So initialization is a separate module.
The game is divided into two modules (computer and player).
After each game, it is necessary to judge whether the computer or the player wins. (so it is a separate module to judge whether to win or lose.)
# define row 3#define col 3void playgame () {char board [row] [col]; / store the sub-content int result = 0; / / receive the return value Initboard (board, row, col) determined by the Iswin function; / / initialize the chessboard, that is, the chessboard does not have a sub-Displayboard (board, row, col) at the beginning / / display the chessboard, that is, show the current state of the chessboard while (1) / / start the game until one side wins or the draw ends the cycle {player (board, row, col); / / the player's next subfunction if ((result = Iswin (board, row, col)) = = 1) break; / / Iswin function is set to return 1 computer (board, row, col) if the player wins / / the subfunction if ((result = Iswin (board, row, col)) =-1) break; / / Iswin function is set to return-1 if (! result) break; / / if the computer wins, and if there is neither a win nor a draw, continue to play if (result = = 1) printf ("Congratulations on your victory!") Else if (result =-1) printf ("you are too hot for the computer to win"); else printf ("Ah ~ ~, your technology is not good enough to tie with the computer");}
Explanation:
The logical structure of the playgame () function can be clearly understood.
One of the steps is if (Iswin (board, row, col) > = 0). Its execution is to call the function first (to meet the requirement that we need to judge who wins), and then use the return result to determine whether to end the cycle. One program achieves two effects, which is better.
Concrete implementation of ④ Branch Module
0.remind function implementation
Just play the role of a hint, just print directly.
Void remind () {printf ("+-+"+-0 end the game-+" +-1 start / continue Game-+ "+-+") }
1.Initboard function implementation
Since it is an initialization function, our requirement is to initialize the two-dimensional array board to full spaces.
Void Initboard (char (* board) [3], int Row,int Col) {int I = 0, j = 0; for (I = 0; I
< Row; i++) { for (j = 0; j < Col; j++) { board[i][j] = " "; // 赋予空格 } }} 2.Displayboard函数实现 显示棋盘内容显示的不止是二维数组board的内容,还要加上边界(如图),所以Displayboard的要求是什么?Display both the boundary and the storage content of the board.
On the other hand, the display boundary can be divided into small blocks and displayed in a loop, such as three bars plus the last lower boundary.
Each bar can be seen as three small squares plus a small right boundary.
Void Displayboard (char (* board) [3], int Row, int Col) {int I = 0, j = 0; for (I = 0; I < Row; iTunes +) {printf ("+-- +"); / / print the upper boundary of the bar for (j = 0; j < Col; jacks +) {printf ("|% c", board [I] [j]); / / print the contents of the squares} printf ("|") / / add the small right boundary} printf ("+-- +"); / / fill the upper and lower boundary}
3.player function implementation
Players use coordinate input to play chess.
The player enters a coordinate, then the corresponding position on the chessboard should be updated. (the corresponding position is indicated by *)
When entering coordinates, you also need to check whether the location has been dropped.
Void player (char (* board) [3], int Row, int Col) {printf ("Please enter horizontal and vertical coordinates separately (please separate the input data with a space):"); scanf ("% d% d", & Row, & Col); while (1) {if (1)
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.