POJ2996Help Me with the Game

转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1299148268

 

提示:很烦很简单的国际象棋棋盘模拟,输入比较麻烦而已

 

输出时:

1、不论黑白,KQRBN P均是依次输出,强制大写,但不输出“P”,只输出其坐标

2、对白棋的位置,小行优先大行输出(行的数字越小则优先)同行则按列的顺序(a~h)

3、对黑棋的位置,大行优先小行输出(行的数字越大则优先)同行则按列的顺序(a~h)

4、从23点可以看出,黑棋总是先被输入,白棋总是后输入,即黑棋总在棋盘上方,白棋总在棋盘下方,所以输入完成后,对于黑色棋子只需要按类型次序输出,同类型棋子的顺序就是输入的顺序;而白色棋子要考虑同类型棋子之间的排序,而同类型棋子之间又仅需要考虑不同行棋子之间的排序,同一行棋子的排序就是输入顺序

5、棋子可能不齐全,不存在的棋子不输出,用标记解决

6、最后的棋子后面不带逗号,要找出最后的棋子

 

注意:坐标系的变换

 

 

  1 //Memory Time 
2 //236K 0MS
3
4
5 #include<iostream>
6 using namespace std;
7
8 class white_piece
9 {
10 public:
11 int row;
12 char col;
13 bool flag; //在class中bool型的默认值为false
14 }K,Q,R[3],B[3],N[3];
15
16 bool pawn[9]['i']={false}; //记录白色pawn的位置
17 int PR=0,PB=0,PN=0; //同类型棋子的指针
18
19 class black_piece
20 {
21 public:
22 int row;
23 char col;
24 bool flag;
25 }k,q,r[2],b[2],n[2],p[8];
26
27 int pr=0,pb=0,pn=0,pp=0;
28
29 char chess[9]['i']; // ASCII: 'i'>'I',use the Row 1 to 8,and Col 'a' to 'h'
30 int x,z;
31 char y;
32 int w_count=0; //白棋总数
33 int b_count=0; //黑棋总数
34
35 void judge(void)
36 {
37 if(chess[x][y]=='.' || chess[x][y]==':')
38 return;
39 else if(chess[x][y]=='k') //黑棋判断
40 {
41 k.row=9-x;
42 k.col=y;
43 k.flag=true;
44 b_count++;
45 return;
46 }
47 else if(chess[x][y]=='q')
48 {
49 q.row=9-x;
50 q.col=y;
51 q.flag=true;
52 b_count++;
53 return;
54 }
55 else if(chess[x][y]=='r')
56 {
57 r[pr].row=9-x;
58 r[pr++].col=y;
59 b_count++;
60 return;
61 }
62 else if(chess[x][y]=='b')
63 {
64 b[pb].row=9-x;
65 b[pb++].col=y;
66 b_count++;
67 return;
68 }
69 else if(chess[x][y]=='n')
70 {
71 n[pn].row=9-x;
72 n[pn++].col=y;
73 b_count++;
74 return;
75 }
76 else if(chess[x][y]=='p')
77 {
78 p[pp].row=9-x;
79 p[pp++].col=y;
80 b_count++;
81 return;
82 }
83 else if(chess[x][y]=='K') //白棋判断
84 {
85 K.row=9-x;
86 K.col=y;
87 K.flag=true;
88 w_count++;
89 return;
90 }
91 else if(chess[x][y]=='Q')
92 {
93 Q.row=9-x;
94 Q.col=y;
95 Q.flag=true;
96 w_count++;
97 return;
98 }
99 else if(chess[x][y]=='R')
100 {
101 R[PR].row=9-x;
102 R[PR++].col=y;
103 w_count++;
104 return;
105 }
106 else if(chess[x][y]=='B')
107 {
108 B[PB].row=9-x;
109 B[PB++].col=y;
110 w_count++;
111 return;
112 }
113 else if(chess[x][y]=='N')
114 {
115 N[PN].row=9-x;
116 N[PN++].col=y;
117 w_count++;
118 return;
119 }
120 else if(chess[x][y]=='P')
121 {
122 pawn[9-x][y]=true;
123 w_count++;
124 return;
125 }
126 }
127
128 void Print(void)
129 {
130 cout<<"White: ";
131 if(K.flag)
132 {
133 cout<<'K'<<K.col<<K.row;
134 if(--w_count>0)
135 cout<<',';
136 }
137 if(Q.flag)
138 {
139 cout<<'Q'<<Q.col<<Q.row;
140 if(--w_count>0)
141 cout<<',';
142 }
143
144 if(PR==2)
145 if(R[1].row<R[0].row)
146 {
147 R[2]=R[0];
148 R[0]=R[1];
149 R[1]=R[2];
150 }
151 for(x=0;x<PR;x++)
152 {
153 cout<<'R'<<R[x].col<<R[x].row;
154 if(--w_count>0)
155 cout<<',';
156 }
157
158 if(PB==2)
159 if(B[1].row<B[0].row)
160 {
161 B[2]=B[0];
162 B[0]=B[1];
163 B[1]=B[2];
164 }
165 for(x=0;x<PB;x++)
166 {
167 cout<<"B"<<B[x].col<<B[x].row;
168 if(--w_count>0)
169 cout<<',';
170 }
171
172 if(PN==2)
173 if(N[1].row<N[0].row)
174 {
175 N[2]=N[0];
176 N[0]=N[1];
177 N[1]=N[2];
178 }
179 for(x=0;x<PN;x++)
180 {
181 cout<<'N'<<N[x].col<<N[x].row;
182 if(--w_count>0)
183 cout<<',';
184 }
185
186 for(x=1;x<=8;x++)
187 for(y='a';y<='h';y++)
188 if(pawn[x][y])
189 {
190 cout<<y<<x;
191 if(--w_count>0)
192 cout<<',';
193 }
194
195 cout<<endl;
196
197 cout<<"Black: ";
198 if(k.flag)
199 {
200 cout<<'K'<<k.col<<k.row;
201 if(--b_count>0)
202 cout<<',';
203 }
204 if(q.flag)
205 {
206 cout<<'Q'<<q.col<<q.row;
207 if(--b_count>0)
208 cout<<',';
209 }
210 for(x=0;x<pr;x++)
211 {
212 cout<<'R'<<r[x].col<<r[x].row;
213 if(--b_count>0)
214 cout<<',';
215 }
216 for(x=0;x<pb;x++)
217 {
218 cout<<"B"<<b[x].col<<b[x].row;
219 if(--b_count>0)
220 cout<<',';
221 }
222 for(x=0;x<pn;x++)
223 {
224 cout<<'N'<<n[x].col<<n[x].row;
225 if(--b_count>0)
226 cout<<',';
227 }
228 for(x=0;x<pp;x++)
229 {
230 cout<<p[x].col<<p[x].row;
231 if(--b_count>0)
232 cout<<',';
233 }
234
235 cout<<endl;
236
237 return;
238 }
239
240 int main(void)
241 {
242 char temp;
243
244 /*Input*/
245
246 for(z=0;z<33;z++)
247 cin>>temp;
248
249 for(x=1;x<=8;x++)
250 {
251 cin>>temp;
252 for(y='a';y<='h';y++)
253 {
254 cin>>temp>>chess[x][y]>>temp>>temp;
255 judge();
256 }
257 for(z=0;z<33;z++)
258 cin>>temp;
259 }
260
261 /*Print*/
262
263 Print();
264
265 return 0;
266 }

原文地址:https://www.cnblogs.com/lyy289065406/p/2121387.html