current position:Home>Smart three-piece - nanny-level teaching.
Smart three-piece - nanny-level teaching.
2022-08-06 09:25:37【ex-boyfriend in C】
目录
序言:
Presumably everyone has played three pieces of chess,I recall drawing lines with stones on the ground when I was a child,Some use stones,Some use branches,Play three pieces,Can play all day.Looking back now is full of memories.Bring it with you todayC语言,To realize the simulation to realize the game of three-bang,Let me help you find your childhood memories.下面进入正题:
一.整体思路
The principle of our three-piece chess is mainly realized,Print out a chessboard using characters,Use a two-dimensional array to store the pieces we play.Let me show you what it is.
大致就是这个样子.
Then play chess step by step,每下一步,Let's just print the chessboard aside.
二.加载逻辑
First of all we need a menu every time the game starts:
菜单代码:
void menu()
{
printf("*****************************************\n");
printf("************1.play 0.exit*************\n");
printf("*****************************************\n");
}
游戏的加载逻辑,我们希望可以选择游戏开始或者退出,也不是玩完一局就没有了,而是玩完一局以后还可以继续选择玩或者不玩,只有我们选组退出时,程序才会结束.
代码:
void game()
{
printf("三子棋\n");
}
int main()
{
int input = 0;
do
{
menu();
printf("请输入选项:>\n");
scanf("%d", &input);
printf("\n");
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入:>\n");
break;
}
} while (input);
return 0;
}
运行效果:
After setting the loading logic of the game,The rest of the game is all theregame()函数里面完成.And our order is program modularization,We put the main function in main.c文件里面,函数声明,头文件,宏等,我们就放在game.h文件里面,The specific implementation of the function is placed here,game.c文件里面.This way our overall program will be very orderly,可读性提高.
三.Checkerboard layout
We use macro constants,Specifies the size of the chessboard :
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 3
#define LIN 3
#include<stdio.h>
void menu();
void game();
void game()
{
//创建二维数组
char chess[ROW][LIN] = { 0 };
//Initialize the two-dimensional array used to hold the pieces
init_board(chess,ROW,LIN);
//打印棋盘
print_board(chess,ROW,LIN);
}
void init_board(char chess[ROW][LIN], int row, int lin)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < lin; j++)
{
//Two-dimensional arrays are all initialized with spaces
chess[i][j] = ' ';
}
}
}
void print_board(char chess[ROW][LIN],int row,int lin)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < lin; j++)
{
//空格 + 棋子 + 空格 + | ,为一组,一共
//需要 lin 组,But the last group doesn't need it |
printf(" %c ",chess[i][j]);
//Control the last one | 不打印
if (j < row - 1)
{
printf("|");
}
}
//换行
printf("\n");
// | |
//---|---|---
//为一组,一共需要 row 组,But the last group,不需要 ---|---|---
if (i < lin - 1)
{
for (int j = 0; j < row; j++)
{
// ---| 为一组,一共 row 组,The last group does not need to be printed |
printf("---");
if (j < row - 1)
{
printf("|");
}
}
}
printf("\n");
}
}
运行效果:
四.下棋
The main logic implementation of chess is,Encapsulates two functionsplayer_chess()Used to indicate that the player plays chess and the computer plays chesscomputer_board(),玩家输入坐标,And store the pieces represented by the player into the two-dimensional array of the chessboard,Before the computer plays chess, set him to play chess randomly,Then every time a piece is played we print the entire board.We need to keep playing chess,For every piece played, there is a possibility of a winning side,封装一个函数is_win()Used to determine whether someone has won again,Therefore, it is necessary to judge whether the player has won again every time a piece is played,If you win the player again, go straightis_winReturn directly to the winner's pawn,Return if no one wins‘C’代表继续,If the board is full,就返回‘Q’代表平局.
void game()
{
char set;
//随机数生成器
srand((unsigned int)time(NULL));
//创建二维数组
char chess[ROW][LIN] = { 0 };
//Initialize the two-dimensional array used to hold the pieces
init_board(chess,ROW,LIN);
//打印棋盘
print_board(chess,ROW,LIN);
while (1)//一直循环,until the result shows a winner or loser,或者平局
{
//玩家下棋
player_chess(chess, ROW, LIN );
//打印棋盘
print_board(chess, ROW, LIN);
//判断结果
set=is_win(chess,ROW,LIN);
if ('C' != set)
{
break;
}
//电脑下棋
computer_board(chess, ROW, LIN);
//打印棋盘
print_board(chess, ROW, LIN);
//判断结果
set = is_win(chess, ROW, LIN);
if ('C' != set)
{
break;
}
}
//If the pawn represented by the player is returned,
if (set == '*')
{
printf("恭喜你,你赢了!!\n");
}
//If it returns the pawn represented by the computer
else if ('#' == set)
{
printf("电脑赢了!!\n");
}
//如果是平局
else if ('Q' == set)
{
printf("平局\n");
}
}
void player_chess(char chess[ROW][LIN], int row, int lin)
{
//Count the number of player pieces
count_board++;
//坐标
int x = 0;
int y = 0;
while (1)
{
printf("请输入棋子坐标:>");
scanf("%d %d", &x, &y);
//判断坐标合理性,是否在棋盘内
if (x >= 1 && x <= row && y >= 1 && y <= lin)
{
//Whether the coordinate location is empty
if (chess[x-1][y-1] == ' ')
{
chess[x-1][y-1] = '*';
break;
}
else
{
printf("该位置已有棋子\n");
}
}
else
{
printf("The coordinates of the pieces are wrong,请重新输入\n");
}
}
}
void computer_board(char chess[ROW][LIN], int row, int lin)
{
while (1)//Until the location is found, the cycle ends.
{
//随机数生成,0~2
int x = rand()%row;
int y = rand()%lin;
//Determine if the location is empty,
if (chess[x][y] == ' ')
{
chess[x][y] = '#';
break;
}
}
}
Determine if a player winsis_win()
//判断棋盘是否满了
int is_full(char chess[ROW][LIN], int row, int lin)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < lin; j++)
{
if (chess[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char is_win(char chess[ROW][LIN], int row, int lin)
{
//Check sideways to see if there are three of them the same
for (int i = 0; i < row; i++)
{
if (chess[i][0] == chess[i][1] &&
chess[i][1] == chess[i][2] &&
chess[i][0]!=' ')
{
return chess[i][0];
}
}
//Vertically judge whether there are three the same
for (int i = 0; i < lin; i++)
{
if (chess[0][i] == chess[1][i] &&
chess[1][i] == chess[2][i] &&
chess[0][i] != ' ')
{
return chess[0][i];
}
}
//Slant to determine whether there are three of the same
if (chess[0][0] == chess[1][1] &&
chess[1][1] == chess[2][2] &&
chess[0][0] != ' ')
{
return chess[0][0];
}
//Slant to determine whether there are three of the same
if (chess[0][2] == chess[1][1] &&
chess[1][1] == chess[2][0] &&
chess[0][2] != ' ')
{
return chess[0][2];
}
//判断棋盘是否满了
int tmp = is_full(chess, ROW, LIN);
if (tmp == 1)
{
return 'Q';
}
return 'C';
}
In this way, the main logic of our three-piece chess is realized
五.智能化
But everyone found this computer stupid,Now let's give him intelligence,But the level of intelligence is up to the designer.Analysis of those situations is a priority,It's like winning when your computer is one piece away,This is the first consideration,The second is when the opponent is one piece short of winning,There is also a situation where the computer can achieve a two-piece lead with the next one,Wait, there are still some situations that the editor can't analyze,就不多介绍了.
void computer_board(char chess[ROW][LIN], int row, int lin)
{
//Just one pawn,priority(1)
for (int i = 0; i < row; i++)
{
if (chess[i][0] == chess[i][1] && chess[i][0] == '#'&& chess[i][2] == ' ')
{
chess[i][2] = '#';
return;
}
if (chess[i][0] == chess[i][2] && chess[i][0] == '#'&& chess[i][1] == ' ')
{
chess[i][1] = '#';
return;
}
if (chess[i][1] == chess[i][2] && chess[i][1] == '#'&& chess[i][0] == ' ')
{
chess[i][0] = '#';
return;
}
}
//Just one pawn,priority(2)
for (int i = 0; i < lin; i++)
{
if (chess[0][i] == chess[1][i] && chess[0][i] == '#'&& chess[2][i]==' ')
{
chess[2][i] = '#';
return;
}
if (chess[0][i] == chess[2][i] && chess[0][i] == '#'&&chess[1][i] == ' ')
{
chess[1][i] = '#';
return;
}
if (chess[1][i] == chess[2][i] && chess[1][i] == '#'&& chess[0][i] == ' ')
{
chess[0][i] = '#';
return;
}
}
//Just one pawn,priority(3)
if (chess[0][0] == chess[1][1] && chess[0][0] == '#' && chess[2][2] == ' ')
{
chess[2][2] = '#';
return;
}
if (chess[0][0] == chess[2][2] && chess[0][0] == '#' && chess[1][1] == ' ')
{
chess[1][1] = '#';
return;
}
if (chess[2][2] == chess[1][1] && chess[1][1] == '#' && chess[0][0] == ' ')
{
chess[0][0] = '#';
return;
}
//Just one pawn,priority(4)
if (chess[0][2] == chess[1][1] && chess[0][2] == '#' && chess[2][0] == ' ')
{
chess[2][0] = '#';
return;
}
if (chess[0][2] == chess[2][0] && chess[0][2] == '#' && chess[1][1] == ' ')
{
chess[1][1] = '#';
return;
}
if (chess[2][0] == chess[1][1] && chess[1][1] == '#' && chess[0][2] == ' ')
{
chess[0][2] = '#';
return;
}
//Victory against one pawn,priority(5)
for (int i = 0; i < row; i++)
{
if (chess[i][0] == chess[i][1] && chess[i][0] == '*' && chess[i][2] == ' ')
{
chess[i][2] = '#';
return;
}
if (chess[i][0] == chess[i][2] && chess[i][0] == '*' && chess[i][1] == ' ')
{
chess[i][1] = '#';
return;
}
if (chess[i][1] == chess[i][2] && chess[i][1] == '*' && chess[i][0] == ' ')
{
chess[i][0] = '#';
return;
}
}
//Victory against one pawn,priority(6)
for (int i = 0; i < lin; i++)
{
if (chess[0][i] == chess[1][i] && chess[0][i] == '*' && chess[2][i] == ' ')
{
chess[2][i] = '#';
return;
}
if (chess[0][i] == chess[2][i] && chess[0][i] == '*' && chess[1][i] == ' ')
{
chess[1][i] = '#';
return;
}
if (chess[1][i] == chess[2][i] && chess[1][i] == '*' && chess[0][i] == ' ')
{
chess[0][i] = '#';
return;
}
}
//Victory against one pawn,priority(7)
if (chess[0][0] == chess[1][1] && chess[0][0] == '*' && chess[2][2] == ' ')
{
chess[2][2] = '#';
return;
}
if (chess[0][0] == chess[2][2] && chess[0][0] == '*' && chess[1][1] == ' ')
{
chess[1][1] = '#';
return;
}
if (chess[2][2] == chess[1][1] && chess[1][1] == '*' && chess[0][0] == ' ')
{
chess[0][0] = '#';
return;
}
//Victory against one pawn,priority(8)
if (chess[0][2] == chess[1][1] && chess[0][2] == '*' && chess[2][0] == ' ')
{
chess[2][0] = '#';
return;
}
if (chess[0][2] == chess[2][0] && chess[0][2] == '*' && chess[1][1] == ' ')
{
chess[1][1] = '#';
return;
}
if (chess[2][0] == chess[1][1] && chess[1][1] == '*' && chess[0][2] == ' ')
{
chess[0][2] = '#';
return;
}
//抢占先机,Finish this chess piece,One step closer to victory;(1)
for (int i = 0; i < lin; i++)
{
if (chess[0][i] == '#' && chess[1][i] == ' ' && chess[2][i] == ' ')
{
chess[1][i] = '#';
return;
}
if (chess[1][i] == '#' && chess[0][i] == ' ' && chess[2][i] == ' ')
{
chess[0][i] = '#';
return;
}
if (chess[2][i] == '#' && chess[0][i] == ' ' && chess[1][i] == ' ')
{
chess[0][i] = '#';
return;
}
}
//抢占先机,Finish this chess piece,One step closer to victory;(2)
for (int i = 0; i < row; i++)
{
if (chess[i][0] == '#' && chess[i][1] == ' ' && chess[i][2] == ' ')
{
chess[i][1] = '#';
return;
}
if (chess[i][1] == '#' && chess[i][0] == ' ' && chess[i][2] == ' ')
{
chess[i][0] = '#';
return;
}
if (chess[i][2] == '#' && chess[i][0] == ' ' && chess[i][1] == ' ')
{
chess[i][0] = '#';
return;
}
}
//抢占先机,Finish this chess piece,One step closer to victory;(3)
if (chess[0][0] == '#' && chess[1][1] == ' ' && chess[2][2] == ' ')
{
chess[1][1] = '#';
return;
}
if (chess[0][0] == ' ' && chess[1][1] == '#' && chess[2][2] == ' ')
{
chess[2][2] = '#';
return;
}
if (chess[0][0] == ' ' && chess[1][1] == ' ' && chess[2][2] == '#')
{
chess[0][0] = '#';
return;
}
//抢占先机,Finish this chess piece,One step closer to victory;(4)
if (chess[0][2] == '#' && chess[1][1] == ' ' && chess[2][0] == ' ')
{
chess[1][1] = '#';
return;
}
if (chess[0][2] == ' ' && chess[1][1] == '#' && chess[2][0] == ' ')
{
chess[0][2] = '#';
return;
}
if (chess[0][2] == ' ' && chess[1][1] == ' ' && chess[2][0] == '#')
{
chess[0][2] = '#';
return;
}
//The first chess piece,针对
if (count_board == 1 && chess[0][0] == '*')
{
chess[2][2] = '#';
return;
}
if (count_board == 1 && chess[0][2] == '*')
{
chess[2][0] = '#';
return;
}
if (count_board == 1 && chess[2][0] == '*')
{
chess[0][2] = '#';
return;
}
if (count_board == 1 && chess[2][2] == '*')
{
chess[0][0] = '#';
return;
}
while (1)
{
int x = rand()%row;
int y = rand()%lin;
if (chess[x][y] == ' ')
{
chess[x][y] = '#';
break;
}
}
}
最后展示成果:
I've already lost here,哈哈哈.
最后:
Perseverance means not being afraid of suffering.Suffering is the whetstone of success,It's the courage of people、A test of wisdom and perseverance.way of life The road has not been smooth sailing,往往荆棘丛生.Many people just can't get past this hurdle,害怕、退缩、放弃、变向,结果只 Can miss success.Try to be the kind of person you like best,就算不成功,At least you'll like yourself working so hard.加油,诸君,山顶见!!!
copyright notice
author[ex-boyfriend in C],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/218/202208060919556850.html
The sidebar is recommended
- Detailed explanation of Mysql things (important)
- Linux - several ways to install MySQL
- /var/log/messages is empty
- The 22nd day of the special assault version of the sword offer
- Stone Atom Technology officially joined the openGauss community
- 18 days (link aggregation of configuration, the working process of the VRRP, IPV6 configuration)
- From "prairie cattle" to "digital cattle": Mengniu's digital transformation!
- Summary of the experience of project operation and maintenance work
- WPF - Styles and Templates
- BigEvent Demo
guess what you like
rain cloud animation
VS namespace names of different projects of the same solution are unique
Flashing Neon Text Animation
ACM common header files
Free and open source web version of Xshell [Happy New Year to everyone]
Timed task appears A component required a bean named ‘xxx‘ that could not be found
Two important self-learning functions in pytorch dir(); help()
[Mathematical Modeling] Linear Programming
Folyd
【Untitled】
Random recommended
- HCIP 18 days notes
- The web version of Xshell supports FTP connection and SFTP connection
- The values in the array into another array, and capital
- Remember to deduplicate es6 Set to implement common menus
- View the Linux log on the web side, and view the Linux log on the web side
- 21-day Learning Challenge--Pick-in on the third day (dynamically change the app icon)
- Xshell download crack, the history of the most simple tutorial
- How is the LinkedList added?
- Web version Xshell supports FTP connection and SFTP connection [Detailed tutorial] Continue from the previous article
- Usage of torch.utils.data in pytorch ---- Loading Data
- Experiment 9 (Exchange Comprehensive Experiment)
- [Mathematical Modeling] Integer Programming
- "Introduction to nlp + actual combat: Chapter 9: Recurrent Neural Network"
- Expansion mechanism of ArrayList
- (5) BuyFigrines Hd 2022 school training
- [Nanny-level tutorial] How does Tencent Cloud obtain secretId and secretKey, and enable face service
- RL reinforcement learning summary (2)
- ELT.zip 】 【 OpenHarmony chew club - the methodology of academic research paper precipitation series
- Hdu 2022 Multi-School Training (5) Slipper
- GEE(9): Area area statistics (using connectedPixelCount and ee.Image.pixelArea())
- Hdu2022 Multi-School Training (5) BBQ
- ACM common template directory
- SPFA Template
- Dijkstr heap optimization
- Looking back at ResNet - a key step in the history of deep learning
- jupyter notebook & pycharm (anaconda)
- Hongke Sharing|How to ensure the security of medical data?Moving target defense technology gives you satisfactory answers
- Let's talk about the pits of mysql's unique index, why does it still generate duplicate data?
- C. Robot in a Hallway (recursion/prefix sum/dynamic programming)
- PHP online examination system 4.0 version source code computer + mobile terminal
- Minesweeper implemented in C language
- A. Two 0-1 Sequences (greedy)
- E. Count Seconds (DAG/topological sort/tree dp)
- C. Virus (greedy)
- Domain name authorization verification system v1.0.6 open source version website source code
- F. Colouring Game (game theory/sg function)
- White, concise and easy company website source WordPress theme 2 or more
- [mysql chapter - advanced chapter] index
- B. Luke is a Foodie (greedy/simulation)
- grpc uses consul for service registration and discovery