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++ to realize backgammon

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of how to use C++ to achieve Gobang game, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to achieve Gobang games with C++. Let's take a look.

Game description:

Let's start with something real! In fact, backgammon is the tic-tac-toe chess that we used to play when we were young. If one player achieves three horizontal or vertical, or diagonal three, it is a game victory!

(from scratch) the header file is also written in front: # include # define H 3max / row # define L 3max / column first write the game and the main function first: game function: void game () / / game implementation step {char gamemap [L] [H] = {0}; / two-dimensional array initgamemap (gamemap, H, L) for storing data; / / chessboard initialization showgamemap (gamemap, H, L) / / print out the chessboard char ret= 0; while (1) / / cycle chess {playgame1 (gamemap, H, L); / / the player plays chess showgamemap (gamemap, H, L); / / after the player has finished playing chess and prints out the chessboard ret=gameover (gamemap, H, L) / / judge whether the player wins if (ret! ='C') break; playgame2 (gamemap, H, L); / / computer plays chess showgamemap (gamemap, H, L); / / after the computer plays chess and prints out the chessboard ret=gameover (gamemap, H, L) / / judge whether the computer wins if (ret! ='C') break;} if (ret = ='X') {printf ("players win!") ;} else if (ret = ='O') {printf ("computer wins!") ;} else {printf ("draw");}} main function: int main (void) {Menu (); / / display game menu srand ((unsigned int) time (NULL)); int input = 0; do {printf ("Please enter your choice!") ; scanf_s ("% d", & input); switch (input) {case 1: game (); / / Game function to achieve backgammon! Break; case 0: printf ("quit the game!") ; break; default: printf ("input error, please re-enter!") ; break;}} while (input); return 0;} 1. Build the game menu

We use the Menu function with no range value type to represent the game menu, which can be read directly in the main function later.

Simple and unadorned printf, no feelings! Players enter 1 for playing the game, and enter 0 for quitting the game! I went to do my homework.

Void Menu () / / Game menu {printf ("* *\ n"); printf ("* 1.play *\ n"); printf ("* 0.exit *\ n") Printf ("* *\ n");} 2. Build a chessboard

2.1 Chessboard initialization

We use the initgame function of Void type to represent the initialization of the chessboard, and we use the space of the chessboard to represent the space.

Void initgamemap (char gamemap [L] [H], int l, int h) / / statement of the chessboard initialization function {int I = 0; int j = 0; for (I = 0; I

< H; i++)//H,J分别表示为行和列 { for (j = 0; j < L; j++) { gamemap[i][j] = ' '; } }} 我们可以选择直接打印出棋盘,但是这样只能把棋盘规定为3x3的,所以我们看第二种方法 void showgamemap(char gamemap[L][H], int l, int h)//直接打印出棋盘{ int i = 0; for (i = 0; i < H; i++) { printf("%c | %c | %c\n", gamemap[i][0], gamemap[i][1],gamemap[i][2]); if (i < L - 1) printf("---|---|---\n"); }}void showgamemap(char gamemap[L][H], int l, int h)//打印棋盘{ int i = 0; for (i = 0; i < H; i++) { int j = 0; for (j = 0; j < L; j++) { printf(" %c ", gamemap[i][j]); if (j < L - 1) printf("|"); } printf("\n"); if (i < H - 1) { int j = 0; for (j = 0; j < L; j++) { printf("---"); if (j < L - 1) printf("|"); } printf("\n"); } }}3.玩家和电脑下棋3.1玩家先下 我们默认为玩家先下棋! 我们定义使用playgame1(gamemap, H, L);函数来实现玩家下棋! oid playgame1(char gamemap[L][H], int l, int h)//玩家下棋{ int x = 0; int y = 0; printf("玩家先下!"); while (1) { printf("请输入下棋的坐标:>

"); scanf_s ("% d% d ", & x, & y); / / determine the validity of coordinates if (x > = 1 & & x = 1 & & y = 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: 267

*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