# 《信任的进化》游戏简易版逻辑算法的实现（C语言）

## 一.每种角色的初始化并打印菜单

7个源文件，分别为执行文件，函数文件，和5个分别存储各自角色代码的文件。

struct player
{
int score;//硬币的数量
int choice;//每个人的选择，1为合作，0为欺骗
int n;//轮数
char name[100];//用来存储自己的名字
};

//初始化复读机
void InitFuduji(struct player* Fuduji)
{
Fuduji->choice = 1;
Fuduji->score = 0;
Fuduji->n = 0;
strcpy(Fuduji->name, "Fuduji");
}

//对小红进行初始化
void Initxiaohong(struct player* xiaohong)
{
xiaohong->choice = 1;
xiaohong->score = 0;
xiaohong->n = 0;
strcpy(xiaohong->name, "xiaohong");
}

//对黑帮进行初始化
void Initheibang(struct player* heibang)
{
heibang->choice = 1;
heibang->score = 0;
heibang->n = 0;
strcpy(heibang->name, "heibang");
}

//初始化福尔摩斯
void Initfuer(struct player* fuer)
{
fuer->choice = 1;
fuer->score = 0;
fuer->n = 1;//福尔摩斯需要记录轮数，所以初始化轮数为1
strcpy(fuer->name, "fuer");
}

//打印游戏的开头
printf("欢迎来到信任游戏模拟\n");
struct player Fuduji;
struct player xiaohong;
struct player youtiao;
struct player heibang;
struct player fuer;
//初始化每种角色
InitFuduji(&Fuduji);
Initxiaohong(&xiaohong);
Inityoutiao(&youtiao);
Initheibang(&heibang);
Initfuer(&fuer);
printf("注意，人数请不要输入负数,否则游戏会退出\n");
printf("请输入复读机的数量:>");
int Fuduji_num = 0;
scanf("%d", &Fuduji_num);
printf("请输入小红的数量:>");
int xiaohong_num = 0;
scanf("%d", &xiaohong_num);
printf("请输入油条的数量:>");
int youtiao_num = 0;
scanf("%d", &youtiao_num);
printf("请输入黑帮的数量:>");
int heibang_num = 0;
scanf("%d", &heibang_num);
printf("请输入福尔摩斯的数量:>");
int fuer_num = 0;
scanf("%d", &fuer_num);
//判断人数输入是否有误
if (fuer_num < 0 || xiaohong_num < 0 || youtiao_num < 0 || heibang_num < 0 || Fuduji_num < 0)
{
printf("输入人数错误，游戏退出\n");
exit(-1);
}
if (fuer_num==0 && xiaohong_num == 0 && youtiao_num==0 && heibang_num==0 &&Fuduji_num== 0)
{
printf("游戏结束！\n");
exit(-1);
}

void menu(struct player* Fuduji, struct player* xiaohong, struct player* youtiao, struct player* heibang, struct player* fuer, int Fuduji_num, int xiaohong_num, int youtiao_num, int heibang_num, int fuer_num)
{
printf("------------------------------------------------------------\n");
printf("-----复读机:%d --------------------小红:%d----------------\n",Fuduji_num,xiaohong_num);
printf("-----油条:%d ----------------------黑帮:%d------------------\n",youtiao_num,heibang_num);
printf("-----福尔摩斯:%d -------------------------------------------\n",fuer_num);
printf("------------------------------------------------------------\n");
}

## 二.比赛的逻辑

void isscore(struct player* p1, struct player* p2)//判断胜负的函数
{
if (p1->choice == 1 && p2->choice == 0)
{
p2->score += 3;
p1->score -= 1;
}
if (p2->choice == 1 && p1->choice == 0)
{
p1->score += 3;
p2->score -= 1;
}
if (p1->choice == 1 && p2->choice == 1)
{
p2->score += 2;
p1->score += 2;
}
}

isscore(p1, p2);
//实现复读机的函数
if (strcmp(p1->name, "Fuduji") == 0)
{
p1->choice = p2->choice;
}
if (strcmp(p2->name, "Fuduji") == 0)
{
p2->choice = p1->choice;
}
//实现黑帮老大的函数
if ((strcmp(p1->name, "heibang") == 0) && (p2->choice == 0))
{
p1->choice = 0;
}
if ((strcmp(p2->name, "heibang") == 0) && (p1->choice == 0))
{
p2->choice = 0;
}

//注:int fuerp=0;已经在比赛函数之外定义了，因为之后还有别的函数需要用到
//实现福尔摩斯函数
//判断是否反击
if ((strcmp(p1->name, "fuer") == 0) && ((p1->n == 2) || (p1->n == 3) || (p1->n == 4) || (p1->n == 1)))
{
if (p2->choice == 0)
{
fuerp = 1;
}
}
if ((strcmp(p2->name, "fuer") == 0) && ((p2->n == 2) || (p2->n == 3) || (p2->n == 4) || (p2->n == 1)))
{
if (p1->choice == 0)
{
fuerp = 1;
}
}
//在不同轮数做出不同选择
if ((strcmp(p1->name, "fuer") == 0) && p1->n == 2)
{
p1->choice = 0;
}
if ((strcmp(p2->name, "fuer") == 0) && p2->n == 2)
{
p2->choice = 0;
}
if ((strcmp(p2->name, "fuer") == 0) && ((p2->n == 3) || (p2->n == 4)))
{
p2->choice = 1;
}
if ((strcmp(p1->name, "fuer") == 0) && ((p1->n == 3) || (p1->n == 4)))
{
p1->choice = 1;
}
if ((strcmp(p2->name, "fuer") == 0) && (p2->n > 4))
{
if (fuerp == 1)
{
p2->choice = p1->choice;
}
else if (fuerp == 0)
{
p2->choice = 0;
}
}
if ((strcmp(p1->name, "fuer") == 0) && (p1->n > 4))
{
if (fuerp == 1)
{
p1->choice = p2->choice;
}
else if (fuerp == 0)
{
p1->choice = 0;
}
}
isscore(p1, p2);
//将轮数加1
if (strcmp(p1->name, "fuer") == 0)
{
p1->n++;
}
if (strcmp(p2->name, "fuer") == 0)
{
p2->n++;
}

//两者比赛
void paly(struct player* p1, struct player* p2)
{
//实现福尔摩斯函数
//判断是否反击
if ((strcmp(p1->name, "fuer") == 0) && ((p1->n == 2) || (p1->n == 3) || (p1->n == 4) || (p1->n == 1)))
{
if (p2->choice == 0)
{
fuerp = 1;
}
}
if ((strcmp(p2->name, "fuer") == 0) && ((p2->n == 2) || (p2->n == 3) || (p2->n == 4) || (p2->n == 1)))
{
if (p1->choice == 0)
{
fuerp = 1;
}
}
if ((strcmp(p1->name, "fuer") == 0) && p1->n == 2)
{
p1->choice = 0;
}
if ((strcmp(p2->name, "fuer") == 0) && p2->n == 2)
{
p2->choice = 0;
}
if ((strcmp(p2->name, "fuer") == 0) && ((p2->n == 3) || (p2->n == 4)))
{
p2->choice = 1;
}
if ((strcmp(p1->name, "fuer") == 0) && ((p1->n == 3) || (p1->n == 4)))
{
p1->choice = 1;
}
if ((strcmp(p2->name, "fuer") == 0) && (p2->n > 4))
{
if (fuerp == 1)
{
p2->choice = p1->choice;
}
else if (fuerp == 0)
{
p2->choice = 0;
}
}
if ((strcmp(p1->name, "fuer") == 0) && (p1->n > 4))
{
if (fuerp == 1)
{
p1->choice = p2->choice;
}
else if (fuerp == 0)
{
p1->choice = 0;
}
}
isscore(p1, p2);
if (strcmp(p1->name, "fuer") == 0)
{
p1->n++;
}
if (strcmp(p2->name, "fuer") == 0)
{
p2->n++;
}
//实现复读机的函数
if (strcmp(p1->name, "Fuduji") == 0)
{
p1->choice = p2->choice;
}
if (strcmp(p2->name, "Fuduji") == 0)
{
p2->choice = p1->choice;
}
//实现黑帮老大的函数
if ((strcmp(p1->name, "heibang") == 0) && (p2->choice == 0))
{
p1->choice = 0;
}
if ((strcmp(p2->name, "heibang") == 0) && (p1->choice == 0))
{
p2->choice = 0;
}
}

## 三.完成一次比赛之后的成员选择，轮数恢复

void hireplayer(struct player* p1, struct player* p2)
{
if (strcmp(p1->name, "Fuduji") == 0)
{
p1->choice = 1;
}
if (strcmp(p2->name, "Fuduji") == 0)
{
p2->choice = 1;
}
if (strcmp(p2->name, "heibang") == 0)
{
p2->choice = 1;
}
if (strcmp(p1->name, "heibang") == 0)
{
p1->choice = 1;
}
if (strcmp(p1->name, "fuer") == 0)
{
p1->choice = 1;
p1->n = 1;
fuerp = 0;
}
if (strcmp(p2->name, "fuer") == 0)
{
p2->choice = 1;
p2->n = 1;
fuerp = 0;
}
}

## 四.一次比赛中，成员多次比赛后数据的恢复

struct player* playxiaohong(struct player* Fuduji, struct player* xiaohong, struct player* youtiao, struct player* heibang, struct player* fuer, int Fuduji_num, int xiaohong_num, int youtiao_num, int heibang_num, int fuer_num)
{
int i = 0;
if (xiaohong_num == 0)
{
return xiaohong;
}
for (i = 0; i < Fuduji_num; i++)
{
paly(Fuduji, xiaohong);
}
hireplayer(Fuduji, xiaohong);
for (i = 0; i < xiaohong_num - 1; i++)
{
paly(xiaohong, xiaohong);
}
hireplayer(xiaohong, xiaohong);
for (i = 0; i < youtiao_num; i++)
{
paly(xiaohong, youtiao);
}
hireplayer(xiaohong, youtiao);
for (i = 0; i < heibang_num; i++)
{
paly(xiaohong, heibang);
}
hireplayer(xiaohong, heibang);
for (i = 0; i < fuer_num; i++)
{
paly(xiaohong, fuer);
}
hireplayer(xiaohong, fuer);
return xiaohong;
InitFuduji(&Fuduji);
Initxiaohong(&xiaohong);
Inityoutiao(&youtiao);
Initheibang(&heibang);
Initfuer(&fuer);
}

void Competition(struct player* Fuduji, struct player* xiaohong, struct player* youtiao, struct player* heibang, struct player* fuer, int Fuduji_num, int xiaohong_num, int youtiao_num, int heibang_num, int fuer_num)
{
int i = 0;
int j = 0;
struct player* newFuduji=playFuduji(Fuduji, xiaohong, youtiao, heibang, fuer, Fuduji_num, xiaohong_num, youtiao_num, heibang_num, fuer_num);
struct player* newxiaohong = playxiaohong(Fuduji, xiaohong, youtiao, heibang, fuer, Fuduji_num, xiaohong_num, youtiao_num, heibang_num, fuer_num);
struct player* newyoutiao = playyoutiao(Fuduji, xiaohong, youtiao, heibang, fuer, Fuduji_num, xiaohong_num, youtiao_num, heibang_num, fuer_num);
struct player* newheibang = playheibang(Fuduji, xiaohong, youtiao, heibang, fuer, Fuduji_num, xiaohong_num, youtiao_num, heibang_num, fuer_num);
struct player* newfuer = playfuer(Fuduji, xiaohong, youtiao, heibang, fuer, Fuduji_num, xiaohong_num, youtiao_num, heibang_num, fuer_num);
Fuduji = newFuduji;
xiaohong = newxiaohong;
youtiao = newyoutiao;
heibang = newheibang;
fuer = newfuer;
}

## 五.比赛一次后增多减少

struct player* max = (struct player*)malloc(sizeof(struct player));
struct player* min = (struct player*)malloc(sizeof(struct player));
max->score = -1000;
min->score = 2147483647;
//判断最大
if (Fuduji->score > max->score)
{
max->score= Fuduji->score;
}
if (xiaohong->score > max->score)
{
max->score = xiaohong->score;
}
if (youtiao->score > max->score)
{
max->score = youtiao->score;
}
if (heibang->score > max->score)
{
max->score = heibang->score;
}
if (fuer->score > max->score)
{
max->score = fuer->score;
}
//判断最小
if (Fuduji->score < min->score)
{
min->score = Fuduji->score;
}
if (xiaohong->score < min->score)
{
min->score = xiaohong->score;
}
if (youtiao->score < min->score)
{
min->score = youtiao->score;
}
if (heibang->score < min->score)
{
min->score = heibang->score;
}
if (fuer->score < min->score)
{
min->score = fuer->score;
}
//找最大最小
//找最小
if (Fuduji->score == min->score)
{
*Fuduji_num-=1;
}
if (xiaohong->score == min->score)
{
*xiaohong_num -= 1;
}
if (heibang->score == min->score)
{
*heibang_num -= 1;
}
if (fuer->score == min->score)
{
*fuer_num -= 1;
}
if (youtiao->score == min->score)
{
*youtiao_num -= 1;
}
//找最大
if ((Fuduji->score == max->score) && (*Fuduji_num != 0))
{
*Fuduji_num+=1;
}
if ((xiaohong->score == max->score) && (*xiaohong_num != 0))
{
*xiaohong_num += 1;
}
if ((heibang->score == max->score) && (*heibang_num != 0))
{
*heibang_num+=1;
}
if (fuer->score == max->score)
{
*fuer_num += 1;
}
if ((youtiao->score == max->score) && (*youtiao_num != 0))
{
*youtiao_num += 1;
}
}

## 六.比赛过后打印菜单与游戏的最终效果

|
1天前
|

【经典LeetCode算法题目专栏分类】【第9期】深度优先搜索DFS与并查集：括号生成、岛屿问题、扫雷游戏
【经典LeetCode算法题目专栏分类】【第9期】深度优先搜索DFS与并查集：括号生成、岛屿问题、扫雷游戏
4 0
|
1天前
|

【️C语言-游戏设置】---三子棋（N满）
【️C语言-游戏设置】---三子棋（N满）
4 0
|
1天前
|
C语言
️️【C语言-游戏设置】---猜数字游戏1~100
️️【C语言-游戏设置】---猜数字游戏1~100
7 0
|
5天前
|

LeetCode题目55:跳跃游戏【python5种算法贪心/回溯/动态规划/优化贪心/索引哈希映射 详解】
LeetCode题目55:跳跃游戏【python5种算法贪心/回溯/动态规划/优化贪心/索引哈希映射 详解】
15 4
|
5天前
|
SQL 算法 数据可视化
python 贪心算法 动态规划实现 跳跃游戏ll【力扣题45】
python 贪心算法 动态规划实现 跳跃游戏ll【力扣题45】
5 0
|
8天前
|

9 0
|
8天前
|

C语言学习记录——操作符详解知识点选记（算术操作符、单目操作符、移位操作符、关系操作符、逻辑操作符、条件操作符......）二
C语言学习记录——操作符详解知识点选记（算术操作符、单目操作符、移位操作符、关系操作符、逻辑操作符、条件操作符......）二
15 3
|
8天前
|

C语言学习记录——操作符详解知识点选记（算术操作符、单目操作符、移位操作符、关系操作符、逻辑操作符、条件操作符......）一
C语言学习记录——操作符详解知识点选记（算术操作符、单目操作符、移位操作符、关系操作符、逻辑操作符、条件操作符......）一
17 1
|
9天前
|
C语言
C语言——oj刷题——猜数字游戏
C语言——oj刷题——猜数字游戏
|
9天前
|
C语言