c++代码
#include <bits/stdc++.h>
#include <xiaohoucode.h>
#include<time.h>
#include<stdlib.h>
#include <unistd.h>
#include <termio.h>
#include <ctype.h>
#include <stdio.h>
#include<math.h>
using namespace std;
int cnt1=0;
int cnt2=0;
bool andchachengchu(int a1)
{
while (cnt1!=10)
{
cnt1++;
srand((unsigned int)time(NULL));
int a = rand() % 100 + 1;
int b = rand() % 100 + 1;
int c = rand() % 4 + 1;
if (c == 1)
{
int da = a + b;
cout << a << " + " << b << " = ";
int n;
cin >> n;
if (n == da)
{
cout << "对了" << endl;
cnt2++;
}
else
{
cout << "错了" << endl;
}
}
if (c == 2)
{
int da = a + b;
cout << a << " + " << b << " = ";
int n;
cin >> n;
if (n == da)
{
cout << "对了" << endl;
cnt2++;
}
else
{
cout << "错了" << endl;
}
}
if (c == 3)
{
double da = a * b;
cout << a << " * " << b << " = ";
int n;
cin >> n;
if (n == da)
{
cout << "对了" << endl;
cnt2++;
}
else
{
cout << "错了" << endl;
}
}
if (c == 4)
{
if (a < b)
{
int temp = a;
a = b;
b = temp;
}
bool flag = false;
for (int i = a; i >= 1; i--)
{
for (int j = b; j >= 1; j--)
{
if (i % j == 0)
{
flag = true;
a = i;
b = j;
break;
}
}
if (flag)
{
break;
}
}
double da = a * 1.0 / b;
cout << a << " / " << b << " = ";
double n;
cin >> n;
if (n == da)
{
cout << "对了" << endl;
cnt2++;
}
else
{
cout << "错了" << endl;
}
}
}
}
int health1 = 5, health2 = 5;
int power1 = 0, power2 = 0;
int cnt = 0;
void intro1()
{
srand(time(NULL));
cout << endl << endl;
cout << "两人回合制游戏,每回合双方同时在“攻击、防御、集气、必杀”四种行动中选一种。" << endl << endl;
// 补充游戏介绍函数
cout << "两人回合制游戏,每回合双方同时在“攻击、防御、集气、必杀”四种行动中选一种。" << endl << endl;
cout << "1. “生命”:每人 5 点生命,生命归 0 立即死亡;" << endl << endl;
cout << "2. “伤害”:攻击造成 1 伤害,防御免除攻击伤害,互相攻击双方都不受伤害,必杀造成 3 伤害;" << endl << endl;
cout << "“必杀”:必杀需消耗 2“气”,双方同时必杀则相安无事,否则放必杀者造成伤害,对手的行动无任何效果;" << endl << endl;
cout << "4. “气”:防御对方攻击增加 1 气,集气且不受到伤害增加 1 气。" << endl << endl;
cout << "----------单挑开始,你是武将1,电脑指挥武将2----------" << endl << endl;
}
// 人的策略函数
namespace human
{
int strategy()
{
// 输出出卡提示
cout << "用数字代表本回合武将1的行动:1攻击,2防御,3集气,4必杀" << endl;
// 读入人的策略
int x;
cin >> x;
return x;
}
}
// 片段4 确定电脑策略
// 确定电脑策略
namespace ai
{
int strategy()
{
//补充函数:产生1~3的随机数,并返回
if (power2 >= 2)return 4;
{
return rand() % 3 + 1;
}
}
}
//片段5 根据双方行动, 执行一回合的对战
bool fight(int d1, int d2)
{
if (d1 < 1 || d1 > 4)
{
cout << "【【【武将1没有这种策略啦~重新开始回合" << cnt << "!】】】" << endl;
return true;
}
if ((d1 == 4 && power1 < 2))
{
cout << "【【【武将1不够气来放必杀!重新开始回合" << cnt << "!】】】" << endl;
return true;
}
cout << "【【【" ;
if (d1 == 1 && d2 == 1)
{
cout << "两人同时攻击,相安无事";
}
if (d1 == 1 && d2 == 2)
{
power2++;
cout << "武将2防御了武将1的攻击,得1气";
}
if (d1 == 2 && d2 == 1)
{
power1++;
cout << "武将1防御了武将2的攻击,得1气";
}
if (d1 == 2 && d2 == 2)
{
cout << "两人同时防御,相安无事";
}
if (d1 == 1 && d2 == 3)
{
health2--;
cout << "武将2集气时受到武将1的攻击,损失1生命";
}
if (d1 == 3 && d2 == 1)
{
health1--;
cout << "武将1集气时受到武将2的攻击,损失1生命";
}
if (d1 == 2 && d2 == 3)
{
power2++;
cout << "武将2趁武将1防御时集气,得1气";
}
if (d1 == 3 && d2 == 2)
{
power1++;
cout << "武将1趁武将2防御时集气,得1气";
}
if (d1 == 3 && d2 == 3)
{
power1++;
power2++;
cout << "双方同时集气,各得1气";
}
if (d1 == 4 && d2 != 4)
{
power1 -= 2;
health2 -= 3;
cout << "武将1使出必杀,对对手造成3伤害";
}
if (d1 != 4 && d2 == 4)
{
power2 -= 2;
health1 -= 3;
cout << "武将2使出必杀,对对手造成3伤害";
}
if (d1 == 4 && d2 == 4)
{
power1 -= 2;
power2 -= 2;
cout << "双方同时必杀,相安无事";
}
cout << "】】】" << endl;
return false;
}
//片段6 本回合战后统计
void result()
{
cout << "【双方状态】" << endl;
cout << "*武将1: 生命" << health1 << " 气" << power1 << endl;
cout << "*武将2: 生命" << health2 << " 气" << power2 << endl << endl;
}
//片段7 公布对战结果, winner == 1 代表武将1胜, winner == 2 代表武将2胜
void showWinner(int winner)
{
cout << endl << "【单挑结束!!!!!】" << endl;
if (winner == 1) cout << "武将1击败对手获胜!!!!" << endl;
else cout << "武将2击败对手获胜!!!!" << endl;
cout << endl << endl;
}
//片段2 进行游戏
int pk()
{
for(;;)
{
// 一方倒下游戏结束
if (health1 <= 0) return 2;
if (health2 <= 0) return 1;
cout << "【开始回合" << ++cnt << "】" << endl;
// 片段3 确定人类策略,
int human = human::strategy();
// 片段4 确定电脑策略
int ai = ai::strategy();
//片段5 对战
bool err = fight(human, ai);
//片段6 本回合战后统计
if (!err) result();
else cnt--;
}
}
int a[230];
string d[3]={" ","O","X"};
int correct, wrong;
int getch()
{// 不回显的输入
struct termios nts, ots;
// 得到当前终端(0表示标准输入)的设置
if (tcgetattr(0, &ots) < 0) return EOF;
// 设置终端为Raw原始模式,该模式下所有的输入数据以字节为单位被处理
nts = ots;
cfmakeraw(&nts);
// 设置上更改之后的设置
if (tcsetattr(0, TCSANOW, &nts) < 0) return EOF;
// 设置还原成老的模式
int cr;
cr = getchar();
if (tcsetattr(0, TCSANOW, &ots) < 0) return EOF;
return cr;
}
int print(int black)
{// 打印0-9数字 black数字标黑
cout << "\033[0;0H";
for (int i = 1; i <= 10; i++)
{
if (i%10 == black)
cout << "\033[7m " << i%10 << " \033[0m";
else
cout << "\033[47;30m " << i%10 << " \033[0m";
}
}
int print_keyboard(char ch, string color)
{// 打印键盘 ch字符设定为color颜色,其余默认
char key[5][15] = {
"1234567890-=",
"QWERTYUIOP[]\\",
"ASDFGHJKL;'",
"ZXCVBNM,./"
};
cout << "\033[2;1H";
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < i; j++)
{
cout << " ";
}
for (int j = 0; key[i][j] != 0; j++)
{
if (ch == key[i][j])
{
if (color == "black")
cout << "\033[7m " << key[i][j] << " \033[0m";
else if (color == "green")
cout << "\033[42;30m " << key[i][j] << " \033[0m";
else if (color == "red")
cout << "\033[41;30m " << key[i][j] << " \033[0m";
}
else
cout << "\033[47;30m " << key[i][j] << " \033[0m";
}
cout << endl;
}
}
int typewritting()
{// 打字过程,返回总用时
// 键盘字符集
char s[] = "1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
int len = strlen(s);
srand(time(0));
correct = 0;
wrong = 0;
int sta = time(0);
const int TIMES = 30; // 出题数
int last = 0;
for (int i = 1; i <= TIMES; i++)
{
cout << "\033c" << flush;
cout << "对: " << correct << " 错: " << wrong << "\n";
// 抽取键盘字符集下标
int r = rand() % len;
// 避免连续两次按键相同
while (r == last) r = rand() % len;
last = r;
print_keyboard(s[r], "black");
char ch = getch();
if (ch == s[r] || (islower(ch) && ch == tolower(s[r])))
{
print_keyboard(s[r], "green");
correct++;
}
else
{
if (islower(ch))
print_keyboard(toupper(ch), "red");
else
print_keyboard(ch, "red");
wrong++;
}
usleep(100000);
}
int end = time(0);
return end - sta;
}
void intro()
{// 规则介绍
cout << "\033c" << flush;
cout << "欢迎使用C++打字通!" << endl;
sleep(1);
cout << "开始后,你需要用最快的速度按下黑色字符所对应的按键。" << endl;
sleep(1);
cout<<"共30个字符。"<<endl;
sleep(1);
cout << "*注意:请将输入法改为英文!" << endl;
sleep(1);
cout << "准备好了嘛?" << endl;
cout << "\033c" << flush;
cout << "3" << endl;
sleep(1);
cout << "2" << endl;
sleep(1);
cout << "1" << endl;
sleep(1);
cout << "开始!" << endl;
usleep(300000);
}
// 问题和答案的结构体
struct Question {
string question;
string answer;
};
// 初始化问题和答案
void initQuestions(Question questions[]) {
questions[0] = {"What is the capital of France?", "Paris"};
questions[1] = {"What is the largest planet in our solar system?", "Jupiter"};
questions[2] = {"Which country is known as the Land of the Rising Sun?", "Japan"};
questions[3] = {"What is the currency of China?", "Yuan"};
questions[4] = {"Is orange round?", "Yes"};
questions[5] = {"What is the chemical symbol for gold?", "Au"};
questions[6] = {"Is James a basketball player?", "Yes"};
questions[7] = {"Usain Bolt is fast or slow?", "fast"};
questions[8] = {"Which animal is known as the 'King of the Jungle'?", "Lion"};
questions[9] = {"What is the day between Monday and Wednesday?", "Tuesday"};
}
const int ROW = 10;
const int COL = 10;
const int MINE_NUM = 10;
vector<vector<int>> mineMap(ROW, vector<int>(COL, 0));
vector<vector<int>> showMap(ROW, vector<int>(COL, -1));
bool checkBound(int x, int y)
{
if (x < 0 || x >= ROW || y < 0 || y >= COL) return false;
return true;
}
void randomMine()
{
int count = 0;
while (count < MINE_NUM) {
int x = rand() % ROW;
int y = rand() % COL;
if (mineMap[x][y] == 0) {
mineMap[x][y] = 9;
count++;
}
}
}
int calCount(int x, int y)
{
int count = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (checkBound(i, j) && mineMap[i][j] == 9) count++;
}
}
return count;
}
void spread(int x, int y)
{
if (!checkBound(x, y) || showMap[x][y] > -1 || mineMap[x][y] == 9) return;
int count = calCount(x, y);
showMap[x][y] = count;
if (count == 0) {
spread(x - 1, y - 1);
spread(x - 1, y);
spread(x - 1, y + 1);
spread(x, y - 1);
spread(x, y + 1);
spread(x + 1, y - 1);
spread(x + 1, y);
spread(x + 1, y + 1);
}
}
void printMap()
{
for (int i = 0; i < ROW; i++)
{
cout << i << " ";
for (int j = 0; j < COL; j++)
{
if (showMap[i][j] == -1)
{
cout << "* ";
}
else if (showMap[i][j] == 0)
{
cout << " ";
}
else if (showMap[i][j] == 10)
{
cout << "X ";
}
else
{
cout << showMap[i][j] << " ";
}
}
cout << endl;
}
}
bool checkWin()
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (showMap[i][j] == -1 && mineMap[i][j] != 9)
{
return false;
}
}
}
return true;
}
int program()
{
cout << "想体验哪种模式(1 : 挑战模式,2 : 练习模式):" << "\n";
int x;
cin >> x;
if(x == 1)
{
srand((unsigned)time(NULL));
randomMine();
while (true) {
cout << " 0 1 2 3 4 5 6 7 8 9\n";
printMap();
char cx, cy;
cout << "请输入要打开的位置坐标(如0(x)1(y)),或输入-1结束游戏:" << endl;
cin >> cx >> cy;
int x = cx - '0',y = cy - '0';
if(x == 12345666787878 && y == 45456663222) {
cout << "你赢了!高手高高手!" << endl;
break;
}
if (x == -1 || y == -1) {
break;
}
if (mineMap[x][y] == 9) {
cout << "你输了,请再来一局吧!" << endl;
break;
} else {
spread(x, y);
if (checkWin()) {
cout << "你赢了!高手高高手!" << endl;
break;
}
}
}
}
else if(x == 2)
{
bool f = 1;
while(f = 1)
{
srand((unsigned)time(NULL));
randomMine();
while (true)
{
cout << " 0 1 2 3 4 5 6 7 8 9\n";
printMap();
char cx = '^',cy = '^';
cout << "请输入要打开的位置坐标(如0(x)1(y)),或输入-1结束游戏:" << endl;
cin >> cx >> cy;
int x = cx - '0',y = cy - '0';
if(x == 12345666787878 && y == 45456663222)
{
cout << "你赢了!高手高高手!" << endl;
break;
}
if (x == -1 || y == -1)
{
break;
}
if (mineMap[x][y] == 9)
{
cout << "你输了,是否再试试有没有其他方法吧!(Yes/No):";
string s;
cin >> s;
if(s != "Yes")
{
return 1;
}
}
else
{
spread(x, y);
}
}
}
return 1;
}
}
void recursion()
{
if(program() > -1)
{
cout << "是否继续(Yes/No):";
string s;
cin >> s;
if(s == "Yes")
{
mineMap.assign(ROW, vector<int>(COL, 0));
showMap.assign(ROW, vector<int>(COL, -1));
recursion();
}
}
}
int main()
{
long long B;
int A;
B=10000000000;
for(int j=0;j<1;B=100000000000)
{
cout<<"请选择你想玩的游戏:"<<endl<<"1、趣味22题 2、石头剪刀布 3、计算小达人 4、计算器 5、牛人游戏 6、打字不要后悔 7、金币 8、无头小子照片 9、学霸才做的加法题 10、火影忍者 11、欢迎贵宾玩扫雷 12、英语回答问题 13、愤怒的皮皮 14、打字游戏 15、战争世界 16、双人五子棋 17、小伙见到一沓钱 18、计算(除法为整数除法,只要余数) 19、武将pk 20、猜数 21、死亡空格 22、循环诗 23、打印单位矩阵 24、计算 25、运算 26、打印数字金字塔 27、ASCII码查询(密码114666888)。如果不想玩了输入0 (注意:不要乱输入字符、21和22谨慎使用)<( ̄︶ ̄)↗[GO!]"<<endl;
cin>>A;
if(A==3)
{
clear();
sleep(1);
srand(time(0));
int tsum = 0;
int maxt = 0;
long long mint = 99999999999;
int wrongCount = 0, rightCount = 0;
for (int i = 1; i <= 10 ; i++) //请写出循环条件
{
long double a = rand() % 10 + 1;
long double b = rand() % 10 + 1;
long double op = rand() % 4 + 1;
int res;
if (op == 1) // 加法
{
cout << a << " + " << b << " = " << endl;
res = a + b;
}
if (op == 2) // 减法
{
if (a < b)
{
int c = a;
a = b;
b = c;
}
cout << a << " - " << b << " = " << endl;
res = a - b;
}
if (op == 3) // 乘法
{
cout << a << " * " << b << " = " << endl;
res = a * b;
}
if (op == 4) // 除法
{
cout << a * b << " / " << a << " = " << endl;
res = b;
}
int ans, beg = time(0); // 开始时间
cout << "请输入你的计算结果:" << endl;
cin >> ans;
if (ans == res)
{
cout << "(^-^)V 答对了!" << endl;
rightCount++;
}
else
{
cout << "┭┮﹏┭┮ 答错了!" << endl;
wrongCount++;
}
cout << endl;
int end = time(0); // 结束时间
tsum += end - beg;
if (end - beg > maxt)
{
maxt = end - beg;
}
if(mint>end-beg)
{
mint=end-beg;
}
cout << "本题用时:" << end - beg << " 秒" << endl << endl;
sleep(1);
clear();
sleep(1);
}
cout << "( ̄▽ ̄)~* 游戏结束!" << endl;
cout << "总计:" << rightCount * 10.0<< " 分" <<endl;
cout << "对:" << rightCount << " 道" << endl;
cout << "错:" << wrongCount << " 道" << endl;
cout << "总用时:" << tsum << " 秒" << endl;
cout << "平均用时:" << tsum / 10.0 << " 秒" << endl;
cout << "最慢用时:" << maxt <<"秒"<< endl;
cout << "最快用时:" << mint <<"秒"<< endl;
}
else if(A==2)
{
int e1,u,h;
cout<<"和垃圾版AI玩石头剪刀布,你想要几局几胜?"<<endl;
cout<<"1、三局两胜 2、五局三胜 3、七局四胜";
cin>>e1;
if(e1==1)
{
e1=3;
}
else if(e1==2)
{
e1=5;
}
else if(e1==3)
{
e1=7;
}
else if(e1>3)
{
e1=3;
}
else
{
e1=7;
}
// 任务1 玩家出招
for(int u=0;u<e1;u++)
{
cout << "和AI玩石头剪刀布,请输入你的选择:"<< endl;
cout << "1、石头" << endl;
cout << "2、剪刀" << endl;
cout << "3、布" << endl;
int a1;
cin >> a1;
if(a1>3)
{
a1=a1%3+1;
}
if(a1== 1)
{
cout << "你出的是石头" << endl;
}
if(a1== 2)
{
cout << "你出的是剪刀" << endl;
}
if(a1 == 3)
{
cout << "你出的是布" << endl;
}
// 任务2 AI出招
srand(time(0)); //评测时请注释掉这行数据
int b1 = rand()%3 +1;
if(b1 == 1)
{
cout << "AI出的是石头" << endl;
}
if(b1== 2)
{
cout << "AI出的是剪刀" << endl;
}
if(b1 == 3)
{
cout << "AI出的是布" << endl;
}
// 任务3 判断胜负
// 使用if嵌套来完成判断
if(a1==b1)
{
cout<<"平手"<<endl;
}
else
{
if(a1<b1)
{
if(b1-a1==2)
{
h+=1;
cout<<"你输了";
}
else
{
u+=1;
cout<<"你赢了";
}
}
else
{
if(a1-b1==2)
{
u+=1;
cout<<"你赢了";
}
else
{
h+=1;
cout<<"你输了";
}
}
}
}
if(u>=h)
{
cout<<"总场结算:你赢了。";
}
else
{
cout<<"总场结算:你输了。";
}
}
else if(A==1)
{
int a,b,c,d;
long double e;//a是选择,b是做对题目数,c是想做的题目数,d是你想做的关卡;
cout<<"请选择你想做的题目数:";
cin>>c;
b=0;
for(int i=1;i<=c;i++)
{
cout<<"第"<<i<<"次答题,请选择你想做的关卡(一共22题)";
cin>>d;
if(d>22)
d=d%22+1;
if(d==1)
{
cout<<"请问地球
标签:a%,游戏,int,t2,c++,else,time,分享,cout
From: https://blog.csdn.net/2401_88851881/article/details/143658951