当前位置:网站首页>Three chess (C language implementation)
Three chess (C language implementation)
2022-08-01 09:28:00 【xiaoHu_C】
今天小编给大家带来的是CA small game for beginners of language learning(三子棋),The whole process of the game is relatively easy to understand,But for contactCFor students who are new to language,Many details still need to be further grasped,This is a great help for our knowledge consolidation.
好了,闲话少说,Let's experience it together!
目录
2The block implementation of each module
1游戏的整体思路
I believe that you should have played a lot of small games,He is also very familiar with the flow of the game.when we start playing a game,The choice faced is nothing more than starting the game and exiting the game.Then according to this general direction,We can first write his main function process and menu.
在主函数中,Our task is to let the player choose,Therefore, it is necessary to provide selectable options in the menu.
有了菜单,Of course there is no shortage of options,We can pass it in the main functionswitch case statement to achieve this function,At the same time, we must also consider that players may play the game multiple times,因此我们选择do while循环来解决.实现过程,如下所示:
不可避免,Players may enter wrong numbers,So we also have to give the player a chance to re-enter,提醒玩家重新输入!
对于刚接触C语言的同学,We need to develop a good habit of taking one step at a time,每写一段代码,测试一下,This way we can correct mistakes in time,Avoid the wreckage of the final error check!
2The block implementation of each module
1)打印棋盘+初始化棋盘
刚开始,We can think of the chessboard as consisting of data and delimited rows.
如上图所示,We divided the chessboard into three parts: red, yellow, and blue,Each section consists of a row of data and a delimiter row,Note that the blue part lacks a separate line relative to the other two parts.
同理可得,We can also block each column like this.具体实现的代码如下:
But when we run it, the effect we see is different from what we expected,That's because we didn't initialize the board,We need to pass the data bits into a space as well.So we also need an initialization function.
After defining the initialization function,We can get the expected effect.
2)玩家下棋
What players need to pay attention to when playing chess is the rationality of the position of the move,The first point is that the coordinates of the pieces must be within the chessboard,If over the board,Then you can't fall;The second point is to determine whether the move position has been placed,If it was dropped,Then you have to change the position of the drop.
3)电脑下棋
在这里,In computer chess, we take the principle of randomness,We utilize a timestamp fitsrandTo randomly generate the coordinates of the computer to be placed.
4)判断输赢
The way of judging winners and losers in chess is simple,It is a line, a column or any one of the diagonal lines that satisfies the three sub-connections to form a line,Then one of them wins,游戏结束.Except for winning and losing,There are also draws,因此,In the program we set the player to win the return‘*’、电脑赢返回‘#’、平局返回‘Q’、继续游戏返回‘C’.
It contains a wrapper functionis_full,It is used to judge whether the chessboard is full.
3运行结果
The above are the running results of three different states,实现到这里,Our three-piece game is almost done.
Although our basic functions have been implemented,但是很容易发现,The computers in our backgammon don't seem to be that smart,It can only play chess at random,So interested students can try to optimize,Make your computer more capable!
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game1.h"
//三子棋
void game()
{
//打印棋盘
// | |
//---|---|---
// | |
//---|---|---
// | |
char ret = 0;
char board[ROW][COL] = { 0 };
init_board(board, ROW, COL);
display_board(board, ROW, COL);
while (1)
{
player_move(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
display_board(board, ROW, COL);
computer_move(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
display_board(board, ROW, COL);
}
if (ret == '*')
printf("玩家赢\n");
else if (ret == '#')
printf("电脑赢\n");
else if (ret == 'Q')
printf("平局\n");
display_board(board, ROW, COL);
}
void menu()
{
printf("***************************\n");
printf("********1.Play Game********\n");
printf("********0.Exit Game********\n");
printf("***************************\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do//Want to play the game multiple times,使用do whileNo matter what the situation is, you can play it once before making a judgment
{
menu();
printf("请选择>:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新输入!\n");
break;
}
} while (input);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"game1.h"
void init_board(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
// | |
//---|---|---
// | |
//---|---|---
// | |
void display_board(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
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 player_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋:>\n");
while (1)
{
printf("Please enter the next sub-coordinates:>");
scanf("%d %d", &x, &y);
//Judge the legitimacy of the move
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
//Determine whether the location is already occupied
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该位置已被占用,Please change location\n");
}
}
else
{
printf("非法输入,请重新输入\n");
}
}
}
void computer_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋:>\n");
while (1)
{
//Use timestamps to make the computer play chess randomly
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
//棋盘满了返回1没满返回0
int is_full(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
if (' ' == board[i][j])
{
return 0;
}
}
}
return 1;
}
char is_win(char board[ROW][COL], int row, int col)
{
//It is temporarily written to death here3×3的棋盘
int i = 0;
//Check if the rows are equal
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];
}
}
//判断列相等
for (i = 0; i < col; i++)
{
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][i] != ' ')
{
return board[1][1];
}
//判断副对角线
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][i] != ' ')
{
return board[1][1];
}
//判断平局,Whether the board is full
if (is_full(board, row, col) == 1)
{
return 'Q';
}
return 'C';
}
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3 //行
#define COL 3 //列
//初始化棋盘
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);
//电脑下棋
void computer_move(char board[ROW][COL], int row, int col);
//判断输赢
char is_win(char board[ROW][COL], int row, int col);
边栏推荐
猜你喜欢
50.【动态二维数组的运用】
基于MySql,Redis,Mq,ES的高可用方案解析
企业微信群:机器人定时提醒功能数据库配置化
leetcode-6134: Find the closest node to the given two nodes
[Dataset] Dataset summary of various insulators, bird's nests and anti-vibration hammers
Redis middleware (from building to refuse pit)
net stop/start mysql80 access denied
网络基础学习
改版去不图床 Token 的获取
HoloView——实时数据
随机推荐
Lsky Pro 企业版手动升级、优化教程
HoloView -- Tabular Datasets
【编程之外】当遮羞布被掀开,当人们开始接受一切
various network protocols
GBase 8c中怎么查询数据库配置参数,例如datestyle
Prime Ring Problem(素数环问题)
Install GBase 8 c database, the error shows "Resource, how to solve?
JVM内存模型之深究模型特征
程序员如何学习开源项目,这篇文章告诉你
热修复技术可谓是百花齐放
Ogg synchronizes oracle to mysql, there may be characters that need to be escaped in the field, how to configure escape?
SkiaSharp's WPF self-painted five-ring bouncing ball (case version)
【STM32】入门(二):跑马灯-GPIO端口输出控制
SAP ABAP ALV+SMARTFORS 表分页 报表打印程序
Redis middleware (from building to refuse pit)
In the background of the GBase 8c database, what command is used to perform the master-slave switchover operation for the gtm and dn nodes
STM32个人笔记-看门狗
Parsing MySQL Databases: "SQL Optimization" vs. "Index Optimization"
[Beyond programming] When the fig leaf is lifted, when people begin to accept everything
ASP.NET Core 6框架揭秘实例演示[30]:利用路由开发REST API