In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 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 use C language to achieve Gobang Mini 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 Mini Game in C language. Let's take a look.
Thought improvement and important code snippets
Why should be divided into three files to write: 1. It is easy to reuse code. The repetitive function can be written once, and the next time you want to use it in other programs, you only need to change a small part or you don't have to change it. two。 It is convenient for many people to cooperate. The tasks of each development department can be clearly assigned at the beginning of designing the software. The writer of the module himself only needs to pay attention to what he has written, understand the function of this part, and set aside the interface. In addition, for the person in charge of the whole project, it will be convenient to browse the overall work progress and arrange for the co-ordinators. 3. Easy to modify and maintain. If you can be sure that there is only a problem with a certain module, you can solve it in the module, and there is no need to move the whole body. To upgrade a certain part of the function, you can only redevelop for specific modules to save costs. This is not only a style, but also a good method for software design.
1. Play a game and then continue to judge whether you want to play it or not, often using the do...while () loop, which is similar to guessing numbers.
The code is as follows:
Int main () {int input = 0; srand ((unsigned int) time (NULL)); do {menu (); printf ("do you want to play? \ n "); scanf ("% d ", & input); switch (input) {case 1: game (); break; case 0: break Default: printf ("input error, re-enter\ n"); break;}} while (input); return 0;}
two。 Create a two-dimensional array, initialize the contents of the array to spaces, and then use special symbols to build the chessboard of 3x3
Initialize array
The code is as follows:
Void Initboard (char board [ROW] [COL], int row, int col) {int I = 0; int j = 0; for (I = 0; I
< row; i++) { for (j = 0; j < col; j++) { board[i][j] = ' '; } }} for循环嵌套for循环,有不太懂的小伙伴可以看这篇文章哦: for循环嵌套for循环 构建棋盘(for循环嵌套for循环) 代码如下: void Displayboard(char board[ROW][COL], int row, int col){ int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); if (i < row - 1) { for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) { printf("|"); } } printf("\n"); } }} 3. 玩家和电脑下完一步棋就要判断输赢,对于3x3的棋盘,行、列和对角线分开讨论,满足条件跳出循环,不满足继续下棋,直到棋盘已经满了 特别注意:用多个文件使用的好处,尝试多适应使用多个文件来完成代码,构建棋盘和判断输赢等代码要多动手来一步步的试错,错误都会成为养分。 三子棋代码 这是低配的三子棋,在下面的代码中,因为电脑产生的坐标是随机的,电脑下棋是非常随意的。 因此电脑想赢很困难。大家也可以尝试设计算法增加难度。 以下是低配三子棋的所有代码: test.c #include "game.h"void menu(){ printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"); printf("^^^^^^^^ 1.玩 ^^^^^^^^^\n"); printf("^^^^^^^^ 0.不玩 ^^^^^^^^^\n"); printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");}void game(){ //创建数组 char board[ROW][COL] = { 0 }; //初始化数组 Initboard(board, ROW, COL); //打印棋盘 Displayboard(board, ROW, COL); char ret = 0; while (1) { //玩家下棋 Playermove(board, ROW, COL); Displayboard(board, ROW, COL); ret=Iswin(board, ROW, COL); if (ret != 'C') break; //电脑下棋 Computermove(board, ROW, COL); Displayboard(board, ROW, COL); ret=Iswin(board, ROW, COL); if (ret != 'C') break; } if (ret == '*') printf("玩家赢\n"); else if (ret == '#') printf("电脑赢\n"); else printf("平局\n"); Displayboard(board, ROW, COL);}int main(){ int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("玩不玩?\n"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: break; default: printf("输入错误,重新输入\n"); break; } } while (input); return 0;} game.c #include "game.h"void Initboard(char board[ROW][COL], int row, int col){ int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { board[i][j] = ' '; } }}void Displayboard(char board[ROW][COL], int row, int col){ int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); if (i < row - 1) { for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) { printf("|"); } } printf("\n"); } }}void Playermove(char board[ROW][COL], int row, int col){ int x = 0; int y = 0; printf("玩家下棋\n"); while (1) { printf("请输入坐标:"); scanf("%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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.