太长了哇靠很恐怖 主要刚开始想的时候没有优化代码量。
总结一下:该题目要求我们去用代码来模拟俩个下棋的人,棋的规则相当于五子棋差不多反正就是喂起来了就吃光,给你一个已经有棋子的棋盘,现在叫你以某一方起手,看一看下到哪里才可以100%将对方棋子围起来吃掉,然后输出这些位置,如果没有呢那就换一个,还有一中操作是,在这些满足条件的点上选一个位置下之后呢将所有被吃掉的棋子换成妮子的颜色的棋子,写到这个地方就知道这个题目代码两会很大。
我先写了一个判断操作的函数,是输出位置还是下棋,然后分别写了俩个函数用以输出位置和变换棋子颜色具体看看下面注解吧,我这个输出位值的代码完全还可以优化~~很蠢的写了8个方向 8哥啊
~~
注意的点:我输出点的函数是这么想的,我是以你操作的颜色棋子的另一个颜色入手,在这些点的8个方向各自看看有没有我的颜色的棋子在上面如果有的话,那么在他的相反的方向放一个棋子就可以围起来了*.*。
// 黑白棋.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include<string>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 10;
char graph[maxn][maxn];
bool is[maxn][maxn];
int CaseNumber;
int chan[maxn][maxn];
char first[3] = { '0','W','B' };
void J(int x, int y, char mode)//用以判断8个方向是否有合适的点的函数
{
if (graph[x - 1][y] == '-')//上方
{
for (int i = x + 1;i <= 8;i++)
{
if (graph[i][y] == mode)
{
is[x - 1][y] = true;
}
if (graph[i][y] == '-')
{
break;
}
}
}
if (graph[x + 1][y] == '-')//下方
{
for (int i = x - 1;i >= 1;i--)
{
if (graph[i][y] == mode)
{
is[x + 1][y] = true;
}
if (graph[i][y] == '-')
{
break;
}
}
}
if (graph[x][y - 1] == '-')//左方
{
for (int i = y + 1;i <= 8;i++)
{
if (graph[x][i] == mode)
{
is[x][y - 1] = true;
}
if (graph[x][i] == '-')
{
break;
}
}
}
if (graph[x][y + 1] == '-')//右方
{
for (int i = y - 1;i >= 1;i--)
{
if (graph[x][i] == mode)
{
is[x][y + 1] = true;
}
if (graph[x][i] == '-')
{
break;
}
}
}
if (graph[x - 1][y - 1] == '-')//下面自己揣测方向
{
for (int i = x + 1, j = y + 1;i <= 8 && j <= 8;i++, j++)
{
if (graph[i][j] == mode)
{
is[x - 1][y - 1] = true;
}
if (graph[i][j] == '-')
{
break;
}
}
}
if (graph[x + 1][y + 1] == '-')
{
for (int i = x - 1, j = y - 1;i >= 1 && j >= 1;i--, j--)
{
if (graph[i][j] == mode)
{
is[x + 1][y + 1] = true;
}
if (graph[i][j] == '-')
{
break;
}
}
}
if (graph[x + 1][y - 1] == '-')
{
for (int i = x - 1, j = y + 1;i >= 1 && j <= 8;i--, j++)
{
if (graph[i][j] == mode)
{
is[x + 1][y - 1] = true;
}
if (graph[i][j] == '-')
{
break;
}
}
}
if (graph[x - 1][y + 1] == '-')
{
for (int i = x + 1,j = y - 1;i <= 8 && j >= 1;i++, j--)
{
if (graph[i][j] == mode)
{
is[x - 1][y + 1] = true;
}
if (graph[i][j] == '-')
{
break;
}
}
}
}
void judge(char mode)//判断是找哪个颜色操作函数
{
if (mode == 'W')
{
for (int i = 1;i <= 8;i++)
{
for (int j = 1;j <= 8;j++)
{
if (graph[i][j] == 'B')//如果你操作W那么就找B
{
J(i, j, mode);
}
}
}
/*for (int i = 1;i <= 8;i++)
{
for (int j = 1;j <= 8;j++)
{
cout << is[i][j]<<" ";
}
cout << endl;
}*/
}
if (mode == 'B')
{
for (int i = 1;i <= 8;i++)
{
for (int j = 1;j <= 8;j++)
{
if (graph[i][j] == 'W')
{
J(i, j, mode);
}
}
}
}
}
void change(int x, int y, int cnt,int dx,int dy)//这是改变被围棋子颜色的函数当时才想到的方向函数是可以优化的,很蠢。
{//对了关于这个函数改变函数的话就是看看这个点上的8个方向是否有与其相反颜色的点,有的话将其标称怀疑点 也就是chan[x][y]=1,因为有可能他围不起来。
memset(chan, 0, sizeof(chan));
for (int x1=x+dx,y1=y+dy;x1>=1&&x1<=8&&y1>=1&&y1<=8;x1=x1+dx,y1=y1+dy)
{
if (graph[x1][y1] == first[cnt] && (x1==x+dx&&y1==y+dy))//如果刚刚开始就遇到了与自己相同颜色的棋子那肯定不行
{
break;
}
if (graph[x1][y1] == first[cnt])//如果后面遇到了那就没问题了,所有怀疑点全部变为可行点
{
for (int i = 1;i <= 8;i++)
{
for (int j = 1;j <= 8;j++)
{
if (chan[i][j] == 1)
{
chan[i][j] = 2;
}
}
}
}
if (graph[x1][y1] == first[(cnt + 1 + 2 - 1) % 2 + 1])//(cnt+1+2-2)%2+1是让cnt以步长为1的距离在1-2内进行循环,让颜色改变,看看相反颜色的棋子有几个
{
chan[x1][y1] = 1;
}
else if (graph[x1][y1] == '-')//遇到没旗子的点也不行直接退出
{
break;
}
}
for (int i = 1;i <= 8;i++)
{
for (int j = 1;j <= 8;j++)
{
if (chan[i][j] == 2)
{
graph[i][j] = first[cnt];
}
}
}
}
int black;
int white;
void print()
{
black = 0;
white = 0;
for (int i = 1;i <= 8;i++)
{
for (int j = 1;j <= 8;j++)
{
if (graph[i][j] == 'B')
black++;
if (graph[i][j] == 'W')
white++;
}
}
}
int main()
{
cin >> CaseNumber;int first1 = 1;
while (CaseNumber--)
{
if (first1)first1 = 0;
else { cout << endl; }
memset(graph, '-', sizeof(graph));
for (int i = 1;i <= 8;i++)
{
for (int j = 1;j <= 8;j++)
{
cin >> graph[i][j];
}
}
/*for (int i = 1;i <= 8;i++)
{
for (int j = 1;j <= 8;j++)
{
cout << graph[i][j];
}
cout << endl;
}*/
string temp;char priority;
cin >> priority;int cnt = priority == 'W' ? 1 : 2;
while (cin >> temp&&temp[0]!='Q')
{
if (temp[0] == 'L')
{
memset(is, false, sizeof(is));
judge(first[cnt]);
int cnt1 = 0;int flag = 0;
for (int i = 1;i <= 8;i++)
{
for (int j = 1;j <= 8;j++)
{
if (is[i][j] == true)
{
flag++;
if (cnt1)cout << " ";
printf("(%d,%d)", i, j);
cnt1++;
}
}
}
if (!flag)
cout << "No legal move." << endl;
if (flag)
{
cout << endl;
}
}
if (temp[0] == 'M')
{
memset(is, false, sizeof(is));
int x = temp[1] - '0';
int y = temp[2] - '0';
judge(first[cnt]);
/*for (int i = 1;i <= 8;i++)
{
for (int j = 1;j <= 8;j++)
{
cout << is[i][j]<<" ";
}
cout << endl;
}*/
if (is[x][y] != true)
{
memset(is, false, sizeof(is));cnt = (cnt + 1 + 2 - 1) % 2 + 1;
judge(first[cnt]);
graph[x][y] = first[cnt];
change(x, y, cnt, 1, 0);
change(x, y, cnt, -1, 0);
change(x, y, cnt, 0, 1);
change(x, y, cnt, 0, -1);
change(x, y, cnt, -1, -1);
change(x, y, cnt, 1, -1);
change(x, y, cnt, 1, 1);
change(x, y, cnt, -1, 1);
/*for (int i = 1;i <= 8;i++)
{
for (int j = 1;j <= 8;j++)
{
if (is[i][j] == true)
{
graph[i][j] = first[cnt];
}
}
}*/
print();
printf("Black - %2d White - %2d\n", black,white);
}
else
{
graph[x][y] = first[cnt];
change(x, y, cnt, 1, 0);
change(x, y, cnt, -1, 0);
change(x, y, cnt, 0, 1);
change(x, y, cnt, 0, -1);
change(x, y, cnt, -1, -1);
change(x, y, cnt, 1, -1);
change(x, y, cnt, 1, 1);
change(x, y, cnt, -1, 1);
print();
printf("Black - %2d White - %2d\n", black, white);
}
cnt = (cnt + 1 + 2 - 1) % 2 + 1;
}
}
for (int i = 1;i <= 8;i++)
{
for (int j = 1;j <= 8;j++)
{
cout << graph[i][j];
}
if (CaseNumber == 0 && i == 8)
{
break;
}
cout << endl;
}
}
cout << endl;
}