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 use C language to realize backgammon

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

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use C language to achieve backgammon". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Backgammon

Everyone should have played backgammon when they were young. After learning the C language for such a long time, we already have the ability to do such a small project. Today, I will try my best to teach you how to achieve backgammon. If you read this blog, you can find it in the gitee warehouse link that I attached at last. But I still hope that everyone can finish it by themselves and show their own style in backgammon.

Create a project environment

First, the first step is to open our vs compiler and create a new backgammon project

Create three files in this new project

1.test.c

2.game.c

3.game.h

We put the function declaration and the introduction of the header file in the header file game.h.

Put some custom function definitions in game.c

Put the writing of the main function in test.c

Now directly above the contents of the header file, we will teach you step by step the reasons for implementing the interface in the header file and referencing the header file.

Contents of the header file # ifndef _ GAME_H__#define _ GAME_H__#include#include#include#include#define H 3#define L 3void menu (); void game (); void board (char arr [H] [L], int XMagi int y); void printfboard (char arr [H] [L], int x Magi int y); void play (char arr [H] [L], int x Magi int y); void computerplay (char arr [H] [L], int x line int y) Char win (char arr [H] [L], int xjinint y) # endif#ifndef # define # endif is to prevent repeated introduction of header files. In fact, the # pramga once that comes with when you create your own header files can also achieve the purpose. (test.c) write main functions (test.c) write test.c first step reference header file game.h, that is, # include "game.h" to play games must have a menu, so we need to print and write a menu The implementation of this menu function is done in test.c, but we need to declare void menu () in the header file first. Next, implement the menu void menu () {printf ("*\ n"); printf ("* * 1.play 0.exitually created *\ n"); printf ("*\ n") } We use the printf function. At this time, we need to reference the header file stdio.h with standard input and output streams, and then we add # include to game.h like this option 1 to start the game and choose to exit the program mode. We can directly use switch to implement more than one game. As long as the user does not choose 0, let the user play all the time, so we write the main function int main () {int input. Do {menu (); again: printf ("Please enter:"); scanf ("% d", & input); switch (input) {case 1: game (); break; case 0: printf ("quit the game\ n"); break; default: printf ("input error, please re-enter\ n"); goto again }} while (input); return 0;} in order to prevent user input errors, we directly quit the program, we use the features of goto to implement

Then we assemble it, and the text.c is finished.

# include "game.h" void menu () {printf ("*\ n"); printf ("* * 1.play 0.exit*\ n"); printf ("*\ n");} int main () {int input; do {menu () Again: printf ("Please enter:"); scanf ("% d", & input); switch (input) {case 1: game (); break; case 0: printf ("quit the game\ n"); break; default: printf ("input error, please re-enter\ n"); goto again;}} while (input); return 0 } implement each interface function

This process may be a little complicated, and you may need to experiment more.

The game function is the main function equivalent to game.c, and we need to build it step by step

1.board before playing three pieces, we should know what to use to carry the pieces, three rows and three columns of chess pieces, which correspond to the rows and columns of the two-dimensional array, so we use the two-dimensional array to do it in the game function. We can add int arr [3] [3]. But in the actual design program, people are not recommended to use numbers, there may be errors, and after defining three lines and three columns directly, we think it will be troublesome to transform Gobang into Gobang. At this time, we can use constants instead, so we should add these two lines of code in the header file, H for line, L for column # define H 3#define L 3void game () {char arr [H] [L]; board (arr,H,L) } the board function is used to initialize the chessboard, initializing it to empty space void board (char arr [H] [L], int x, int y) {int I, j; for (I = 0; I)

< x; i++) { for (j = 0; j < y; j++) { arr[i][j] = ' '; } }}2.printfboard 初始化之后,我们需要打印棋盘这个printfboard函数是用来打印棋盘的,这一步比较重要,也比较困难,这个需要大家多动手尝试,先给大家看看我们理想的棋盘效果

As we can see from the figure, we use | and-as delimiters

Void printfboard (char arr [H] [L], int x, int y) {for (int I = 0; I)

< x; i++) { for (int j = 0; j < y; j++) { printf(" %c ", arr[i][j]); if (j < y - 1) printf("|"); } printf("\n"); if (i < x - 1) { for (int j = 0; j < y; j++) { printf("---"); if (j < y - 1) printf("|"); } printf("\n"); } }}接下来讲game函数改造为void game(){char arr[H][L];board(arr,H,L);printfboard(arr,H,L);} 这个函数的实现并不简单,希望大家多多尝试 3.play 接下来我们开始下棋了,我们先人为固定,我们下的是‘*’,电脑下的是‘#’ 首先,用户并不知道数组下标是从零开始的,我们需要设计从1开始 其次,我们需要提醒用户的错误下棋,比如下在已有棋子的地方和下在棋盘以外 最后,我们需要提醒用户正确的下棋方法,防止出现程序错误 void play(char arr[H][L], int x,int y) { int i, j; again: printf("(提示,以x y的形式输入,x代表行,y代表列)请输入你要下棋的位置:"); scanf("%d %d", &i, &j); if (i >

= 1 & & I = 1 & & j 1; jcolor -) {if (arr [I] [j] = = arr [I + 1] [j-1] & & arr [I + 1] [j-1] = = arr [I + 2] [j-2] & & arr [I] [j]! =') return arr [I] [j] } for (I = 0; I

< x; i++) { for (j = 0; j < y; j++) { if (arr[i][j] == ' ') flag = 1; } } if (flag == 1) return ' '; else return '!';} 再将game进行改造 void game() { char arr[H][L] = { 0 }; char ret; board(arr, H, L); printfboard(arr, H, L); while (1) { play(arr, H, L); ret = win(arr, H, L); if (ret != ' ') break; printf("电脑正在下棋请稍等\n"); Sleep(1000); computerplay(arr, H, L); ret = win(arr, H, L); if (ret != ' ') break; } if (ret == '*') printf("恭喜你获胜了\n"); else if (ret == '#') printf("很遗憾你输了\n"); else printf("势均力敌,再接再厉\n");} 然后将game.c拼装在一起就完事了 #include"game.h"void board(char arr[H][L], int x, int y) { int i, j; for (i = 0; i < x; i++) { for (j = 0; j < y; j++) { arr[i][j] = ' '; } }}void printfboard(char arr[H][L], int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { printf(" %c ", arr[i][j]); if (j < y - 1) printf("|"); } printf("\n"); if (i < x - 1) { for (int j = 0; j < y; j++) { printf("---"); if (j < y - 1) printf("|"); } printf("\n"); } }}void play(char arr[H][L], int x,int y) { int i, j; again: printf("(提示,以x y的形式输入,x代表行,y代表列)请输入你要下棋的位置:"); scanf("%d %d", &i, &j); if (i >

= 1 & & I = 1 & & j 1; jcolor -) {if (arr [I] [j] = = arr [I + 1] [j-1] & & arr [I + 1] [j-1] = = arr [I + 2] [j-2] & & arr [I] [j]! =') return arr [I] [j] } for (I = 0; I < x; iTunes +) {for (j = 0; j < y; jacks +) {if (arr [I] [j] = =') flag = 1;} if (flag = = 1) return'; else return'!' } void game () {char arr [H] [L] = {0}; char ret; board (arr, H, L); printfboard (arr, H, L); while (1) {play (arr, H, L); ret = win (arr, H, L); if (ret! =') break; printf ("Please wait a moment while the computer is playing chess") Sleep (1000); computerplay (arr, H, L); ret = win (arr, H, L); if (ret! =') break;} if (ret = ='*') printf ("Congratulations on winning\ n"); else if (ret = ='#') printf ("I'm sorry you lost\ n") Else printf ("even strength, keep up your efforts");} "how to use C language to achieve backgammon" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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