在机房,被教练逼着写完大模拟后,肯定想玩游戏放松一下,发现题目还配送了游戏,可当你写完题目满心欢喜的点开链接以后,却发现需要百度网盘下载!!!在机房的你忘记了密码,无法登录百度网盘愉快的游戏,无奈之下,你就会写出鸭棋游戏版:
#include<bits/stdc++.h>
#include<windows.h>
#define sc(a, b) SetColorAndBackground(a, b)
#define qp system("CLS")
using namespace std;
bool is_died;
void SetColorAndBackground (int ForgC, int BackC) {
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
string name[20] = {"就是歌姬", "车", "马", "象", "士", "王", "兵", "鸭", "车", "马", "象", "士", "王", "兵", "鸭"};
// 0 1 2 3 4 5 6 7 (1至7红方,其余为蓝方)
const int move_cnt[] = {0, 0, 8, 4, 4, 4, 8, 8, 0, 8, 4, 4, 4, 8, 8};
const int dx[20][10] = {
{5, 4, 1, 8, 8},
{-114514},
{-1, -2, -2, -1, 1, 2, 2, 1},
{-2, -2, 2, 2},
{-1, -1, 1, 1},
{-1, 1, 0, 0},
{-1, -1, -1, 0, 1, 1, 1, 0},
{-2, -3, -3, -2, 2, 3, 3, 2},
{-114514},
{-1, -2, -2, -1, 1, 2, 2, 1},
{-2, -2, 2, 2},
{-1, -1, 1, 1},
{-1, 1, 0, 0},
{-1, -1, -1, 0, 1, 1, 1, 0},
{-2, -3, -3, -2, 2, 3, 3, 2},
};
const int dy[20][10] = {
{5, 4, 1, 8, 8},
{-114514},
{-2, -1, 1, 2, 2, 1, -1, -2},
{-2, 2, 2, -2},
{-1, 1, 1, -1},
{0, 0, -1, 1},
{-1, 0, 1, 1, 1, 0, -1, -1},
{-3, -2, 2, 3, 3, 2, -2, -3},
{-114514},
{-2, -1, 1, 2, 2, 1, -1, -2},
{-2, 2, 2, -2},
{-1, 1, 1, -1},
{0, 0, -1, 1},
{-1, 0, 1, 1, 1, 0, -1, -1},
{-3, -2, 2, 3, 3, 2, -2, -3},
};
int board[12][12] =
{
{1 , 2 , 3 , 4 , 5 , 4 , 3 , 2 , 1 },
{0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
{7 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 },
{6 , 0 , 6 , 0 , 6 , 0 , 6 , 0 , 6 },
{0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
{0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
{13, 0 , 13, 0 , 13, 0 , 13, 0 , 13},
{14, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 14},
{0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
{8 , 9 , 10, 11, 12, 11, 10, 9 , 8 },
};
inline bool camp_judge (int x, int y) {return x / 8 == y / 8;}
inline bool check () {
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 9; y++) {
if (!board[x][y]) continue;
int v = board[x][y];
if (v == 1 || v == 8) {
int nx = x, ny = y;
while (--nx >= 0) {
if (board[nx][ny]) {
if (camp_judge(v, board[nx][ny])) break;
if (name[board[nx][ny]] != "王") break;
return true;
}
}
nx = x;
while (++nx < 10) {
if (board[nx][ny]) {
if (camp_judge(v, board[nx][ny])) break;
if (name[board[nx][ny]] != "王") break;
return true;
}
}
nx = x;
while (--ny >= 0) {
if (board[nx][ny]) {
if (camp_judge(v, board[nx][ny])) break;
if (name[board[nx][ny]] != "王") break;
return true;
}
}
ny = y;
while (++ny < 9) {
if (board[nx][ny]) {
if (camp_judge(v, board[nx][ny])) break;
if (name[board[nx][ny]] != "王") break;
return true;
}
}
}
for (int k = 0; k < move_cnt[v]; k++) {
int nx = x + dx[v][k], ny = y + dy[v][k];
if (nx < 0 || nx > 9 || ny < 0 || ny > 8) continue;
if (!board[nx][ny]) continue;
if (camp_judge(v, board[nx][ny])) continue;
if (v % 7 == 3 && board[x + dx[4][k]][y + dy[4][k]]) continue;
if (v % 7 == 2) {
if ((k == 0 || k == 7) && board[x][y - 1]) continue;
if ((k == 1 || k == 2) && board[x - 1][y]) continue;
if ((k == 3 || k == 4) && board[x][y + 1]) continue;
if ((k == 5 || k == 6) && board[x + 1][y]) continue;
}
if (v % 7 == 0) {
if (board[x + dx[2][k]][y + dy[2][k]]) continue;
if ((k == 0 || k == 7) && board[x][y - 1]) continue;
if ((k == 1 || k == 2) && board[x - 1][y]) continue;
if ((k == 3 || k == 4) && board[x][y + 1]) continue;
if ((k == 5 || k == 6) && board[x + 1][y]) continue;
}
if (name[board[nx][ny]] == "王") return true;
}
}
}
return false;
}
void print () {
puts("___________________________________________________");
puts("| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |");
puts("|____|____|____|____|____|____|____|____|____|____|");
for (int i = 0; i <= 9; i++) {
printf("| %d |", i);
for (int j = 0; j <= 8; j++) {
sc(0, 15);
if (!board[i][j]) printf(" |");
else if (board[i][j] / 8) sc(1, 15), cout << " " << name[board[i][j]], sc(0, 15), cout << " |";
else sc(4, 15), cout << " " << name[board[i][j]], sc(0, 15), cout << " |";
}
puts("\n|____|____|____|____|____|____|____|____|____|____|");
}
}
int main () {
system("color F0");
int question_cnt, sx, sy, ex, ey, now = 0;
while (!is_died) {
qp, printf("last move : (%d, %d) -> (%d, %d)\n", sx, sy, ex, ey);
cout << "请 " << (now ? "蓝" : "红") << "方 行动\n";
print();
sc(0, 15);
printf("请移动棋子,格式:起点坐标 终点坐标(坐标先行后列)\n");
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
printf("action: (%d, %d) -> (%d, %d)\n", sx, sy, ex, ey);
if (is_died) {
printf("移动不合法!!!\n");
continue;
}
if (sx < 0 || sx > 9 || sy < 0 || sy > 8) {
printf("\n");
continue;
}
if (ex < 0 || ex > 9 || ey < 0 || ey > 8) {
printf("移动不合法!!!\n");
continue;
}
if (!board[sx][sy]) {
printf("移动不合法!!!\n");
continue;
}
if (board[ex][ey] && camp_judge(board[ex][ey], board[sx][sy])) {
printf("移动不合法!!!\n");
continue;
}
if (now != board[sx][sy] / 8) {
printf("移动不合法!!!\n");
continue;
}
int now_chess = board[sx][sy];
if (now_chess == 1 || now_chess == 8) {
if (sx != ex && sy != ey) {
printf("移动不合法!!!\n");
continue;
}
bool flag = true;
if (sx == ex) {
if (sy < ey) {
for (int i = sy + 1; i < ey; i++) {
if (board[sx][i]) {
flag = false;
break;
}
}
} else {
for (int i = ey + 1; i < sy; i++) {
if (board[sx][i]) {
flag = false;
break;
}
}
}
} else {
if (sx < ex) {
for (int i = sx + 1; i < ex; i++) {
if (board[i][sy]) {
flag = false;
break;
}
}
} else {
for (int i = ex + 1; i < sx; i++) {
if (board[i][sy]) {
flag = false;
break;
}
}
}
}
if (!flag) {
printf("移动不合法!!!\n");
continue;
}
}
bool flag = false;
for (int i = 0; i < move_cnt[now_chess]; i++) {
int nx = sx + dx[now_chess][i], ny = sy + dy[now_chess][i];
if (nx < 0 || nx > 9 || ny < 0 | ny > 8) continue;
if (nx == ex && ny == ey) {
if (now_chess % 7 == 3 && board[sx + dx[4][i]][sy + dy[4][i]]) break;
if (now_chess % 7 == 2) {
if ((i == 0 || i == 7) && board[sx][sy - 1]) break;
if ((i == 1 || i == 2) && board[sx - 1][sy]) break;
if ((i == 3 || i == 4) && board[sx][sy + 1]) break;
if ((i == 5 || i == 6) && board[sx + 1][sy]) break;
}
if (now_chess % 7 == 0) {
if (board[sx + dx[2][i]][sy + dy[2][i]]) break;
if ((i == 0 || i == 7) && board[sx][sy - 1]) break;
if ((i == 1 || i == 2) && board[sx - 1][sy]) break;
if ((i == 3 || i == 4) && board[sx][sy + 1]) break;
if ((i == 5 || i == 6) && board[sx + 1][sy]) break;
}
flag = true;
break;
}
}
if (!flag && now_chess != 1 && now_chess != 8) {
printf("移动不合法!!!\n");
continue;
}
cout << (now_chess / 8 ? "蓝" : "红") << ' ' << name[now_chess] << ' ';
if (!board[ex][ey]) cout << "移动成功!" << '\n';
else cout << "吃掉了 " << (board[ex][ey] / 8 ? "蓝" : "红") << ' ' << name[board[ex][ey]] << '\n';
Sleep(800);
if (board[ex][ey] == 5 || board[ex][ey] == 12) {
if (board[ex][ey] == 5) {
sc(1, 15);
printf("Blue Win!!!\n");
exit(0);
} else {
sc(4, 15);
printf("Red Win!!!\n");
exit(0);
}
is_died = true;
}
board[sx][sy] = 0, board[ex][ey] = now_chess;
if (!is_died && check()) printf("将军!!!\n"), system("pause");
now ^= 1;
}
return 0;
}
就当图一乐qwq