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

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article Xiaobian for you to introduce in detail "C language how to achieve a simple Gobang game", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to achieve a simple Gobang game in C language" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

The realization of chessboard

As we all know, the three-player chessboard is actually a nine-square, so we first have to define a two-dimensional array to store the pieces. We stipulate that each box occupies three small boxes such as'X', and there must be a separator to separate the pieces.

Initialization of chessboard

At the beginning of the game, the chessboard must be empty, so we first initialize the array, set each element to'', and then realize the function of the chessboard. The initialization code of the chessboard is as follows

Void InitBoard (char board [ROW] [COL], int row, int col) {for (int I = 0; I)

< row; i++) { for (int j = 0; j < col; j++) { board[i][j] = ' '; } }}棋盘 棋盘是一个二维数组,且每一个元素都要用分隔符来分隔它们,横行用' | ',竖列用'-'。一行有三个元素所以用两个' | ',有三列所以用两个'-'。而且在'-'所占的一行也要加' | '。棋盘的实现代码如下: void ShowBoard(char board[ROW][COL], int row, int col){ printf("=====================\n"); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { //三个空格 printf(" %c ", board[i][j]); //两列竖线 if (j < col - 1) { printf("|"); } } //换行 printf("\n"); //横线只有2行 if (i < row - 1) { for (int j = 0; j < col; j++) { printf("---"); // if (j < col - 1) { printf("|"); } } printf("\n"); } } printf("=====================\n");} 这两步完成后,棋盘的实现就完成了 落子的实现 棋盘实现后,接下来就是落子,我们得写两个函数:一个是玩家落子,一个是电脑落子。 玩家落子 玩家落子通过数组元素的赋值来实现,通过赋值我们可以把' '换成'X'(我规定玩家落子为'X'),而且对玩家落子有个规定只能填1~3的数,超过规定我们就要提示玩家落子不规范,玩家落子还不能落在数组不为' '的地方,玩家落在数组不为' '的地方,我们要提示玩家正确落子。以上这些,我们要写一个while循环来让玩家正确落子,再在循环写一个if判断语句以此来判断玩家是否正确落子 ,并且玩家没有正确落子这个循环就一直存在,在代码如下: 下面展示一些 内联代码片。 void playermove(char board[ROW][COL], int row, int col){ while (1) { printf("请输入你的坐标:\n"); int x = 0; int y = 0; scanf_s("%d%d", &x,&y); if (x >

= 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