做完这道加上前面两道,这一章果然都是调用函数的。。。
自己写一堆函数来搞
写的过程中甚至觉得自己可以考虑写一个黑白棋的游戏了233
主要还是一个模拟
其实做了象棋那题的话会发现和象棋那题差不多,也是一堆函数堆出来的
象棋那题有一个思路就是需要对不同棋子也不同的函数
通过三个函数分别检测某一行,某一列,某一斜行是否合法
然后通过一个函数来检查8次该位置是否合法
再来几个函数用来更新棋盘、打印棋子个数、打印棋盘
题目(vjudge):https://vjudge.net/problem/UVA-220
代码:
//Decision's template
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<set>
using namespace std;
#define DP_maxn 16
#define maxn 1000000+10
#define INF 1000000007
#define mod 1000000007
#define mst(s,k) memset(s,k,sizeof(s))
typedef long long ll;
struct Edge{
int from,to,dist;
Edge(int u,int v,int d):from(u),to(v),dist(d){}
};
/*-------------------------------template End--------------------------------*/
int _map[9][9],now;
char command[10]; //'W' = 0,'B' = 1
char line[10],c;
int n,m;
void map_init(int row){
for(int i = 0;i<8;i++){
if(line[i]=='W') _map[row][i+1] = 0;
else if(line[i] == 'B') _map[row][i+1] = 1;
}
}
bool on_col(int x,int y,int dic){ //x行,y列
int flag = 0;
for(int i = x+dic;i<=8&&i>=1;i+=dic){
if(_map[i][y]==!now){
if(!flag) flag = 1;
continue;
}
else if(_map[i][y] == now&&flag) return true;
else if(_map[i][y] == now&&!flag) return false;
else if(_map[i][y] == -1) return false;
}
return false;
}
bool on_row(int x,int y,int dic){
int flag = 0;
for(int i = y+dic;i<=8&&i>=1;i+=dic){
if(_map[x][i] == !now){
if(!flag) flag = 1;
continue;
}
else if(_map[x][i] == now&&flag) return true;
else if(_map[x][i] == now&&!flag) return false;
else if(_map[x][i] == -1) return false;
}
return false;
}
bool on_op(int x,int y,int dic_x,int dic_y){
int flag = 0;
for(int i = x+dic_x,j = y+dic_y;i<=8&&i>=1&&j<=8&&j>=1;i+=dic_x,j+=dic_y)
{
if(_map[i][j] == !now){
if(!flag) flag = 1;
continue;
}
else if(_map[i][j] == now&&flag) return true;
else if(_map[i][j] == now&&!flag) return false;
else if(_map[i][j] == -1) return false;
}
return false;
}
bool is_legal(int x,int y){
bool flag;
flag = (on_col(x,y,1)||on_col(x,y,-1));
flag = flag||(on_row(x,y,1)||on_row(x,y,-1));
flag = flag||(on_op(x,y,1,1)||on_op(x,y,-1,1)||on_op(x,y,1,-1)||on_op(x,y,-1,-1));
return flag;
}
void print_board()
{
int num_b = 0,num_w = 0;
for(int i = 1;i<=8;i++){
for(int j = 1;j<=8;j++){
if(_map[i][j] == 0) num_w++;
else if(_map[i][j] == 1) num_b++;
}
}
cout<<"Black -";
if(num_b/10!=0) cout<<" "; else cout<<" ";
cout<<num_b<<" White -";
if(num_w/10!=0) cout<<" "; else cout<<" ";
cout<<num_w<<endl;
}
void change_map(int x,int y){
if(on_col(x,y,1)) for(int i = x+1;i<=8&&i>=1;i++) if(_map[i][y] == !now) _map[i][y] = now; else if(_map[i][y] == now) break;
if(on_col(x,y,-1)) for(int i = x-1;i<=8&&i>=1;i--) if(_map[i][y] == !now) _map[i][y] = now; else if(_map[i][y] == now) break;
if(on_row(x,y,1)) for(int i = y+1;i<=8&&i>=1;i++) if(_map[x][i] == !now) _map[x][i] = now; else if(_map[x][i] == now) break;
if(on_row(x,y,-1)) for(int i = y-1;i<=8&&i>=1;i--) if(_map[x][i] == !now) _map[x][i] = now; else if(_map[x][i] == now) break;
if(on_op(x,y,1,1)) for(int i = x+1,j = y+1;i<=8&&i>=1&&j<=8&&j>=1;i++,j++) if(_map[i][j] == !now) _map[i][j] = now; else if(_map[i][j] == now) break;
if(on_op(x,y,1,-1)) for(int i = x+1,j = y-1;i<=8&&i>=1&&j<=8&&j>=1;i++,j--) if(_map[i][j] == !now) _map[i][j] = now; else if(_map[i][j] == now) break;
if(on_op(x,y,-1,1)) for(int i = x-1,j = y+1;i<=8&&i>=1&&j<=8&&j>=1;i--,j++) if(_map[i][j] == !now) _map[i][j] = now; else if(_map[i][j] == now) break;
if(on_op(x,y,-1,-1)) for(int i = x-1,j = y-1;i<=8&&i>=1&&j<=8&&j>=1;i--,j--) if(_map[i][j] == !now) _map[i][j] = now; else if(_map[i][j] == now) break;
}
void print_map()
{
for(int i = 1;i<=8;i++){
for(int j =1;j<=8;j++){
if(_map[i][j] == -1) cout<<"-";
else if(_map[i][j] == 0) cout<<'W';
else if(_map[i][j] == 1) cout<<'B';
}
cout<<endl;
}
}
int main(){
cin>>n;
while(cin>>line){
m++;
int flag = 0;
mst(_map,-1);
map_init(1);
for(int i = 2;i<=8;i++) {cin>>line; map_init(i);}
cin>>c;
if(c == 'W') now = 0; else now = 1; //'W' = 0 'B' = 1
while(cin>>command&&command[0]){
flag = 0;
if(command[0]=='L'){
for(int i = 1;i<=8;i++){
for(int j = 1;j<=8;j++){
if(_map[i][j] != -1) continue;
if(is_legal(i,j)){
if(flag == 0) flag = 1;
else cout<<" ";
cout<<"("<<i<<","<<j<<")";
}
}
}
if(flag == 0) cout<<"No legal move.";
cout<<endl;
}
else if(command[0]=='M'){
int row = command[1] - '0';
int col = command[2] - '0';
if(is_legal(row,col)){
_map[row][col] = now;
change_map(row,col);
now = !now;
print_board();
}
else{
now = !now;
_map[row][col] = now;
change_map(row,col);
now = !now;
print_board();
}
}
else if(command[0] == 'Q'){
print_map();
if(n!=m)cout<<endl;
break;
}
}
}
return 0;
}