扫雷
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
const WORD FORE_GRAY = FOREGROUND_INTENSITY; //灰色属性文本
const WORD FORE_BLUE = FOREGROUND_BLUE | FORE_GRAY; //蓝色属性文本
const WORD FORE_GREEN = FOREGROUND_GREEN | FORE_GRAY; //蓝色属性文本
const WORD FORE_RED = FOREGROUND_RED | FORE_GRAY; //红色属性文本
const WORD FORE_CYAN = FORE_BLUE | FORE_GREEN; //青色属性文本
const WORD FORE_PURPLE = FORE_BLUE | FORE_RED; //紫色属性文本
int now, lei[10] = {0, 18, 40, 70, 160};
int a[10] = {0, 10, 15, 20, 30};
int fx[] = {0, -1, 1, 0, 0, -1, -1, 1, 1};
int fy[] = {0, 0, 0, -1, 1, -1, 1, -1, 1};
int mp[45][45], vis[50][50];
string s[15] = {"■", "1", "2", "3", "4", "5", "6", "7", "8", "▼"};
void HideCursor(){ //隐藏光标
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
}
void dfs(int x, int y, bool flag){
for(int i = 1; i <= 8; i++){
int xx = x + fx[i], yy = y + fy[i];
if(xx >= 0 && xx < a[now] && yy >= 0 && yy < a[now] && !vis[xx][yy]){
if(!flag && mp[xx][yy] != 9){
vis[x][y] = 1;
dfs(xx, yy, (mp[xx][yy] == 0 ? 0 : 1));
}
else if(flag && mp[xx][yy] != 9){
vis[x][y] = 1;
}
}
}
}
void position(int y, int x){ //打印位置,下标从(0, 0)开始
COORD pos = {x, y};
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}
void print(){
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //定义窗口缓冲区信息结构体
GetConsoleScreenBufferInfo(handle_out, &csbi); //获得窗口缓冲区信息
SetConsoleTextAttribute(handle_out, FORE_CYAN); //将颜色调整为青色
cout << "用wsad移动,Enter是扫雷,space是插旗,Esc重玩。\n";
cout << "未翻开的格子用";
SetConsoleTextAttribute(handle_out, FORE_GREEN); //将颜色调整为绿色
cout << "□";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示,周围没有雷但翻开了用";
SetConsoleTextAttribute(handle_out, FORE_GRAY); //将颜色调整为灰色
cout << "■";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示,鼠标所在地方用";
SetConsoleTextAttribute(handle_out, FORE_BLUE); //将颜色调整为蓝色
cout << "●";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示。\n";
cout << "雷用";
SetConsoleTextAttribute(handle_out, FORE_RED); //将颜色调整为红色
cout << "▼";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示,用";
SetConsoleTextAttribute(handle_out, FORE_PURPLE); //将颜色调整为紫色
cout << "◆";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示旗子,周围有几个雷就是数字几。\n";
return ;
}
bool fail(){
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //定义窗口缓冲区信息结构体
GetConsoleScreenBufferInfo(handle_out, &csbi); //获得窗口缓冲区信息
position(a[now] + 3, 0);
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "这个是雷!!\n";
for(int i = 0; i < a[now]; i++){
for(int j = 0; j < a[now]; j++){
if(mp[i][j] > 8){
position(i + 3, j);
SetConsoleTextAttribute(handle_out, FORE_RED);
cout << "▼";
Sleep(35);
}
}
}
position(a[now] + 4, 0);
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "\n是否重玩(1、Yes,2、No)";
while(1){
if(_kbhit()){
int d = _getch();
if(d == 49) return 1;
else if(d == 50) return 0;
}
}
}
int main(){
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //定义窗口缓冲区信息结构体
GetConsoleScreenBufferInfo(handle_out, &csbi); //获得窗口缓冲区信息
srand(time(0));
system("title 扫雷游戏");
HideCursor();
val: ;
Sleep(500);
memset(vis, 0, sizeof vis);
memset(mp, 0, sizeof mp);
now = 0;
system("cls");
print();
cout << "请选择难度(1 or 2 or 3 or 4)\n";
cout << "1、简单(10×10,18个雷) 2、普通(15×15,40个雷)\n3、困难(20×20,70个雷) 4、大师(30×30,160个雷)\n";
while(!now){
if(_kbhit()){ //判断是否有键按下
int a = _getch(); //获取按键信息
switch(a){
case 49:
now = 1;
break;
case 50:
now = 2;
break;
case 51:
now = 3;
break;
case 52:
now = 4;
break;
}
}
}
system("cls");
cout << "选择成功,现在开始游戏!";
Sleep(1000);
system("cls");
for(int i = 1; i <= lei[now]; i++){
int x = rand() % a[now], y = rand() % a[now];
while(vis[x][y]){
x = rand() % a[now], y = rand() % a[now];
}
vis[x][y] = 1;
mp[x][y] = 9;
}
for(int i = 0; i < a[now]; i++){
for(int j = 0; j < a[now]; j++){
if(!vis[i][j]){
int sum = 0;
for(int k = 1; k <= 8; k++){
int xx = i + fx[k], yy = j + fy[k];
if(xx >= 0 && xx < a[now] && yy >= 0 && yy < a[now] && mp[xx][yy] > 8){
sum++;
}
}
mp[i][j] = sum;
}
}
}
print();
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "●";
for(int i = 1; i <= a[now]; i++){
for(int j = 1; j <= a[now]; j++){
if(i != 1 || j != 1){
SetConsoleTextAttribute(handle_out, FORE_GREEN);
cout << "□";
}
}
if(i == a[now] / 2){
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << " 剩余雷数:" << lei[now];
}
putchar('\n');
}
memset(vis, 0, sizeof vis);
int sum = lei[now], ans = lei[now], x = 0, y = 0;
while(sum){
if(_kbhit()){
int d = _getch();
if(d == 27){
goto val;
}
if(d == 13){
if(mp[x][y] > 8){
if(fail()) goto val;
return 0;
}
else if(mp[x][y] >= 1 && mp[x][y] <= 8){
vis[x][y] = 1;
}
else if(mp[x][y] < 1){
dfs(x, y, 0);
for(int i = 0; i < a[now]; i++){
for(int j = 0; j < a[now]; j++){
if(i == x && j == y){
position(x + 3, y);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "●";
}
else if(vis[i][j] == 1){
position(i + 3, j);
SetConsoleTextAttribute(handle_out, FORE_GRAY);
cout << s[mp[i][j]];
}
else if(vis[i][j] == 2){
position(i + 3, j);
SetConsoleTextAttribute(handle_out, FORE_PURPLE);
cout << "◆";
}
}
putchar('\n');
}
}
}
else if(d == 32){
if(vis[x][y] == 2) ans++, sum += (mp[x][y] == 9 ? 1 : 0), vis[x][y] = 0;
else if(vis[x][y] == 0) ans--, sum += (mp[x][y] == 9 ? -1 : 0), vis[x][y] = 2;
position(a[now] / 2 - 1 + 3, a[now] + 20);
SetConsoleTextAttribute(handle_out, FORE_CYAN);
printf("%02d", ans);
}
else if(d == 87 || d == 119){
int xx = x - 1, yy = y;
if(xx >= 0 && xx < a[now] && yy >= 0 && yy < a[now]){
position(x + 3, y);
if(!vis[x][y]){
SetConsoleTextAttribute(handle_out, FORE_GREEN);
cout << "□";
}
else if(vis[x][y] == 1){
SetConsoleTextAttribute(handle_out, FORE_GRAY);
cout << s[mp[x][y]];
}
else{
SetConsoleTextAttribute(handle_out, FORE_PURPLE);
cout << "◆";
}
x = xx;
position(x + 3, y);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "●";
}
}else if(d == 83 || d == 115){
int xx = x + 1, yy = y;
if(xx >= 0 && xx < a[now] && yy >= 0 && yy < a[now]){
position(x + 3, y);
if(!vis[x][y]){
SetConsoleTextAttribute(handle_out, FORE_GREEN);
cout << "□";
}
else if(vis[x][y] == 1){
SetConsoleTextAttribute(handle_out, FORE_GRAY);
cout << s[mp[x][y]];
}
else{
SetConsoleTextAttribute(handle_out, FORE_PURPLE);
cout << "◆";
}
x = xx;
position(x + 3, y);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "●";
}
}
else if(d == 65 || d == 97){
int xx = x, yy = y - 1;
if(xx >= 0 && xx < a[now] && yy >= 0 && yy < a[now]){
position(x + 3, y);
if(!vis[x][y]){
SetConsoleTextAttribute(handle_out, FORE_GREEN);
cout << "□";
}
else if(vis[x][y] == 1){
SetConsoleTextAttribute(handle_out, FORE_GRAY);
cout << s[mp[x][y]];
}
else{
SetConsoleTextAttribute(handle_out, FORE_PURPLE);
cout << "◆";
}
y = yy;
position(x + 3, y);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "●";
}
}else if(d == 68 || d == 100){
int xx = x, yy = y + 1;
if(xx >= 0 && xx < a[now] && yy >= 0 && yy < a[now]){
position(x + 3, y);
if(!vis[x][y]){
SetConsoleTextAttribute(handle_out, FORE_GREEN);
cout << "□";
}
else if(vis[x][y] == 1){
SetConsoleTextAttribute(handle_out, FORE_GRAY);
cout << s[mp[x][y]];
}
else{
SetConsoleTextAttribute(handle_out, FORE_PURPLE);
cout << "◆";
}
y = yy;
position(x + 3, y);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "●";
}
}
}
}
cout << "扫雷成功!!";
cout << "\n按任意键退出游戏";
while(1){
if(_kbhit()){
break;
}
}
return 0;
}如果有 Bug,请用下方代码:
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
const WORD FORE_GRAY = FOREGROUND_INTENSITY; //灰色属性文本
const WORD FORE_BLUE = FOREGROUND_BLUE | FORE_GRAY; //蓝色属性文本
const WORD FORE_GREEN = FOREGROUND_GREEN | FORE_GRAY; //蓝色属性文本
const WORD FORE_RED = FOREGROUND_RED | FORE_GRAY; //红色属性文本
const WORD FORE_CYAN = FORE_BLUE | FORE_GREEN; //青色属性文本
const WORD FORE_PURPLE = FORE_BLUE | FORE_RED; //紫色属性文本
int now, lei[10] = {0, 18, 40, 70, 160};
int a[10] = {0, 10, 15, 20, 30};
int fx[] = {0, -1, 1, 0, 0, -1, -1, 1, 1};
int fy[] = {0, 0, 0, -1, 1, -1, 1, -1, 1};
int mp[45][45], vis[50][50];
string s[15] = {"o", "1", "2", "3", "4", "5", "6", "7", "8", "#"};
void HideCursor(){ //隐藏光标
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
}
void dfs(int x, int y, bool flag){
for(int i = 1; i <= 8; i++){
int xx = x + fx[i], yy = y + fy[i];
if(xx >= 0 && xx < a[now] && yy >= 0 && yy < a[now] && !vis[xx][yy]){
if(!flag && mp[xx][yy] != 9){
vis[x][y] = 1;
dfs(xx, yy, (mp[xx][yy] == 0 ? 0 : 1));
}
else if(flag && mp[xx][yy] != 9){
vis[x][y] = 1;
}
}
}
}
void position(int y, int x){ //打印位置,下标从(0, 0)开始
COORD pos = {x, y};
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}
void print(){
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //定义窗口缓冲区信息结构体
GetConsoleScreenBufferInfo(handle_out, &csbi); //获得窗口缓冲区信息
SetConsoleTextAttribute(handle_out, FORE_CYAN); //将颜色调整为青色
cout << "用wsad移动,Enter是扫雷,space是插旗,Esc重玩。\n";
cout << "未翻开的格子用";
SetConsoleTextAttribute(handle_out, FORE_GREEN); //将颜色调整为绿色
cout << "O";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示,周围没有雷但翻开了用";
SetConsoleTextAttribute(handle_out, FORE_GRAY); //将颜色调整为灰色
cout << "o";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示,鼠标所在地方用";
SetConsoleTextAttribute(handle_out, FORE_BLUE); //将颜色调整为蓝色
cout << "@";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示。\n";
cout << "雷用";
SetConsoleTextAttribute(handle_out, FORE_RED); //将颜色调整为红色
cout << "#";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示,用";
SetConsoleTextAttribute(handle_out, FORE_PURPLE); //将颜色调整为紫色
cout << "$";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示旗子,周围有几个雷就是数字几。\n";
return ;
}
bool fail(){
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //定义窗口缓冲区信息结构体
GetConsoleScreenBufferInfo(handle_out, &csbi); //获得窗口缓冲区信息
position(a[now] + 3, 0);
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "这个是雷!!\n";
for(int i = 0; i < a[now]; i++){
for(int j = 0; j < a[now]; j++){
if(mp[i][j] > 8){
position(i + 3, j);
SetConsoleTextAttribute(handle_out, FORE_RED);
cout << "#";
Sleep(35);
}
}
}
position(a[now] + 4, 0);
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "\n是否重玩(1、Yes,2、No)";
while(1){
if(_kbhit()){
int d = _getch();
if(d == 49) return 1;
else if(d == 50) return 0;
}
}
}
int main(){
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //定义窗口缓冲区信息结构体
GetConsoleScreenBufferInfo(handle_out, &csbi); //获得窗口缓冲区信息
srand(time(0));
system("title 扫雷游戏");
HideCursor();
val: ;
Sleep(500);
memset(vis, 0, sizeof vis);
memset(mp, 0, sizeof mp);
now = 0;
system("cls");
print();
cout << "请选择难度(1 or 2 or 3 or 4)\n";
cout << "1、简单(10×10,18个雷) 2、普通(15×15,40个雷)\n3、困难(20×20,70个雷) 4、大师(30×30,160个雷)\n";
while(!now){
if(_kbhit()){ //判断是否有键按下
int a = _getch(); //获取按键信息
switch(a){
case 49:
now = 1;
break;
case 50:
now = 2;
break;
case 51:
now = 3;
break;
case 52:
now = 4;
break;
}
}
}
system("cls");
cout << "选择成功,现在开始游戏!";
Sleep(1000);
system("cls");
for(int i = 1; i <= lei[now]; i++){
int x = rand() % a[now], y = rand() % a[now];
while(vis[x][y]){
x = rand() % a[now], y = rand() % a[now];
}
vis[x][y] = 1;
mp[x][y] = 9;
}
for(int i = 0; i < a[now]; i++){
for(int j = 0; j < a[now]; j++){
if(!vis[i][j]){
int sum = 0;
for(int k = 1; k <= 8; k++){
int xx = i + fx[k], yy = j + fy[k];
if(xx >= 0 && xx < a[now] && yy >= 0 && yy < a[now] && mp[xx][yy] > 8){
sum++;
}
}
mp[i][j] = sum;
}
}
}
print();
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "@";
for(int i = 1; i <= a[now]; i++){
for(int j = 1; j <= a[now]; j++){
if(i != 1 || j != 1){
SetConsoleTextAttribute(handle_out, FORE_GREEN);
cout << "O";
}
}
if(i == a[now] / 2){
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << " 剩余雷数:" << lei[now];
}
putchar('\n');
}
memset(vis, 0, sizeof vis);
int sum = lei[now], ans = lei[now], x = 0, y = 0;
while(sum){
if(_kbhit()){
int d = _getch();
if(d == 27){
goto val;
}
if(d == 13){
if(mp[x][y] > 8){
if(fail()) goto val;
return 0;
}
else if(mp[x][y] >= 1 && mp[x][y] <= 8){
vis[x][y] = 1;
}
else if(mp[x][y] < 1){
dfs(x, y, 0);
for(int i = 0; i < a[now]; i++){
for(int j = 0; j < a[now]; j++){
if(i == x && j == y){
position(x + 3, y);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "@";
}
else if(vis[i][j] == 1){
position(i + 3, j);
SetConsoleTextAttribute(handle_out, FORE_GRAY);
cout << s[mp[i][j]];
}
else if(vis[i][j] == 2){
position(i + 3, j);
SetConsoleTextAttribute(handle_out, FORE_PURPLE);
cout << "$";
}
}
putchar('\n');
}
}
}
else if(d == 32){
if(vis[x][y] == 2) ans++, sum += (mp[x][y] == 9 ? 1 : 0), vis[x][y] = 0;
else if(vis[x][y] == 0) ans--, sum += (mp[x][y] == 9 ? -1 : 0), vis[x][y] = 2;
position(a[now] / 2 - 1 + 3, a[now] + 20);
SetConsoleTextAttribute(handle_out, FORE_CYAN);
printf("%02d", ans);
}
else if(d == 87 || d == 119){
int xx = x - 1, yy = y;
if(xx >= 0 && xx < a[now] && yy >= 0 && yy < a[now]){
position(x + 3, y);
if(!vis[x][y]){
SetConsoleTextAttribute(handle_out, FORE_GREEN);
cout << "O";
}
else if(vis[x][y] == 1){
SetConsoleTextAttribute(handle_out, FORE_GRAY);
cout << s[mp[x][y]];
}
else{
SetConsoleTextAttribute(handle_out, FORE_PURPLE);
cout << "$";
}
x = xx;
position(x + 3, y);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "@";
}
}else if(d == 83 || d == 115){
int xx = x + 1, yy = y;
if(xx >= 0 && xx < a[now] && yy >= 0 && yy < a[now]){
position(x + 3, y);
if(!vis[x][y]){
SetConsoleTextAttribute(handle_out, FORE_GREEN);
cout << "O";
}
else if(vis[x][y] == 1){
SetConsoleTextAttribute(handle_out, FORE_GRAY);
cout << s[mp[x][y]];
}
else{
SetConsoleTextAttribute(handle_out, FORE_PURPLE);
cout << "$";
}
x = xx;
position(x + 3, y);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "@";
}
}
else if(d == 65 || d == 97){
int xx = x, yy = y - 1;
if(xx >= 0 && xx < a[now] && yy >= 0 && yy < a[now]){
position(x + 3, y);
if(!vis[x][y]){
SetConsoleTextAttribute(handle_out, FORE_GREEN);
cout << "O";
}
else if(vis[x][y] == 1){
SetConsoleTextAttribute(handle_out, FORE_GRAY);
cout << s[mp[x][y]];
}
else{
SetConsoleTextAttribute(handle_out, FORE_PURPLE);
cout << "$";
}
y = yy;
position(x + 3, y);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "@";
}
}else if(d == 68 || d == 100){
int xx = x, yy = y + 1;
if(xx >= 0 && xx < a[now] && yy >= 0 && yy < a[now]){
position(x + 3, y);
if(!vis[x][y]){
SetConsoleTextAttribute(handle_out, FORE_GREEN);
cout << "O";
}
else if(vis[x][y] == 1){
SetConsoleTextAttribute(handle_out, FORE_GRAY);
cout << s[mp[x][y]];
}
else{
SetConsoleTextAttribute(handle_out, FORE_PURPLE);
cout << "$";
}
y = yy;
position(x + 3, y);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "@";
}
}
}
}
cout << "扫雷成功!!";
cout << "\n按任意键退出游戏";
while(1){
if(_kbhit()){
break;
}
}
return 0;
}贪吃蛇
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
const WORD FORE_GRAY = FOREGROUND_INTENSITY; //灰色属性文本
const WORD FORE_BLUE = FOREGROUND_BLUE | FORE_GRAY; //蓝色属性文本
const WORD FORE_GREEN = FOREGROUND_GREEN | FORE_GRAY; //绿色属性文本
const WORD FORE_RED = FOREGROUND_RED | FORE_GRAY; //红色属性文本
const WORD FORE_CYAN = FORE_BLUE | FORE_GREEN; //青色属性文本
void HideCursor(){ //隐藏光标
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
}
void position(int y, int x){ //打印位置,下标从(0, 0)开始
COORD pos = {x, y};
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}
int n, m, ans, cnt = 5, f = 1, vis[105][105], a[3] = {400, 250, 150};
int fx[] = {0, 0, 1, 0, -1};
int fy[] = {0, 1, 0, -1, 0};
string s[] = {"零级", "一级", "二级"};
signed main(){
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //定义窗口缓冲区信息结构体
GetConsoleScreenBufferInfo(handle_out, &csbi); //获得窗口缓冲区信息
HideCursor();
srand(time(0));
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "请先将窗口最大化(最大化后,按任意键开始游戏)";
while(1){
if(_kbhit()){
break;
}
}
position(0, 0);
SetConsoleTextAttribute(handle_out, FORE_CYAN); //将颜色调整为青色
cout << "苹果用";
SetConsoleTextAttribute(handle_out, FORE_RED); //将颜色调整为红色
cout << "●";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示,蛇头用";
SetConsoleTextAttribute(handle_out, FORE_BLUE); //将颜色调整为蓝色
cout << "◆";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示,蛇身用";
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "■";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示,蛇尾用";
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "▲";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示,用wsad操控蛇的移动,按一下空格一级速度,再按下二级速度, 再按一下还原。\n";
cout << "-------------------------------------------------------------------\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "-------------------------------------------------------------------\n";
position(15, 95);
cout << "分数:";
position(15, 101);
cout << 0;
position(16, 95);
cout << "长度:5";
position(17, 95);
cout << "速度:零级";
int sum = 0, ti = 0;
for(int i = 24; i >= 20; i--) vis[12][i] = 1;
n = 30, m = 65;
int ans = 0, cnt = 5;
position(13, 20);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "▲■■■◆";
int px = rand() % 30 + 1, py = rand() % 65 + 1;
while(vis[px][py] == 1) px = rand() % 30 + 1, py = rand() % 65 + 1;
vis[px][py] = -1;
position(px + 1, py);
SetConsoleTextAttribute(handle_out, FORE_RED);
cout << "●";
int sx = 12, sy = 24, ex = 12, ey = 20;
while(1){
if(_kbhit()){
int a = _getch();
if(a == 32){
ti++;
ti %= 3;
position(17, 101);
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << s[ti];
}
else if(a == 87 || a == 119 && f != 2){
f = 4;
}
else if(a == 83 || a == 115 && f != 4){
f = 2;
}
else if(a == 65 || a == 97 && f != 1){
f = 3;
}
else if(a == 68 || a == 100 && f != 3){
f = 1;
}
}
vis[sx][sy] = f;
if(f % 2) Sleep(a[ti]);
else Sleep(a[ti] * 1.2);
int xx = sx + fx[f], yy = sy + fy[f];
if(vis[xx][yy] == -1){
ans += 50;
cnt++;
SetConsoleTextAttribute(handle_out, FORE_BLUE);
position(sx + 1, sy);
cout << "■";
vis[sx][sy] = f;
sx = xx, sy = yy;
position(px + 1, py);
cout << "◆";
px = rand() % 30 + 1, py = rand() % 65 + 1;
while(vis[px][py] >= 1 && vis[px][py] <= 4) px = rand() % 30 + 1, py = rand() % 65 + 1;
vis[px][py] = -1;
position(px + 1, py);
SetConsoleTextAttribute(handle_out, FORE_RED);
cout << "●";
position(15, 101);
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << ans;
position(16, 101);
cout << cnt;
}
else if(xx < 1 || xx > n || yy < 1 || yy > m || vis[xx][yy] >= 1 && vis[xx][yy] <= 4){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(vis[i][j] != 0){
position(i + 1, j);
cout << " ";
}
}
}
position(sx + 1, sy);
cout << " ";
position(ex + 1, ey);
cout << " ";
SetConsoleTextAttribute(handle_out, FORE_GREEN);
position(12, 23);
cout << "-------------------";
position(13, 23);
cout << "| |";
position(14, 23);
cout << "| g |";
position(15, 23);
cout << "| |";
position(16, 23);
cout << "-------------------";
position(14, 29);
cout << "a";
Sleep(100);
cout << "m";
Sleep(100);
cout << "e";
Sleep(100);
cout << " ";
Sleep(100);
cout << "o";
Sleep(100);
cout << "v";
Sleep(100);
cout << "e";
Sleep(100);
cout << "r";
Sleep(100);
SetConsoleTextAttribute(handle_out, FORE_CYAN);
position(33, 0);
return 0;
}
else{
int ts1 = ex, ts2 = ey;
SetConsoleTextAttribute(handle_out, FORE_BLUE);
position(ex + 1, ey);
cout << " ";
ex += fx[vis[ts1][ts2]], ey += fy[vis[ts1][ts2]];
vis[ts1][ts2] = 0;
position(ex + 1, ey);
cout << "▲";
position(sx + 1, sy);
cout << "■";
sx = xx, sy = yy;
vis[sx][sy] = f;
position(sx + 1, sy);
cout << "◆";
}
}
return 0;
}如果有 Bug,请用下方代码:
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
const WORD FORE_GRAY = FOREGROUND_INTENSITY; //灰色属性文本
const WORD FORE_BLUE = FOREGROUND_BLUE | FORE_GRAY; //蓝色属性文本
const WORD FORE_GREEN = FOREGROUND_GREEN | FORE_GRAY; //绿色属性文本
const WORD FORE_RED = FOREGROUND_RED | FORE_GRAY; //红色属性文本
const WORD FORE_CYAN = FORE_BLUE | FORE_GREEN; //青色属性文本
void HideCursor(){ //隐藏光标
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
}
void position(int y, int x){ //打印位置,下标从(0, 0)开始
COORD pos = {x, y};
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}
int n, m, ans, cnt = 5, f = 1, vis[105][105], a[3] = {400, 250, 150};
int fx[] = {0, 0, 1, 0, -1};
int fy[] = {0, 1, 0, -1, 0};
string s[] = {"零级", "一级", "二级"};
signed main(){
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //定义窗口缓冲区信息结构体
GetConsoleScreenBufferInfo(handle_out, &csbi); //获得窗口缓冲区信息
HideCursor();
srand(time(0));
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "请先将窗口最大化(最大化后,按任意键开始游戏)";
while(1){
if(_kbhit()){
break;
}
}
position(0, 0);
SetConsoleTextAttribute(handle_out, FORE_CYAN); //将颜色调整为青色
cout << "苹果用";
SetConsoleTextAttribute(handle_out, FORE_RED); //将颜色调整为红色
cout << "O";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示,蛇头用";
SetConsoleTextAttribute(handle_out, FORE_BLUE); //将颜色调整为蓝色
cout << "%";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示,蛇身用";
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "=";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示,蛇尾用";
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "+";
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << "表示,用wsad操控蛇的移动,按一下空格一级速度,再按下二级速度, 再按一下还原。\n";
cout << "-------------------------------------------------------------------\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
cout << "-------------------------------------------------------------------\n";
position(15, 95);
cout << "分数:";
position(15, 101);
cout << 0;
position(16, 95);
cout << "长度:5";
position(17, 95);
cout << "速度:零级";
int sum = 0, ti = 0;
for(int i = 24; i >= 20; i--) vis[12][i] = 1;
n = 30, m = 65;
int ans = 250, cnt = 5;
position(13, 20);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
cout << "+===%";
int px = rand() % 30 + 1, py = rand() % 65 + 1;
while(vis[px][py] == 1) px = rand() % 30 + 1, py = rand() % 65 + 1;
vis[px][py] = -1;
position(px + 1, py);
SetConsoleTextAttribute(handle_out, FORE_RED);
cout << "O";
int sx = 12, sy = 24, ex = 12, ey = 20;
while(1){
if(_kbhit()){
int a = _getch();
if(a == 32){
ti++;
ti %= 3;
position(17, 101);
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << s[ti];
}
else if(a == 87 || a == 119 && f != 2){
f = 4;
}
else if(a == 83 || a == 115 && f != 4){
f = 2;
}
else if(a == 65 || a == 97 && f != 1){
f = 3;
}
else if(a == 68 || a == 100 && f != 3){
f = 1;
}
}
vis[sx][sy] = f;
if(f % 2) Sleep(a[ti]);
else Sleep(a[ti] * 1.2);
int xx = sx + fx[f], yy = sy + fy[f];
if(vis[xx][yy] == -1){
ans += 50;
cnt++;
SetConsoleTextAttribute(handle_out, FORE_BLUE);
position(sx + 1, sy);
cout << "=";
vis[sx][sy] = f;
sx = xx, sy = yy;
position(px + 1, py);
cout << "%";
px = rand() % 30 + 1, py = rand() % 65 + 1;
while(vis[px][py] >= 1 && vis[px][py] <= 4) px = rand() % 30 + 1, py = rand() % 65 + 1;
vis[px][py] = -1;
position(px + 1, py);
SetConsoleTextAttribute(handle_out, FORE_RED);
cout << "O";
position(15, 101);
SetConsoleTextAttribute(handle_out, FORE_CYAN);
cout << ans;
position(16, 101);
cout << cnt;
}
else if(xx < 1 || xx > n || yy < 1 || yy > m || vis[xx][yy] >= 1 && vis[xx][yy] <= 4){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(vis[i][j] != 0){
position(i + 1, j);
cout << " ";
}
}
}
SetConsoleTextAttribute(handle_out, FORE_GREEN);
position(12, 23);
cout << "-------------------";
position(13, 23);
cout << "| |";
position(14, 23);
cout << "| g |";
position(15, 23);
cout << "| |";
position(16, 23);
cout << "-------------------";
position(14, 29);
cout << "a";
Sleep(100);
cout << "m";
Sleep(100);
cout << "e";
Sleep(100);
cout << " ";
Sleep(100);
cout << "o";
Sleep(100);
cout << "v";
Sleep(100);
cout << "e";
Sleep(100);
cout << "r";
Sleep(100);
SetConsoleTextAttribute(handle_out, FORE_CYAN);
position(32, 0);
return 0;
}
else{
int ts1 = ex, ts2 = ey;
SetConsoleTextAttribute(handle_out, FORE_BLUE);
position(ex + 1, ey);
cout << " ";
ex += fx[vis[ts1][ts2]], ey += fy[vis[ts1][ts2]];
vis[ts1][ts2] = 0;
position(ex + 1, ey);
cout << "+";
position(sx + 1, sy);
cout << "=";
sx = xx, sy = yy;
vis[sx][sy] = f;
position(sx + 1, sy);
cout << "%";
}
}
return 0;
}名字竞技场
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
#define int long long
using namespace std;
const int B = 754, mod = 1e9 + 7;
const WORD GRAY = FOREGROUND_INTENSITY;
const WORD BLUE = FOREGROUND_BLUE;
const WORD GREEN = FOREGROUND_GREEN;
const WORD RED = FOREGROUND_RED;
const WORD CYAN = BLUE | GREEN;
const WORD PURPLE = BLUE | RED;
const WORD ORANGE = RED | GREEN;
const WORD IGRAY = FOREGROUND_INTENSITY;
const WORD IBLUE = FOREGROUND_BLUE | IGRAY;
const WORD IGREEN = FOREGROUND_GREEN | IGRAY;
const WORD IRED = FOREGROUND_RED | IGRAY;
const WORD ICYAN = IBLUE | IGREEN;
const WORD IPURPLE = IBLUE | IRED;
const WORD YELLOW = IRED | IGREEN;
const WORD WHITE = ICYAN | IRED;
struct node{
string name, Killer;
int HP, nowHP, Kill, De, Fast, nowFast, Magic, Res, Killnum, harmnum, Wallnum;
bool Ice, Wall, Curse, Charm, die;
}play[10005];
int n, num, maxi;
bool mp[100005];
unordered_map<string, bool> vis;
void position(int y, int x){ //打印位置,下标从(0, 0)开始
COORD pos = {x, y};
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}
void HideCursor(){ //隐藏光标
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
}
void color(WORD haha){
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //定义窗口缓冲区信息结构体
GetConsoleScreenBufferInfo(handle_out, &csbi); //获得窗口缓冲区信息
SetConsoleTextAttribute(handle_out, haha);
}
int get_HP(string s){ //计算角色血量
int res = 0;
for(int i = 0; i < s.size(); i++){
res = res * B % mod + abs((int)s[i]);
res = abs(res);
res %= mod;
res %= 147;
}
return abs(res) + 203;
}
int get_Kill(string s){ //计算角色伤害
int res = 0;
for(int i = 0; i < s.size(); i++){
res = res * B % mod + abs((int)s[i]);
res = abs(res);
res %= mod;
res %= 40;
}
return abs(res) + 52;
}
int get_De(string s){ //计算角色防御
int res = 0;
for(int i = 0; i < s.size(); i++){
res = res * B % mod + abs((int)s[i]);
res = abs(res);
res %= mod;
res %= 38;
}
return abs(res) + 61;
}
int get_Fast(string s){ //计算角色速度
int res = 0;
for(int i = 0; i < s.size(); i++){
res = res * B % mod + abs((int)s[i]);
res = abs(res);
res %= mod;
res %= 33;
}
return abs(res) + 67;
}
int get_Magic(string s){ //计算角色魔性
int res = 0;
for(int i = 0; i < s.size(); i++){
res = res * B % mod + abs((int)s[i]);
res = abs(res);
res %= mod;
res %= 43;
}
return abs(res) + 57;
}
int get_Res(string s){ //计算角色抗性
int res = 0;
for(int i = 0; i < s.size(); i++){
res = res * B % mod + abs((int)s[i]);
res = abs(res);
res %= mod;
res %= 47;
}
return abs(res) + 53;
}
void input(){
system("cls");
color(ICYAN);
cout << "请输入名字。\n删除上一个名字请输入 \"del\",重新输入名字请输入 \"clear\"。\n输入完毕后以 \"Ctrl + Z\" 结束。";
color(IRED);
cout << "注:不可组队!!\n";
color(WHITE);
int x = 3;
string name;
while(cin >> name){
if(name == "clear" && n > 0){
system("cls");
x = 3, n = 0;
color(ICYAN);
cout << "请输入名字。\n删除上一个名字请输入 \"del\",重新输入名字请输入 \"clear\"。\n输入完毕后以 \"Ctrl + Z\" 结束。";
color(IRED);
cout << "注:不可组队!!\n";
color(WHITE);
vis.clear();
continue;
}
if(name == "del" && n > 0){
position(x, 0);
cout << " ";
x--;
position(x, 0);
for(int i = 0; i < play[n].name.size(); i++){
cout << " ";
}
position(x, 0);
vis[play[n].name] = 0;
n--;
continue;
}
else if(name == "del"){
position(x, 0);
cout << " ";
position(x, 0);
continue;
}
x++;
if(vis[name]) continue;
n++;
vis[name] = 1;
play[n].name = name;
play[n].HP = play[n].nowHP = get_HP(name);
play[n].Kill = get_Kill(name);
play[n].De = get_De(name);
play[n].Fast = play[n].nowFast = get_Fast(name);
play[n].Magic = get_Magic(name);
play[n].Res = get_Res(name);
}
cout << "按数字键开始战斗. . .";
while(1){
if(_kbhit()){
int ch = _getch();
ch -= '0';
if(ch >= 0 && ch <= 9) break;
}
}
}
int rr(int aa, int bb){
if(aa == bb) return aa;
if(rand() % 2) return rr(aa, rand() % (bb - aa + 1) + aa);
else return rr(rand() % (bb - aa + 1) + aa, bb);
}
void Is(bool flag){
for(int i = 1; i <= n; i++){
if(!play[i].die && play[i].Wall && play[i].Wallnum > 3){
cout << play[i].name << " 从";
color(ORANGE);
cout << "铁壁";
color(CYAN);
cout << "中解除\n";
if(flag) Sleep(300);
play[i].Wall = play[i].Wallnum = 0;
return ;
}
}
}
void Charm(bool flag){
int x;
for(int i = 1; i <= 5; i++){
x = rr(1, n);
if(!play[x].die && play[x].Charm){
cout << play[x].name << " 从";
color(IPURPLE);
cout << "魅惑";
color(CYAN);
cout << "中解除\n";
if(flag) Sleep(500);
play[x].Charm = 0;
return ;
}
}
}
/*
coritom
CBC18
OIer_Kevin
yi_ming_QWQ_ju_ruo
czwhaha
*/
void player(){
system("cls");
for(int i = 1; i <= n; i++) maxi = max(maxi, (int)play[i].name.size());
for(int i = 1; i <= n; i++){
for(int j = 1; j <= (maxi - play[i].name.size()) / 2; j++) cout << " ";
cout << play[i].name;
for(int j = 1; j <= (maxi - play[i].name.size() + 1) / 2; j++) cout << " ";
cout << " 血量:" << play[i].HP << " 攻击:" << play[i].Kill << " 防御:" << play[i].De << " 速度:" << play[i].Fast << " 魔性:" << play[i].Magic << " 抗性:" << play[i].Res << "\n";
}
color(CYAN);
int player1, player2, cnt, flag = 1, total = 0;
while(num != n - 1){
total++;
Is(flag);
if(total % 4 == 0) Charm(flag);
cnt = 0;
player1 = rr(1, n);
while(play[player1].die && cnt < n) player1 = rr(1, n), cnt++;
player2 = rr(1, n);
while(play[player2].die) player2 = rr(1, n);
if(player1 == player2){
int tmp = rr(1, B);
tmp %= 19;
if(play[player1].Ice && tmp >= rr(8, 13)){
cout << play[player1].name << " 从";
color(ICYAN);
cout << "冰冻";
color(CYAN);
cout << "中解除\n";
if(flag) Sleep(500);
play[player1].Ice = 0;
}
else if(play[player1].Wall && tmp >= rr(5, 9)){
cout << play[player1].name << " 从";
color(ORANGE);
cout << "铁壁";
color(CYAN);
cout << "中解除\n";
if(flag) Sleep(500);
play[player1].Wall = play[player1].Wallnum = 0;
}
else if(play[player1].Curse && tmp >= rr(8, 13)){
cout << play[player1].name << " 从";
color(YELLOW);
cout << "诅咒";
color(CYAN);
cout << "中解除\n";
if(flag) Sleep(500);
play[player1].Curse = 0;
}
else if(play[player1].Charm && tmp >= rr(8, 13)){
cout << play[player1].name << " 从";
color(IPURPLE);
cout << "魅惑";
color(CYAN);
cout << "中解除\n";
if(flag) Sleep(500);
play[player1].Charm = 0;
}
else if(play[player1].nowFast != play[player1].Fast && tmp >= rr(11, 15)){
cout << play[player1].name << " 从";
color(ORANGE);
cout << "疾走";
color(CYAN);
cout << "中解除\n";
play[player1].nowFast = play[player1].Fast;
if(flag) Sleep(500);
}
else{
tmp = rr(1, 273);
if(tmp % 18 <= 3){
tmp = rr(1, 7547);
if(tmp % 5 + 1 == 0 && play[player1].nowHP < play[player1].HP){
int HP = rr(play[player1].Magic - rr(10, 20), play[player1].Magic + rr(20, 35));
cout << play[player1].name << " 使用了治愈,";
if(flag) Sleep(250);
cout << play[player1].name << " 恢复了 ";
color(IGREEN);
cout << HP;
color(CYAN);
cout << " 点血,";
if(flag) Sleep(250);
cout << "剩余 ";
play[player1].nowHP += HP;
play[player1].nowHP = min(play[player1].nowHP, play[player1].HP);
color(IGREEN);
cout << play[player1].nowHP << "HP\n";
color(CYAN);
if(flag) Sleep(400);
}
else if(tmp % 5 == 2 && play[player1].nowFast == play[player1].Fast){
int Fast = rr(17, 32);
play[player1].nowFast += Fast;
cout << play[player1].name << " 使用了";
color(ORANGE);
cout << "疾走\n";
color(CYAN);
if(flag) Sleep(500);
}
else if(tmp % 5 == 4 && !play[player1].Wall){
cout << play[player1].name << " 使用了";
color(ORANGE);
cout << "铁壁\n";
color(CYAN);
play[player1].Wall = 1;
if(flag) Sleep(400);
}
}
}
continue;
}
if(play[player1].Ice || play[player1].die || play[player2].die) continue;
int harm = 0;
if(play[player1].Charm){ //玩家一被魅惑
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(250);
harm = play[player1].Kill + rr(11, 23) - rr(3, 5);
cout << play[player1].name << " 受到了 ";
color(RED);
cout << harm;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(250);
play[player1].nowHP -= harm;
cout << "剩余 ";
color(IGREEN);
cout << max(play[player1].nowHP, 0ll) << "HP\n";
if(flag) Sleep(400);
if(play[player1].nowHP <= 0){
color(WHITE);
cout << play[player1].name << " 被击倒了\n";
play[player1].die = 1;
play[player1].Killnum++;
play[player1].Killer = play[player1].name;
num++;
}
color(CYAN);
continue;
}
int tmp;
if(play[player1].Magic >= play[player2].Magic){
tmp = rr(1, 100);
if(tmp < play[player1].Magic / rr(2, 5)){
tmp = rr(1, B);
if(play[player1].nowFast > play[player1].Fast && play[player2].nowFast == play[player2].Fast){
tmp %= 13;
}
else if(play[player1].nowFast == play[player1].Fast && play[player2].nowFast > play[player2].Fast){
tmp %= 5;
}
else tmp %= 8;
if(tmp < 2){
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(500);
cout << play[player2].name << " 闪避了攻击\n";
if(flag) Sleep(500);
continue;
}
if(play[player1].Kill * 2 < play[player2].De + play[player2].Res){
if(play[player2].Curse){ //玩家二被诅咒
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(200);
cout << "因为被";
color(YELLOW);
cout << "诅咒";
color(CYAN);
cout << "而伤害加倍,";
if(flag) Sleep(200);
harm = play[player1].Kill * 2.2 - (play[player2].De - play[player1].Kill);
cout << play[player2].name << " 受到了 ";
color(RED);
cout << harm;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(200);
}
else if(play[player2].Wall){ //玩家二使用了铁壁
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(250);
harm = 0;
cout << play[player2].name << " 受到了 ";
color(RED);
cout << harm;
play[player2].Wallnum++;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(250);
}
else{ //普通攻击
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(250);
cout << play[player2].name << " 受到了 ";
color(RED);
harm = play[player1].Kill * 2 - play[player2].De + rr(17, 23);
cout << harm;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(250);
}
}
else if(play[player1].Kill * 2 == play[player2].De + play[player2].Res){
if(play[player2].Curse){ //玩家二被诅咒
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(200);
cout << "因为被";
color(YELLOW);
cout << "诅咒";
color(CYAN);
cout << "而伤害加倍,";
if(flag) Sleep(200);
harm = play[player1].Kill * 2.2;
cout << play[player2].name << " 受到了 ";
color(RED);
cout << harm;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(200);
}
else if(play[player2].Wall){ //玩家二使用了铁壁
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(250);
harm = 1;
cout << play[player2].name << " 受到了 ";
color(RED);
cout << harm;
play[player2].Wallnum++;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(250);
}
else{
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(250);
cout << play[player2].name << " 受到了 ";
color(RED);
harm = play[player1].Kill + rr(17, 23);
cout << harm;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(250);
}
}
else{
if(play[player2].Curse){ //玩家二被诅咒
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(200);
cout << "因为被";
color(YELLOW);
cout << "诅咒";
color(CYAN);
cout << "而伤害加倍,";
if(flag) Sleep(200);
harm = play[player1].Kill * 2.4;
cout << play[player2].name << " 受到了 ";
color(RED);
cout << harm;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(200);
}
else if(play[player2].Wall){ //玩家二使用了铁壁
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(250);
harm = 2;
cout << play[player2].name << " 受到了 ";
color(RED);
cout << harm;
play[player2].Wallnum++;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(250);
}
else{ //普通攻击
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(250);
cout << play[player2].name << " 受到了 ";
color(RED);
harm = play[player1].Kill + rr(21, 33);
cout << harm;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(250);
}
}
play[player2].nowHP -= harm;
cout << "剩余 ";
color(IGREEN);
cout << max(play[player2].nowHP, 0ll) << "HP\n";
color(CYAN);
if(flag) Sleep(400);
if(play[player2].nowHP <= 0){
color(WHITE);
cout << play[player2].name << " 被击倒了\n";
play[player2].die = 1;
play[player1].Killnum++;
play[player2].Killer = play[player1].name;
num++;
if(flag) Sleep(400);
}
color(CYAN);
}
else{
tmp = rr(1, B);
tmp %= 7;
if(tmp == 0){ //使用了雷击术
cout << play[player1].name << " 使用了";
color(IBLUE);
cout << "雷击术";
color(CYAN);
cout << ",\n";
if(flag) Sleep(500);
int pos = rr(3, 5);
for(int i = 1; i <= pos && !play[player2].die; i++){
tmp = rr(1, B);
if(play[player1].nowFast > play[player1].Fast && play[player2].nowFast == play[player2].Fast){
tmp %= 13;
}
else if(play[player1].nowFast == play[player1].Fast && play[player2].nowFast > play[player2].Fast){
tmp %= 5;
}
else tmp %= 8;
if(tmp < 2){
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(500);
cout << play[player2].name << " 闪避了攻击\n";
if(flag) Sleep(500);
continue;
}
harm = play[player1].Kill / (play[player1].Kill / 10 - rr(1, 2)) + rr(3, 5) - rr(2, 6);
if(play[player2].Wall) harm = 1, play[player2].Wallnum++;
cout << " " << play[player2].name << " 受到了 ";
color(RED);
cout << harm;
color(CYAN);
cout << " 点伤害,";
play[player2].nowHP -= harm;
cout << "剩余 ";
color(IGREEN);
cout << max(play[player2].nowHP, 0ll) << "HP\n";
color(CYAN);
if(play[player2].nowHP <= 0){
color(WHITE);
cout << " " << play[player2].name << " 被击倒了\n";
play[player2].die = 1;
play[player1].Killnum++;
play[player2].Killer = play[player1].name;
num++;
if(flag) Sleep(500);
}
color(CYAN);
}
}
else if(tmp == 1){ //使用了地崩术
cout << play[player1].name << " 使用了";
color(YELLOW);
cout << "地崩术";
color(CYAN);
cout << ",\n";
if(flag) Sleep(500);
int pos = rr(2, 5);
pos = min(pos, n - num - 1);
for(int i = 1; i <= pos && num < n - 1; i++){
tmp = rr(1, B);
if(play[player1].nowFast > play[player1].Fast && play[player2].nowFast == play[player2].Fast){
tmp %= 13;
}
else if(play[player1].nowFast == play[player1].Fast && play[player2].nowFast > play[player2].Fast){
tmp %= 5;
}
else tmp %= 8;
if(tmp < 2){
cout << " " << play[player2].name << " 闪避了攻击\n";
if(flag) Sleep(500);
continue;
}
harm = play[player1].Kill / rr(2, 3) + rr(4, 9) - rr(2, 8);
if(play[player2].Wall) harm = 1, play[player2].Wallnum++;
cout << " " << play[player2].name << " 受到了 ";
color(RED);
cout << harm;
color(CYAN);
cout << " 点伤害,";
play[player2].nowHP -= harm;
cout << "剩余 ";
color(IGREEN);
cout << max(play[player2].nowHP, 0ll) << "HP\n";
color(CYAN);
if(play[player2].nowHP <= 0){
color(WHITE);
cout << " " << play[player2].name << " 被击倒了\n";
play[player2].die = 1;
play[player1].Killnum++;
play[player2].Killer = play[player1].name;
num++;
if(flag) Sleep(500);
}
color(CYAN);
player2 = rr(1, n);
while(player2 == player1 || play[player2].die) player2 = rr(1, n);
}
Sleep(400);
}
else if(tmp == 2 && !play[player2].Ice){ //使用了冰冻
cout << play[player1].name << " 使用了";
color(ICYAN);
cout << "冰冻";
color(CYAN);
cout << ",\n";
tmp = rr(1, B);
if(play[player1].nowFast > play[player1].Fast && play[player2].nowFast == play[player2].Fast){
tmp %= 13;
}
else if(play[player1].nowFast == play[player1].Fast && play[player2].nowFast > play[player2].Fast){
tmp %= 5;
}
else tmp %= 8;
if(tmp < 2){
if(flag) Sleep(400);
cout << play[player2].name << " 闪避了攻击\n";
if(flag) Sleep(500);
continue;
}
if(flag) Sleep(166);
harm = play[player1].Kill - rr(5, 12) + rr(7, 11);
if(play[player2].Wall) harm = 1;
cout << play[player2].name << " 被";
color(ICYAN);
cout << "冰冻";
color(CYAN);
cout << "了,";
if(flag) Sleep(166);
cout << "受到了 ";
color(RED);
if(play[player2].Wall) harm = 1, play[player2].Wallnum++;
cout << harm;
color(CYAN);
cout << " 点伤害,";
play[player2].nowHP -= harm;
if(flag) Sleep(166);
cout << "剩余 ";
color(IGREEN);
cout << max(play[player2].nowHP, 0ll) << "HP\n";
if(play[player2].nowHP <= 0){
color(WHITE);
cout << play[player2].name << " 被击倒了\n";
play[player2].die = 1;
play[player1].Killnum++;
play[player2].Killer = play[player1].name;
num++;
if(flag) Sleep(400);
}
color(CYAN);
play[player2].Ice = 1;
}
else if(tmp == 3 && !play[player2].Curse){ //使用了诅咒
cout << play[player1].name << " 使用了";
color(YELLOW);
cout << "诅咒";
color(CYAN);
cout << ",";
tmp = rr(1, B);
if(play[player1].nowFast > play[player1].Fast && play[player2].nowFast == play[player2].Fast){
tmp %= 13;
}
else if(play[player1].nowFast == play[player1].Fast && play[player2].nowFast > play[player2].Fast){
tmp %= 5;
}
else tmp %= 8;
if(tmp < 2){
if(flag) Sleep(500);
cout << play[player2].name << " 闪避了攻击\n";
if(flag) Sleep(500);
continue;
}
if(flag) Sleep(166);
harm = play[player1].Kill - rr(12, 19) + rr(17, 29);
if(play[player2].Wall) harm = 3, play[player2].Wallnum++;
cout << play[player2].name << " 被";
color(YELLOW);
cout << "诅咒";
color(CYAN);
cout << "了,";
if(flag) Sleep(166);
cout << "受到了 ";
color(RED);
cout << harm;
color(CYAN);
cout << " 点伤害,";
play[player2].nowHP -= harm;
if(flag) Sleep(166);
cout << "剩余 ";
color(IGREEN);
cout << max(play[player2].nowHP, 0ll) << "HP\n";
if(play[player2].nowHP <= 0){
color(WHITE);
cout << play[player2].name << " 被击倒了\n";
play[player2].die = 1;
play[player1].Killnum++;
play[player2].Killer = play[player1].name;
num++;
if(flag) Sleep(400);
}
color(CYAN);
play[player2].Curse = 1;
}
else if(tmp == 4 && !play[player2].Charm){ //使用了魅惑
cout << play[player1].name << " 使用了";
color(IPURPLE);
cout << "魅惑";
color(CYAN);
cout << ",";
tmp = rr(1, B);
if(play[player1].nowFast > play[player1].Fast && play[player2].nowFast == play[player2].Fast){
tmp %= 13;
}
else if(play[player1].nowFast == play[player1].Fast && play[player2].nowFast > play[player2].Fast){
tmp %= 5;
}
else tmp %= 8;
if(tmp < 2){
if(flag) Sleep(500);
cout << play[player2].name << " 闪避了攻击\n";
if(flag) Sleep(500);
continue;
}
if(flag) Sleep(500);
harm = play[player1].Kill - rr(5, 9) + rr(17, 29);
cout << play[player2].name << " 被";
color(IPURPLE);
cout << "魅惑";
color(CYAN);
cout << "了\n";
if(flag) Sleep(250);
play[player2].Charm = 1;
}
else if(tmp == 5){ //使用了背刺
cout << play[player1].name << " 使用了";
color(IRED);
cout << "背刺";
color(CYAN);
cout << "、";
tmp = rr(1, B);
if(play[player1].nowFast > play[player1].Fast && play[player2].nowFast == play[player2].Fast){
tmp %= 13;
}
else if(play[player1].nowFast == play[player1].Fast && play[player2].nowFast > play[player2].Fast){
tmp %= 5;
}
else tmp %= 8;
if(tmp < 2){
if(flag) Sleep(500);
cout << play[player2].name << " 闪避了攻击\n";
if(flag) Sleep(500);
continue;
}
if(flag) Sleep(250);
cout << play[player2].name << " 受到了 ";
color(RED);
harm = play[player1].Kill * rr(2, 3) - rr(17, 27) + rr(7, 14);
if(play[player2].Wall) harm = 3, play[player2].Wallnum++;
cout << harm;
color(CYAN);
cout << " 点伤害,";
play[player2].nowHP -= harm;
if(flag) Sleep(250);
cout << "剩余 ";
color(IGREEN);
cout << max(play[player2].nowHP, 0ll) << "HP\n";
if(play[player2].nowHP <= 0){
color(WHITE);
cout << play[player2].name << " 被击倒了\n";
play[player2].die = 1;
play[player1].Killnum++;
play[player2].Killer = play[player1].name;
num++;
if(flag) Sleep(400);
}
color(CYAN);
}
else{ //使用了瘟疫
cout << play[player1].name << " 使用了";
color(IGREEN);
cout << "瘟疫";
color(CYAN);
cout << ",\n";
tmp = rr(1, B);
if(play[player1].nowFast > play[player1].Fast && play[player2].nowFast == play[player2].Fast){
tmp %= 13;
}
else if(play[player1].nowFast == play[player1].Fast && play[player2].nowFast > play[player2].Fast){
tmp %= 5;
}
else tmp %= 8;
if(tmp < 2){
if(flag) Sleep(500);
cout << play[player2].name << " 闪避了攻击\n";
if(flag) Sleep(500);
continue;
}
if(flag) Sleep(250);
int pos = rr(40, 60);
cout << play[player2].name << " 的血量减少了 " << pos << "%,";
color(RED);
harm = play[player2].nowHP * pos / 100ll;
color(CYAN);
play[player2].nowHP -= harm;
if(flag) Sleep(250);
cout << "剩余 ";
color(IGREEN);
cout << max(play[player2].nowHP, 0ll) << "HP\n";
if(play[player2].nowHP <= 0){
color(WHITE);
cout << play[player2].name << " 被击倒了\n";
play[player2].die = 1;
play[player1].Killnum++;
play[player2].Killer = play[player1].name;
num++;
if(flag) Sleep(400);
}
color(CYAN);
}
}
}
else{
tmp = rr(1, B);
if(play[player1].nowFast > play[player1].Fast && play[player2].nowFast == play[player2].Fast){
tmp %= 13;
}
else if(play[player1].nowFast == play[player1].Fast && play[player2].nowFast > play[player2].Fast){
tmp %= 5;
}
else tmp %= 8;
if(tmp < 2){
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(500);
cout << play[player2].name << " 闪避了攻击\n";
if(flag) Sleep(500);
continue;
}
if(play[player1].Kill * 2 < play[player2].De + play[player2].Res){
if(play[player2].Curse){ //玩家二被诅咒
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(200);
cout << "因为被";
color(YELLOW);
cout << "诅咒";
color(CYAN);
cout << "而伤害加倍,";
if(flag) Sleep(200);
harm = play[player1].Kill * 2.2 - (play[player2].De - play[player1].Kill);
cout << play[player2].name << " 受到了 ";
color(RED);
cout << harm;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(200);
}
else if(play[player2].Wall){ //玩家二使用了铁壁
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(250);
harm = 0;
cout << play[player2].name << " 受到了 ";
color(RED);
cout << harm;
play[player2].Wallnum++;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(250);
}
else{
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(250);
cout << play[player2].name << " 受到了 ";
color(RED);
harm = play[player1].Kill * 2 - play[player2].De + rr(17, 23);
cout << harm;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(250);
}
}
else if(play[player1].Kill * 2 == play[player2].De + play[player2].Res){
if(play[player2].Curse){ //玩家二被诅咒
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(200);
cout << "因为被";
color(YELLOW);
cout << "诅咒";
color(CYAN);
cout << "而伤害加倍,";
if(flag) Sleep(200);
harm = play[player1].Kill * 2.2;
cout << play[player2].name << " 受到了 ";
color(RED);
cout << harm;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(200);
}
else if(play[player2].Wall){ //玩家二使用了铁壁
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(250);
harm = 1;
play[player2].Wallnum++;
cout << play[player2].name << " 受到了 ";
color(RED);
cout << harm;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(250);
}
else{
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(250);
cout << play[player2].name << " 受到了 ";
color(RED);
harm = play[player1].Kill + rr(17, 23);
cout << harm;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(250);
}
}
else{
if(play[player2].Curse){ //玩家二被诅咒
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(200);
cout << "因为被";
color(YELLOW);
cout << "诅咒";
color(CYAN);
cout << "而伤害加倍,";
if(flag) Sleep(200);
harm = play[player1].Kill * 2.4;
cout << play[player2].name << " 受到了 ";
color(RED);
cout << harm;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(200);
}
else if(play[player2].Wall){ //玩家二使用了铁壁
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(250);
harm = 2;
play[player2].Wallnum++;
cout << play[player2].name << " 受到了 ";
color(RED);
cout << harm;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(250);
}
else{ //普通攻击
cout << play[player1].name << " 发起攻击,";
if(flag) Sleep(250);
cout << play[player2].name << " 受到了 ";
color(RED);
harm = play[player1].Kill + rr(23, 37);
cout << harm;
play[player1].harmnum += harm * rr(1, 3);
color(CYAN);
cout << " 点伤害,";
if(flag) Sleep(250);
}
}
play[player2].nowHP -= harm;
cout << "剩余 ";
color(IGREEN);
cout << max(play[player2].nowHP, 0ll) << "HP\n";
if(flag) Sleep(400);
if(play[player2].nowHP <= 0){
color(WHITE);
cout << play[player2].name << " 被击倒了\n";
play[player2].die = 1;
play[player1].Killnum++;
play[player2].Killer = play[player1].name;
num++;
if(flag) Sleep(500);
}
color(CYAN);
}
if(_kbhit()){
int a = _getch();
if(a == 32) flag = 0;
}
}
}
bool cmp(node x, node y){
return x.harmnum > y.harmnum;
}
int callen(int x){
int len = 1;
x /= 10;
while(x) len++, x /= 10;
return len;
}
void output(){
int ma = 0, len = 0;
for(int i = 1; i <= n; i++) ma = max(ma, play[i].harmnum);
len = callen(ma);
cout << "\n";
cout << "胜者:\n";
for(int i = 1; i <= n; i++){
if(!play[i].die){
for(int j = 1; j <= (maxi - play[i].name.size()) / 2; j++) cout << " ";
cout << play[i].name;
for(int j = 1; j <= (maxi - play[i].name.size() + 1) / 2; j++) cout << " ";
cout << " 分数:" << play[i].harmnum;
for(int j = 1; j <= len - callen(play[i].harmnum); j++) cout << " ";
cout << " 击杀数:" << play[i].Killnum << "\n";
}
}
cout << "败者:\n";
sort(play + 1, play + n + 1, cmp);
for(int i = 1; i <= n; i++){
if(play[i].die){
for(int j = 1; j <= (maxi - play[i].name.size()) / 2; j++) cout << " ";
cout << play[i].name;
for(int j = 1; j <= (maxi - play[i].name.size() + 1) / 2; j++) cout << " ";
cout << " 分数:" << play[i].harmnum;
for(int j = 1; j <= len - callen(play[i].harmnum); j++) cout << " ";
cout << " 击杀数:" << play[i].Killnum << " 致命一击:" << play[i].Killer << "\n";
}
}
}
signed main(){
cout.tie(0), cin.tie(0);
srand(time(0));
color(WHITE);
cout << "欢迎来到名字竞技场\n注:名字只可以用字母、数字、下划线、汉字组成\n战斗时,可以按 [空格] 加速\n";
system("pause");
input();
player();
output();
color(WHITE);
cout << "\n按任意键结束游戏. . .";
while(1){
if(_kbhit()){
break;
}
}
return 0;
}五子棋(QWQ__QWQ所有)
#include<bits/stdc++.h>
#include<windows.h>
#include<unistd.h>
#include<conio.h>
using namespace std;
struct point {
int y;
int x;
};
struct dir {
int dy;
int dx;
};
const dir d1 = {0, 1};//横
const dir d2 = {1, 0};//竖
const dir d3 = {1, -1};//撇
const dir d4 = {1, 1};//捺
const WORD GRAY = FOREGROUND_INTENSITY;
const WORD RED = FOREGROUND_RED | GRAY;
const WORD IBLUE = FOREGROUND_BLUE | GRAY;
const WORD IGREEN = FOREGROUND_GREEN | GRAY;
const WORD IRED = FOREGROUND_RED | GRAY;
const WORD ICYAN = IBLUE | IGREEN;
const WORD WHITE = ICYAN | IRED;
class gobang {
private:
int chessboard[15][15];//记录棋盘信息
int player;//玩家棋子
int computer;//电脑棋子
int Y, X;//光标位置
point laozi;//落子位置
public:
void play();
int selMode();
void turnPlayer();
void turnComputer();
bool isEnd();
protected:
bool isInBoard(point p);
void display();
int score(point p, int who);
point newPoint(point p, dir d, int lenth);
};
void color(WORD haha){
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //定义窗口缓冲区信息结构体
GetConsoleScreenBufferInfo(handle_out, &csbi); //获得窗口缓冲区信息
SetConsoleTextAttribute(handle_out, haha);
}
void gobang::play() {
int i = selMode();
if(i == 1) {
computer = 1;
player = 2;
}
else {
player = 1;
computer = 2;
}
int cur = 1;
while(1) {
if(cur == player)
turnPlayer();
else
turnComputer();
if(isEnd()) {
if(cur == player) {
cout << "你赢了!";
system("pause");
break;
}
else {
cout << "FIAR AI赢了!";
system("pause");
break;
}
}
else
cur = (cur == 1) ? 2 : 1;
}
}
int gobang::selMode() {
system("cls");
cout << "*****欢迎游玩由KevinKim开发的五子棋人机对战******\n";
cout << "**我是待会儿要和你对战的FIAR AI(five-in-a-row)***\n";
cout << "***我可是很厉害的!(KevinKim测试的时候输2赢1)****\n";
cout << "******这是基本操作:WASD上下左右移动,J落子******\n";
cout << "*************************************************\n";
cout << "******************0、退出************************\n";
cout << "******************1、我先手**********************\n";
cout << "******************2、你先手**********************\n";
cout << "*************************************************\n";
while(1) {
int i;
cin >> i;
if(i == 0)
exit(1);
else if(i == 1 || i == 2) {
Y = 9;
X = 9;
for(int j = 0; j < 15; j ++) {
for(int k = 0; k < 15; k ++)
chessboard[j][k] = 0;
}
display();
return i;
}
else
cout << "无效输入,请重新输入!\n";
}
}
void gobang::turnPlayer() {
cout << "轮到你了!\n";
while(1) {
char c = getch();
if(c == 'w') {
if(Y != 0) {
Y --;
display();
}
}
else if(c == 's') {
if(Y != 14) {
Y ++;
display();
}
}
else if(c == 'a') {
if(X != 0) {
X --;
display();
}
}
else if(c == 'd') {
if(X != 14) {
X ++;
display();
}
}
else if(c == 'j' && chessboard[Y][X] == 0) {
laozi.y = Y;
laozi.x = X;
chessboard[Y][X] = player;
display();
break;
}
}
}
void gobang::turnComputer() {
cout << "FIAR AI思考中……\n";
sleep(1);
point best1, best2;
do {
srand(time(NULL));
best1.y = best2.y = rand() % 15;
best1.x = best2.x = rand() % 15;
}
while(chessboard[best1.y][best1.x] != 0);
int a1 = score(best1, computer), b1 = score(best1, player);
for(int i = 0; i < 15; i ++) {
for(int j = 0; j < 15; j ++) {
if(chessboard[i][j] != 0)
continue;
point cur = {i, j};
int m1 = score(cur, computer);
int m2 = score(cur, player);
if(m1 > a1) {
best1 = cur;
a1 = m1;
b1 = m2;
}
else if(m1 == a1) {
if(m2 > b1) {
best1 = cur;
b1 = m2;
}
}
}
}
int a2 = score(best2, player), b2 = score(best2, computer);
for(int i = 0; i < 15; i ++) {
for(int j = 0; j < 15; j ++) {
if(chessboard[i][j] != 0)
continue;
point cur = {i, j};
int m1 = score(cur, player);
int m2 = score(cur, computer);
if(m1 > a2) {
best2 = cur;
a2 = m1;
b2 = m2;
}
else if(m1 == a2) {
if(m2 > b2) {
best2 = cur;
b2 = m2;
}
}
}
}
if(a1 >= a2)
laozi = best1;
else
laozi = best2;
chessboard[laozi.y][laozi.x] = computer;
display();
}
int gobang::score(point p, int who) {
int win5 = 0, alive4 = 0, die4 = 0, ddie4 = 0, alive3 = 0,
dalive3 = 0, die3 = 0, alive2 = 0, dalive2 = 0, die2 = 0, nothing = 0;
int opp;
if(who == 1)
opp = 2;
else
opp = 1;
for(int i = 1; i <= 4; i ++) {
dir d;
switch(i) {
case 1:
d = d1;
break;
case 2:
d = d2;
break;
case 3:
d = d3;
break;
case 4:
d = d4;
break;
}
int l = 1;
point le, ri, p1;
int left[5], right[5];
p1 = newPoint(p, d, -1);
le = p;
while(isInBoard(p1) && chessboard[p1.y][p1.x] == who) {
le = p1;
p1 = newPoint(p1, d, -1);
l ++;
}
p1 = newPoint(p, d, 1);
ri = p;
while(isInBoard(p1) && chessboard[p1.y][p1.x] == who) {
ri = p1;
p1 = newPoint(p1, d, 1);
l ++;
}
for(int j = 1; j <= 4; j ++) {
p1 = newPoint(le, d, -j);
if(isInBoard(p1))
left[j] = chessboard[p1.y][p1.x];
else
left[j] = opp;
p1 = newPoint(ri, d, j);
if(isInBoard(p1))
right[j] = chessboard[p1.y][p1.x];
else
right[j] = opp;
}
//具体棋型判断
if(l == 5)
win5 ++;
else if(l == 4) {
if(left[1] == 0 && right[1] == 0) //alive4
alive4 ++;
else if(left[1] == 0 || right[1] == 0) //die4
die4 ++;
else //nothing
nothing ++;
}
else if(l == 3) {
if((left[1] == 0 && left[2] == who) || (right[1] == 0 && right[2] == who)) //ddie4
ddie4 ++;
else if(left[1] == 0 && right[1] == 0 && (left[2] == 0 || right[2] == 0)) //alive3
alive3 ++;
else if((left[1] == 0 && left[2] == 0) || (right[1] == 0 && right[2] == 0)) //die3
die3 ++;
else if(left[1] == 0 && right[1] == 0) //die3
die3 ++;
else //nothing
nothing ++;
}
else if(l == 2) {
if((left[1] == 0 && left[2] == who && left[3] == who) &&
(right[1] == 0 && right[2] == who && right[3] == who)) //die4
ddie4 ++;
else if(left[1] == 0 && right[1] == 0 &&
((left[2] == who && left[3] == 0) || (right[2] == who && right[3] == 0))) //dalive3
dalive3 ++;
else if((left[1] == 0 && left[3] == 0 && left[2] == who) ||
(right[1] == 0 && right[3] == 0 && right[2] == who)) //die3
die3 ++;
else if((left[1] == 0 && right[1] == 0) &&
(left[2] == who || right[2] == who)) //die3
die3 ++;
else if((left[1] == 0 && left[2] == 0 && left[3] == who) ||
(right[1] == 0 && right[2] == 0 && right[3] == who)) //die3
die3 ++;
else if((left[1] == 0 && right[1] == 0 && right[2] == 0 && right[3] == 0) ||
(left[1] == 0 && left[2] == 0 && right[1] == 0 && right[2] == 0) ||
(left[1] == 0 && left[2] == 0 && left[3] == 0 && right[1] == 0)) //alive2
alive2 ++;
else if((left[1] == 0 && left[2] == 0 && left[3] == 0) ||
(right[1] == 0 && right[2] == 0 && right[3] == 0)) //die2
die2 ++;
else //nothing
nothing ++;
}
else if(l == 1) {
if((left[1] == 0 && left[2] == who && left[3] == who && left[4] == who) ||
(right[1] == 0 && right[2] == who && right[3] == who && right[4] == who)) //ddie4
ddie4 ++;
else if((left[1] == 0 && right[1] == 0) && ((left[2] == who && left[3] == who && left[4] == 0) ||
(right[2] == who && right[3] == who && right[4] == 0))) //dalive3
dalive3 ++;
else if((left[1] == 0 && right[1] == 0) &&
((left[2] == who && left[3] == who) || (right[2] == who && right[3] == who))) //die3
die3 ++;
else if((left[1] == 0 && left[4] == 0 && left[2] == who && left[3] == who) ||
(right[1] == 0 && right[4] == 0 && right[2] == who && right[3] == who)) //die3
die3 ++;
else if((left[1] == 0 && left[2] == 0 && left[3] == who && left[4] == who) ||
(right[1] == 0 && right[2] == 0 && right[3] == who && right[4] == who)) //die3
die3 ++;
else if((left[1] == 0 && left[3] == 0 && left[2] == who && left[4] == who) ||
(right[1] == 0 && right[3] == 0 && right[2] == who && right[4] == who)) //die3
die3 ++;
else if((left[1] == 0 && right[1] == 0 && right[3] == 0 && right[2] == who) && (left[2] == 0 || right[4] == 0)) //dalive2
dalive2 ++;
else if((right[1] == 0 && left[1] == 0 && left[3] == 0 && left[2] == who) &&
(right[2] == 0 || left[4] == 0)) //dalive2
dalive2 ++;
else if((left[1] == 0 && right[1] == 0 && right[2] == 0 && right[4] == 0 && right[3] == who) ||
(right[1] == 0 && left[1] == 0 && left[2] == 0 && left[4] == 0 && left[3] == who)) //dalive2
dalive2 ++;
else if((left[1] == 0 && left[3] == 0 && left[4] == 0 && left[2] == who) ||
(right[1] == 0 && right[3] == 0 && right[4] == 0 && right[2] == who)) //die2
die2 ++;
else if((left[1] == 0 && right[1] == 0 && right[2] == 0 && left[2] == who) ||
(right[1] == 0 && left[1] == 0 && left[2] == 0 && right[2] == who)) //die2
die2 ++;
else if((left[1] == 0 && left[2] == 0 && left[4] == 0 && left[3] == who) ||
(right[1] == 0 && right[2] == 0 && right[4] == 0 && right[3] == who)) //die2
die2 ++;
else if((left[1] == 0 && left[2] == 0 && right[1] == 0 && left[3] == who) ||
(right[1] == 0 && right[2] == 0 && left[1] == 0 && right[3] == who)) //die2
die2 ++;
else if((left[1] == 0 && left[2] == 0 && left[3] == 0 && left[4] == who) ||
(right[1] == 0 && right[2] == 0 && right[3] == 0 && right[4] == who)) //die2
die2 ++;
else //nothing
nothing ++;
}
}
if(win5 >= 1)
return 14;//赢5
if(alive4 >= 1 || die4 >= 2 || (die4 >= 1 && alive3 >= 1))
return 13;//活4 双死4 死4活3
if(alive3 >= 2)
return 12;//双活3
if(die3 >= 1 && alive3 >= 1)
return 11;//死3高级活3
if(die4 >= 1)
return 10;//高级死4
if(ddie4 >= 1)
return 9;//低级死4
if(alive3 >= 1)
return 8;//单活3
if(dalive3 >= 1)
return 7;//跳活3
if(alive2 >= 2)
return 6;//双活2
if(alive2 >= 1)
return 5;//活2
if(dalive2 >= 1)
return 4;//低级活2
if(die3 >= 1)
return 3;//死3
if(die2 >= 1)
return 2;//死2
return 1;//没有威胁
}
bool gobang::isEnd() {
for(int i = 1; i <= 4; i ++) {
dir d;
int count = 0;
switch(i) {
case 1:
d = d1;
break;
case 2:
d = d2;
break;
case 3:
d = d3;
break;
case 4:
d = d4;
break;
}
for(int j = -4; j <= 4; j ++) {
point p1 = newPoint(laozi, d, j);
if(isInBoard(p1) && chessboard[p1.y][p1.x] == chessboard[laozi.y][laozi.x])
count ++;
else
count = 0;
if(count == 5)
return true;
}
}
return false;
}
bool gobang::isInBoard(point p) {
if(p.y >= 0 && p.y < 15 && p.x >= 0 && p.x < 15)
return true;
else
return false;
}
point gobang::newPoint(point p, dir d, int lenth) {
point p1 = {p.y + d.dy * lenth, p.x + d.dx * lenth};
return p1;
}
void gobang::display() {
system("cls");
for(int i = 0; i < 15; i ++) {
for(int j = 0; j < 15; j ++) {
color(WHITE);
if(i == Y && j == X){
color(RED);
cout << "+ ";
}
else if(chessboard[i][j] == 1)
cout << "○";
else if(chessboard[i][j] == 2)
cout << "●";
else
cout << "。";
}
cout << "\n";
}
}
int main() {
gobang game;
game.play();
return 0;
}迷宫
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
#define int long long
#define mid ((lt + rt) >> 1)
using namespace std;
const int N = 5005;
const WORD GRAY = FOREGROUND_INTENSITY; //灰色属性文本
const WORD BLUE = FOREGROUND_BLUE | GRAY; //蓝色属性文本
const WORD GREEN = FOREGROUND_GREEN | GRAY; //蓝色属性文本
const WORD RED = FOREGROUND_RED | GRAY; //红色属性文本
const WORD CYAN = BLUE | GREEN; //青色属性文本
const WORD YELLOW = RED | GREEN; //黄色属性文本
const WORD WHITE = CYAN | RED; //白色属性文本
void position(int y, int x){ //打印位置,下标从(0, 0)开始
COORD pos = {x, y};
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}
void color(WORD haha){
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //定义窗口缓冲区信息结构体
GetConsoleScreenBufferInfo(handle_out, &csbi); //获得窗口缓冲区信息
SetConsoleTextAttribute(handle_out, haha);
}
int n, m;
char a[N][N];
bool vis[N][N];
int fx[] = {0, 1, -1, 0, 0};
int fy[] = {0, 0, 0, 1, -1};
int szn[] = {0, 25, 30, 35, 45};
int szm[] = {0, 40, 65, 125, 150};
struct node{
int px, py, tot;
};
void print(string c){
for(int i = 0; i < c.size(); i++){
cout << c[i];
Sleep(40);
}
}
int Rand(int lt, int rt){
if(lt == rt) return rt;
if(rand() % 2) return Rand(lt, mid);
else return Rand(mid + 1, rt);
}
bool check(int x, int y){
int cnt = 0;
for(int i = 1; i <= 4; i++){
int xx = x + fx[i], yy = y + fy[i];
if(!(xx >= 1 && xx <= n && yy >= 1 && yy <= m)) continue;
if(a[xx][yy] == '.') cnt++;
}
return cnt < 2;
}
void build(){
memset(vis, 0, sizeof vis);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++) a[i][j] = '#';
}
queue<node> q;
q.push({1, 1});
a[1][1] = '.';
while(q.size()){
node now = q.front();
q.pop();
int x = now.px;
int y = now.py;
if(vis[x][y]) continue;
vis[x][y] = 1;
if(x == n && y == m) return ;
for(int i = 1; i <= 4; i++){
int xx = x + fx[i], yy = y + fy[i];
if(Rand(1, 1000) % 12 > 0 && xx >= 1 && xx <= n && yy >= 1 && yy <= m && !vis[xx][yy] && check(xx, yy)){
q.push({xx, yy});
a[xx][yy] = '.';
}
}
}
a[n][m] = '.';
if(check(n, m)) return ;
while(q.size()){
node now = q.front();
q.pop();
int x = now.px;
int y = now.py;
if(vis[x][y]) continue;
vis[x][y] = 1;
if(!check(x, y)) return ;
for(int i = 1; i <= 4; i++){
int xx = x + fx[i], yy = y + fy[i];
if(Rand(1, 1000) % 17 > 0 && xx >= 1 && xx <= n && yy >= 1 && yy <= m && !vis[xx][yy] && check(xx, yy)){
q.push({xx, yy});
a[xx][yy] = '.';
}
}
}
}
bool f(int x, int y){ return a[x][y] != '#' && x >= 1 && x <= n && y >= 1 && y <= m; }
signed main(){
cin.tie(0), cout.tie(0);
color(CYAN);
system("title TYF 走迷宫");
print("欢迎来到走迷宫小游戏\n");
Sleep(500);
print("该游戏可以锻炼你的观察能力和耐心,也有助于你集中注意力\n");
Sleep(500);
print("创作者学校:长郡双语洋湖实验中学\n");
Sleep(500);
print("创作者:汤奕凡\n");
Sleep(500);
print("制作时间:2025/03/10\n");
Sleep(1000);
system("cls");
print("请先将窗口最大化(否则会出 Bug)");
Sleep(3500);
system("cls");
print("最大化后,按[空格]开始游戏");
srand(time(0));
while(1){
if(_kbhit()){
int ch = _getch();
if(ch == ' ') break;
}
}
val:;
position(0, 0);
system("cls");
color(CYAN);
print("请选择难度\n");
Sleep(500);
cout << "[1]简单(30 * 30)\t \t[2]普通(40 * 40)\n[3]困难(45 * 75)\t \t[4]地狱(45 * 150)\n[5]自定义\n";
while(1){
if(_kbhit()){
int ch = _getch();
ch -= '0';
if(ch == 5){
print("行数(Min: 10; Max: 45):");
cin >> n;
while(n < 10 || n > 50){
print("超范围了,请重新输入:");
cin >> n;
}
Sleep(300);
print("列数(Min: 10; Max: 150):");
cin >> m;
while(m < 10 || m > 160){
print("超范围了,请重新输入:");
cin >> m;
}
break;
}
else if(ch >= 1 && ch <= 4){
n = szn[ch];
m = szm[ch];
break;
}
}
}
system("cls");
color(GREEN);
cout << '#';
color(CYAN);
cout << "表示墙,";
color(WHITE);
cout << '.';
color(CYAN);
cout << "表示路,";
color(RED);
cout << '@';
color(CYAN);
cout << "表示角色,";
color(YELLOW);
cout << "&";
color(CYAN);
cout << "表示终点,你可以用[w], [s], [a], [d]来操控角色,也可以按 [b] 重新开始,按 [f] 重新选择迷宫难度。\n";
build();
a[1][1] = '@';
a[n][m] = '&';
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(a[i][j] == '@') color(RED);
else if(a[i][j] == '.') color(WHITE);
else if(a[i][j] == '#') color(GREEN);
else if(a[i][j] == '&') color(YELLOW);
cout << a[i][j];
}
cout << "\n";
}
color(CYAN);
int nx = 1, ny = 1;
while(1){
if(nx == n && ny == m) break;
if(_kbhit()){
int ch = _getch();
if(ch == 'w' || ch == 'W'){
if(!f(nx - 1, ny)) continue;
position(nx, ny - 1);
color(WHITE);
cout << '.';
nx--;
position(nx, ny - 1);
color(RED);
cout << '@';
}
else if(ch == 's' || ch == 'S'){
if(!f(nx + 1, ny)) continue;
position(nx, ny - 1);
color(WHITE);
cout << '.';
nx++;
position(nx, ny - 1);
color(RED);
cout << '@';
}
else if(ch == 'a' || ch == 'A'){
if(!f(nx, ny - 1)) continue;
position(nx, ny - 1);
color(WHITE);
cout << '.';
ny--;
position(nx, ny - 1);
color(RED);
cout << '@';
}
else if(ch == 'd' || ch == 'D'){
if(!f(nx, ny + 1)) continue;
position(nx, ny - 1);
color(WHITE);
cout << '.';
ny++;
position(nx, ny - 1);
color(RED);
cout << '@';
}
else if(ch == 'f' || ch == 'F'){
Sleep(500);
goto val;
}
else if(ch == 'b' || ch == 'B'){
position(nx, ny - 1);
color(WHITE);
cout << '.';
nx = ny = 1;
position(nx, ny - 1);
color(RED);
cout << '@';
}
}
}
Sleep(500);
system("cls");
color(RED);
cout << "# ### # ####### ### ## ##\n";
cout << " # # # # ### ## # ## ##\n";
cout << " # # # # ### ## # ## ##\n";
cout << " # # # # ### ## # ## ##\n";
cout << " # # # # ### ## # ## ##\n";
cout << " # # # # ### ## # ## \n";
cout << " ### ### ####### ## ### ##\n";
color(CYAN);
print("是否继续游玩([1]是;[2]否)\n");
while(1){
if(_kbhit()){
int ch = _getch();
if(ch == '1') goto val;
else if(ch == '2') goto ex;
}
}
ex:print("感谢您的游玩,再见!\n");
Sleep(1500);
system("cls");
return 0;
}