Uva 220 Othello 黑白棋 (習題4-3)
阿新 • • 發佈:2018-12-19
做完這道加上前面兩道,這一章果然都是呼叫函式的。。。
自己寫一堆函式來搞
寫的過程中甚至覺得自己可以考慮寫一個黑白棋的遊戲了233
主要還是一個模擬
其實做了象棋那題的話會發現和象棋那題差不多,也是一堆函式堆出來的
象棋那題有一個思路就是需要對不同棋子也不同的函式
通過三個函式分別檢測某一行,某一列,某一斜行是否合法
然後通過一個函式來檢查8次該位置是否合法
再來幾個函式用來更新棋盤、列印棋子個數、列印棋盤
程式碼:
//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; }