Initial commit: C++ courseware
This commit is contained in:
commit
4a4ee7c287
1789 files changed
+373489
No files matched your search
@@ -0,0 +1,122 @@
|
||||
//
|
||||
// Created by xiaolinquan on 2024/12/13.
|
||||
//
|
||||
#include <bits/stdc++.h>
|
||||
#include <windows.h>
|
||||
using namespace std;
|
||||
|
||||
|
||||
void setColor(int color) {
|
||||
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
SetConsoleTextAttribute(hConsole, color);
|
||||
}
|
||||
|
||||
int main() {
|
||||
// 提示用户输入雷区大小
|
||||
cout << "请输入您想要的雷区大小: ";
|
||||
int n, mineCount = 0;
|
||||
cin >> n; // 读取用户输入的雷区大小
|
||||
|
||||
// 声明雷区数组并初始化
|
||||
char mineArea[n][n];
|
||||
|
||||
// 初始化随机数种子
|
||||
srand((unsigned int)time(0));
|
||||
|
||||
// 随机初始化雷区
|
||||
// 遍历每个方格,随机放置地雷,20%的概率放置地雷
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (rand() % 5 == 0) { // 1/5的概率放置地雷
|
||||
mineArea[i][j] = '*'; // 用 '*' 表示地雷
|
||||
mineCount++; // 统计地雷数量
|
||||
} else {
|
||||
mineArea[i][j] = '0'; // 用 '0' 表示无地雷
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化每个方格周围的地雷数
|
||||
|
||||
int dx[3] = {-1, 0, 1}; // x方向上的偏移量
|
||||
int dy[3] = {-1, 0, 1}; // y方向上的偏移量
|
||||
|
||||
// 遍历雷区,为每个不是地雷的方格计算周围地雷的数量
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (mineArea[i][j] == '*') {
|
||||
// 对于每一个地雷,更新其周围8个方格的地雷计数
|
||||
for (int a = 0; a < 3; a++) {
|
||||
for (int b = 0; b < 3; b++) {
|
||||
int x = dx[a] + i;
|
||||
int y = dy[b] + j;
|
||||
// 检查是否在边界内并不是地雷的方格
|
||||
if (x >= 0 && x < n && y >= 0 && y < n && mineArea[x][y] != '*') {
|
||||
mineArea[x][y]++; // 增加相邻地雷计数
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 打印初始化后的雷区
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
cout << setw(3) << mineArea[i][j]; // 格式化输出,保证对齐
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
// 初始化用户视图数组,所有方格初始为未揭示状态 '?'
|
||||
char userArea[n][n];
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
userArea[i][j] = '?';
|
||||
}
|
||||
}
|
||||
|
||||
int remainSquareCount = n * n; // 剩余未揭示方格数量
|
||||
while (mineCount != remainSquareCount) {
|
||||
// 提示用户输入要探查的方格位置
|
||||
cout << "请输入你想要探查方格的位置,横坐标在前,纵坐标在后,以空格隔开:";
|
||||
int x, y;
|
||||
cin >> x >> y;
|
||||
x--; y--; // 调整为0开始的索引
|
||||
|
||||
// 检查输入是否合法
|
||||
if (x < 0 || x >= n || y < 0 || y >= n) {
|
||||
cout << "输入无效,请输入有效坐标。" << endl;
|
||||
continue; // 跳过当前循环,重新输入
|
||||
}
|
||||
|
||||
// 检查是否踩到地雷
|
||||
if (mineArea[x][y] == '*') {
|
||||
cout << "踩到雷了!游戏结束!";
|
||||
|
||||
return 0; // 结束程序
|
||||
} else {
|
||||
userArea[x][y] = mineArea[x][y]; // 更新用户视图
|
||||
}
|
||||
|
||||
// 打印当前用户视图
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (i == x && j == y) {
|
||||
setColor(4); // 红色显示用户最新选择
|
||||
} else if (userArea[i][j] != '?') {
|
||||
setColor(1); // 蓝色显示已揭示的安全区
|
||||
} else {
|
||||
setColor(7); // 白色显示未揭示区域
|
||||
}
|
||||
cout << setw(3) << userArea[i][j]; // 格式化输出
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
remainSquareCount--; // 减少剩余未揭示方格数量
|
||||
}
|
||||
|
||||
// 如果循环结束且所有安全区已揭示,用户胜利
|
||||
cout << "恭喜你!!游戏成功!!";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
|
||||
struct Hero{
|
||||
int life = 10000;
|
||||
int attack = 10;
|
||||
};
|
||||
|
||||
struct Monster{
|
||||
int life = 10000;
|
||||
int attack = 100;
|
||||
};
|
||||
|
||||
int main(){
|
||||
Hero h; Monster m;
|
||||
cout << "Hero的血量内存地址为:" << &h.life << endl;
|
||||
cout << "Hero的攻击力内存地址为:" << &h.attack << endl;
|
||||
cout << "Monster的血量内存地址为:" << &m.life << endl;
|
||||
cout << "Monster的攻击力内存地址为:" << &m.attack << endl;
|
||||
system("pause");
|
||||
int count = 0;
|
||||
while(true){
|
||||
if(m.life <= 0){
|
||||
cout << "玩家打败了怪物!" << endl;
|
||||
break;
|
||||
}
|
||||
if(h.life <= 0){
|
||||
cout << "怪物打败了玩家!" << endl;
|
||||
break;
|
||||
}
|
||||
h.life -= m.attack;
|
||||
m.life -= h.attack;
|
||||
cout << "--------第<< count << "次攻击-----------";
|
||||
cout << "玩家对怪物造成了:" << h.attack << "点伤害" << endl;
|
||||
cout << "怪物对玩家造成了:" << m.attack << "点伤害" << endl;
|
||||
cout << "玩家血量:" << h.life << endl;
|
||||
cout << "怪物血量:" << m.life << endl;
|
||||
cout << endl;
|
||||
_sleep(5000);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <windows.h>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int pid;
|
||||
cout << "请输入目标进程 PID:" << endl;
|
||||
cin >> pid;
|
||||
|
||||
// 使用 OpenProcess 打开目标进程,可以通过 hProcess 去访问这个程序内的所有内存数据
|
||||
// PROCESS_ALL_ACCESS 表示请求所有权限
|
||||
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
|
||||
if(hProcess == nullptr){
|
||||
cout << "无效进程";
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 要写入的内存地址以字符串形式输入
|
||||
string addr;
|
||||
cin >> addr;
|
||||
|
||||
// 将字符串转换为无符号长整型 (unsigned long long),然后转换为 int* 指针
|
||||
// stoull 可以将字符串解转为 unsigned long long
|
||||
int* address = (int*)stoull(addr);
|
||||
// 你想要修改的值
|
||||
int newLife;
|
||||
cin >> newLife;
|
||||
|
||||
// 将 newLife 的值写入到目标进程的指定内存地址
|
||||
WriteProcessMemory(hProcess, address, &newLife, sizeof(newLife), 0);
|
||||
|
||||
CloseHandle(hProcess);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
//
|
||||
// Created by xiaolinquan on 2024/12/21.
|
||||
//
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <cmath>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
int randNum;
|
||||
|
||||
// 设置控制台文本颜色
|
||||
void setConsoleColor(int color) {
|
||||
#ifdef _WIN32
|
||||
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
|
||||
#else
|
||||
cout << "\033[" << color << "m";
|
||||
#endif
|
||||
}
|
||||
|
||||
// 恢复控制台文本颜色
|
||||
void resetConsoleColor() {
|
||||
#ifdef _WIN32
|
||||
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
|
||||
#else
|
||||
cout << "\033[0m";
|
||||
#endif
|
||||
}
|
||||
|
||||
// 奖励机制函数
|
||||
void calculateReward(int attempts, int &smallSnacks) {
|
||||
if (attempts <= 1){
|
||||
smallSnacks = 10;
|
||||
}else if (attempts <= 2) {
|
||||
smallSnacks = 7;
|
||||
}else if (attempts <= 4) {
|
||||
smallSnacks = 5;
|
||||
} else if (attempts <= 7) {
|
||||
smallSnacks = 2;
|
||||
} else {
|
||||
smallSnacks = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 提示机制函数
|
||||
void giveHint(int guessNum, int randNum) {
|
||||
int difference = abs(guessNum - randNum);
|
||||
if (difference == 0) return;
|
||||
|
||||
// 1. 基础提示
|
||||
setConsoleColor(14);
|
||||
if (difference <= 5) cout << "非常非常接近了!!" << endl;
|
||||
else if (difference <= 10) cout << "接近了!" << endl;
|
||||
else if (difference <= 25) cout << "差距适中。" << endl;
|
||||
else cout << "!差距很大哦!" << endl;
|
||||
|
||||
// 2. 随机触发一种额外提示 (抽取 0-5)
|
||||
int hintType = rand() % 10;
|
||||
|
||||
switch (hintType) {
|
||||
case 0: // 奇偶性
|
||||
if (randNum % 2 == 0) cout << "线索:它是偶数。" << endl;
|
||||
else cout << "线索:它是奇数。" << endl;
|
||||
break;
|
||||
case 1: // 各位数字之和
|
||||
cout << "线索:各位数字之和是 " << (randNum/10 + randNum%10) << endl;
|
||||
break;
|
||||
case 2: // 尾数 (你的原逻辑)
|
||||
cout << "线索:最后一位数字是 " << randNum % 10 << endl;
|
||||
break;
|
||||
case 3: // 倍数
|
||||
if (randNum % 3 == 0) cout << "线索:是 3 的倍数。" << endl;
|
||||
else if (randNum % 5 == 0) cout << "线索:是 5 的倍数。" << endl;
|
||||
else cout << "线索:不是 3 也不是 5 的倍数。" << endl;
|
||||
break;
|
||||
case 4:
|
||||
cout << "这次没有额外线索,靠直觉吧!" << endl;
|
||||
break;
|
||||
case 5: // 什么都不提示
|
||||
cout << "这次没有额外线索,靠直觉吧!" << endl;
|
||||
break;
|
||||
default:
|
||||
cout << "这次没有额外线索,靠直觉吧!" << endl;
|
||||
|
||||
}
|
||||
|
||||
resetConsoleColor();
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
srand((unsigned int)time(nullptr));
|
||||
int round = 1;
|
||||
|
||||
while (true) {
|
||||
randNum = rand() % 100 + 1;
|
||||
int guessNum = -1;
|
||||
int attempts = 0;
|
||||
|
||||
setConsoleColor(11); // 设置颜色为亮青色
|
||||
cout << "开始第" << round << "轮竞猜!" << endl;
|
||||
resetConsoleColor();
|
||||
|
||||
while (guessNum != randNum) {
|
||||
cout << "请输入你要猜的数字(范围1-100): ";
|
||||
cin >> guessNum;
|
||||
attempts++;
|
||||
|
||||
if (guessNum > randNum) {
|
||||
setConsoleColor(12); // 红色
|
||||
cout << "你猜得太大了!" << endl;
|
||||
resetConsoleColor();
|
||||
} else if (guessNum < randNum) {
|
||||
setConsoleColor(12); // 红色
|
||||
cout << "你猜得太小了!" << endl;
|
||||
resetConsoleColor();
|
||||
}
|
||||
|
||||
// 提示玩家
|
||||
giveHint(guessNum, randNum);
|
||||
|
||||
if (guessNum == randNum) {
|
||||
setConsoleColor(10); // 绿色
|
||||
cout << "你猜中了!!恭喜!" << endl;
|
||||
resetConsoleColor();
|
||||
|
||||
int Snacks = 0;
|
||||
calculateReward(attempts,Snacks);
|
||||
|
||||
setConsoleColor(13); // 紫色
|
||||
cout << "你获得了 " << Snacks << " 个零食!" << endl;
|
||||
resetConsoleColor();
|
||||
}
|
||||
}
|
||||
|
||||
round++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user