当前位置:网站首页>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 !
边栏推荐
猜你喜欢

竣达技术丨多品牌精密空调集中监控方案

双位置继电器DLS-34A DC0.5A 220VDC

Leetcode99 week race record

Double position relay jdp-1440/dc110v

Cognition - how to fill in 2022 college entrance examination volunteers

stm32读取IO高低电平状态

免费的 SSH 和 Telnet 客户端PuTTY

Reading graph augmentations to learn graph representations (lg2ar)

Go日志-Uber开源库zap使用

高翔slam14讲-笔记1
随机推荐
Machunmei, the first edition of principles and applications of database... Compiled final review notes
项目-h5列表跳转详情,实现后退不刷新,修改数据则刷新的功能(记录滚动条)
Asp.Net Core6 WebSocket 简单案例
[nips 2017] pointnet++: deep feature learning of point set in metric space
[collection] Introduction to basic knowledge of point cloud and functions of point cloud catalyst software
Asp. Net core6 websocket simple case
Add widget on qlistwidgetitem
面试:Selenium 中有哪几种定位方式?你最常用的是哪一种?
WebRTC系列-网络传输之7-ICE补充之提名(nomination)与ICE_Model
双位置继电器RXMD2-1MRK001984 DC220V
Two position relay hjws-9440
leetcode298周赛记录
重映像(STM32)
Contents in qlistwidget are not displayed
Remapping (STM32)
leetcode299周赛记录
Spark 之 Projection
Configuring the help class iconfiguration in C # NETCORE
C语言实现定时器
expect脚本中使用scp命令的方法,expect脚本中scp命令获取不了值的问题完美解决方法