当前位置:网站首页>"C language game" entry-level chess game (robot enhanced version)
"C language game" entry-level chess game (robot enhanced version)
2022-07-31 07:58:00 【Guo Guo study tour】
目录
Today I bring you a very simple little game“三子棋”
First of all the rules are simple,A row or a column or a positive and negative diagonal is the same child to win,管你听没听懂,看就完了~
Let's analyze the idea first Then combine the ideas as a whole Get all the modules you need first
打印界面
First, we need to create an interface
Then it is very simple 直接上代码 No difficulty whatsoever We wrap it in a function
void menu() {
printf("开始游戏\n");
printf("***********************************\n");
printf("********** 1.play **********\n");
printf("********** 0.txit **********\n");
printf("***********************************\n");
printf("请选择>>\n");
}
初始化数组的值
First we are going to print out an interface
And when we use this kind of rectangle, we first consider using a two-dimensional array,So we have to first define a two-digit array and initialize the two-dimensional array ,And this grid can be divided into several parts to view
First we consider the red area blocks separately
void display_board(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");
for (j = 0; j < col; j++) {
printf("---");
if (j < col - 1) {
printf("|");
}
}
printf("\n");
}
}
can get our effect But it's ugly that arrays are not initialized
So we want to initialize all the spaces in the array
void init_board(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] = ' ';
}
}
}
Then you can get our square if you print it again
玩家下棋
Then let's make a simulated player playing chess First we can consider The subscript of the player's corresponding input should be1-3区间 ,So our first step is to determine whether the value entered by the player is legal And also take into account A place where players play chess Whether a pawn already exists
And we use the subscript in the array to correspond to from0开始的 So we have to deal with it
void player_move(char board[ROW][COL], int row, int col) {
int x = 0;
int y = 0;
while (1) {
scanf("%d %d", &x, &y);
getchar();
if ((x >= 1 && x <= 3 && y >= 1 && y <= 3)&& board[x - 1][y - 1]==' ') {
board[x - 1][y - 1] = '*';
break;
}
else {
printf("输入错误 请重新输入\n");
continue;
}
}
}
判断是否胜利
Then we need to take into account After each chess game, it is necessary to judge whether or not it has been won So let's construct a function to judge victory
char is_win(char board[ROW][COL], int row, int col) {
int i = 0;
int t = 1;
for (i = 0; i < row; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2]&&board[i][0]!=' ') {
return board[i][0];
}
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') {
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') {
return board[i][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ') {
return board[i][2];
}
for (i = 0; i < row; i++) {
int j = 0;
for (j = 0; j < col; j++) {
if (board[i][j] == ' ') {
t = 0;
}
}
}
if (t) {
return 'p';
}
return 'c';
}
电脑下棋
Then let's consider computer chess,First we need to build a dumb computer,Then inject some more intelligence into him ,First, let's construct a random number
srand((unsigned int)time(NULL));
void computer_move(char board[ROW][COL], int row, int col) {
printf("电脑下棋\n");
while (!(computer_ai(board, row, col))) {
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ') {
board[x][y] = '#';
break;
}
}
}
Careful classmates have already discovered the callcomputer_ai函数啦,Then this is the intelligence quotient of the robot
Let's construct it
char computer_ai(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int k = 0;
while (0 == k)
{
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == '*' && board[i][2] == ' ')
{
board[i][2] = '#';
k = 1;
break;
}
if (board[i][0] == board[i][2] && board[i][0] == '*' && board[i][1] == ' ')
{
board[i][1] = '#';
k = 1;
break;
}
if (board[i][1] == board[i][2] && board[i][1] == '*' && board[i][0] == ' ')
{
board[i][0] = '#';
k = 1;
break;
}
}
if (k != 0)
break;
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == '*' && board[2][j] == ' ')
{
board[2][j] = '#';
k = 1;
break;
}
if (board[0][j] == board[2][j] && board[2][j] == '*' && board[1][j] == ' ')
{
board[1][j] = '#';
k = 1;
break;
}
if (board[1][j] == board[2][j] && board[2][j] == '*' && board[0][j] == ' ')
{
board[0][j] = '#';
k = 1;
break;
}
}
break;
}
if (board[0][0] == board[1][1] && board[1][1] == '*' && board[2][2] == ' ')
{
board[2][2] = '#';
return 1;
}
if (board[0][0] == board[2][2] && board[2][2] == '*' && board[1][1] == ' ')
{
board[1][1] = '#';
return 1;
}
if (board[1][1] == board[2][2] && board[1][1] == '*' && board[0][0] == ' ')
{
board[0][0] = '#';
return 1;
}
if (board[0][2] == board[1][1] && board[0][2] == '*' && board[2][0] == ' ')
{
board[2][0] = '#';
return 1;
}
if (board[0][2] == board[2][0] && board[2][0] == '*' && board[1][1] == ' ')
{
board[1][1] = '#';
return 1;
}
if (board[1][1] == board[2][0] && board[2][0] == '*' && board[0][2] == ' ')
{
board[0][2] = '#';
return 1;
}
return k;
}
So that's all the functions we need We directly attach the complete code
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
//打印界面
void menu();
//初始化数组的值
void init_board(char board[ROW][COL],int row,int col);
//打印数组的值
void display_board(char board[ROW][COL], int row, int col);
//玩家下棋
void player_move(char board[ROW][COL], int row, int col);
//判断是否胜利 Returns if the player wins* Return if the computer wins# If continue to returnc 如果平局返回p
char is_win(char board[ROW][COL], int row, int col);
//电脑下棋
void computer_move(char board[ROW][COL], int row, int col);
//电脑ai优化
char computer_ai(char board[ROW][COL], int row, int col);
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu() {
printf("开始游戏\n");
printf("***********************************\n");
printf("********** 1.play **********\n");
printf("********** 0.txit **********\n");
printf("***********************************\n");
printf("请选择>>\n");
}
void init_board(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 display_board(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");
for (j = 0; j < col; j++) {
printf("---");
if (j < col - 1) {
printf("|");
}
}
printf("\n");
}
}
void player_move(char board[ROW][COL], int row, int col) {
int x = 0;
int y = 0;
while (1) {
scanf("%d %d", &x, &y);
getchar();
if ((x >= 1 && x <= 3 && y >= 1 && y <= 3)&& board[x - 1][y - 1]==' ') {
board[x - 1][y - 1] = '*';
break;
}
else {
printf("输入错误 请重新输入\n");
continue;
}
}
}
void computer_move(char board[ROW][COL], int row, int col) {
printf("电脑下棋\n");
while (!(computer_ai(board, row, col))) {
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ') {
board[x][y] = '#';
break;
}
}
}
char is_win(char board[ROW][COL], int row, int col) {
int i = 0;
int t = 1;
for (i = 0; i < row; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2]&&board[i][0]!=' ') {
return board[i][0];
}
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') {
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') {
return board[i][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ') {
return board[i][2];
}
for (i = 0; i < row; i++) {
int j = 0;
for (j = 0; j < col; j++) {
if (board[i][j] == ' ') {
t = 0;
}
}
}
if (t) {
return 'p';
}
return 'c';
}
char computer_ai(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int k = 0;
while (0 == k)
{
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == '*' && board[i][2] == ' ')
{
board[i][2] = '#';
k = 1;
break;
}
if (board[i][0] == board[i][2] && board[i][0] == '*' && board[i][1] == ' ')
{
board[i][1] = '#';
k = 1;
break;
}
if (board[i][1] == board[i][2] && board[i][1] == '*' && board[i][0] == ' ')
{
board[i][0] = '#';
k = 1;
break;
}
}
if (k != 0)
break;
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == '*' && board[2][j] == ' ')
{
board[2][j] = '#';
k = 1;
break;
}
if (board[0][j] == board[2][j] && board[2][j] == '*' && board[1][j] == ' ')
{
board[1][j] = '#';
k = 1;
break;
}
if (board[1][j] == board[2][j] && board[2][j] == '*' && board[0][j] == ' ')
{
board[0][j] = '#';
k = 1;
break;
}
}
break;
}
if (board[0][0] == board[1][1] && board[1][1] == '*' && board[2][2] == ' ')
{
board[2][2] = '#';
return 1;
}
if (board[0][0] == board[2][2] && board[2][2] == '*' && board[1][1] == ' ')
{
board[1][1] = '#';
return 1;
}
if (board[1][1] == board[2][2] && board[1][1] == '*' && board[0][0] == ' ')
{
board[0][0] = '#';
return 1;
}
if (board[0][2] == board[1][1] && board[0][2] == '*' && board[2][0] == ' ')
{
board[2][0] = '#';
return 1;
}
if (board[0][2] == board[2][0] && board[2][0] == '*' && board[1][1] == ' ')
{
board[1][1] = '#';
return 1;
}
if (board[1][1] == board[2][0] && board[2][0] == '*' && board[0][2] == ' ')
{
board[0][2] = '#';
return 1;
}
return k;
}
#define _CRT_SECURE_NO_WARNINGS 1
//三子棋游戏
#include<stdio.h>
#include"game.h"
void game() {
char ret = 'c';
char board[ROW][COL] = { 0 };
init_board(board,ROW,COL);//初始化数组
display_board(board, ROW, COL);//打印数组
//玩家下棋
while (1) {
printf("请输入你要下棋的坐标 示例:1 1(中间间隔空格)\n");
player_move(board, ROW, COL);
display_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'c') {
break;
}
computer_move(board, ROW, COL);
display_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'c') {
break;
}
}
switch (ret) {
case '*':printf("恭喜玩家获得胜利,Reward a touch\n"); break;
case '#':printf("很遗憾 电脑获得胜利\n"); break;
case 'p':printf("本次游戏平局\n"); break;
}
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do {
menu();
scanf("%d", &input);
getchar();
switch (input) {
case 0:
printf("退出游戏\n");//如果为0 则退出游戏
break;
case 1:
game();//如果为1 则开始游戏
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while ((int)input);
return 0;
}
Then we can complete a simple three-piece game Let's take a look at the renderings below
I can't win, of course it's not my thing(I won't admit it) Because we add IQ to the robot,So the highest is to play to a draw Let's see if anyone can beat my robot
边栏推荐
- [Interview: Concurrency 38: Multithreading: Thread Pool] Basic concepts of the ThreadPoolExecutor class
- Yu Mr Series 】 【 2022 July 022 - Go Go teaching course of container in the dictionary
- 机器学习---线性回归、Logistic回归问题相关笔记及实现
- 2022.07.13_每日一题
- 《opencv学习笔记》-- 仿射变换
- opencv、pil和from torchvision.transforms的Resize, Compose, ToTensor, Normalize等差别
- 2022.07.12_每日一题
- CY7C68013A之LED闪烁
- 《c语言》青蛙跳台阶递归问题
- 手把手教你开发微信小程序自定义底部导航栏
猜你喜欢
regex bypass
双倍数据速率同步动态随机存储器(Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM)- 逻辑描述部分
Leetcode952. Calculate maximum component size by common factor
'vite' is not an internal or external command, nor is it a runnable program or batch file.
机器学习---线性回归、Logistic回归问题相关笔记及实现
【面试题】从输入URL到游览器渲染完成,经历了什么
基于LSTM的诗词生成
安装部署KubeSphere管理kubernetes
毫米波技术基础
嵌入式系统驱动初级【2】——内核模块下_参数和依赖
随机推荐
【网络攻防】常见的网络攻防技术——黑客攻防(通俗易懂版)
2022.07.14_每日一题
Spark 在 Yarn 上运行 Spark 应用程序
Leetcode952. 按公因数计算最大组件大小
《opencv学习笔记》-- 仿射变换
Yu Mr Series 】 【 2022 July 022 - Go Go teaching course of container in the dictionary
2022.07.22 _ a day
Tasks and task switching
任务及任务切换
Collation and sharing of related classic papers and datasets in the field of deep learning communication
MySQL detailed explanation
解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
2022.07.26_Daily Question
MySql数据库优化查询工具
van-uploader uploads images, and cannot preview the image using base64 echo
CNN--Introduction to each layer
文件 - 04 下载文件: 根据文件下载链接下载文件
DAY18:Xss 靶场通关手册
《C语言小游戏》扫雷
XSS靶场prompt.ml过关详解