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 Gobang Game in C language

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to implement backgammon in C language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Train of thought:

Deployment of main function structure

The realization of game function

① creates storage space

② initializes the storage space as a space

③ print chessboard

④ enables players to play chess.

⑤ realizes computer playing chess

⑥ realizes the judgment between the player and the computer after playing chess.

Files used:

Test.c- is used to test games.

Game.c---- is used to implement some functions in the game.

Game.h- header file

Test.c

# define _ CRT_SECURE_NO_WARNINGS 1#include#include "game.h" # includevoid menu () {printf ("% s\ n", "*"); printf ("% s\ n", "* 1.play *"); printf ("% s\ n", "* 0.exit *"); printf ("% s\ n", "*") } void game () {/ / establish storage space-- two-dimensional array char ch [ROW] [COL] / / if we want to make the array space flexible, we need to make sure that the rows and columns are variable. The single array does not accept variables and can only use the constants defined by define-preferably in the header file-to change the header file to achieve flexible array space changes / / initialize storage space-display normal innit (ch,ROW,COL); / / print chessboard pri_board (ch,ROW,COL); char re='\ 0' / / receive the results of the game / / while (1) {/ / players play chess pla_move (ch,ROW,COL); pri_board (ch,ROW,COL); / / use functions to determine whether winning or losing re=is_win (ch,ROW,COL) is generated; / / as long as the result is not equal to "c", the game if (re! ='C') break is terminated directly. / / computer chess com_move (ch, ROW, COL); pri_board (ch, ROW, COL); / / use functions to determine whether to generate winning re=is_win (ch, ROW, COL); if (re! ='C') break;} if (re= ='*') printf ("players win! Else if (re = ='#') printf ("computer wins! Else printf ("draw!") \ n "); pri_board (ch, ROW, COL);} int main () {/ / srand function, the following com_move function uses (difficulty) srand ((unsigned int) time (NULL)); int input = 0; do {/ / print game menu prompts the player menu (); / / player selects printf (" Please choose >: "); scanf ("% d ", & input) / / A pair of players choose to implement switch (input) {case 1: game (); break; case 0: printf ("quit the game\ n"); break; default: printf ("input error, re-enter\ n"); break;}} while (input);}

Game.c

# define _ CRT_SECURE_NO_WARNINGS 1#include "game.h" void innit (char ch [ROW] [COL], int row, int col) {/ / traverse the two-dimensional array to change the value to a space to initialize int I = 0; int w = 0; for (I = 0; I

< row; i++) { for (w = 0; w < col; w++) { ch[i][w] = ' '; } }}void pri_board(char ch[ROW][COL],int row,int col){// int i = 0; int w = 0; //行的循环里有两个列的循环 //列的循环里有两个打印循环 for (i = 0; i < row; i++) { for (w = 0; w < col; w++) { printf(" %c ", ch[i][w]); if (w < col - 1) printf("|"); } printf("\n"); if (i < row - 1) { for (w = 0; w < col; w++) { printf("---"); if (w < col - 1) printf("|"); } printf("\n"); } } }void pla_move(char ch[ROW][COL], int row, int col){ while (1) { //玩家输入下棋坐标 int a = 0; int b = 0; printf("玩家走>

:\ n "); printf (" Please enter the pawn coordinates you want to play: "); scanf ("% d% d ", & a, & b); / / determine whether the coordinates are legal if ((a > = 1 & & a = 1 & & b = 2) return ch [Q] [I];}

The judgment of diagonals

/ / judge the diagonal (Gobang version) / / from the upper left corner to the lower right corner for (Q = 0; Q < row; QQ +) {for (I = 0; I < col; I +) {if (ch [Q] [I] = ='') {continue } else {if (ch [Q] [I] = = ch [Q + 1] [I + 1]) {if (ch [Q + 1] [I + 1] = = ch [Q + 2] [I + 2]) return ch [Q] [I];}} / / upper right corner to lower left corner for (Q = 0; Q < row; Q +) {for (I = 0; I < col ) {if (ch [Q] [I] = ='') {continue;} else {if (ch [Q] [I] = = ch [Q + 1] [I-1]) {if (ch [Q + 1] [I-1] = = ch [Q + 2] [I-2]) return ch [Q] [I];} thank you for reading! This is the end of the article on "how to realize Gobang in C language". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!

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