-
Notifications
You must be signed in to change notification settings - Fork 1
/
xiangqi.txt
2152 lines (1791 loc) · 59.2 KB
/
xiangqi.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class Chess{
public static void main(String args[]){
new ChessMainFrame("中国象棋:观棋不语真君子,棋死无悔大丈夫");
}
}
class ChessMainFrame extends JFrame implements ActionListener,MouseListener,Runnable{
//玩家
JLabel play[] = new JLabel[32];
//棋盘
JLabel image;
//窗格
Container con;
//工具栏
JToolBar jmain;
//重新开始
JButton anew;
//悔棋
JButton repent;
//退出
JButton exit;
//当前信息
JLabel text;
//保存当前操作
Vector Var;
//规则类对象(使于调用方法)
ChessRule rule;
/**
** 单击棋子
** chessManClick = true 闪烁棋子 并给线程响应
** chessManClick = false 吃棋子 停止闪烁 并给线程响应
*/
boolean chessManClick;
/**
** 控制玩家走棋
** chessPlayClick=1 黑棋走棋
** chessPlayClick=2 红棋走棋 默认红棋
** chessPlayClick=3 双方都不能走棋
*/
int chessPlayClick=2;
//控制棋子闪烁的线程
Thread tmain;
//把第一次的单击棋子给线程响应
static int Man,i;
ChessMainFrame(){
new ChessMainFrame("中国象棋");
}
/**
** 构造函数
** 初始化图形用户界面
*/
ChessMainFrame(String Title){
//获行客格引用
con = this.getContentPane();
con.setLayout(null);
//实例化规则类
rule = new ChessRule();
Var = new Vector();
//创建工具栏
jmain = new JToolBar();
text = new JLabel("欢迎使用象棋对弈系统");
//当鼠标放上显示信息
text.setToolTipText("信息提示");
anew = new JButton(" 新 游 戏 ");
anew.setToolTipText("重新开始新的一局");
exit = new JButton(" 退 出 ");
exit.setToolTipText("退出象棋程序程序");
repent = new JButton(" 悔 棋 ");
repent.setToolTipText("返回到上次走棋的位置");
//把组件添加到工具栏
jmain.setLayout(new GridLayout(0,4));
jmain.add(anew);
jmain.add(repent);
jmain.add(exit);
jmain.add(text);
jmain.setBounds(0,0,558,30);
con.add(jmain);
//添加棋子标签
drawChessMan();
//注册按扭监听
anew.addActionListener(this);
repent.addActionListener(this);
exit.addActionListener(this);
//注册棋子移动监听
for (int i=0;i<32;i++){
con.add(play[i]);
play[i].addMouseListener(this);
}
//添加棋盘标签
con.add(image = new JLabel(new ImageIcon("image\\Main.GIF")));
image.setBounds(0,30,558,620);
image.addMouseListener(this);
//注册窗体关闭监听
this.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
);
//窗体居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height){
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width){
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2 - 280 ,(screenSize.height - frameSize.height ) / 2 - 350);
//设置
this.setIconImage(new ImageIcon("image\\红将.GIF").getImage());
this.setResizable(false);
this.setTitle(Title);
this.setSize(558,670);
this.show();
}
/**
** 添加棋子方法
*/
public void drawChessMan(){
//流程控制
int i,k;
//图标
Icon in;
//黑色棋子
//车
in = new ImageIcon("image\\黑车.GIF");
for (i=0,k=24;i<2;i++,k+=456){
play[i] = new JLabel(in);
play[i].setBounds(k,56,55,55);
play[i].setName("车1");
}
//马
in = new ImageIcon("image\\黑马.GIF");
for (i=4,k=81;i<6;i++,k+=342){
play[i] = new JLabel(in);
play[i].setBounds(k,56,55,55);
play[i].setName("马1");
}
//相
in = new ImageIcon("image\\黑象.GIF");
for (i=8,k=138;i<10;i++,k+=228){
play[i] = new JLabel(in);
play[i].setBounds(k,56,55,55);
play[i].setName("象1");
}
//士
in = new ImageIcon("image\\黑士.GIF");
for (i=12,k=195;i<14;i++,k+=114){
play[i] = new JLabel(in);
play[i].setBounds(k,56,55,55);
play[i].setName("士1");
}
//卒
in = new ImageIcon("image\\黑卒.GIF");
for (i=16,k=24;i<21;i++,k+=114){
play[i] = new JLabel(in);
play[i].setBounds(k,227,55,55);
play[i].setName("卒1" + i);
}
//炮
in = new ImageIcon("image\\黑炮.GIF");
for (i=26,k=81;i<28;i++,k+=342){
play[i] = new JLabel(in);
play[i].setBounds(k,170,55,55);
play[i].setName("炮1" + i);
}
//将
in = new ImageIcon("image\\黑将.GIF");
play[30] = new JLabel(in);
play[30].setBounds(252,56,55,55);
play[30].setName("将1");
//红色棋子
//车
in = new ImageIcon("image\\红车.GIF");
for (i=2,k=24;i<4;i++,k+=456){
play[i] = new JLabel(in);
play[i].setBounds(k,569,55,55);
play[i].setName("车2");
}
//马
in = new ImageIcon("image\\红马.GIF");
for (i=6,k=81;i<8;i++,k+=342){
play[i] = new JLabel(in);
play[i].setBounds(k,569,55,55);
play[i].setName("马2");
}
//相
in = new ImageIcon("image\\红象.GIF");
for (i=10,k=138;i<12;i++,k+=228){
play[i] = new JLabel(in);
play[i].setBounds(k,569,55,55);
play[i].setName("象2");
}
//士
in = new ImageIcon("image\\红士.GIF");
for (i=14,k=195;i<16;i++,k+=114){
play[i] = new JLabel(in);
play[i].setBounds(k,569,55,55);
play[i].setName("士2");
}
//兵
in = new ImageIcon("image\\红卒.GIF");
for (i=21,k=24;i<26;i++,k+=114){
play[i] = new JLabel(in);
play[i].setBounds(k,398,55,55);
play[i].setName("卒2" + i);
}
//炮
in = new ImageIcon("image\\红炮.GIF");
for (i=28,k=81;i<30;i++,k+=342){
play[i] = new JLabel(in);
play[i].setBounds(k,455,55,55);
play[i].setName("炮2" + i);
}
//帅
in = new ImageIcon("image\\红将.GIF");
play[31] = new JLabel(in);
play[31].setBounds(252,569,55,55);
play[31].setName("帅2");
}
/**
** 线程方法控制棋子闪烁
*/
public void run(){
while (true){
//单击棋子第一下开始闪烁
if (chessManClick){
play[Man].setVisible(false);
//时间控制
try{
tmain.sleep(200);
}
catch(Exception e){
}
play[Man].setVisible(true);
}
//闪烁当前提示信息 以免用户看不见
else {
text.setVisible(false);
//时间控制
try{
tmain.sleep(250);
}
catch(Exception e){
}
text.setVisible(true);
}
try{
tmain.sleep(350);
}
catch (Exception e){
}
}
}
/**
** 单击棋子方法
*/
public void mouseClicked(MouseEvent me){
System.out.println("Mouse");
//当前坐标
int Ex=0,Ey=0;
//启动线程
if (tmain == null){
tmain = new Thread(this);
tmain.start();
}
//单击棋盘(移动棋子)
if (me.getSource().equals(image)){
//该红棋走棋的时候
if (chessPlayClick == 2 && play[Man].getName().charAt(1) == '2'){
Ex = play[Man].getX();
Ey = play[Man].getY();
//移动卒、兵
if (Man > 15 && Man < 26){
rule.armsRule(Man,play[Man],me);
}
//移动炮
else if (Man > 25 && Man < 30){
rule.cannonRule(play[Man],play,me);
}
//移动车
else if (Man >=0 && Man < 4){
rule.cannonRule(play[Man],play,me);
}
//移动马
else if (Man > 3 && Man < 8){
rule.horseRule(play[Man],play,me);
}
//移动相、象
else if (Man > 7 && Man < 12){
rule.elephantRule(Man,play[Man],play,me);
}
//移动仕、士
else if (Man > 11 && Man < 16){
rule.chapRule(Man,play[Man],play,me);
}
//移动将、帅
else if (Man == 30 || Man == 31){
rule.willRule(Man,play[Man],play,me);
}
//是否走棋错误(是否在原地没有动)
if (Ex == play[Man].getX() && Ey == play[Man].getY()){
text.setText(" 红棋走棋");
chessPlayClick=2;
}
else {
text.setText(" 黑棋走棋");
chessPlayClick=1;
}
}//if
//该黑棋走棋的时候
else if (chessPlayClick == 1 && play[Man].getName().charAt(1) == '1'){
Ex = play[Man].getX();
Ey = play[Man].getY();
//移动卒、兵
if (Man > 15 && Man < 26){
rule.armsRule(Man,play[Man],me);
}
//移动炮
else if (Man > 25 && Man < 30){
rule.cannonRule(play[Man],play,me);
}
//移动车
else if (Man >=0 && Man < 4){
rule.cannonRule(play[Man],play,me);
}
//移动马
else if (Man > 3 && Man < 8){
rule.horseRule(play[Man],play,me);
}
//移动相、象
else if (Man > 7 && Man < 12){
rule.elephantRule(Man,play[Man],play,me);
}
//移动仕、士
else if (Man > 11 && Man < 16){
rule.chapRule(Man,play[Man],play,me);
}
//移动将、帅
else if (Man == 30 || Man == 31){
rule.willRule(Man,play[Man],play,me);
}
//是否走棋错误(是否在原地没有动)
if (Ex == play[Man].getX() && Ey == play[Man].getY()){
text.setText(" 黑棋走棋");
chessPlayClick=1;
}
else {
text.setText(" 红棋走棋");
chessPlayClick=2;
}
}//else if
//当前没有操作(停止闪烁)
chessManClick=false;
}//if
//单击棋子
else{
//第一次单击棋子(闪烁棋子)
if (!chessManClick){
for (int i=0;i<32;i++){
//被单击的棋子
if (me.getSource().equals(play[i])){
//告诉线程让该棋子闪烁
Man=i;
//开始闪烁
chessManClick=true;
break;
}
}//for
}//if
//第二次单击棋子(吃棋子)
else if (chessManClick){
//当前没有操作(停止闪烁)
chessManClick=false;
for (i=0;i<32;i++){
//找到被吃的棋子
if (me.getSource().equals(play[i])){
//该红棋吃棋的时候
if (chessPlayClick == 2 && play[Man].getName().charAt(1) == '2'){
Ex = play[Man].getX();
Ey = play[Man].getY();
//卒、兵吃规则
if (Man > 15 && Man < 26){
rule.armsRule(play[Man],play[i]);
}
//炮吃规则
else if (Man > 25 && Man < 30){
rule.cannonRule(0,play[Man],play[i],play,me);
}
//车吃规则
else if (Man >=0 && Man < 4){
rule.cannonRule(1,play[Man],play[i],play,me);
}
//马吃规则
else if (Man > 3 && Man < 8){
rule.horseRule(play[Man],play[i],play,me);
}
//相、象吃规则
else if (Man > 7 && Man < 12){
rule.elephantRule(play[Man],play[i],play);
}
//士、仕吃棋规则
else if (Man > 11 && Man < 16){
rule.chapRule(Man,play[Man],play[i],play);
}
//将、帅吃棋规则
else if (Man == 30 || Man == 31){
rule.willRule(Man,play[Man],play[i],play);
play[Man].setVisible(true);
}
//是否走棋错误(是否在原地没有动)
if (Ex == play[Man].getX() && Ey == play[Man].getY()){
text.setText(" 红棋走棋");
chessPlayClick=2;
break;
}
else{
text.setText(" 黑棋走棋");
chessPlayClick=1;
break;
}
}//if
//该黑棋吃棋的时候
else if (chessPlayClick == 1 && play[Man].getName().charAt(1) == '1'){
Ex = play[Man].getX();
Ey = play[Man].getY();
//卒吃规则
if (Man > 15 && Man < 26){
rule.armsRule(play[Man],play[i]);
}
//炮吃规则
else if (Man > 25 && Man < 30){
rule.cannonRule(0,play[Man],play[i],play,me);
}
//车吃规则
else if (Man >=0 && Man < 4){
rule.cannonRule(1,play[Man],play[i],play,me);
}
//马吃规则
else if (Man > 3 && Man < 8){
rule.horseRule(play[Man],play[i],play,me);
}
//相、象吃规则
else if (Man > 7 && Man < 12){
rule.elephantRule(play[Man],play[i],play);
}
//士、仕吃棋规则
else if (Man > 11 && Man < 16){
rule.chapRule(Man,play[Man],play[i],play);
}
//将、帅吃棋规则
else if (Man == 30 || Man == 31){
rule.willRule(Man,play[Man],play[i],play);
play[Man].setVisible(true);
}
//是否走棋错误(是否在原地没有动)
if (Ex == play[Man].getX() && Ey == play[Man].getY()){
text.setText(" 黑棋走棋");
chessPlayClick=1;
break;
}
else {
text.setText(" 红棋走棋");
chessPlayClick=2;
break;
}
}//else if
}//if
}//for
//是否胜利
if (!play[31].isVisible()){
JOptionPane.showConfirmDialog(
this,"黑棋胜利","玩家一胜利",
JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);
//双方都不可以在走棋了
chessPlayClick=3;
text.setText(" 黑棋胜利");
}//if
else if (!play[30].isVisible()){
JOptionPane.showConfirmDialog(
this,"红棋胜利","玩家二胜利",
JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);
chessPlayClick=3;
text.setText(" 红棋胜利");
}//else if
}//else
}//else
}
public void mousePressed(MouseEvent me){
}
public void mouseReleased(MouseEvent me){
}
public void mouseEntered(MouseEvent me){
}
public void mouseExited(MouseEvent me){
}
/**
** 定义按钮的事件响应
*/
public void actionPerformed(ActionEvent ae) {
//重新开始按钮
if (ae.getSource().equals(anew)){
int i,k;
//重新排列每个棋子的位置
//黑色棋子
//车
for (i=0,k=24;i<2;i++,k+=456){
play[i].setBounds(k,56,55,55);
}
//马
for (i=4,k=81;i<6;i++,k+=342){
play[i].setBounds(k,56,55,55);
}
//相
for (i=8,k=138;i<10;i++,k+=228){
play[i].setBounds(k,56,55,55);
}
//士
for (i=12,k=195;i<14;i++,k+=114){
play[i].setBounds(k,56,55,55);
}
//卒
for (i=16,k=24;i<21;i++,k+=114){
play[i].setBounds(k,227,55,55);
}
//炮
for (i=26,k=81;i<28;i++,k+=342){
play[i].setBounds(k,170,55,55);
}
//将
play[30].setBounds(252,56,55,55);
//红色棋子
//车
for (i=2,k=24;i<4;i++,k+=456){
play[i].setBounds(k,569,55,55);
}
//马
for (i=6,k=81;i<8;i++,k+=342){
play[i].setBounds(k,569,55,55);
}
//相
for (i=10,k=138;i<12;i++,k+=228){
play[i].setBounds(k,569,55,55);
}
//士
for (i=14,k=195;i<16;i++,k+=114){
play[i].setBounds(k,569,55,55);
}
//兵
for (i=21,k=24;i<26;i++,k+=114){
play[i].setBounds(k,398,55,55);
}
//炮
for (i=28,k=81;i<30;i++,k+=342){
play[i].setBounds(k,455,55,55);
}
//帅
play[31].setBounds(252,569,55,55);
chessPlayClick = 2;
text.setText(" 红棋走棋");
for (i=0;i<32;i++){
play[i].setVisible(true);
}
//清除Vector中的内容
Var.clear();
}
//悔棋按钮
else if (ae.getSource().equals(repent)){
try{
//获得setVisible属性值
String S = (String)Var.get(Var.size()-4);
//获得X坐标
int x = Integer.parseInt((String)Var.get(Var.size()-3));
//获得Y坐标
int y = Integer.parseInt((String)Var.get(Var.size()-2));
//获得索引
int M = Integer.parseInt((String)Var.get(Var.size()-1));
//赋给棋子
play[M].setVisible(true);
play[M].setBounds(x,y,55,55);
if (play[M].getName().charAt(1) == '1'){
text.setText(" 黑棋走棋");
chessPlayClick = 1;
}
else{
text.setText(" 红棋走棋");
chessPlayClick = 2;
}
//删除用过的坐标
Var.remove(Var.size()-4);
Var.remove(Var.size()-3);
Var.remove(Var.size()-2);
Var.remove(Var.size()-1);
//停止旗子闪烁
chessManClick=false;
}
catch(Exception e){
}
}
//退出
else if (ae.getSource().equals(exit)){
int j=JOptionPane.showConfirmDialog(
this,"真的要退出吗?","退出",
JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);
if (j == JOptionPane.YES_OPTION){
System.exit(0);
}
}
}
/*定义中国象棋规则的类*/
class ChessRule {
/**卒子的移动规则*/
public void armsRule(int Man,JLabel play,MouseEvent me){
//黑卒向下
if (Man < 21){
//向下移动、得到终点的坐标模糊成合法的坐标
if ((me.getY()-play.getY()) > 27 && (me.getY()-play.getY()) < 86 && (me.getX()-play.getX()) < 55 && (me.getX()-play.getX()) > 0){
//当前记录添加到集合(用于悔棋)
Var.add(String.valueOf(play.isVisible()));
Var.add(String.valueOf(play.getX()));
Var.add(String.valueOf(play.getY()));
Var.add(String.valueOf(Man));
play.setBounds(play.getX(),play.getY()+57,55,55);
}
//向右移动、得到终点的坐标模糊成合法的坐标、必须过河
else if (play.getY() > 284 && (me.getX() - play.getX()) >= 57 && (me.getX() - play.getX()) <= 112){
play.setBounds(play.getX()+57,play.getY(),55,55);
}
//向左移动、得到终点的坐标模糊成合法的坐标、必须过河
else if (play.getY() > 284 && (play.getX() - me.getX()) >= 2 && (play.getX() - me.getX()) <=58){
//模糊坐标
play.setBounds(play.getX()-57,play.getY(),55,55);
}
}
//红卒向上
else{
//当前记录添加到集合(用于悔棋)
Var.add(String.valueOf(play.isVisible()));
Var.add(String.valueOf(play.getX()));
Var.add(String.valueOf(play.getY()));
Var.add(String.valueOf(Man));
//向上移动、得到终点的坐标模糊成合法的坐标
if ((me.getX()-play.getX()) >= 0 && (me.getX()-play.getX()) <= 55 && (play.getY()-me.getY()) >27 && play.getY()-me.getY() < 86){
play.setBounds(play.getX(),play.getY()-57,55,55);
}
//向右移动、得到终点的坐标模糊成合法的坐标、必须过河
else if (play.getY() <= 341 && (me.getX() - play.getX()) >= 57 && (me.getX() - play.getX()) <= 112){
play.setBounds(play.getX()+57,play.getY(),55,55);
}
//向左移动、得到终点的坐标模糊成合法的坐标、必须过河
else if (play.getY() <= 341 && (play.getX() - me.getX()) >= 3 && (play.getX() - me.getX()) <=58){
play.setBounds(play.getX()-57,play.getY(),55,55);
}
}
}//卒移动结束
/**卒吃棋规则*/
public void armsRule(JLabel play1,JLabel play2){
//向右走
if ((play2.getX() - play1.getX()) <= 112 && (play2.getX() - play1.getX()) >= 57 && (play1.getY() - play2.getY()) < 22 && (play1.getY() - play2.getY()) > -22 && play2.isVisible() && play1.getName().charAt(1)!=play2.getName().charAt(1)){
//黑棋要过河才能右吃棋
if (play1.getName().charAt(1) == '1' && play1.getY() > 284 && play1.getName().charAt(1) != play2.getName().charAt(1)){
play2.setVisible(false);
//把对方的位置给自己
play1.setBounds(play2.getX(),play2.getY(),55,55);
}
//红棋要过河才左能吃棋
else if (play1.getName().charAt(1) == '2' && play1.getY() < 341 && play1.getName().charAt(1) != play2.getName().charAt(1)){
play2.setVisible(false);
//把对方的位置给自己
play1.setBounds(play2.getX(),play2.getY(),55,55);
}
}
//向左走
else if ((play1.getX() - play2.getX()) <= 112 && (play1.getX() - play2.getX()) >= 57 && (play1.getY() - play2.getY()) < 22 && (play1.getY() - play2.getY()) > -22 && play2.isVisible() && play1.getName().charAt(1)!=play2.getName().charAt(1)){
//黑棋要过河才能左吃棋
if (play1.getName().charAt(1) == '1' && play1.getY() > 284 && play1.getName().charAt(1) != play2.getName().charAt(1)){
play2.setVisible(false);
//把对方的位置给自己
play1.setBounds(play2.getX(),play2.getY(),55,55);
}
//红棋要过河才能右吃棋
else if (play1.getName().charAt(1) == '2' && play1.getY() < 341 && play1.getName().charAt(1) != play2.getName().charAt(1)){
play2.setVisible(false);
//把对方的位置给自己
play1.setBounds(play2.getX(),play2.getY(),55,55);
}
}
//向上走
else if (play1.getX() - play2.getX() >= -22 && play1.getX() - play2.getX() <= 22 && play1.getY() - play2.getY() >= -112 && play1.getY() - play2.getY() <= 112){
//黑棋不能向上吃棋
if (play1.getName().charAt(1) == '1' && play1.getY() < play2.getY() && play1.getName().charAt(1) != play2.getName().charAt(1)){
play2.setVisible(false);
//把对方的位置给自己
play1.setBounds(play2.getX(),play2.getY(),55,55);
}
//红棋不能向下吃棋
else if (play1.getName().charAt(1) == '2' && play1.getY() > play2.getY() && play1.getName().charAt(1) != play2.getName().charAt(1)){
play2.setVisible(false);
//把对方的位置给自己
play1.setBounds(play2.getX(),play2.getY(),55,55);
}
}
//当前记录添加到集合(用于悔棋)
Var.add(String.valueOf(play1.isVisible()));
Var.add(String.valueOf(play1.getX()));
Var.add(String.valueOf(play1.getY()));
Var.add(String.valueOf(Man));
//当前记录添加到集合(用于悔棋)
Var.add(String.valueOf(play2.isVisible()));
Var.add(String.valueOf(play2.getX()));
Var.add(String.valueOf(play2.getY()));
Var.add(String.valueOf(i));
}//卒吃结束
/**炮、车移动规则*/
public void cannonRule(JLabel play,JLabel playQ[],MouseEvent me){
//起点和终点之间是否有棋子
int Count = 0;
//上、下移动
if (play.getX() - me.getX() <= 0 && play.getX() - me.getX() >= -55){
//指定所有模糊Y坐标
for (int i=56;i<=571;i+=57){
//移动的Y坐标是否有指定坐标相近的
if (i - me.getY() >= -27 && i - me.getY() <= 27){
//所有的棋子
for (int j=0;j<32;j++){
//找出在同一条竖线的所有棋子、并不包括自己
if (playQ[j].getX() - play.getX() >= -27 && playQ[j].getX() - play.getX() <= 27 && playQ[j].getName()!=play.getName() && playQ[j].isVisible()){
//从起点到终点(从左到右)
for (int k=play.getY()+57;k<i;k+=57){
//大于起点、小于终点的坐标就可以知道中间是否有棋子
if (playQ[j].getY() < i && playQ[j].getY() > play.getY()){
//中间有一个棋子就不可以从这条竖线过去
Count++;
break;
}
}//for
//从起点到终点(从右到左)
for (int k=i+57;k<play.getY();k+=57){
//找起点和终点的棋子
if (playQ[j].getY() < play.getY() && playQ[j].getY() > i){
Count++;
break;
}
}//for
}//if
}//for
//起点和终点没有棋子就可以移动了
if (Count == 0){
//当前记录添加到集合(用于悔棋)
Var.add(String.valueOf(play.isVisible()));
Var.add(String.valueOf(play.getX()));
Var.add(String.valueOf(play.getY()));
Var.add(String.valueOf(Man));
play.setBounds(play.getX(),i,55,55);
break;
}
}//if
}//for
}//if
//左、右移动
else if (play.getY() - me.getY() >=-27 && play.getY() - me.getY() <= 27){
//指定所有模糊X坐标
for (int i=24;i<=480;i+=57){
//移动的X坐标是否有指定坐标相近的
if (i - me.getX() >= -55 && i-me.getX() <= 0){
//所有的棋子
for (int j=0;j<32;j++){
//找出在同一条横线的所有棋子、并不包括自己
if (playQ[j].getY() - play.getY() >= -27 && playQ[j].getY() - play.getY() <= 27 && playQ[j].getName()!=play.getName() && playQ[j].isVisible()){
//从起点到终点(从上到下)
for (int k=play.getX()+57;k<i;k+=57){
//大于起点、小于终点的坐标就可以知道中间是否有棋子
if (playQ[j].getX() < i && playQ[j].getX() > play.getX()){
//中间有一个棋子就不可以从这条横线过去
Count++;
break;
}
}//for
//从起点到终点(从下到上)
for (int k=i+57;k<play.getX();k+=57){
//找起点和终点的棋子
if (playQ[j].getX() < play.getX() && playQ[j].getX() > i){
Count++;
break;
}
}//for
}//if
}//for
//起点和终点没有棋子
if (Count == 0){
//当前记录添加到集合(用于悔棋)
Var.add(String.valueOf(play.isVisible()));
Var.add(String.valueOf(play.getX()));
Var.add(String.valueOf(play.getY()));
Var.add(String.valueOf(Man));
play.setBounds(i,play.getY(),55,55);
break;
}
}//if
}//for
}//else
}//炮、车移动方法结束
/**炮、车吃棋规则*/
public void cannonRule(int Chess,JLabel play,JLabel playTake,JLabel playQ[],MouseEvent me){
//起点和终点之间是否有棋子
int Count = 0;
//所有的棋子
for (int j=0;j<32;j++){
//找出在同一条竖线的所有棋子、并不包括自己
if (playQ[j].getX() - play.getX() >= -27 && playQ[j].getX() - play.getX() <= 27 && playQ[j].getName()!=play.getName() && playQ[j].isVisible()){
//自己是起点被吃的是终点(从上到下)
for (int k=play.getY()+57;k<playTake.getY();k+=57){
//大于起点、小于终点的坐标就可以知道中间是否有棋子
if (playQ[j].getY() < playTake.getY() && playQ[j].getY() > play.getY()){
//计算起点和终点的棋子个数
Count++;
break;
}
}//for
//自己是起点被吃的是终点(从下到上)
for (int k=playTake.getY();k<play.getY();k+=57){
//找起点和终点的棋子
if (playQ[j].getY() < play.getY() && playQ[j].getY() > playTake.getY()){
Count++;
break;
}
}//for
}//if
//找出在同一条竖线的所有棋子、并不包括自己