C++坦克大战源代码

news2024/12/30 2:11:09

  源码:

#include <iostream>
#include <time.h> 
#include <windows.h>
 
 
#define W 1       //上
#define S 2         //下
#define A 3         //左
#define D 4          //右
#define L 5       // 坦克有4条命
 
void HideCursor() {  //隐藏光标            
    CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); 
}
void GoToxy(int x, int y) {  //光标移动,X、Y表示横、纵坐标
    COORD coord = { x, y };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);  
}
 
//全局变量
int map[50][40];//地图二维数组
int B_num;      //子弹编号
int Pos;     //敌方坦克生成位置,-1为左边,0为中间,1为右边,2为我的坦克位置
int Speed = 7;  //游戏速度
int Enemy; //还未出现的敌人
const char* Tank_Model[3][4] ={
    {"◢┃ ◣", "◢╦ ◣", "◢╦◣", "◢╦◣"},
    {"╠ █╣", "╠ █╣", "━█╣", "╠█━"},
    {"◥╩ ◤", "◥┃ ◤", "◥╩◤", "◥╩◤"}
     }; 
 
//坦克
class Tank{
public:
    int x, y; //中心坐标
    int Direction; //方向
    int Model;  //模型
    int Revival; //复活次数
    int Num; //敌方坦克编号  
    bool Type;   //我方坦克此参数为1
    bool Exist;  //存活为1,不存活为0
}AI_tank[6], my_tank;
//子弹
class Bullet{      
public:
    int x, y;    //坐标
    int Direction;  //方向
    bool Exist;  //1为存在,0不存在
    bool Type;   //0为敌方子弹,1为我方子弹
}bullet[50] ;
 
//基本函数
void GoToxy(int x, int y);    //光标移动
void HideCursor();           //隐藏光标
 
void Key();  //键盘输入
void Init(); //初始化
void Pause(); //暂停
void Show(); //打印框架
void Print_Map();  //打印地图
void Cheak_Game(); //检测游戏胜负
void GameOver();  //游戏结束
 
//坦克
void Creat_AI_T(Tank* AI_tank); //建立坦克  
void Creat_My_T(Tank* my_tank);               
 
void Move_AI_T(Tank* AI_tank);//坦克移动
void Move_My_T(int turn);                     
 
void Clear_T(int x, int y);  //清除坦克
void Print_T(Tank tank);  //打印坦克
bool Cheak_T(Tank tank, int direction); //检测障碍,1阻碍
 
//子弹
void Creat_AI_B(Tank* tank);  //敌方坦克发射子弹
void Creat_My_B(Tank tank);//我方坦克发射子弹
void Move_B(Bullet bullet[50]); //子弹移动
void Break_B(Bullet* bullet); //子弹碰撞
void Print_B(int x, int y);//打印子弹
void Clear_B(int x, int y); //清除子弹
int  Cheak_B(int x, int y);  //子弹前方情况
 
void Show() {       //打印框架   
    std::cout << "  ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁";
    std::cout << "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁\n";
    for (int i = 0; i < 48; i++) {
        std::cout << "▕                                                                             ▏\n";
    }
    std::cout << "  ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔";
    std::cout << "▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n";
}
void Print_Map() {     // 打印地图   
    int Map[50][40] = {
//map里的值: 0为可通过陆地,1为砖,6为墙,100~105为敌方坦克,200为我的坦克,
       { 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4 },
       { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
       { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
       { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0,1,1,0,0,0,4 },
       { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
       { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
       { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
       { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
       { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
       { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
       { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,9,9,9,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
       { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,9,9,9,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
       { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,9,9,9,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
       { 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4 }
    };
    for (int i = 0; i < 50; i++)
        for (int j = 0; j < 40; j++)        
            map[i][j] = Map[i][j];
    for (int i = 0; i < 50; i++)
        for (int j = 0; j < 40; j++)        
            if (map[i][j] == 1) {
                GoToxy(2 * j, i);
                std::cout << "▓";
            }            
            else if (map[i][j] == 6) {
                GoToxy(2 * j, i);
                std::cout << "■";
            }             
    GoToxy(38, 46);     
    std::cout << " ◣◢";
    GoToxy(38, 47);     
    std::cout << "◣█ ◢";
    GoToxy(38, 48);     
    std::cout << "◢█ ◣"; 
}
void Cheak_Game() {
    //敌人坦克全部不存活
    if (Enemy <= 0 && !AI_tank[0].Exist && !AI_tank[1].Exist && !AI_tank[2].Exist
        && !AI_tank[3].Exist && !AI_tank[4].Exist && !AI_tank[5].Exist)
        GameOver();
    if (my_tank.Revival >= L)//我复活次数用完
        GameOver();//游戏结束
}
void GameOver() {
    bool home = 1;
    while (home) { 
        GoToxy(37, 21);
        std::cout << "游戏结束!";        
        if (GetAsyncKeyState(0xD) & 0x8000) {  //回车键
            system("cls");   //清屏
            Show();
            Init(); //初始化
            break;
        }
        else if (GetAsyncKeyState(0x1B) & 0x8000)  //Esc键退出   
                 exit(0);
    }
}
void Creat_My_T(Tank* my_tank) {//建立我的坦克
    my_tank->x = 15;
    my_tank->y = 47;
    my_tank->Direction = 1;
   // my_tank->Model = 0;
    my_tank->Exist = 1;
    my_tank->Type = 1;
    Print_T(*my_tank);   //打印我的坦克
}
void Move_My_T(int turn) {//turn为Key()函数传入的方向值
    Clear_T(my_tank.x, my_tank.y);
    my_tank.Direction = turn; 
    if (Cheak_T(my_tank, my_tank.Direction))  //我方坦克当前方向上无障碍
        switch (turn) {
        case W: my_tank.y--; break;  //上
        case S: my_tank.y++; break;  //下
        case A: my_tank.x--; break;  //左
        case D: my_tank.x++; break;  //右
        }  
    Print_T(my_tank);
}
void Print_T(Tank tank) {//打印
    for (int i = 0; i < 3; i++) {
        GoToxy((tank.x - 1) * 2, tank.y - 1 + i);//在坦克中心坐标的左边,上中下三行打印
        std::cout << Tank_Model[i][tank.Direction - 1]; //打印的是地址,地址既字符串
        for (int j = 0; j < 3; j++)
            if (tank.Type)//若为我的坦克
                map[tank.y + j - 1][tank.x + i - 1] = 200;
        //在map上敌方值为100~105,我方为200
            else
                map[tank.y + j - 1][tank.x + i - 1] = 100 +tank.Num;
        //这样可以通过map值读取坦克编号
    }
}
void Creat_AI_T(Tank* AI_tank) {
        AI_tank->x = 19 + 17 * (Pos); //pos为坦克生成位置,-1为左位置,0为中间,1为右,2为我的坦克位置
        AI_tank->y = 2;
        AI_tank->Direction = 2;  //方向朝下
        AI_tank->Revival++; //复活次数+1
        AI_tank->Exist = 1;//存在
        Pos++;
        Enemy--;
        if (Pos == 2)  //循环重置(pos只能为-1,0,1)
            Pos = -1;
        Print_T(*AI_tank);
        return;          
}
void Move_AI_T(Tank* AI_tank) { 
    if (AI_tank->Exist) {  //存在 
        Clear_T(AI_tank->x, AI_tank->y);
        if (Cheak_T(*AI_tank, AI_tank->Direction))//前方无障碍
            switch (AI_tank->Direction) {
            case W: AI_tank->y--; break;  //上
            case S: AI_tank->y++; break;  //下
            case A: AI_tank->x--; break;  //左
            case D: AI_tank->x++; break;  //右
            }
        else {//前方有障碍 
           for (int i = rand() % 4 + 1; i <= 4; i++)
                if (Cheak_T(*AI_tank, i)){  //循环判断,返1可通过
                    AI_tank->Direction = i;
                    break;
               }
        }
        Print_T(*AI_tank);     //打印敌方坦克
    }
}
bool Cheak_T(Tank tank, int direction) {  //检测坦克前方障碍,返1为可通过
    switch (direction) {                   
    case W: 
        if (map[tank.y - 2][tank.x] == 0 && map[tank.y - 2][tank.x - 1] == 0 && map[tank.y - 2][tank.x + 1] == 0)
            return 1;
        else return 0;
    case S:
        if (map[tank.y + 2][tank.x] == 0 && map[tank.y + 2][tank.x - 1] == 0 && map[tank.y + 2][tank.x + 1] == 0)
            return 1;
        else return 0;
    case A:
        if (map[tank.y][tank.x - 2] == 0 && map[tank.y - 1][tank.x - 2] == 0 && map[tank.y + 1][tank.x - 2] == 0)
            return 1;
        else return 0;
    case D:
        if (map[tank.y][tank.x + 2] == 0 && map[tank.y - 1][tank.x + 2] == 0 && map[tank.y + 1][tank.x + 2] == 0)
            return 1;
        else return 0;
    default: return 0;
    }
}
void Clear_T(int x, int y) {   //清除坦克
    for (int i = 0; i <= 2; i++)
        for (int j = 0; j <= 2; j++) {//将坦克占用的地图清零
            map[y + j - 1][x + i - 1] = 0;
            GoToxy(2 * x + 2 * j - 2, y + i - 1);
            std::cout << "  ";
        }
}
 
//键盘输入
void Key() {                 
    //上下左右键
    if (GetAsyncKeyState('W') & 0x8000)
        Move_My_T(W);
    else if (GetAsyncKeyState('S') & 0x8000)
        Move_My_T(S);
    else if (GetAsyncKeyState('A') & 0x8000)
        Move_My_T(A);
    else if (GetAsyncKeyState('D') & 0x8000)
        Move_My_T(D);
//子弹发射
    else if (GetAsyncKeyState('P') & 0x8000) {
            Creat_My_B(my_tank);
        }
    else if (GetAsyncKeyState(0x1B) & 0x8000)// Esc键退出
        exit(0); 
    else if (GetAsyncKeyState(0x20) & 0x8000)//空格暂停
        Pause();
}
void Pause() {    //暂停
    while (1) {
        if (GetAsyncKeyState(0xD) & 0x8000) {      //回车键继续  
            break;
        }
        else if (GetAsyncKeyState(0x1B) & 0x8000) //Esc键退出   
            exit(0);
    }
}
void Creat_AI_B(Tank* tank){ //敌方发射子弹
        if (!(rand() % 10)) { //在随后的每个游戏周期中有10分之一的可能发射子弹       
            Creat_My_B(*tank);
        }
}
void Creat_My_B(Tank tank) {
    switch (tank.Direction) 
    {  
    case W:
        bullet[B_num].x = tank.x;
        bullet[B_num].y = tank.y - 2;
        bullet[B_num].Direction = 1;//1表示向上
        break;
    case S:
        bullet[B_num].x = tank.x;
        bullet[B_num].y = tank.y + 2;
        bullet[B_num].Direction = 2;//2表示向下
        break;
    case A:
        bullet[B_num].x = tank.x - 2;
        bullet[B_num].y = tank.y;
        bullet[B_num].Direction = 3;//3表示向左
        break;
    case D:
        bullet[B_num].x = tank.x + 2;
        bullet[B_num].y = tank.y;
        bullet[B_num].Direction = 4;//4表示向右
        break;
    }
    bullet[B_num].Exist = 1; //子弹存在
    bullet[B_num].Type = tank.Type; //我方坦克发射的子弹bullet.Type=1
    B_num++;
    if (B_num == 50) //如果子弹编号增长到50号,那么重头开始编号
        B_num = 0;   //考虑到地图上不可能同时存在50颗子弹,所以数组元素设置50个
}
void Move_B(Bullet bullet[50]) {  //子弹移动                            
    for (int i = 0; i < 50; i++) {
        if (bullet[i].Exist) {//如果子弹存在        
           if (map[bullet[i].y][bullet[i].x] == 0) {         
                Clear_B(bullet[i].x, bullet[i].y);//子弹当前位置无障碍,抹除子弹图形
                switch (bullet[i].Direction) {//子弹变到下一个坐标
                    case W:(bullet[i].y)--; break;
                    case S:(bullet[i].y)++; break;
                    case A:(bullet[i].x)--; break;
                    case D:(bullet[i].x)++; break;
                }
           }
            //判断子弹当前位置情况
           if (map[bullet[i].y][bullet[i].x] == 0) //子弹坐标无障碍
               Print_B(bullet[i].x, bullet[i].y);//打印
           else Break_B(&bullet[i]);     //子弹碰撞       
           for (int j = 0; j < 50; j++) 
                //子弹间的碰撞判断,若是我方子弹和敌方子弹碰撞则都删除,若为两敌方子弹则无视
                if (bullet[j].Exist && j != i && (bullet[i].Type || bullet[j].Type) 
                    && bullet[i].x == bullet[j].x && bullet[i].y == bullet[j].y)
                {                              //同样的两颗我方子弹不可能产生碰撞
                    bullet[j].Exist = 0;
                    bullet[i].Exist = 0;
                    Clear_B(bullet[j].x, bullet[j].y);
  
                    break;
                }
        }
    }
}
void Break_B(Bullet* bullet) {  
    int x = bullet->x;  
    int y = bullet->y;  //子弹坐标
    int i;
    if (map[y][x] == 1) {  //子弹碰到砖块   
        if (bullet->Direction == A || bullet->Direction == D)     
            //若子弹是横向的
            for (i = -1; i <= 1; i++)
                if (map[y + i][x] == 1) {
                    map[y + i][x] = 0;
                    GoToxy(2 * x, y + i);
                    std::cout << "  ";
                }
        if (bullet->Direction == W || bullet->Direction == S)   //子弹是向上或是向下移动的
            for (i = -1; i <= 1; i++)
                if (map[y][x + i] == 1) {  //如果子弹打中砖块两旁为砖块,则删除砖,若不是则忽略    
                    map[y][x + i] = 0;    //砖块碎
                    GoToxy(2 * (x + i), y);
                    std::cout << "  ";
                }
        bullet->Exist = 0; //子弹不存在
    }
    else if (map[y][x] == 4 || map[y][x] == 6)  //子弹碰到边框或者不可摧毁方块
        bullet->Exist = 0;
    else if (bullet->Type ==1 && map[y][x] >= 100 && map[y][x] <= 105) { //我方子弹碰到了敌方坦克    
        AI_tank[(int)map[y][x] % 100].Exist = 0;
        bullet->Exist = 0; 
        Clear_T(AI_tank[(int)map[y][x] % 100].x, AI_tank[(int)map[y][x] % 100].y);  //清除坦克
        
    }
    else if (bullet->Type == 0 && map[y][x] == 200) {   //若敌方子弹击中我的坦克    
        my_tank.Exist = 0;
        bullet->Exist = 0;
        Clear_T(my_tank.x, my_tank.y);
        my_tank.Revival++; //我方坦克复活次数加1
    }
    else if (map[y][x] == 9) { //子弹碰到巢    
        bullet->Exist = 0;
        GoToxy(38, 46);      std::cout << "      "; 
        GoToxy(38, 47);      std::cout << "      ";
        GoToxy(38, 48);      std::cout << "◢◣  ";
        GameOver();
    }
}
int Cheak_B(int x, int y) {//子弹当前位置情况
    if (map[y][x] == 0)
        return 1;
    else
        return 0;
}
void Print_B(int x, int y){
    GoToxy(2 * x, y);
    std::cout << "o";
}
void Clear_B(int x, int y){
    GoToxy(2 * x, y);
    if (Cheak_B(x, y) == 1) {//子弹当前坐标在空地上 
        std::cout << "  ";
    }
}
 
void Init() {      //初始化
    Enemy = 24;
    my_tank.Revival = 0;  //我的坦克复活次数为0
    Pos = 0;
    B_num = 0;
    Print_Map();
    Creat_My_T(&my_tank);
    for (int i = 0; i < 50; i++) {//子弹
        bullet[i].Exist = 0;
    }
    for (int i = 0; i <= 5; i++) {//敌方坦克
        AI_tank[i].Revival = 0;
        AI_tank[i].Exist = 0;  //初始化坦克全是不存活的,用Creat_AI_T()建立不存活的坦克
        AI_tank[i].Num = i;
        AI_tank[i].Type = 0;
    }
}
 
int main() {                              
    int i;
    int gap[16] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };  //间隔数组,用于控制速度
    HideCursor();     //隐藏光标
    Show();      //打印框架
    Init();     //初始化
    while(1) {
        if (gap[0]++ % Speed == 0) {
            //速度调整,     
            Cheak_Game();  //游戏胜负检测
            for (i = 0; i <= 5; i++) {//敌方坦克移动循环
                if (gap[i + 7]++ % 3 == 0)
                    Move_AI_T(&AI_tank[i]);
            }
            for (i = 0; i <= 5; i++)//建立敌方坦克
                if (AI_tank[i].Exist == 0 && AI_tank[i].Revival < 4 && gap[i+1]++ % 50 == 0) {  //一个敌方坦克每局只有4条命
                                 //坦克死掉后间隔一段时间建立
                    Creat_AI_T(&AI_tank[i]);
                    break;          
                } 
            for (i = 0; i <= 5; i++)
                if (AI_tank[i].Exist)
                    Creat_AI_B(&AI_tank[i]);
            if (my_tank.Exist && gap[14]++ % 2 == 0)
                Key();
            if (my_tank.Exist == 0 && my_tank.Revival < L && gap[15]++ % 15 == 0)//我方坦克复活
                Creat_My_T(&my_tank);            
            Move_B(bullet);            
        }
        Sleep(5);
    }
    return 0;
}
 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/891875.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

[LeetCode]两数相加

解题 思路1 : 先将每个链表的节点数求出来,将短的链表的每个节点的值加到长链表对应的节点上,在判断加上之后的值是否大于10,若大于则该节点减10,下一个节点加1,由此循环之后,只有长链表的最后一个节点的数可能大于10,则对最后一个节点进行判断,如果大于10,则new一个新的节点,将…

videojs 播放视频

背景&#xff1a;在项目中使用第三方插件videojs进行播放视频&#xff0c;点击事件更改播放的数据源。 一、视频相关理论 (一)、背景 网络流媒体的呈现形式分为两种&#xff1a;直播点播 (二)、流媒体的3种协议 分类&#xff1a;HTTPHLSRTMP定义&#xff1a;基于HTTP的流媒体…

踩坑串口通信 serialPort.RtsEnable = true

背景&#xff1a; 最近在调试一个激光模块&#xff0c;使用的是422通信&#xff0c;然后买了一个485转422的转换器。 通过串口监控软件观察&#xff0c;明明和串口助手发的东西一模一样&#xff0c;但是就是不返回&#xff01; 解决方案&#xff1a; 我加了&#xff0c;这句&…

2023年最新国内八款低代码平台盘点,国内低代码是否已经跑出独角兽?

什么是低代码平台&#xff1f;低代码平台的优势在哪里&#xff1f;低代码平台是否已经形成了自己的核心竞争力&#xff1f;低代码平台是否在国内跑出独角兽&#xff1f;本篇&#xff0c;我们将一起针对上述问题深入浅出的分析低代码平台的特点和前景&#xff0c;并且为大家盘点…

【推荐】深入浅出学习Spring框架【中】

目录 1.AOP是什么? 2.案列&#xff1a; 3.spring的aop的专业术语 4.代码模拟 4.1 前置通知 3.2.后置通知 3.3.环绕通知 3.4.异常通知 3.5.过滤通知 1.AOP是什么? 面向切面编程&#xff08;Aspect-Oriented Programming&#xff09;是一种编程范式&#xff0c;它的主要…

Python面向对象版本贪吃蛇实现

先来一波效果图吧 看看如何设计代码实现 import random import sysimport pygame# 游戏状态 ready 未开始 gameing 游戏中 end 游戏结束class Util:"""工具类&#xff1a; 提供静态方法"""staticmethoddef click_check(sprite):""&…

Java基础篇--错误处理机制

尽管人人希望自己身体健康&#xff0c;处理的事情都能顺利进行&#xff0c;但在实际生活中总会遇到各种状况&#xff0c;比如感冒发烧&#xff0c;工作时电脑蓝屏、死机等。同样&#xff0c;在程序运行的过程中&#xff0c;也会发生各种非正常状况&#xff0c;例如&#xff0c;…

Netty注解实现服务调用

在之前完成了原生服务间的简单通信&#xff0c;现在我们将它整合到Spring环境中&#xff0c;这里就以实现服务的远程调用&#xff0c;简单模拟即可&#xff0c;具体代码需要自己动手改造。 既然是服务调用&#xff0c;那我们就使用代理模式来实现。 新建代理类&#xff0c;这里…

java八股文面试[java基础]—— 重载 和 重写

Java中&#xff0c;有一个名叫方法签名的东西&#xff0c;它的定义是这样的 Definition: Two of the components of a method declaration comprise the method signature—the method’s name and the parameter types. 大概意思是&#xff0c;方法签名有两部分组成——方法…

14、缓存预热+缓存雪崩+缓存击穿+缓存穿透

缓存预热缓存雪崩缓存击穿缓存穿透 ● 缓存预热、雪崩、穿透、击穿分别是什么&#xff1f;你遇到过那几个情况&#xff1f; ● 缓存预热你是怎么做到的&#xff1f; ● 如何避免或者减少缓存雪崩&#xff1f; ● 穿透和击穿有什么区别&#xff1f;它两一个意思还是截然不同&am…

8月16日上课内容 部署LVS-DR群集

本章结构&#xff1a; 数据包流向分析: 数据包流向分析&#xff1a; &#xff08;1&#xff09;客户端发送请求到 Director Server&#xff08;负载均衡器&#xff09;&#xff0c;请求的数据报文&#xff08;源 IP 是 CIP,目标 IP 是 VIP&#xff09;到达内核空间。 &#xf…

Linux 网络发包流程

哈喽大家好&#xff0c;我是咸鱼 之前咸鱼在《Linux 网络收包流程》一文中介绍了 Linux 是如何实现网络接收数据包的 简单回顾一下&#xff1a; 数据到达网卡之后&#xff0c;网卡通过 DMA 将数据放到内存分配好的一块 ring buffer 中&#xff0c;然后触发硬中断CPU 收到硬中…

跨境外贸业务,选择动态IP还是静态IP?

在跨境业务中&#xff0c;代理IP是一个关键工具。它们提供了匿名的盾牌&#xff0c;有助于克服网络服务器针对数据提取设置的限制。无论你是需要经营管理跨境电商店铺、社交平台广告投放&#xff0c;还是独立站SEO优化&#xff0c;代理IP都可以让你的业务程度更加丝滑&#xff…

神经网络基础-神经网络补充概念-54-softmax回归

概念 Softmax回归&#xff08;Softmax Regression&#xff09;是一种用于多分类任务的机器学习算法&#xff0c;特别是在神经网络中常用于输出层来进行分类。它是Logistic回归在多分类问题上的推广。 原理 Softmax回归的主要思想是将原始的线性分数&#xff08;得分&#xf…

【学习日记】【FreeRTOS】任务调度时如何考虑任务优先级——任务的自动切换

写在前面 本文开始为 RTOS 加入考虑任务优先级的自动调度算法&#xff0c;代码大部分参考野火。 本文主要是一篇学习笔记&#xff0c;加入了笔者自己对野火代码的梳理和理解。 一、基本思路 首先我们要知道&#xff0c;在 RTOS 中&#xff0c;优先级越高、越需要被先执行的的…

小程序商品如何指定人员

一般而言&#xff0c;商家小程序中有很多商品&#xff0c;不同商品可能由不同的供应商提供。当客户购买商品时&#xff0c;如何直接将订单发给不同的供应商呢&#xff1f;下面就来具体介绍一下。 1. 设置订单分发模式。在 订单管理->待处理订单 后面点击设置按钮&#xff0…

cve-2016-7193:wwlib 模块堆数据结构溢出

简介 漏洞编号&#xff1a;cve-2016-7193漏洞类型&#xff1a;堆溢出软件名称&#xff1a;Office模块名称&#xff1a;wwlib历史漏洞&#xff1a;较多影响的版本 攻击利用&#xff1a;APT 攻击利器-Word 漏洞 CVE-2016-7193 原理揭秘 操作环境 系统&#xff1a;Win10 1607软…

编译器过程

编译器过程 如果这个框架对应LLVM,为什么这么说LLVM是个框架呢?是因为它提供了中间表示的定义,即前端输出的文本格式定义. 那么 "前端" 可以是两者其一 : Clang 或者 LLVM-GCC "通用优化" 和 "x86后端" 是 LLVM 提供的. // LLVM 也提供 riscv后…

网络机顶盒什么牌子好?自费5000+测评整理网络机顶盒排行榜

在挑选网络机顶盒的时候很多人贪便宜选山寨杂牌&#xff0c;买回家问题频发&#xff0c;我做数码测评几年来身边的朋友们总会问我网络机顶盒什么牌子好&#xff0c;我自费购入了将近二十款网络机顶盒&#xff0c;通过软硬件的全方位对比后整理了网络机顶盒排行榜TOP5&#xff1…

CentOS7配置yum清华源、阿里源

CentOS7配置yum清华源、阿里源 本文为自己安装记录回顾用 下面的是Centos7 更换yum清华源、阿里源 Centos7默认的服务器是在国外&#xff0c;连接很慢。 更换成国内的镜像源&#xff0c;使用yum清华源、阿里源&#xff0c;连接就会快一点 下面介绍更换方法 前提&#xff1a;打…