跳到正文

个人记录

奇妙小代码

ComeTheWay约 25 分钟
原文
文章信息与版本

文章信息

发布
2023年11月25日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 25 分钟

版本

  1. v1当前

2048

C++
#include<bits/stdc++.h>
#include<time.h>
#include<windows.h>
#include<conio.h>
using namespace std;
int sq[5][5], c = 16, m, mx = 0;
bool b;
char a[5];
bool up() { //上移
	bool flag = false;
	for(int i = 0; i < 3; i++)
		for(int j = 1; j <= 4; j++)
			for(int k = 2; k <= 4; k++)
				if(sq[k - 1][j] == 0 && sq[k][j] != 0) {
					sq[k - 1][j] = sq[k][j];
					sq[k][j] = 0;
					flag = true;
				}
	for(int i = 1; i <= 4; i++) {
		if(sq[1][i] == sq[2][i] && sq[1][i] != 0) {
			sq[1][i] *= 2;
			sq[2][i] = 0;
			flag = true;
		}
		if(sq[2][i] == sq[3][i] && sq[2][i] != 0) {
			sq[2][i] *= 2;
			sq[3][i] = 0;
			flag = true;
		}
		if(sq[3][i] == sq[4][i] && sq[3][i] != 0) {
			sq[3][i] *= 2;
			sq[4][i] = 0;
			flag = true;
		}
	}
	for(int i = 0; i < 2; i++)
		for(int j = 1; j <= 4; j++)
			for(int k = 2; k <= 4; k++)
				if(sq[k - 1][j] == 0 && sq[k][j] != 0) {
					sq[k - 1][j] = sq[k][j];
					sq[k][j] = 0;
				}
	c = 0;
	for(int i = 1; i <= 4; i++)
		for(int j = 1; j <= 4; j++)
			if(sq[i][j] == 0)
				c++;
	return flag;
}

bool down() { //下移
	bool flag = false;
	for(int i = 0; i < 3; i++)
		for(int j = 1; j <= 4; j++)
			for(int k = 2; k <= 4; k++)
				if(sq[k][j] == 0 && sq[k - 1][j] != 0) {
					sq[k][j] = sq[k - 1][j];
					sq[k - 1][j] = 0;
					flag = true;
				}
	for(int i = 1; i <= 4; i++) {
		if(sq[3][i] == sq[4][i] && sq[4][i] != 0) {
			sq[4][i] *= 2;
			sq[3][i] = 0;
			flag = true;
		}
		if(sq[2][i] == sq[3][i] && sq[3][i] != 0) {
			sq[3][i] *= 2;
			sq[2][i] = 0;
			flag = true;
		}
		if(sq[1][i] == sq[2][i] && sq[2][i] != 0) {
			sq[2][i] *= 2;
			sq[1][i] = 0;
			flag = true;
		}
	}
	for(int i = 0; i < 2; i++)
		for(int j = 1; j <= 4; j++)
			for(int k = 2; k <= 4; k++)
				if(sq[k][j] == 0 && sq[k - 1][j] != 0) {
					sq[k][j] = sq[k - 1][j];
					sq[k - 1][j] = 0;
				}
	c = 0;
	for(int i = 1; i <= 4; i++)
		for(int j = 1; j <= 4; j++)
			if(sq[i][j] == 0)
				c++;
	return flag;
}
bool left() { //左移
	bool flag = false;
	for(int i = 0; i < 3; i++)
		for(int j = 2; j <= 4; j++)
			for(int k = 1; k <= 4; k++)
				if(sq[k][j - 1] == 0 && sq[k][j] != 0) {
					sq[k][j - 1] = sq[k][j];
					sq[k][j] = 0;
					flag = true;
				}
	for(int i = 1; i <= 4; i++) {
		if(sq[i][1] == sq[i][2] && sq[i][1] != 0) {
			sq[i][1] *= 2;
			sq[i][2] = 0;
			flag = true;
		}
		if(sq[i][2] == sq[i][3] && sq[i][2] != 0) {
			sq[i][2] *= 2;
			sq[i][3] = 0;
			flag = true;
		}
		if(sq[i][3] == sq[i][4] && sq[i][3] != 0) {
			sq[i][3] *= 2;
			sq[i][4] = 0;
			flag = true;
		}
	}
	for(int i = 0; i < 2; i++)
		for(int j = 2; j <= 4; j++)
			for(int k = 1; k <= 4; k++)
				if(sq[k][j - 1] == 0 && sq[k][j] != 0) {
					sq[k][j - 1] = sq[k][j];
					sq[k][j] = 0;
				}
	c = 0;
	for(int i = 1; i <= 4; i++)
		for(int j = 1; j <= 4; j++)
			if(sq[i][j] == 0)
				c++;
	return flag;
}

bool right() { //右移
	bool flag = false;
	for(int i = 0; i < 3; i++)
		for(int j = 2; j <= 4; j++)
			for(int k = 1; k <= 4; k++)
				if(sq[k][j] == 0 && sq[k][j - 1] != 0) {
					sq[k][j] = sq[k][j - 1];
					sq[k][j - 1] = 0;
					flag = true;
				}
	for(int i = 1; i <= 4; i++) {
		if(sq[i][3] == sq[i][4] && sq[i][4] != 0) {
			sq[i][4] *= 2;
			sq[i][3] = 0;
			flag = true;
		}
		if(sq[i][2] == sq[i][3] && sq[i][3] != 0) {
			sq[i][3] *= 2;
			sq[i][2] = 0;
			flag = true;
		}
		if(sq[i][1] == sq[i][2] && sq[i][2] != 0) {
			sq[i][2] *= 2;
			sq[i][1] = 0;
			flag = true;
		}
	}
	for(int i = 0; i < 2; i++)
		for(int j = 2; j <= 4; j++)
			for(int k = 1; k <= 4; k++)
				if(sq[k][j] == 0 && sq[k][j - 1] != 0) {
					sq[k][j] = sq[k][j - 1];
					sq[k][j - 1] = 0;
				}
	c = 0;
	for(int i = 1; i <= 4; i++)
		for(int j = 1; j <= 4; j++)
			if(sq[i][j] == 0)
				c++;
	return flag;
}
void try_again() {
	for(int i = 1; i <= 4; i++)
		for(int j = 1; j <= 4; j++)
			sq[i][j] = 0;
	c = 16;
	m += rand() % c;
	m %= c;
	int x = 1, y = 1;
	for(int i = 0; i <= m; i++) {
		do {
			x++;
			if(x > 4) {
				y++;
				x = 1;
			}
			if(y > 4) {
				y = 1;
				x = 1;
			}
			rand();
		} while(sq[x][y] != 0);
	}
	sq[x][y] = (rand() % (rand() % 2 + 1) + 1) * 2;
}
/*
void r(){
	srand(time(0));
	int m = rand() % c;
	int x = 1, y = 1;
	for(int i = 0; i <= m; i++){
		do{
			x++;
			if(x > 4){
				y++;
				x = 1;
			}
			if(y > 4){
				y = 1;
				x = 1;
			}
			rand();
		} while(sq[x][y] != 0);
	}
	sq[x][y] = (rand() % 2 + 1) * 2;
}
*/
int main() {
	srand(time(0));
	m = rand() % c;
	int x = 1, y = 1;
	for(int i = 0; i <= m; i++) {
		do {
			x++;
			if(x > 4) {
				y++;
				x = 1;
			}
			if(y > 4) {
				y = 1;
				x = 1;
			}
			rand();
		} while(sq[x][y] != 0);
	}
	sq[x][y] = (rand() % (rand() % 2 + 1) + 1) * 2;
	m += rand() % c;
	m %= c;
	for(int i = 0; i <= m; i++) {
		do {
			x++;
			if(x > 4) {
				y++;
				x = 1;
			}
			if(y > 4) {
				y = 1;
				x = 1;
			}
			rand();
		} while(sq[x][y] != 0);
	}
	sq[x][y] = (rand() % (rand() % 2 + 1) + 1) * 2;
	while(true) {
		system("cls");
		cout << "+----------------+\n";
		for(int i = 1; i <= 3; i++) {
			cout << '|';
			for(int j = 1; j <= 4; j++)
				if(sq[i][j]) printf("%4d", sq[i][j]);
				else cout << "   _";
			cout << "|\n|                |\n";
		}
		cout << '|';
		for(int j = 1; j <= 4; j++)
			if(sq[4][j]) printf("%4d", sq[4][j]);
			else cout << "   _";
		cout << "|\n";
		cout << "+----------------+\nw = up; s = down; a = left; d = right. (press e to exit, press r to try again)\n-- ";
		a[0]=getch();
		if(a[0] == 'e') return 0;
		else if(a[0] == 'w') b = up();
		else if(a[0] == 's') b = down();
		else if(a[0] == 'a') b = left();
		else if(a[0] == 'd') b = right();
		else if(a[0] == 'r') {
			b = true;
			try_again();
		} else b = false;
		//cout << b;
		while(!b) {
//			cout << "\nerror\nplease press again.\n-- ";
			a[0]=getch();
			if(a[0] == 'e') return 0;
			else if(a[0] == 'w') b = up();
			else if(a[0] == 's') b = down();
			else if(a[0] == 'a') b = left();
			else if(a[0] == 'd') b = right();
			else if(a[0] == 'r') {
				b = true;
				try_again();
			} else b = false;
		}
		m += rand() % c;
		m %= c;
		for(int i = 0; i <= m; i++) {
			do {
				x++;
				if(x > 4) {
					y++;
					x = 1;
				}
				if(y > 4) {
					y = 1;
					x = 1;
				}
				rand();
			} while(sq[x][y] != 0);
		}
		sq[x][y] = (rand() % (rand() % 2 + 1) + 1) * 2;
	}
	return 0;
}

https://www.luogu.com.cn/discuss/737491

名字大作战

作者:@zjr0330

侵权必删

C++
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
string name[400];
int hp[400] = {0}, fy[400] = {0}, sh[400] = {0}, meig[400] = {1, 10, 10, 10}, huog[400] = {1, 10, 10, 10}, names = 0;
int weny[400] = {5};
bool si[500] = {false}, bei[500] = {false};
void color(const int textColor) {
    if (textColor >= 0 && textColor <= 15)
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), textColor);
    else
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
void meiq(int rrs, int rrm) {
    cout << name[rrs] << " 对 " << name[rrm] << " 使用了";
    color(13);
    cout << " 魅惑 ";
    color(7);
    Sleep(800);
    if (rand() % 5 == 1) {
        cout << ",但 " << name[rrm];
        color(11);
        cout << " 回避 ";
        color (7);
        cout << "了这个攻击";
        Sleep(800);
        goto anssn;
    }
    cout << ", " << name[rrm] << " 被";
    color(13);
    cout << " 魅惑 ";
    color(7);
    cout << "了";
    bei[rrm] = true;
anssn:
    Sleep(200);
    cout << endl;
}
void huoq(int x, int y) {
    int s1 = rand() % 30, xyz = 0, cp = rand() % 114;
    cout << name[x] << " 对 " << name[y] << " 使用了";
    color(12);
    cout << " 火球攻 ";
    color(7);
    Sleep(800);
    if (rand() % 5 == 1) {
        cout << ",但 " << name[y];
        color(11);
        cout << " 回避 ";
        color (7);
        cout << "了这个攻击";
        Sleep(800);
        goto AAS2;
    } else if (rand() % 4 == 2) {
        xyz = 1;
        cout << ", " << name[y] << " 使用了 ";
        color(11);
        cout << "防御 ";
        color(7);
        Sleep(600);
    }
    if (fy[y] >= sh[x] + s1 + cp / (xyz == 0 ? 1 : 2))hp[y]--;
    else hp[y] -= (sh[x] - fy[y] + s1 + cp) / (xyz == 0 ? 2 : 1);
    cout << ", " << name[y] << " 受到了 " << max(1, (sh[x] - fy[y] + s1 + cp) / (xyz == 0 ? 2 : 1)) << " 点伤害, ";
    Sleep(500);
    cout << name[y] << " 还有 " << hp[y] << " 滴血";
AAS2:
    Sleep(200);
    cout << endl;
}
void sha(int x, int y, int fj = 0) {
    int s1 = rand() % 30, xyz = 0;
    if (fj == 0) {
        cout << name[x] << " 对 " << name[y] << " 使用了 攻击 ";
    }
    Sleep(800);
    bool s123 = false;
    if (rand() % 5 == 1) {
        cout << ",但 " << name[y];
        color(11);
        cout << " 回避 ";
        color (7);
        cout << "了这个攻击";
        Sleep(800);
        goto AAS;
    } else if (rand() % 5 == 2) {
        xyz = 1;
        cout << ", " << name[y] << " 使用了 ";
        color(11);
        cout << "防御 ";
        color(7);
        Sleep(600);
    }
    if ((fy[y] >= sh[x] + s1) / (xyz == 0 ? 1 : 2)) {
        hp[y]--;
        s123 = true;
    } else {
        hp[y] -= (sh[x] - fy[y] + s1) / (xyz == 0 ? 1 : 2);
    }
    cout << ", " << name[y] << " 受到了 " << max(1, (sh[x] - fy[y] + s1) / (xyz == 0 ? 1 : 2)) << " 点伤害, ";
    Sleep(500);
    cout << name[y] << " 还有 " << hp[y] << " 滴血 ";
    if (s123)cout << "(︶︿︶)=凸";
AAS:
    Sleep(800);
    cout << endl;
}
void wenyi(int rrs, int rrm) {
    int s1 = rand() % 95, sn = hp[rrm];
    cout << name[rrs] << " 对 " << name[rrm] << " 使用了 ";
    color(3);
    cout << "瘟疫 ";
    color(7);
    Sleep(500);
    if (rand() % 5 == 1) {
        cout << ",但 " << name[rrm];
        color(11);
        cout << " 回避 ";
        color (7);
        cout << "了这个攻击";
        Sleep(800);
        goto AAAAAA;
    }
    cout << ", " << name[rrm] << " 生命减少 " << s1 << " %, ";
    Sleep(300);
    hp[rrm] = hp[rrm] * 1.0 * (100 - s1) / 100;
    cout << name[rrm] << " 受到了 " << sn - hp[rrm] << " 点伤害, ";
    Sleep(300);
    cout << name[rrm] << " 还有 " << hp[rrm] << " 滴血 ";
AAAAAA:
    Sleep(200);
    cout << endl;
}
int main() {
    srand(time(NULL));
    cout << "人数?:";
    cin >> names;
    fill(meig, meig + 250, 5);
    fill(huog, huog + 250, 5);
    fill(weny, weny + 250, 5);
    for (int i = 1; i <= names; i++) {
        cout << "名字 " << i << ":";
        cin >> name[i];
        if (name[i] == "SB")hp[i] += 60, fy[i] += 20, sh[i] += 20;
        if (rand() % 8 == 0) {
            meig[i] = rand() % 50;
        }
        if (rand() % 8 == 2) {
            huog[i] = rand() % 50;
        }
        if (rand() % 8 == 6) {
            weny[i] = rand() % 50;
        }
        for (int j = 0; j < 450; j++) {
            int xp = rand() % 100;
            if (xp < 65)hp[i]++;
            else if (xp >= 65 && xp <= 79)fy[i]++;
            else if (xp > 79 && xp <= 100)sh[i]++;
        }
        cout << name[i] << ": 血量 " << hp[i] << "  防御 " << fy[i] << "  伤害 " << sh[i] << endl;
    }
    int rrs = ((rand() % (names * 6)) % names) + 1, rrm = ((rand() % (names * 6)) % names) + 1, jcmh = 0, fj = 0;
    for (int i = 1; i < 1000000; i++) {
ERTYU:
        rrs = ((rand() % (names * 6)) % names) + 1, rrm = ((rand() % (names * 6)) % names) + 1, jcmh = 0, fj = 0;
        if (rrm == rrs)goto ERTYU;
        if (hp[rrs] <= 0 || hp[rrm] <= 0)goto ERTYU;
        if (bei[rrs] == true)rrm = rrs, bei[rrs] = false, jcmh = 1;
        if (rand() % 100 <= meig[rrs]) {
            meiq(rrs, rrm);
        } else if (huog[rrs] > rand() % 100) {
            huoq(rrs, rrm);
        } else if (weny[rrs] > rand() % 100) {
            wenyi(rrs, rrm);
        } else {
            sha(rrs, rrm);
        }
        if (rand() % 10 == 8) {
            fj = 1;
        }
        int mp = 0, x = 0;
FJ:
        for (int m = 1; m <= names; m++) {
            if (hp[m] <= 0 && si[m] == false) {
                cout << "\t" << name[m];
                color(4);
                cout << " 死亡";
                color(7);
                cout << "了" << endl;
                si[m] = true;
            }
        }
        color(14);
        for (int k = 1; k <= names; k++) {
            if (hp[k] > 0)x++, mp = k;
        }
        if (x == 1) {
            cout << name[mp] << " win!" << endl;
            color(7);
            return 0;
        }
        color(7);
        if (fj == 1 && hp[rrm] > 0) {
            cout << "\t" << name[rrm] << " 反击 o(`ω′*)o,";
            sha(rrm, rrs, 1);
            fj = 0;
            goto FJ;
        }
        if (jcmh == 1 && hp[rrm] > 0) {
            cout << "\t" << name[rrm] << " 解除了 ";
            color(13);
            cout << "魅惑 " << endl;
            color(7);
        }
        Sleep(200);
    }
    return 0;
}
原文