没有压缩 缩减的核心部分代码
1 if(a[20][20]=='-')
2 {
3 a[20][20]='O';
4 return;
5 }
6 bool done(0);
7 int t_i,t_j;//存储临时值
8 // protect
9 int x,y,x1,y1;//记录连珠数
10 bool need[4];
11 for(i=1;i<=40;i++)
12 {
13 for(j=1;j<=40;j++)
14 {
15 x=y=x1=y1=0;
16 if(a[i][j]=='-')
17 {
18 memset(need,1,sizeof(need));
19 // xx
20 t_i=i,t_j=j;
21 while(t_i * t_j >0 && a[t_i-1][t_j-1]=='X'){
22 x1++;
23 t_i--,t_j--;
24 }
25 if(a[t_i-1][t_j-1]=='O' || (t_i-1) * (t_j-1)==0)
26 {
27 need[0]=0;
28 }
29 t_i=i,t_j=j;
30 while(t_i <= 40 && t_j <= 40 && a[t_i+1][t_j+1]=='X'){
31 x1++,
32 t_i++,t_j++;
33 }
34 if(a[t_i+1][t_j+1]=='O' || t_i+1>40 || t_j+1>40)
35 {
36 need[0]=0;
37 }
38 //yy
39 t_i=i,t_j=j;
40 while(t_i >0 && t_j <= 40 && a[t_i-1][t_j+1]=='X'){
41 y1++;
42 t_i--,t_j++;
43 }
44 if(a[t_i-1][t_j+1]=='O' || t_i-1<1 || t_j+1>40)
45 {
46 need[1]=0;
47 }
48 t_i=i,t_j=j;
49 while(t_i <= 40 && t_j > 0 && a[t_i+1][t_j-1]=='X'){
50 y1++;
51 t_i++,t_j--;
52 }
53 if(a[t_i+1][t_j-1]=='O' || t_i+1 > 40 || t_j-1 < 1)
54 {
55 need[1]=0;
56 }
57 //x
58 t_i=i,t_j=j;
59 while(t_j > 0 && a[t_i][t_j-1]=='X'){
60 x++;
61 t_j--;
62 }
63 if(a[t_i][t_j-1]=='O' || t_j-1 < 1)
64 {
65 need[2]=0;
66 }
67 t_i=i,t_j=j;
68 while(t_j <= 40 && a[t_i][t_j+1]=='X'){
69 x++;
70 t_j++;
71 }
72 if(a[t_i][t_j+1]=='O' || t_j+1 > 40)
73 {
74 need[2]=0;
75 }
76 //y
77 t_i=i,t_j=j;
78 while(t_i > 0 && a[t_i-1][t_j]=='X'){
79 y++;
80 t_i--;
81 }
82 if(a[t_i-1][t_j]=='O' || t_i-1 < 1)
83 {
84 need[3]=0;
85 }
86 t_i=i,t_j=j;
87 while(t_i <= 40 && a[t_i+1][t_j]=='X'){
88 y++;
89 t_i++;
90 }
91 if(a[t_i+1][t_j]=='O' || t_i+1 > 40)
92 {
93 need[3]=0;
94 }
95 //交叉
96 int t=0;
97 if(x1==2 || (x1==3 && !need[0]))t++;
98 if(y1==2 || (y1==3 && !need[1]))t++;
99 if(x==2 || (x==3 && !need[2]))t++;
100 if(y==2 || (y==3 && !need[3]))t++;
101 //防守 落子
102 if((x>=3 && need[2]) || (y>=3 && need[3]) || (x1>=3 && need[0])
103 || (y1>=3 && need[1]) || x>3 || y>3 || x1>3 || y1>3 || t>=2)
104 {
105 a[i][j]='O';
106 char t;
107 if(j<=20)t='a'+j-1;
108 else t='A'+j-21;
109 cout<<"置棋:"<<i<<" "<<t<<endl;
110 done=1;
111 break;
112 }
113 }
114 }
115 if(done)break;
116 }
117
118 if(!done)
119 {
120 //atack
121 for(i=1;i<=40;i++)
122 {
123 for(j=1;j<=40;j++)
124 {
125
126 x=y=x1=y1=0;
127 if(a[i][j]=='-')
128 {
129 memset(need,1,sizeof(need));
130 // xx
131 t_i=i,t_j=j;
132 while(t_i * t_j >0 && a[t_i-1][t_j-1]=='O'){
133 x1++;
134 t_i--,t_j--;
135 }
136 if(a[t_i-1][t_j-1]=='X' || (t_i-1) * (t_j-1)==0)
137 {
138 need[0]=0;
139 }
140 t_i=i,t_j=j;
141 while(t_i <= 40 && t_j <= 40 && a[t_i+1][t_j+1]=='O'){
142 x1++,
143 t_i++,t_j++;
144 }
145 if(a[t_i+1][t_j+1]=='X' || t_i+1>40 || t_j+1>40)
146 {
147 need[0]=0;
148 }
149 //yy
150 t_i=i,t_j=j;
151 while(t_i >0 && t_j <= 40 && a[t_i-1][t_j+1]=='O'){
152 y1++;
153 t_i--,t_j++;
154 }
155 if(a[t_i-1][t_j+1]=='X' || t_i-1<1 || t_j+1>40)
156 {
157 need[1]=0;
158 }
159 t_i=i,t_j=j;
160 while(t_i <= 40 && t_j > 0 && a[t_i+1][t_j-1]=='O'){
161 y1++;
162 t_i++,t_j--;
163 }
164 if(a[t_i+1][t_j-1]=='X' || t_i+1 > 40 || t_j-1 < 1)
165 {
166 need[1]=0;
167 }
168 //x
169 t_i=i,t_j=j;
170 while(t_j > 0 && a[t_i][t_j-1]=='O'){
171 x++;
172 t_j--;
173 }
174 if(a[t_i][t_j-1]=='X' || t_j-1 < 1)
175 {
176 need[2]=0;
177 }
178 t_i=i,t_j=j;
179 while(t_j <= 40 && a[t_i][t_j+1]=='O'){
180 x++;
181 t_j++;
182 }
183 if(a[t_i][t_j+1]=='X' || t_j+1 > 40)
184 {
185 need[2]=0;
186 }
187 //y
188 t_i=i,t_j=j;
189 while(t_i > 0 && a[t_i-1][t_j]=='O'){
190 y++;
191 t_i--;
192 }
193 if(a[t_i-1][t_j]=='X' || t_i-1 < 1)
194 {
195 need[3]=0;
196 }
197 t_i=i,t_j=j;
198 while(t_i <= 40 && a[t_i+1][t_j]=='O'){
199 y++;
200 t_i++;
201 }
202 if(a[t_i+1][t_j]=='X' || t_i+1 > 40)
203 {
204 need[3]=0;
205 }
206 //交叉
207 int t=0;
208 if(x1==2 || (x1==3 && !need[0]))t++;
209 if(y1==2 || (y1==3 && !need[1]))t++;
210 if(x==2 || (x==3 && !need[2]))t++;
211 if(y==2 || (y==3 && !need[3]))t++;
212
213 if((x>=2 && need[2]) || (y>=2 && need[3]) || (x1>=2 && need[0])
214 || (y1>=2 && need[1]) || x>2 || y>2 || x1>2 || y1>2 || t>=2)
215 {
216 a[i][j]='O';
217 char t;
218 if(j<=20)t='a'+j-1;
219 else t='A'+j-21;
220 cout<<"置棋:"<<i<<" "<<t<<endl;
221 done=1;
222 break;
223 }
224 if(done)break;
225 }
226 }
227 }
228 while(!done){
229 {
230 memset(b,0,sizeof(b));
231 int max(0);
232 int _i,_j;
233 for(int i=1;i<=40;i++)
234 {
235 for(int j=1;j<=40;j++)
236 {
237 if(a[i][j]=='-'){
238 if(a[i+1][j]=='O')b[i][j]++;
239 else if(a[i-1][j]=='O')b[i][j]++;
240 else if(a[i][j-1]=='O')b[i][j]++;
241 else if(a[i][j+1]=='O')b[i][j]++;
242 else if(a[i-1][j-1]=='O')b[i][j]++;
243 else if(a[i+1][j+1]=='O')b[i][j]++;
244 else if(a[i-1][j+1]=='O')b[i][j]++;
245 else if(a[i+1][j-1]=='O')b[i][j]++;
246 }
247 if(b[i][j]>max)
248 {
249 max=b[i][j];
250 _i=i,_j=j;
251 }
252 }
253 }
254 a[_i][_j]='O';
255 done=1;
256 }
257 }
258 }