Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize backgammon in C language

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how to realize Gobang in C language, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to realize Gobang in C language. let's take a look.

1. What is Gobang? 1.1 Baidu encyclopedia

Backgammon is a kind of black and white chess. Gobang is a traditional folk game, also known as Jiugong Chess, Circle Fork, one-stop, Jingzi Chess and so on. Connect the diagonal of the square and put three pieces on both sides in turn. As long as you walk the three pieces into a line, the other side loses. However, there are many times when there is a draw.

1.2 preparation for game programming

By looking at the picture above, Gobang is played in a well-shaped or nine-grid chessboard. Therefore, we can start by printing a chessboard. For convenience, we require the output of the following nine-grid chessboard.

With reference to the rules of backgammon, make a preliminary programming idea:

1. Output some symbols and text before playing the game to make the interface more ceremonial, such as:

Printf ("*\ n"); printf ("* 1. Start the game *\ n"); printf ("* 0. Quit the game *\ n"); printf ("*\ n")

2. Prompt players to choose:

Enter 1 for playing games

Enter 0 to quit the game

Enter something else to prompt the input error, which requires the player to re-enter

3. Initialize the chessboard before playing chess and print out the chessboard

4. After playing chess, the player prints out the chessboard again.

5. After the computer plays chess, print the chessboard again

6. after a few steps in this cycle, determine who the winner is, and the chess game is over.

7. Players can choose to continue to play or quit the program.

two。 Program implementation 2.1 build a program framework

We first build the program framework so that the program can run, and then modify the program to achieve the basic functions.

Because the game is played many times, choose the do-while loop structure and write the program theme structure.

Void menu () {/ / output prompt characters printf ("*\ n"); printf ("* 1. Start the game *\ n "); printf (" * 0. Quit the game *\ n "); printf (" *\ n ");} void play () {/ / the code to play the game printf (" players choose to play the game! \ n ");} int main () {/ / using the do-while loop, write the body structure int input = 0; do {menu (); / / output the prompt message printf (" Please select 1 or zero = > "); / / 1 means to play, and 0 means to quit the game scanf ("% d ", & input) / / the player enters switch (input) {/ / decide what the input is through the branch structure, case 1 case case / play the game play (); / / call the function break that plays the game Case 0Plux / quit the game printf ("quit the game! \ n "); break; default:// re-enter printf (" input error, please re-enter 1 or 0\ n "); break } / / enter the loop output prompt message first, when enter 1, meet the loop condition, and then execute the program in the loop, that is, play the game / / when you enter 0, it does not meet the loop condition, that is, exit the game / / use the do-while loop to match the logic} while (input); return 0;}

Run the program, input 120 respectively, and the output results are as follows, achieving the desired interface effect:

Enter 1 for playing games

Enter something else to prompt the input error, which requires the player to re-enter

Enter 0 to quit the game

2.2 Modular programming

The code in the above section builds the framework of the program, which can realize the basic functions of the program. We then add code to the play function to improve the function of playing games.

Because there is more code, modular programming will be used, which makes the code a little more readable. Put the function implementation of different functions in different source files.

2.2.1 Source file test.c

Put the functions main, test, menu, play into the source file test.c, here is only a preliminary plan, the code will gradually increase with the implementation of the function.

/ / will be gradually added later, does not mean that only these # include "play.h" / / header files must be added, printf and other functions work normally void menu () {/ / output prompt characters printf ("*\ n"); printf ("* 1. Start the game *\ n "); printf (" * 0. Quit the game *\ n); printf ("*\ n");} void play () {/ / the specific implementation code for playing the game printf ("players choose to play the game!" \ n "); / / stores data for chess char board [ROW] [COL] = {0}; / / initializes the chessboard as full-space InitBoard (board, ROW, COL);} void test () {/ / game interface code int input = 0; do {menu (); printf (" Please select 1 or zero = > ") Scanf ("% d", & input); switch (input) {case 1: play (); break; case 0: printf ("quit the game! Break; default: printf ("input error, please re-enter 1 or 0\ n"); break;}} while (input);} int main () {test (); return 0;} 2.2.2 Source File play.c

Initialize the function of the chessboard, print the chessboard, players play chess, and so on into the source file play.c, here is only a preliminary plan, the code will gradually increase with the implementation of the function.

/ / it will be added step by step later, which does not mean that only these # include "play.h" / / header files must be added, including stdio.h Make printf and other functions work properly / / initialize chessboard void InitBoard (char board [ROW] [COL], int row, int col) {} / / print chessboard void DisplayBoard (char board [ROW] [COL], int row, int col) {} / / players play chess void PlayMover (char board [ROW] [COL], int row, int col) {} / / computer chess void ComputerMove (char board [ROW] [COL], int row, int col) {} 2.2.3 header files play.h

Put the macro definition and function declaration in the header file play.h, here is only a preliminary plan, the code will gradually increase with the implementation of the function.

/ / will be gradually added later, does not mean that only these # include// header files # define ROW 3gamble / number of chessboard rows # define COL 3gamble / number of chessboard rows void play (); / initialize chessboard void InitBoard (char board [ROW] [COL], int row, int col); / / print chessboard void DisplayBoard (char board [ROW] [COL], int row, int col); / / player chess void PlayMover (char board [ROW] [COL], int row, int col) / / computer chess void ComputerMove (char board [ROW] [COL], int row, int col); 2.3 Program implementation-expand play function 2.3.1 Chessboard initialization and printing function

There are two things that need to be done before players play chess.

Use the function InitBoard: initialize the chessboard, initialize it with spaces

Use the function DisplayBoard: print out the chessboard.

/ / test.cvoid play () {printf ("players choose to play games! \ n "); / / data stored in chess ROW represents the number of chessboard rows, COL represents the number of chessboard rows char board [ROW] [COL] = {0}; / / initializes the chessboard to full space InitBoard (board, ROW, COL); / / displays the chessboard and prints DisplayBoard (board, ROW, COL) } / / play.cvoid InitBoard (char board [ROW] [COL], int row, int col) {int I = 0; for (I = 0; I < row; iTunes +) {int j = 0; for (j = 0; j < col; jacks +) {board [I] [j] ='' / / assign spaces to format} / / first draft void DisplayBoard (char board [ROW] [COL], int row, int col) {int I = 0; for (I = 0; I < row; iTunes +) {int j = 0; for (j = 0; j < col) ) {/ / the array is full of spaces printf ("% c", board [I] [j]); / / spaces are invisible} printf ("\ n");}}

The running results show that the function InitBoard is initialized with spaces, but the direct printing of spaces is blank and invisible, so it is necessary to modify the print function DisplayBoard.

2.3.1.1 print function DisplayBoard-- Modification 1

/ / second draft void DisplayBoard (char board [ROW] [COL], int row, int col) {int I = 0; for (I = 0; I < row; iTunes +) {printf ("% c |% c |% c\ n", board [I] [0], board [I] [1], board [I] [2]) / / print the delimiter printf ("- |-- |--\ n");} ruxiat}

The printed result is shown above, which is basically a nine-palace checkered chessboard. But the characters on the third line are redundant and need to be modified again.

2.3.1.2 print function DisplayBoard-- Modification 2

/ / third draft void DisplayBoard (char board [ROW] [COL], int row, int col) {int I = 0; for (I = 0; I < row) Printf ("% c |% c |% c\ n", board [I] [0], board [I] [1], board [I] [2]); / / print delimiter if (I = 1 & & x = 1 & y)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report