当前位置:网站首页>Small program of C language practice (consolidate and deepen the understanding of knowledge points)
Small program of C language practice (consolidate and deepen the understanding of knowledge points)
2022-06-27 05:53:00 【Xiao Zhang ﹉】
Catalog
Introduction to small projects
The implementation part of the function
Header file and the name part of the function
The expanded results are displayed
Preface
Hello everyone , What I bring you today is a C A small program of language practice , Many little friends are finishing their studies C After some knowledge of language , I don't know how to verify myself Do you really understand this knowledge point , Whether you can use this knowledge to do some operations you want to complete . At this time, we should do some small projects to feel Take control of yourself , Now let's introduce the small projects that we brought to you today .
Introduction to small projects
This small project uses C/C++ Realize a small game that we should all have played --- Sanzi ( It can be extended to N Sub chess enables players to play chess with computers ), This small project is divided into three parts , The first part is the main implementation part of the code , The second part is the specific function part of the code , The third part is about the names of functions and header files . Which applies to the cycle , Functions and some logical aspects operation , It can help us consolidate and practice this knowledge point , Improve our logical thinking , Let's take a look at the specific implementation of this small project :
Main part
#include"game.h"
int main()
{
// Initialize random values
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
cout << " Please select a number :\n";
cin >> input;
switch (input)
{
case 1:
// Functions implemented by Sanzi chess
game();
break;
case 0:
cout << " Quit successfully \n";
break;
default:
cout << " This option does not exist , Please re-enter \n";
break;
}
} while (input);
return 0;
}The implementation part of the function
#include"game.h"
// Implementation of menu function
void menu()
{
cout << "*****************************\n";
cout << "*******1.play 0.exit********\n";
cout << "*****************************\n";
}
void InitBoard(char board[][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
// Assign each element in the chessboard as a space first
// To initialize
board[i][j] = ' ';
}
}
}
void displayBoard(char board[][COL], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
// Print row
for (j = 0; j < col; j++)
{
// Print Columns
cout <<" "<<board[i][j]<<" ";
// Restricted printing conditions
if (j < col - 1)
{
cout << "|";
}
}
cout << "\n";
// Print split lines
// If the number of rows is less than the total row minus one
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
cout << "---";
// Restricted printing conditions
if (j < col - 1)
{
cout << "|";
}
}
}
cout << "\n";
}
}
void PlayerMove(char board[][COL], int row, int col)
{
int x = 0, y = 0;
cout << " Players play chess \n";
while (1)
{
cout << " Please enter the coordinates \n";
cin >> x >> y;
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
cout << " The coordinates are occupied , Please play chess again \n";
}
}
else
{
cout << " Incorrect input , Please re-enter \n";
}
}
}
void ComeputerMove(char board[][COL], int row, int col)
{
cout << " The computer plays chess \n";
int x = 0, y = 0;
x = rand() % row;// Range 0—2
y = rand() % col;// Range 0—2
while (1)
{
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
else
{
x = rand() % row;// Range 0—2
y = rand() % col;// Range 0—2
}
}
}
char IsWin(char board[][COL], int row, int col)
{
// That's ok
int i = 0, j = 0, count = 1, x = 0;
char a = 0;
for (i = 0; i < row; i++)
{
x = 0, count = 1;
for (j = 0; j < col; j++)
{
if (board[i][x] == board[i][x + 1] && x < col&&board[i][x]!=' ')
{
a = board[i][x];
count++;
x++;
}
if (count == col)
{
return a;
}
}
}
// Column
i = 0, j = 0, count = 1, x = 0;
for (i = 0; i < col; i++)
{
count = 1, x = 0;
for (j = 0; j < row; j++)
{
if (board[x][i] == board[x + 1][i] && x < row&&board[x][i] != ' ')
{
a = board[x][i];
count++;
x++;
}
if (count == row)
{
return a;
}
}
}
// Diagonals
i = 0, j = 0, count = 0, x = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
x = board[0][0];
if (i == j && board[i][j] == x && x != ' ')
{
a = board[i][j];
count++;
}
}
}
if (count == row)
{
return a;
}
i = 0, j = 0, count = 1;
char y = 0, z = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
y = board[0][row-1];
if (i == j && board[i][j] == y && y != ' '&& board[row - 1][0] == board[i][j] && board[row-1][0]==y)
{
z = board[i][j];
count++;
}
}
}
if (board[row - 1][0] == board[0][row - 1])
{
count++;
}
if (count == row)
{
return z;
}
// If you come here, the previous situation is not consistent, that is, no one wins
// At this point, judge the draw or continue
i = 0, j = 0, count = 0, x = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;// under
}
}
}
return 1;// Is full
}
void game()
{
// Initialize the elements in the two-dimensional array to 0
char board[ROW][COL] = { 0 };
char ret = 0;
// Initialize chessboard
InitBoard(board, ROW, COL);
// Print chessboard
displayBoard(board, ROW, COL);
// Playing chess
while (1)
{
// Players play chess
PlayerMove(board, ROW, COL);
displayBoard(board, ROW, COL);
// Every time a player finishes playing chess, he must decide whether to win or lose
ret = IsWin(board, ROW, COL);
if (ret != 0)
{
break;
}
// The computer plays chess
ComeputerMove(board, ROW, COL);
displayBoard(board, ROW, COL);
// Every time the computer plays chess, it has to judge whether it wins or loses
ret = IsWin(board, ROW, COL);
if (ret != 0)
{
break;
}
}
if (ret == '*')
{
cout << " Congratulations , You win !\n";
}
else if (ret == '#')
{
cout << " unfortunately , You lost to the computer \n";
}
else
{
cout << " It ends in a draw !\n";
}
}Header file and the name part of the function
#pragma once
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#define ROW 3
#define COL 3
using namespace std;
// The name of the menu
void menu();
// Initialize chessboard
void InitBoard(char board[][COL], int row, int col);
// Display chessboard
void displayBoard(char board[][COL], int row, int col);
// Players play chess
void PlayerMove(char board[][COL], int row, int col);
// The computer plays chess
// Find a random player who doesn't play chess
void ComeputerMove(char board[][COL], int row, int col);
// Judgement of winning or losing
char IsWin(char board[][COL], int row, int col);
// Game interface
void game();Expand as needed
#define ROW 3
#define COL 3
// You just need to define the ROW( That's ok ) as well as COL( Number of columns ) Just modify it Operation result display

The expanded results are displayed
#define ROW 5
#define COL 5
// Extended to 5 Ziqi


Conclusion
This is the end of the small project , You can run the code yourself and try it , If there is anything you don't understand , You can leave a message in the comment area or send me a private message directly , Thank you for your support !
边栏推荐
- Two position relay rxmvb2 r251 204 110dc
- Webrtc series - Nomination and ice of 7-ice supplement for network transmission_ Model
- 多线程基础部分Part 1
- Codeforces Round #802 (Div. 2)
- Nlp-d62-nlp competition d31 & question brushing D15
- 我对于测试团队建设的意见
- Using domain name forwarding mqtt protocol, pit avoidance Guide
- Luogu p2939 [usaco09feb]revamping trails G
- Double position relay rxmd2-1mrk001984 dc220v
- 【FPGA】UART串口_V1.1
猜你喜欢

程序猿学习抖音短视频制作

Niuke practice 101-c reasoning clown - bit operation + thinking

免费的 SSH 和 Telnet 客户端PuTTY
![Navigation [machine learning]](/img/79/8311a409113331e72f650a83351b46.png)
Navigation [machine learning]

LeetCode-515. 在每个树行中找最大值

Pycharm 中 Terminal 无法进入 venv 环境的问题

重映像(STM32)

Implementation of easyexcel's function of merging cells with the same content and dynamic title

Qt使用Valgrind分析内存泄漏

什么是BFC?有什么用?
随机推荐
three. JS first person camera gun following
【Cocos Creator 3.5.1】input. Use of on
Flink production problems (1.10)
【Cocos Creator 3.5.1】this. node. Use of getposition (this.\u curpos)
Qt使用Valgrind分析内存泄漏
软件测试年终总结报告模板
[cocos creator 3.5.1] addition of coordinates
QListWidgetItem上附加widget
Navigation [machine learning]
[FPGA] UART serial port_ V1.1
Database - index
函数栈帧的形成与释放
stm32读取IO高低电平状态
Machunmei, the first edition of principles and applications of database... Compiled final review notes
Logu p4683 [ioi2008] type printer problem solving
leetcode298周赛记录
【FPGA】UART串口_V1.1
免费的 SSH 和 Telnet 客户端PuTTY
多线程基础部分Part3
Built in functions of spark