本文实例为大家分享了java实现五子棋大战的具体代码,供大家参考,具体内容如下

这是我接近一年前的项目了,以前没有养成写博客的习惯,打算陆续把以前做过的项目补上来。

一、介绍
主要实现的功能有棋子颜色选择,悔棋,重新开始,玩家对战和人机对战,效果图如图所是:

模式选择:
https://zsrimg.ikafan.com/file_images/article/202203/2022325111336174.jpg?2022225111353
棋子选择:
https://zsrimg.ikafan.com/file_images/article/202203/2022325111403369.jpg?2022225111413
人机对战:
https://zsrimg.ikafan.com/file_images/article/202203/2022325111421262.jpg?2022225111431
玩家对战:
https://zsrimg.ikafan.com/file_images/article/202203/2022325111440409.jpg?2022225111449
*二、具体实现
五子棋的开发首先需要在界面上绘制一个表格,因为七班是不变的,棋子大小是不变的,所以我们首先可以自定义一个接口来设置项目中的常量,这样改变这些参数时也比较方便,CS.java代码如下:
public interface CS {
public static final int x0=60;//棋盘开始位置
public static final int y0=70;
public static final int line=15;//棋盘有多少条线
public static final int size=40;//棋子大小
和上一篇博客中的画图板类似,首先需要一个界面,这里可以定义一个Chess类继承Jframe,然后再重写paint(Graphics g)方法,来绘制棋盘,Chess.java代码如下:

Chess.java:
public class Chess extends JFrame implements CS{

  1. int i=2;
  2. private qizi qizilarry2[][]=new qizi[line][line];//用qizi类型的二维数组来存储棋子
  3. private JButton b = new JButton("悔棋");
  4. private JButton b2=new JButton("重新开始");
  5. private JLabel jLabel=new JLabel("请选择对战模式");
  6. private JLabel jLabel2=new JLabel("请选择棋子颜色");
  7. private int value;//提示框选项的值
  8. private int value2;
  9. Dimension dimension=new Dimension(100, 30);
  10. String color[]= {"白棋","黑棋"};
  11. String moshi[]= {"玩家对战","人机对战"};
  12. public void chessUI() {
  13. b.setPreferredSize(dimension);
  14. b2.setPreferredSize(dimension);
  15. this.setSize(700, 700);
  16. this.setLayout(new FlowLayout());
  17. this.setTitle("五子棋");
  18. this.setLocationRelativeTo(null);
  19. this.setDefaultCloseOperation(3);
  20. this.setVisible(true);
  21. this.add(b);
  22. this.add(b2);
  23. value=JOptionPane.showOptionDialog(this, jLabel, "提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
  24. null, moshi, null);
  25. value2=JOptionPane.showOptionDialog(this, jLabel2, "提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
  26. null, color, null);
  27. //关闭提示框则退出程序 if(value==JOptionPane.CLOSED_OPTION||value2==JOptionPane.CLOSED_OPTION) {
  28. System.exit(1);
  29. }
  30. Graphics g=this.getGraphics();
  31. mouslistener mouslistener =new mouslistener(g,qizilarry2,this,value,value2);
  32. this.addMouseListener(mouslistener);//窗口添加监听
  33. b.addActionListener(mouslistener);//按钮添加监听
  34. b2.addActionListener(mouslistener);
  35. }
  36. public void paint(Graphics g) {//重绘棋盘和棋子
  37. super.paint(g);
  38. drawChess(g);
  39. drawQZ();
  40. //画一个棋盘
  41. public void drawChess(Graphics g) {
  42. g.setColor(Color.black);
  43. for(int i=0;i<line;i++) {
  44. g.drawLine(x0, y0+size*i, x0+(line-1)*size, y0+size*i);
  45. }
  46. for(int j=0;j<line;j++) {
  47. g.drawLine(x0+size*j, y0, x0+size*j, y0+(line-1)*size);
  48. }
  49. }
  50. //重构画棋子
  51. public void drawQZ() {
  52. for(int ix=0;ix<line;ix++) {
  53. for(int iy=0;iy<line;iy++) {
  54. if(qizilarry2[ix][iy]!=null) {
  55. qizi qizi21=qizilarry2[ix][iy];
  56. qizi21.drawq();
  57. }
  58. }
  59. }
  60. }
  61. public static void main(String[] args) {
  62. new Chess().chessUI();
  63. }
  64. }

用qizi类型的二维数组来存储棋子,在重绘时重绘整个棋盘和二维数组上的棋子,如果二维数组为null则不用重绘。
接下来该创建监听类了,在鼠标点击棋盘时,要使得棋子在棋盘的正中央,代码如下:
//x轴坐标

  1. for(ix=0;x1>0;ix++) {
  2. x1-=size;
  3. }
  4. x1+=size;
  5. x1-=size/2;
  6. ix--;
  7. if(x1<=0) {
  8. x=x0+ix*size;
  9. }else
  10. x=x0+(++ix)*size;
  11. //y轴坐标
  12. for(iy=0;y1>0;iy++) {
  13. y1-=size;
  14. }
  15. y1+=size;
  16. y1-=size/2;
  17. iy--;
  18. if(y1<=0) {
  19. y=y0+iy*size;
  20. }else
  21. y=y0+(++iy)*size;

判赢的方法非常简单,只要计算棋子在它八个方向的相邻的且颜色相同的棋子个数即可,这里只展现向左查找棋子的代码(后续会附上整个监听类的代码):

  1. public int zuo(int x,int y,Color c) {//向左找
  2. int a=x;
  3. for(int i=1;i<5;i++) {
  4. a--;
  5. if(a<0||qizilarry[a][y]==null) {
  6. break;
  7. }else if(qizilarry[a][y].getColor()==c)
  8. count1++;
  9. else
  10. break;
  11. }

return count1;
}
当模式为玩家和玩家模式时,需要每下一个棋子颜色改变,实现的代码如下:
if(a) {
color =c;
a=false;
}else {
color=c2;
a=true;
}
g.setColor(color);
g.fillOval(x-size/2, y-size/2, size, size);
prex=ix;
prey=iy;
qizi qizi=new qizi(g, color,ix,iy);
qizilarry[ix][iy]=qizi;
inte++;
玩家VS玩家和玩家VS电脑的判赢方法基本类似,这里只展现了玩家与玩家的判赢方法,即每下一个新的棋子,就计算这个棋子八个方向上相同且相邻棋子的个数,当同一直线两个方向的棋子个数之和为5时,则获取棋子颜色,判定为获胜,具体代码实现如下:
//判断输赢
if(zuo(ix,iy,color)+you(ix,iy,color)>=4||shang(ix,iy,color)+xia(ix,iy,color)>=4
||zuoshang(ix, iy,color)+youxia(ix, iy,color)>=4||zuoxia(ix, iy,color)+youshang(ix, iy,color)>=4) {
JLabel jLabel =new JLabel(“白棋获胜!”);
JLabel jlabel2 =new JLabel(“黑棋获胜!”);
if(color==Color.white)
JOptionPane.showMessageDialog(jFrame, jLabel, “游戏结束”, JOptionPane.PLAIN_MESSAGE);
else
JOptionPane.showMessageDialog(jFrame, jlabel2, “游戏结束”, JOptionPane.PLAIN_MESSAGE);
}else {
count1=0;//如果没有赢重新置0重新计算
count2=0;
countS=0;
countX=0;
countZS=0;
countZX=0;
countYS=0;
countYX=0;
}
这样玩家与玩家模式的大体功能就基本实现了,接下类就要实现五子棋的AI功能了

五子棋AI

这里我们主要针权值法讨论下,大致思路如下:

1.我们绘制好一个棋盘后,大小为 15*15;
2.下棋之前,对于棋盘中的每个空位,我们每都替电脑人“掂一掂”下在哪里合算;(估权过程)
3.对每个空位按照规则都计算完权重,我们找出权重最大的位置,此位置就是npc落子位置。

空子位置我们用 “0” 表示,白子用“2”表示,黑子用“1”表示;
我们主要分为以下几种情况:

定义 棋子相连情况 权值
活一连 010、020 40
活二连 0110、0220 400
活三连 01110、02220 3000
活四连 011110、022220 10000
眠一连 012、021 20
眠二连 0112、0221 200
眠三连 01112、02221 500
眠四连 011112、022221 3000
用hash表存储所有可能的情况并赋予一定的权值,每下一个棋子便更新棋盘上所有空位置的权值,电脑再寻找棋盘上权值最大的点下棋,computerChess()函数代码如下:
public void computerChess() {

  1. hashMap.put("10000", 15);//眠1连
  2. hashMap.put("20000", 10);//眠1连
  3. hashMap.put("20100",17);//眠1连,15
  4. hashMap.put("10200",12);//眠1连,10
  5. hashMap.put("21000",15);//眠1连,15
  6. hashMap.put("12000",10);//眠1连,10
  7. hashMap.put("20010",19);//眠1连,15
  8. hashMap.put("10020",14);//眠1连,10
  9. hashMap.put("20100",17);//眠1连,15
  10. hashMap.put("10200",12);//眠1连,10
  11. //
  12. // hashMap.put("00010",21);//活1连,15
  13. // hashMap.put("00020",16);//活1连,10
  14. // hashMap.put("00100",19);//活1连,15
  15. // hashMap.put("00200",14);//活1连,10
  16. // hashMap.put("01000",17);//活1连,15
  17. // hashMap.put("02000",12);//活1连,10
  18. //
  19. //被堵住
  20. hashMap.put("10100",65);//眠2连,40
  21. hashMap.put("20200",60);//眠2连,30
  22. hashMap.put("01100",65);//眠2连,40
  23. hashMap.put("02200",60);//眠2连,30
  24. hashMap.put("11000",65);//眠2连,40
  25. hashMap.put("22000",60);//眠2连,30
  26. hashMap.put("21010",65);//眠2连,40
  27. hashMap.put("12020",60);//眠2连,30
  28. hashMap.put("20110",65);//眠2连,40
  29. hashMap.put("10220",60);//眠2连,30
  30. hashMap.put("21100",65);//眠2连,40
  31. hashMap.put("12200",60);//眠2连,30
  32. // hashMap.put("01010",75);//活2连,40
  33. // hashMap.put("02020",70);//活2连,30
  34. // hashMap.put("00110",75);//活2连,40
  35. // hashMap.put("00220",70);//活2连,30
  36. // hashMap.put("01100",75);//活2连,40
  37. // hashMap.put("02200",70);//活2连,30
  38. // hashMap.put("11000",75);//活2连,40
  39. // hashMap.put("00022",70);//活2连,30
  40. //
  41. // //被堵住
  42. hashMap.put("11100",150);//眠3连,100
  43. hashMap.put("22200",140);//眠3连,80
  44. hashMap.put("21110",150);//眠3连,100
  45. hashMap.put("12220",140);//眠3连,80
  46. //
  47. // hashMap.put("10110",1000);//活3连,130
  48. // hashMap.put("20220",800);//活3连,110
  49. // hashMap.put("11010",1000);//活3连,130
  50. // hashMap.put("22020",800);//活3连,110
  51. // hashMap.put("01110", 1000);//活3连
  52. // hashMap.put("02220", 800);//活3连
  53. hashMap.put("11110",3000);//4连,300
  54. hashMap.put("11112",3000);//4连,300
  55. hashMap.put("22220",3500);//4连,280
  56. hashMap.put("22221",3500);//4连,280
  57. int a;
  58. int b;
  59. for(int y=0;y<line;y++) {
  60. for(int x=0;x<line;x++) {
  61. if(qizilarry[x][y]==null) {
  62. //向左
  63. a=x;
  64. for(int i=1;i<6;i++) {
  65. a--;
  66. if(a<0)
  67. break;
  68. if(qizilarry[a][y]!=null) {
  69. if(qizilarry[a][y].getColor()==c) {
  70. zuo+='2';
  71. }else
  72. zuo+='1';
  73. }else
  74. zuo+=0;
  75. }
  76. Integer integer=hashMap.get(zuo);
  77. if(integer!=null)
  78. chessValue[x][y]+=integer;
  79. //向右
  80. a=x;
  81. for(int i=1;i<6;i++) {
  82. a++;
  83. if(a==line)
  84. break;
  85. if(qizilarry[a][y]!=null) {
  86. if(qizilarry[a][y].getColor()==c) {
  87. you+='2';
  88. }else
  89. you+='1';
  90. }else
  91. you+=0;
  92. }
  93. integer=hashMap.get(you);
  94. if(integer!=null)
  95. chessValue[x][y]+=integer;
  96. //向上
  97. b=y;
  98. for(int i=1;i<6;i++) {
  99. b--;
  100. if(b<0)
  101. break;
  102. if(qizilarry[x][b]!=null) {
  103. if(qizilarry[x][b].getColor()==c) {
  104. shang+='2';
  105. }else
  106. shang+='1';
  107. }else
  108. shang+=0;
  109. }
  110. integer=hashMap.get(shang);
  111. if(integer!=null)
  112. chessValue[x][y]+=integer;
  113. //向下
  114. b=y;
  115. for(int i=1;i<6;i++) {
  116. b++;
  117. if(b==line)
  118. break;
  119. if(qizilarry[x][b]!=null) {
  120. if(qizilarry[x][b].getColor()==c) {
  121. xia+='2';
  122. }else
  123. xia+='1';
  124. }else
  125. xia+=0;
  126. }
  127. integer=hashMap.get(xia);
  128. if(integer!=null)
  129. chessValue[x][y]+=integer;
  130. //向左上
  131. a=x;
  132. b=y;
  133. for(int i=1;i<6;i++) {
  134. a--;
  135. b--;
  136. if(a<0||b<0)
  137. break;
  138. if(qizilarry[a][b]!=null) {
  139. if(qizilarry[a][b].getColor()==c) {
  140. zuoshang+='2';
  141. }else
  142. zuoshang+='1';
  143. }else
  144. zuoshang+=0;
  145. }
  146. integer=hashMap.get(zuoshang);
  147. if(integer!=null)
  148. chessValue[x][y]+=integer;
  149. //向右下
  150. a=x;
  151. b=y;
  152. for(int i=1;i<6;i++) {
  153. a++;
  154. b++;
  155. if(a==line||b==line)
  156. break;
  157. if(qizilarry[a][b]!=null) {
  158. if(qizilarry[a][b].getColor()==c) {
  159. youxia+='2';
  160. }else
  161. youxia+='1';
  162. }else
  163. youxia+=0;
  164. }
  165. integer=hashMap.get(youxia);
  166. if(integer!=null)
  167. chessValue[x][y]+=integer;
  168. //向左下
  169. a=x;
  170. b=y;
  171. for(int i=1;i<6;i++) {
  172. a--;
  173. b++;
  174. if(a<0||b==line)
  175. break;
  176. if(qizilarry[a][b]!=null) {
  177. if(qizilarry[a][b].getColor()==c) {
  178. zuoxia+='2';
  179. }else
  180. zuoxia+='1';
  181. }else
  182. zuoxia+=0;
  183. }
  184. integer=hashMap.get(zuoxia);
  185. if(integer!=null)
  186. chessValue[x][y]+=integer;
  187. //向右上
  188. a=x;
  189. b=y;
  190. for(int i=1;i<6;i++) {
  191. a++;
  192. b--;
  193. if(a==line||b<0)
  194. break;
  195. if(qizilarry[a][b]!=null) {
  196. if(qizilarry[a][b].getColor()==c) {
  197. youshang+='2';
  198. }else
  199. youshang+='1';
  200. }else
  201. youshang+=0;
  202. }
  203. integer=hashMap.get(youshang);
  204. if(integer!=null)
  205. chessValue[x][y]+=integer;
  206. zuo="";
  207. you="";
  208. shang="";
  209. xia="";
  210. zuoshang="";
  211. zuoxia="";
  212. youshang="";
  213. youxia="";
  214. }
  215. }
  216. }
  217. }

完整代码

mouslistener.java:
public class mouslistener extends MouseAdapter implements CS,ActionListener{

  1. private int x;//棋子像素坐标
  2. private int y;
  3. private int ix;//用户棋子坐标
  4. private int iy;
  5. private int cx;//电脑棋子坐标
  6. private int cy;
  7. private int n=1;//下黑棋或白棋
  8. public int inte=0;//棋子数目
  9. private int prex;//上一个棋子的坐标
  10. private int prey;
  11. private int count1=0;
  12. private int count2=0;
  13. private int countS=0;
  14. private int countX=0;
  15. private int countZS=0;
  16. private int countYX=0;
  17. private int countZX=0;
  18. private int countYS=0;
  19. private int value;
  20. private int value2;
  21. String zuo="";
  22. String you="";
  23. String shang="";
  24. String xia="";
  25. String zuoshang="";
  26. String zuoxia="";
  27. String youshang="";
  28. String youxia="";
  29. private boolean a=true;//玩家对战下棋顺序
  30. private boolean b=true;//是否在同一个位置
  31. // private boolean end=true;//是否电脑胜利
  32. private Color c;//用户棋子颜色
  33. private Color c2;//电脑棋子颜色
  34. private Color color;//玩家对战颜色交替下棋
  35. public qizi qizilarry[][]=new qizi[line][line];
  36. private int chessValue[][]=new int[line][line];//权值表
  37. private JLabel j=new JLabel("请选择对战模式");
  38. private JLabel j2=new JLabel("请选择棋子颜色");
  39. String moshi[]= {"玩家对战","人机对战"};
  40. String co[]= {"白棋","黑棋"};
  41. JFrame jFrame;//悔棋重绘传参
  42. Graphics g;
  43. HashMap<String, Integer> hashMap=new HashMap<String, Integer>();
  44. public mouslistener(Graphics g,qizi qizi[][],JFrame jFrame,int value,int value2) {
  45. this.g=g;
  46. qizilarry=qizi;
  47. this.jFrame=jFrame;
  48. this.value=value;
  49. this.value2=value2;
  50. if(value2==1) {
  51. c=Color.black;
  52. c2=Color.white;
  53. }else {
  54. c=Color.white;
  55. c2=Color.black;
  56. }
  57. }
  58. public void mouseClicked(MouseEvent e) {
  59. x=e.getX();
  60. y=e.getY();
  61. int x1=x-x0;
  62. int y1=y-y0;
  63. //x轴坐标
  64. for(ix=0;x1>0;ix++) {
  65. x1-=size;
  66. }
  67. x1+=size;
  68. x1-=size/2;
  69. ix--;
  70. if(x1<=0) {
  71. x=x0+ix*size;
  72. }else
  73. x=x0+(++ix)*size;
  74. //y轴坐标
  75. for(iy=0;y1>0;iy++) {
  76. y1-=size;
  77. }
  78. y1+=size;
  79. y1-=size/2;
  80. iy--;
  81. if(y1<=0) {
  82. y=y0+iy*size;
  83. }else
  84. y=y0+(++iy)*size;
  85. //判断是否在同一个地方
  86. b=true;
  87. if(qizilarry[ix][iy]!=null) {
  88. JLabel jLabel =new JLabel("不能下在同一个地方!");
  89. JOptionPane.showMessageDialog(jFrame, jLabel, "警告", JOptionPane.WARNING_MESSAGE);
  90. b=false;
  91. }
  92. //--------------------
  93. //画圆及重构,一个黑棋一个白棋
  94. //--------------------
  95. if(b) {
  96. if(value==1) {
  97. g.setColor(c);
  98. g.fillOval(x-size/2, y-size/2, size, size);
  99. prex=ix;
  100. prey=iy;
  101. qizi qizi=new qizi(g, c,ix,iy);
  102. qizilarry[ix][iy]=qizi;
  103. inte++;
  104. System.out.println("用户下棋");
  105. computerChess();
  106. int qzmax=0;
  107. for(int b=0;b<line;b++) {
  108. for(int a =0;a<line;a++) {
  109. if(chessValue[a][b]>qzmax) {
  110. qzmax=chessValue[a][b];
  111. cx=a;
  112. cy=b;
  113. }
  114. }
  115. }
  116. g.setColor(c2);
  117. g.fillOval(x0+cx*size-size/2, y0+cy*size-size/2, size, size);
  118. qizi qizi2=new qizi(g, c2,cx,cy);
  119. qizilarry[cx][cy]=qizi2;
  120. inte++;
  121. System.out.println("电脑下棋");
  122. for(int b=0;b<line;b++) {
  123. for(int a =0;a<line;a++) {
  124. chessValue[a][b]=0;
  125. }
  126. }
  127. //判断输赢
  128. if(zuo(ix,iy,c)+you(ix,iy,c)>=4||shang(ix,iy,c)+xia(ix,iy,c)>=4
  129. ||zuoshang(ix, iy,c)+youxia(ix, iy,c)>=4||zuoxia(ix, iy,c)+youshang(ix, iy,c)>=4) {
  130. JLabel jLabel =new JLabel("玩家获胜!");
  131. JOptionPane.showMessageDialog(jFrame, jLabel, "游戏结束", JOptionPane.PLAIN_MESSAGE);
  132. }else {
  133. count1=0;
  134. count2=0;
  135. countS=0;
  136. countX=0;
  137. countZS=0;
  138. countZX=0;
  139. countYS=0;
  140. countYX=0;
  141. if((zuo(cx,cy,c2)+you(cx,cy,c2)>=4||shang(cx,cy,c2)+xia(cx,cy,c2)>=4
  142. ||zuoshang(cx, cy,c2)+youxia(cx, cy,c2)>=4||zuoxia(cx, cy,c2)+youshang(cx, cy,c2)>=4) ){
  143. JLabel jLabel =new JLabel("电脑获胜!");
  144. JOptionPane.showMessageDialog(jFrame, jLabel, "游戏结束", JOptionPane.PLAIN_MESSAGE);
  145. }else {
  146. count1=0;
  147. count2=0;
  148. countS=0;
  149. countX=0;
  150. countZS=0;
  151. countZX=0;
  152. countYS=0;
  153. countYX=0;
  154. }
  155. }
  156. }else {
  157. if(a) {
  158. color =c;
  159. a=false;
  160. }else {
  161. color=c2;
  162. a=true;
  163. }
  164. g.setColor(color);
  165. g.fillOval(x-size/2, y-size/2, size, size);
  166. prex=ix;
  167. prey=iy;
  168. qizi qizi=new qizi(g, color,ix,iy);
  169. qizilarry[ix][iy]=qizi;
  170. inte++;
  171. //判断输赢
  172. if(zuo(ix,iy,color)+you(ix,iy,color)>=4||shang(ix,iy,color)+xia(ix,iy,color)>=4
  173. ||zuoshang(ix, iy,color)+youxia(ix, iy,color)>=4||zuoxia(ix, iy,color)+youshang(ix, iy,color)>=4) {
  174. JLabel jLabel =new JLabel("白棋获胜!");
  175. JLabel jlabel2 =new JLabel("黑棋获胜!");
  176. if(color==Color.white)
  177. JOptionPane.showMessageDialog(jFrame, jLabel, "游戏结束", JOptionPane.PLAIN_MESSAGE);
  178. else
  179. JOptionPane.showMessageDialog(jFrame, jlabel2, "游戏结束", JOptionPane.PLAIN_MESSAGE);
  180. }else {
  181. count1=0;//如果没有赢重新置0重新计算
  182. count2=0;
  183. countS=0;
  184. countX=0;
  185. countZS=0;
  186. countZX=0;
  187. countYS=0;
  188. countYX=0;
  189. }
  190. }
  191. }
  192. }
  193. public int zuo(int x,int y,Color c) {//向左找
  194. int a=x;
  195. for(int i=1;i<5;i++) {
  196. a--;
  197. if(a<0||qizilarry[a][y]==null) {
  198. break;
  199. }else if(qizilarry[a][y].getColor()==c)
  200. count1++;
  201. else
  202. break;
  203. }
  204. return count1;
  205. }
  206. public int you(int x,int y,Color c){//向右找
  207. int a =x;
  208. for(int i=1;i<5;i++) {
  209. a++;
  210. if(a==15||qizilarry[a][y]==null) {
  211. break;
  212. }else if(qizilarry[a][y].getColor()==c)
  213. count2++;
  214. else
  215. break;
  216. }
  217. return count2;
  218. }
  219. public int xia(int x,int y,Color c) {//向下找
  220. int a=y;
  221. for(int i=1;i<5;i++) {
  222. a++;
  223. if(a==15||qizilarry[x][a]==null) {
  224. break;
  225. }else if(qizilarry[x][a].getColor()==c)
  226. countX++;
  227. else
  228. break;
  229. }
  230. return countX;
  231. }
  232. public int shang(int x,int y,Color c){//向上找
  233. int a =y;
  234. for(int i=1;i<5;i++) {
  235. a--;
  236. if(a<0||qizilarry[x][a]==null) {
  237. break;
  238. }else if(qizilarry[x][a].getColor()==c)
  239. countS++;
  240. else
  241. break;
  242. }
  243. return countS;
  244. }
  245. public int zuoshang(int x,int y,Color c) {//向左上找
  246. int a=x;
  247. int b=y;
  248. for(int i=1;i<5;i++) {
  249. a--;
  250. b++;
  251. if(a<0||b==15||qizilarry[a][b]==null) {
  252. break;
  253. }else if(qizilarry[a][b].getColor()==c)
  254. countZS++;
  255. else
  256. break;
  257. }
  258. return countZS;
  259. }
  260. public int youxia(int x,int y,Color c) {//向右下找
  261. int a=x;
  262. int b=y;
  263. for(int i=1;i<5;i++) {
  264. a++;
  265. b--;
  266. if(b<0||a==15||qizilarry[a][b]==null) {
  267. break;
  268. }else if(qizilarry[a][b].getColor()==c)
  269. countYX++;
  270. else
  271. break;
  272. }
  273. return countYX;
  274. }
  275. public int zuoxia(int x,int y,Color c) {//向左下找
  276. int a=x;
  277. int b=y;
  278. for(int i=1;i<5;i++) {
  279. a--;
  280. b--;
  281. if(a<0||b<0||qizilarry[a][b]==null) {
  282. break;
  283. }else if(qizilarry[a][b].getColor()==c)
  284. countZX++;
  285. else
  286. break;
  287. }
  288. return countZX;
  289. }
  290. public int youshang(int x,int y,Color c) {//向右上找
  291. int a=x;
  292. int b=y;
  293. for(int i=1;i<5;i++) {
  294. a++;
  295. b++;
  296. if(a==15||b==15||qizilarry[a][b]==null) {
  297. break;
  298. }else if(qizilarry[a][b].getColor()==c)
  299. countYS++;
  300. else
  301. break;
  302. }
  303. return countYS;
  304. }
  305. public void computerChess() {
  306. hashMap.put("10000", 15);//眠1连
  307. hashMap.put("20000", 10);//眠1连
  308. hashMap.put("20100",17);//眠1连,15
  309. hashMap.put("10200",12);//眠1连,10
  310. hashMap.put("21000",15);//眠1连,15
  311. hashMap.put("12000",10);//眠1连,10
  312. hashMap.put("20010",19);//眠1连,15
  313. hashMap.put("10020",14);//眠1连,10
  314. hashMap.put("20100",17);//眠1连,15
  315. hashMap.put("10200",12);//眠1连,10
  316. //
  317. // hashMap.put("00010",21);//活1连,15
  318. // hashMap.put("00020",16);//活1连,10
  319. // hashMap.put("00100",19);//活1连,15
  320. // hashMap.put("00200",14);//活1连,10
  321. // hashMap.put("01000",17);//活1连,15
  322. // hashMap.put("02000",12);//活1连,10
  323. //
  324. //被堵住
  325. hashMap.put("10100",65);//眠2连,40
  326. hashMap.put("20200",60);//眠2连,30
  327. hashMap.put("01100",65);//眠2连,40
  328. hashMap.put("02200",60);//眠2连,30
  329. hashMap.put("11000",65);//眠2连,40
  330. hashMap.put("22000",60);//眠2连,30
  331. hashMap.put("21010",65);//眠2连,40
  332. hashMap.put("12020",60);//眠2连,30
  333. hashMap.put("20110",65);//眠2连,40
  334. hashMap.put("10220",60);//眠2连,30
  335. hashMap.put("21100",65);//眠2连,40
  336. hashMap.put("12200",60);//眠2连,30
  337. // hashMap.put("01010",75);//活2连,40
  338. // hashMap.put("02020",70);//活2连,30
  339. // hashMap.put("00110",75);//活2连,40
  340. // hashMap.put("00220",70);//活2连,30
  341. // hashMap.put("01100",75);//活2连,40
  342. // hashMap.put("02200",70);//活2连,30
  343. // hashMap.put("11000",75);//活2连,40
  344. // hashMap.put("00022",70);//活2连,30
  345. ////被堵住
  346. hashMap.put("11100",150);//眠3连,100
  347. hashMap.put("22200",140);//眠3连,80
  348. hashMap.put("21110",150);//眠3连,100
  349. hashMap.put("12220",140);//眠3连,80
  350. /
  351. // hashMap.put("10110",1000);//活3连,130
  352. // hashMap.put("20220",800);//活3连,110
  353. // hashMap.put("11010",1000);//活3连,130
  354. // hashMap.put("22020",800);//活3连,110
  355. // hashMap.put("01110", 1000);//活3连
  356. // hashMap.put("02220", 800);//活3连
  357. hashMap.put("11110",3000);//4连,300
  358. hashMap.put("11112",3000);//4连,300
  359. hashMap.put("22220",3500);//4连,280
  360. hashMap.put("22221",3500);//4连,280
  361. int a;
  362. int b;
  363. for(int y=0;y<line;y++) {
  364. for(int x=0;x<line;x++) {
  365. if(qizilarry[x][y]==null) {
  366. //向左
  367. a=x;
  368. for(int i=1;i<6;i++) {
  369. a--;
  370. if(a<0)
  371. break;
  372. if(qizilarry[a][y]!=null) {
  373. if(qizilarry[a][y].getColor()==c) {
  374. zuo+='2';
  375. }else
  376. zuo+='1';
  377. }else
  378. zuo+=0;
  379. }
  380. Integer integer=hashMap.get(zuo);
  381. if(integer!=null)
  382. chessValue[x][y]+=integer;
  383. //向右
  384. a=x;
  385. for(int i=1;i<6;i++) {
  386. a++;
  387. if(a==line)
  388. break;
  389. if(qizilarry[a][y]!=null) {
  390. if(qizilarry[a][y].getColor()==c) {
  391. you+='2';
  392. }else
  393. you+='1';
  394. }else
  395. you+=0;
  396. }
  397. integer=hashMap.get(you);
  398. if(integer!=null)
  399. chessValue[x][y]+=integer;
  400. //向上
  401. b=y;
  402. for(int i=1;i<6;i++) {
  403. b--;
  404. if(b<0)
  405. break;
  406. if(qizilarry[x][b]!=null) {
  407. if(qizilarry[x][b].getColor()==c) {
  408. shang+='2';
  409. }else
  410. shang+='1';
  411. }else
  412. shang+=0;
  413. }
  414. integer=hashMap.get(shang);
  415. if(integer!=null)
  416. chessValue[x][y]+=integer;


b=y;
for(int i=1;i<6;i++) {
b++;
if(b==line)
break;
if(qizilarry[x][b]!=null) {
if(qizilarry[x][b].getColor()==c) {
xia+=’2’;
}else
xia+=’1’;
}else
xia+=0;
}
integer=hashMap.get(xia);
if(integer!=null)
chessValue[x][y]+=integer;

  1. //向左上
  2. a=x;
  3. b=y;
  4. for(int i=1;i<6;i++) {
  5. a--;
  6. b--;
  7. if(a<0||b<0)
  8. break;
  9. if(qizilarry[a][b]!=null) {
  10. if(qizilarry[a][b].getColor()==c) {
  11. zuoshang+='2';
  12. }else
  13. zuoshang+='1';
  14. }else
  15. zuoshang+=0;
  16. }
  17. integer=hashMap.get(zuoshang);
  18. if(integer!=null)
  19. chessValue[x][y]+=integer;
  20. //向右下
  21. a=x;
  22. b=y;
  23. for(int i=1;i<6;i++) {
  24. a++;
  25. b++;
  26. if(a==line||b==line)
  27. break;
  28. if(qizilarry[a][b]!=null) {
  29. if(qizilarry[a][b].getColor()==c) {
  30. youxia+='2';
  31. }else
  32. youxia+='1';
  33. }else
  34. youxia+=0;
  35. }
  36. integer=hashMap.get(youxia);
  37. if(integer!=null)
  38. chessValue[x][y]+=integer;
  39. //向左下
  40. a=x;
  41. b=y;
  42. for(int i=1;i<6;i++) {
  43. a--;
  44. b++;
  45. if(a<0||b==line)
  46. break;
  47. if(qizilarry[a][b]!=null) {
  48. if(qizilarry[a][b].getColor()==c) {
  49. zuoxia+='2';
  50. }else
  51. zuoxia+='1';
  52. }else
  53. zuoxia+=0;
  54. }
  55. integer=hashMap.get(zuoxia);
  56. if(integer!=null)
  57. chessValue[x][y]+=integer;
  58. //向右上
  59. a=x;
  60. b=y;
  61. for(int i=1;i<6;i++) {
  62. a++;
  63. b--;
  64. if(a==line||b<0)
  65. break;
  66. if(qizilarry[a][b]!=null) {
  67. if(qizilarry[a][b].getColor()==c) {
  68. youshang+='2';
  69. }else
  70. youshang+='1';
  71. }else
  72. youshang+=0;
  73. }
  74. integer=hashMap.get(youshang);
  75. if(integer!=null)
  76. chessValue[x][y]+=integer;
  77. zuo="";
  78. you="";
  79. shang="";
  80. xia="";
  81. zuoshang="";
  82. zuoxia="";
  83. youshang="";
  84. youxia="";
  85. }
  86. }
  87. }
  88. }
  89. @Override
  90. public void actionPerformed(ActionEvent e) {
  91. count1=0;
  92. count2=0;
  93. countS=0;
  94. countX=0;
  95. countZS=0;
  96. countZX=0;
  97. countYS=0;
  98. countYX=0;
  99. if("重新开始".equals(e.getActionCommand())) {
  100. inte=0;
  101. for(int ix=0;ix<line;ix++) {//设置所有棋子为null
  102. for(int iy=0;iy<line;iy++) {
  103. if(qizilarry[ix][iy]!=null) {
  104. qizilarry[ix][iy]=null;
  105. }
  106. }
  107. }
  108. value=JOptionPane.showOptionDialog(jFrame, j, "提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
  109. null, moshi, null);
  110. value2=JOptionPane.showOptionDialog(jFrame, j2, "提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
  111. null, co, null);
  112. if(value==JOptionPane.CLOSED_OPTION||value2==JOptionPane.CLOSED_OPTION) {
  113. System.exit(1);
  114. }
  115. if(value2==1) {
  116. c=Color.black;
  117. c2=Color.white;
  118. }else {
  119. c=Color.white;
  120. c2=Color.black;
  121. }
  122. a=true;
  123. jFrame.repaint();
  124. }else {
  125. if(qizilarry[prex][prey]==null) {//如果上一步棋子为null了
  126. JLabel jLabel1 =new JLabel("只能悔一步棋!");
  127. JOptionPane.showMessageDialog(jFrame, jLabel1, "提示!", JOptionPane.WARNING_MESSAGE);
  128. }else {
  129. qizilarry[prex][prey]=null;
  130. inte--;
  131. if(value==1) {
  132. qizilarry[cx][cy]=null;
  133. inte--;
  134. }else {
  135. if(color ==c)
  136. a=true;
  137. else
  138. a=false;
  139. }
  140. jFrame.repaint();

qizi.java:
package com.lxr.wzq1230;

  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import jicheng.boss;
  4. public class qizi implements CS{
  5. Graphics g;
  6. Color color;
  7. int x;
  8. int y;

public qizi(Graphics g,Color color,int x,int y) {
this.g=g;
this.color=color;
this.x=x;
this.y=y;
}

  1. //获取x坐标
  2. public int getX() {
  3. return x;
  4. }
  5. //获取y坐标
  6. public int getY() {
  7. return y;
  8. }
  9. //获取颜色
  10. public Color getColor() {
  11. return color;
  12. }
  13. public void drawq(){
  14. g.setColor(color);
  15. g.fillOval(x0+x*size-size/2, y0+y*size-size/2, size, size);

更多相关文章

  1. android 五子棋之小白java
  2. Android之自定义五子棋View
  3. Android开发-一个简单的五子棋游戏
  4. Android五子连珠
  5. Android(安卓)UI详解之动态布局
  6. Android开发-一个简单的五子棋游戏
  7. C语言之三字棋的实现及扩展
  8. 扫雷游戏
  9. 2021-02-22:一个象棋的棋盘,然后把整个棋盘放入第一象限,棋盘的最左

随机推荐

  1. Android Material Design之CoordinatorLa
  2. Android之蓝牙设备使用
  3. 安卓课程二十一 SeekBar拖动控件的使用
  4. Android 获取包名,版本信息
  5. Android attrs文件(自定义)属性详解
  6. android中的spinner动态加载内容
  7. android8.0屏蔽状态栏下拉
  8. Qt for Android Flutter - AAPT: error:
  9. Android 创建悬停通知栏
  10. android之hardwareAccelerated你不知道的