413 lines
14 KiB
C++
413 lines
14 KiB
C++
#include <bits/stdc++.h>
|
||
|
||
using namespace std;
|
||
#define MAXN 10
|
||
|
||
//哪个位置可以放本方的棋子
|
||
int legal[MAXN][MAXN];
|
||
|
||
//操作者
|
||
char chess;
|
||
|
||
//棋盘
|
||
char chessboard[MAXN][MAXN]; //开大一点
|
||
|
||
//获取对手
|
||
//如果现在操作者是白,就获取到黑;如果现在操作者是黑,就获取到白。
|
||
char getOpponent() {
|
||
return chess == 'B' ? 'W' : 'B';
|
||
}
|
||
|
||
/**
|
||
* 功能:标识是不是有可以放棋子的可能?如果有的话,在哪个位置上?
|
||
* 返回: ok-->是不是有放棋子的可能
|
||
* 返回: legal-->哪个位置可以放棋子
|
||
* @return
|
||
*/
|
||
bool check() {
|
||
//初始化可以放置棋子的位置
|
||
memset(legal, 0, sizeof(legal));
|
||
//获取到对手的是黑棋还是白棋
|
||
char other = getOpponent();
|
||
//是不是存在可以放置的位置
|
||
bool ok = false;
|
||
|
||
int p, q;
|
||
//搜索当前棋子所在的位置
|
||
for (int i = 0; i < MAXN; i++)
|
||
for (int j = 0; j < MAXN; j++)
|
||
//找到每一个自己的棋子
|
||
if (chessboard[i][j] == chess) {
|
||
//左上有对手棋子
|
||
if (i - 1 >= 0 && j - 1 >= 0 && chessboard[i - 1][j - 1] == other) {
|
||
p = i - 1; //准备继续向前
|
||
q = j - 1; //准备继续向上
|
||
while (p >= 0 && q >= 0) {
|
||
//如果还有对手棋子
|
||
if (chessboard[p][q] == other) {
|
||
p--;
|
||
q--;
|
||
continue;
|
||
}
|
||
//直到发现自己的棋子
|
||
if (chessboard[p][q] == chess) break;
|
||
//如果发现空白
|
||
if (chessboard[p][q] == '-') {
|
||
legal[p][q] = 1;//标识为可以放置本方棋子
|
||
ok = true; //标识存在可以放置的可能
|
||
break; //退出本轮 while循环
|
||
}
|
||
}
|
||
}
|
||
//正上有对手棋子
|
||
if (i - 1 >= 0 && chessboard[i - 1][j] == other) {
|
||
p = i - 1;
|
||
q = j;
|
||
while (p >= 0) {
|
||
if (chessboard[p][q] == other) {
|
||
p--;
|
||
continue;
|
||
}
|
||
if (chessboard[p][q] == chess) break;
|
||
if (chessboard[p][q] == '-') {
|
||
legal[p][q] = 1;
|
||
ok = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
//右上有对手棋子
|
||
if (i - 1 >= 0 && j + 1 < 8 && chessboard[i - 1][j + 1] == other) {
|
||
p = i - 1;
|
||
q = j + 1;
|
||
while (p >= 0 && q < 8) {
|
||
if (chessboard[p][q] == other) {
|
||
p--;
|
||
q++;
|
||
continue;
|
||
}
|
||
if (chessboard[p][q] == chess) break;
|
||
if (chessboard[p][q] == '-') {
|
||
legal[p][q] = 1;
|
||
ok = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
//右有对手棋子
|
||
if (j + 1 < 8 && chessboard[i][j + 1] == other) {
|
||
p = i;
|
||
q = j + 1;
|
||
while (q < 8) {
|
||
if (chessboard[p][q] == other) {
|
||
q++;
|
||
continue;
|
||
}
|
||
if (chessboard[p][q] == chess) break;
|
||
if (chessboard[p][q] == '-') {
|
||
legal[p][q] = 1;
|
||
ok = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
//右下有对手棋子
|
||
if (i + 1 < 8 && j + 1 < 8 && chessboard[i + 1][j + 1] == other) {
|
||
p = i + 1;
|
||
q = j + 1;
|
||
while (p < 8 && q < 8) {
|
||
if (chessboard[p][q] == other) {
|
||
p++;
|
||
q++;
|
||
continue;
|
||
}
|
||
if (chessboard[p][q] == chess) break;
|
||
if (chessboard[p][q] == '-') {
|
||
legal[p][q] = 1;
|
||
ok = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
//下有对手棋子
|
||
if (i + 1 < 8 && chessboard[i + 1][j] == other) {
|
||
p = i + 1;
|
||
q = j;
|
||
while (p < 8) {
|
||
if (chessboard[p][q] == other) {
|
||
p++;
|
||
continue;
|
||
}
|
||
if (chessboard[p][q] == chess) break;
|
||
if (chessboard[p][q] == '-') {
|
||
legal[p][q] = 1;
|
||
ok = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
//左下有对手棋子
|
||
if (i + 1 < 8 && j - 1 >= 0 && chessboard[i + 1][j - 1] == other) {
|
||
p = i + 1;
|
||
q = j - 1;
|
||
while (p < 8 && q >= 0) {
|
||
if (chessboard[p][q] == other) {
|
||
p++;
|
||
q--;
|
||
continue;
|
||
}
|
||
if (chessboard[p][q] == chess) break;
|
||
if (chessboard[p][q] == '-') {
|
||
legal[p][q] = 1;
|
||
ok = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
//左有对手棋子
|
||
if (j - 1 >= 0 && chessboard[i][j - 1] == other) {
|
||
p = i;
|
||
q = j - 1;
|
||
while (q >= 0) {
|
||
if (chessboard[p][q] == other) {
|
||
q--;
|
||
continue;
|
||
}
|
||
if (chessboard[p][q] == chess) break;
|
||
if (chessboard[p][q] == '-') {
|
||
legal[p][q] = 1;
|
||
ok = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return ok;
|
||
}
|
||
|
||
/**
|
||
* 功能:修改棋盘上的每步结果
|
||
* @param x
|
||
* @param y
|
||
*/
|
||
void change_chess(int const x, int const y) {
|
||
int i, j, p, q;
|
||
char other = getOpponent();
|
||
if (x - 1 >= 0 && y - 1 >= 0 && chessboard[x - 1][y - 1] == other) { //左上
|
||
i = p = x - 1;
|
||
j = q = y - 1;
|
||
while (i >= 0 && j >= 0) {
|
||
if (chessboard[i][j] == other) {
|
||
i--;
|
||
j--;
|
||
continue;
|
||
}
|
||
if (chessboard[i][j] == '-') break;
|
||
if (chessboard[i][j] == chess) {
|
||
while (p != i && q != j)
|
||
chessboard[p--][q--] = chess;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
if (x - 1 >= 0 && chessboard[x - 1][y] == other) { //上
|
||
i = p = x - 1;
|
||
j = q = y;
|
||
while (i >= 0) {
|
||
if (chessboard[i][j] == other) {
|
||
i--;
|
||
continue;
|
||
}
|
||
if (chessboard[i][j] == '-') break;
|
||
if (chessboard[i][j] == chess) {
|
||
while (p != i)
|
||
chessboard[p--][q] = chess;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
if (x - 1 >= 0 && y + 1 < 8 && chessboard[x - 1][y + 1] == other) { //右上
|
||
i = p = x - 1;
|
||
j = q = y + 1;
|
||
while (i >= 0 && j < 8) {
|
||
if (chessboard[i][j] == other) {
|
||
i--;
|
||
j++;
|
||
continue;
|
||
}
|
||
if (chessboard[i][j] == '-') break;
|
||
if (chessboard[i][j] == chess) {
|
||
while (p != i && q != j)
|
||
chessboard[p--][q++] = chess;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
if (y + 1 < 8 && chessboard[x][y + 1] == other) { //右
|
||
i = p = x;
|
||
j = q = y + 1;
|
||
while (j < 8) {
|
||
if (chessboard[i][j] == other) {
|
||
j++;
|
||
continue;
|
||
}
|
||
if (chessboard[i][j] == '-') break;
|
||
if (chessboard[i][j] == chess) {
|
||
while (q != j)
|
||
chessboard[p][q++] = chess;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
if (x + 1 < 8 && y + 1 < 8 && chessboard[x + 1][y + 1] == other) { //右下
|
||
i = p = x + 1;
|
||
j = q = y + 1;
|
||
while (i < 8 && j < 8) {
|
||
if (chessboard[i][j] == other) {
|
||
i++;
|
||
j++;
|
||
continue;
|
||
}
|
||
if (chessboard[i][j] == '-') break;
|
||
if (chessboard[i][j] == chess) {
|
||
while (p != i && q != j)
|
||
chessboard[p++][q++] = chess;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
if (x + 1 < 8 && chessboard[x + 1][y] == other) { //下
|
||
i = p = x + 1;
|
||
j = q = y;
|
||
while (i < 8) {
|
||
if (chessboard[i][j] == other) {
|
||
i++;
|
||
continue;
|
||
}
|
||
if (chessboard[i][j] == '-') break;
|
||
if (chessboard[i][j] == chess) {
|
||
while (p != i)
|
||
chessboard[p++][q] = chess;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
if (x + 1 < 8 && y - 1 >= 0 && chessboard[x + 1][y - 1] == other) { //左下
|
||
i = p = x + 1;
|
||
j = q = y - 1;
|
||
while (i < 8 && j >= 0) {
|
||
if (chessboard[i][j] == other) {
|
||
i++;
|
||
j--;
|
||
continue;
|
||
}
|
||
if (chessboard[i][j] == '-') break;
|
||
if (chessboard[i][j] == chess) {
|
||
while (p != i && q != j)
|
||
chessboard[p++][q--] = chess;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
if (y - 1 >= 0 && chessboard[x][y - 1] == other) { //左
|
||
i = p = x;
|
||
j = q = y - 1;
|
||
while (j >= 0) {
|
||
if (chessboard[i][j] == other) {
|
||
j--;
|
||
continue;
|
||
}
|
||
if (chessboard[i][j] == '-') break;
|
||
if (chessboard[i][j] == chess) {
|
||
while (q != j)
|
||
chessboard[p][q--] = chess;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
int B = 0, W = 0;
|
||
for (int i = 0; i < 8; i++)
|
||
for (int j = 0; j < 8; j++)
|
||
if (chessboard[i][j] == 'B') B++;
|
||
else if (chessboard[i][j] == 'W') W++;
|
||
printf("Black - %2d White - %2d\n", B, W);
|
||
}
|
||
|
||
/**
|
||
* 功能:打印棋盘
|
||
*/
|
||
void print_chessboard() {
|
||
for (int i = 0; i < 8; i++)
|
||
printf("%s\n", chessboard[i]);
|
||
return;
|
||
}
|
||
|
||
int main() {
|
||
int t, T = 0;
|
||
//几次?
|
||
scanf("%d", &t);
|
||
getchar(); //为了吃掉回车,C语言这点真是~ https://zhidao.baidu.com/question/147733301.html
|
||
|
||
while (t--) {
|
||
//第一次不显示换行,其它次显示换行
|
||
T ? printf("\n") : T++;
|
||
//初始化棋盘
|
||
memset(chessboard, 0, sizeof(chessboard));
|
||
|
||
//按行进行读取字符串
|
||
for (int i = 0; i < 8; i++)
|
||
//gets()函数用来从标准输入设备(键盘)读取字符串直到换行符结束,但换行符会被丢弃,然后在末尾添加'\0'字符。
|
||
gets(chessboard[i]);
|
||
|
||
//下一个操作的游戏者,W-->白,B-->黑
|
||
scanf("%c", &chess);
|
||
getchar();
|
||
|
||
while (1) //一直等待输入,直到Q结束
|
||
{
|
||
//检查是不是有位置可以放置本方棋子
|
||
check();
|
||
//读入操作命令
|
||
char cmd[MAXN];
|
||
scanf("%s", cmd);
|
||
getchar();
|
||
//Q指令退出游戏,并打印当前棋盘(格式同输入)。
|
||
if (cmd[0] == 'Q') break;
|
||
|
||
//L指令打印所有合法操作,按照从上到下,从左到右的顺序排列(没有合法操作时输出No legal move)。
|
||
if (cmd[0] == 'L') {
|
||
bool ok = false;
|
||
//两轮循环,看看哪个位置有可以放置本方棋子的位置
|
||
for (int i = 0; i < 8; i++) {
|
||
for (int j = 0; j < 8; j++) {
|
||
if (legal[i][j]) {
|
||
if (ok) printf(" "); //万恶的格式
|
||
printf("(%d,%d)", i + 1, j + 1);
|
||
ok = true;
|
||
}
|
||
}
|
||
}
|
||
if (!ok) printf("No legal move.");
|
||
printf("\n");
|
||
}
|
||
//Mrc指令放一枚棋子在(r,c)。如果当前游戏者没有合法操作,则是先切换游戏者再操作。
|
||
//输入保证这个操作是合法的。输出操作完毕后黑白方的棋子总数。
|
||
if (cmd[0] == 'M') {
|
||
int x = cmd[1] - '0' - 1; //放入一个棋子
|
||
int y = cmd[2] - '0' - 1;
|
||
//没有地方可以放,让对方下棋
|
||
if (!check()) chess = getOpponent();
|
||
//这个位置放入这个棋子
|
||
chessboard[x][y] = chess;
|
||
//尝试吃掉对方的棋子
|
||
change_chess(x, y);
|
||
//修改为下一个棋手
|
||
chess = getOpponent();
|
||
}
|
||
}
|
||
//打印棋盘
|
||
print_chessboard();
|
||
}
|
||
return 0;
|
||
}
|