当前位置:   article > 正文

JAVA五子棋AI(人机对战 颜色选择 悔棋等 可直接使用)

五子棋ai

        五子棋是全国智力运动会竞技项目之一,容易上手,老少皆宜,而且趣味横生,引人入胜,不仅能增强思维能力,提高智力,而且富含哲理,有助于修身养性。当然,用 JAVA 语言编写五子棋 AI 小游戏也是一件非常有意思的事:

先上效果图:

一、五子棋规则

五子棋的基本规则如下:
(1) 棋盘:采用国际上标准的 15×15 路线的正方形棋盘。
(2)下法:两人分别执黑白两色棋子,轮流在棋盘上选择一个无子的交叉点走子,无子的交叉点又称为空点,规定由黑方先行走棋。
(3)输赢判断:黑、白双方有一方的五个棋子在衡、竖或斜的方向上联接成无间断的一条线即为该方赢。

二、思路分析

(1)下五子棋首先需要一个棋盘,国际标准是 15×15 路线,为了增加趣味性,我们在编写程序时可以使用不同线路的棋盘。

(2)用户在开始游戏前可进行相关选择,如:模式选择(人人对战、人机对战),颜色选择(用户要黑棋、用户要白棋),下棋顺序(黑棋先行、白棋先行),并且设置有“开始游戏”、“重新开始”、“认输结束”、“悔棋”等按钮。

(3)开始游戏后,用户在棋盘上点击相应的位置就可以下棋子(人人模式黑白子交替出现,人机模式用户在下完一个子后电脑也会自动下一个子),且每下一步棋系统需要判断横竖斜这几个方向上是否有连续相同颜色的五个棋子。

(4)在游戏过程中,当用户改变界面大小和拖动界面,即调用 paint 函数时棋盘和棋子不会消失。

简简单单几个步骤,顿时感觉难度也不高,下面开工!let's go !!!

三、代码详解

        首先,我们需要画一个棋盘,new 一个 ImageUI 类(当然也可以取其他名字),在这个类中,我们需要把五子棋的界面画出来,为了方便管理,在 JFrame 上设置了两个 JPanel,一个JPanel 里放 ImagePanel,用于棋盘和棋子的绘制,另一个JPanel 里放相关按钮。

        为了能够实现模式选择等单选功能,这边使用了单选按钮 JRadioButton,并打包放入一个 group 中,由于按钮紧挨在一起不美观,我们在部分按钮后面增加了一个空的 JLabel。

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionListener;
  4. public class ImageUI extends JFrame implements Config{
  5. ImageListener ul = new ImageListener();
  6. ImagePanel imgPanel = new ImagePanel();
  7. public static void main(String[] args){
  8. new ImageUI();
  9. }
  10. ImageUI(){
  11. initUI();
  12. }
  13. private void initUI(){
  14. JFrame jf = frameInitUI();
  15. JPanel[] jps = addPanel(jf);
  16. addButton(jps,ul);
  17. jf.setVisible(true);
  18. Graphics gr = imgPanel.getGraphics();
  19. ul.g = gr;
  20. ul.imgPanel = imgPanel;
  21. imgPanel.qz = ul.qz;
  22. imgPanel.addMouseListener(ul);
  23. }
  24. private void addButton(JPanel[] jps, ActionListener al) {
  25. JLabel j0 = new JLabel(" ");
  26. jps[1].add(j0);
  27. String[] btnstrs = new String[]{"开始游戏","重新开始","认输结束","悔棋一步",
  28. " 模式 ","人人对战","人机对战"," 颜色 ","我要黑棋","我要白棋",
  29. " 顺序 ","黑棋先行","白棋先行"};
  30. for (int i = 0; i < 4; i++){
  31. JButton btn = new JButton(btnstrs[i]);
  32. Dimension dim = new Dimension(120,40);
  33. btn.setPreferredSize(dim);
  34. btn.setFont(new Font("黑体",1,20));
  35. btn.setBackground(Color.WHITE);
  36. btn.addActionListener(ul);
  37. jps[1].add(btn);
  38. }
  39. JLabel j1 = new JLabel(" ");
  40. jps[1].add(j1);
  41. JLabel j2 = new JLabel(btnstrs[4]);
  42. j2.setFont(new Font("楷体",1,30));
  43. jps[1].add(j2);
  44. ButtonGroup group1 = new ButtonGroup();
  45. for (int i = 5; i < 7; i++){
  46. JRadioButton jrb = new JRadioButton(btnstrs[i]);
  47. jrb.setPreferredSize(new Dimension(120,40));
  48. jrb.setFont(new Font("黑体",1,20));
  49. jrb.setSelected(true);
  50. jrb.setBackground(Color.YELLOW);
  51. jrb.addActionListener(ul);
  52. group1.add(jrb);
  53. jps[1].add(jrb);
  54. }
  55. JLabel j3 = new JLabel(" ");
  56. jps[1].add(j3);
  57. JLabel j4 = new JLabel(btnstrs[7]);
  58. j4.setFont(new Font("楷体",1,30));
  59. jps[1].add(j4);
  60. ButtonGroup group2 = new ButtonGroup();
  61. for (int i = 8; i < 10; i++){
  62. JRadioButton jrb = new JRadioButton(btnstrs[i]);
  63. jrb.setPreferredSize(new Dimension(120,40));
  64. jrb.setFont(new Font("黑体",1,20));
  65. jrb.setSelected(true);
  66. jrb.setBackground(Color.YELLOW);
  67. jrb.addActionListener(ul);
  68. group2.add(jrb);
  69. jps[1].add(jrb);
  70. }
  71. JLabel j5 = new JLabel(" ");
  72. jps[1].add(j5);
  73. JLabel j6 = new JLabel(btnstrs[10]);
  74. j6.setFont(new Font("楷体",1,30));
  75. jps[1].add(j6);
  76. ButtonGroup group3 = new ButtonGroup();
  77. for (int i = 11; i < 13; i++){
  78. JRadioButton jrb = new JRadioButton(btnstrs[i]);
  79. jrb.setPreferredSize(new Dimension(120,40));
  80. jrb.setFont(new Font("黑体",1,20));
  81. jrb.setSelected(true);
  82. jrb.setBackground(Color.YELLOW);
  83. jrb.addActionListener(ul);
  84. group3.add(jrb);
  85. jps[1].add(jrb);
  86. }
  87. }
  88. private JPanel[] addPanel(JFrame jf) {
  89. JPanel[] jps = new JPanel[2];
  90. String[] layoustrs = {BorderLayout.CENTER,BorderLayout.EAST};
  91. imgPanel.setBackground(Color.LIGHT_GRAY);
  92. jf.add(imgPanel,layoustrs[0]);
  93. jps[0] = imgPanel;
  94. Dimension dim = new Dimension(200,0);
  95. JPanel jp_s = new JPanel();
  96. jp_s.setBackground(Color.ORANGE);
  97. jp_s.setPreferredSize(dim);
  98. jf.add(jp_s,layoustrs[1]);
  99. jps[1] = jp_s;
  100. return jps;
  101. }
  102. private JFrame frameInitUI() {
  103. setTitle("五子棋AI");
  104. setSize(X0+SIZE*LINE+200,Y0+SIZE*LINE+SIZE);
  105. setLocationRelativeTo(null);
  106. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  107. return this;
  108. }
  109. }

        界面画好后,我们需要考虑下棋了,new 一个 ImageListener 类:

  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.MouseEvent;
  4. public class ImageListener extends Listen implements Config{
  5. public Graphics g = null;
  6. private int count = 0; //偶数黑棋 奇数白棋
  7. private int index = 0; //0无棋 1黑棋 2白棋
  8. private int m,n; //第m行 第n列
  9. private String name = null;
  10. private String option = null;
  11. public int[][] qz = new int[LINE][LINE];
  12. public int[][] score = new int[LINE][LINE];
  13. public int[] cc = new int[4];
  14. public int goalX = 0;
  15. public int goalY = 0;
  16. public int mark = 0;
  17. public int p = 1;
  18. Boolean moshi = true;
  19. Boolean yanse = true;
  20. Boolean shunxu = true;
  21. Boolean renji = true;
  22. Boolean same = true;
  23. ImagePanel imgPanel = new ImagePanel();
  24. Login lo = new Login();
  25. @Override
  26. public void actionPerformed(ActionEvent e){
  27. System.out.println("按钮被调用");
  28. name = e.getActionCommand();
  29. if("人人对战".equals(name)) moshi = true;
  30. if("人机对战".equals(name)) moshi = false;
  31. if("我要黑棋".equals(name)) yanse = true;
  32. if("我要白棋".equals(name)) yanse = false;
  33. if("黑棋先行".equals(name)) shunxu = true;
  34. if("白棋先行".equals(name)) shunxu = false;
  35. if (moshi && shunxu) {
  36. count = 2;
  37. }
  38. if (moshi && !shunxu) {
  39. count = 1;
  40. }
  41. if(!moshi && yanse && shunxu){ //人先出黑
  42. count = 2;
  43. }
  44. if(!moshi && yanse && !shunxu){ //机先出白
  45. count = 2;
  46. }
  47. if(!moshi && !yanse && shunxu){ //机先出黑
  48. count = 1;
  49. }
  50. if(!moshi && !yanse && !shunxu){ //人先出白
  51. count = 1;
  52. }
  53. if("认输结束".equals(name)){
  54. option = "认输结束";
  55. lo.showU(option);
  56. }
  57. if("悔棋一步".equals(name)){
  58. cancel(qz);
  59. }
  60. if("开始游戏".equals(name)){
  61. same = true;
  62. for(int i = 0; i < 4; i++){
  63. cc[i] = 0;
  64. }
  65. for(int i = 0; i < LINE; i++){
  66. for(int j = 0; j < LINE; j++){
  67. qz[i][j] = 0;
  68. }
  69. }
  70. if(!moshi && yanse && !shunxu) {
  71. cc[2] = LINE/2;
  72. cc[3] = LINE/2;
  73. qz[LINE/2][LINE/2] = 2;
  74. }
  75. if(!moshi && !yanse && shunxu) {
  76. cc[2] = LINE/2;
  77. cc[3] = LINE/2;
  78. qz[LINE/2][LINE/2] = 1;
  79. }
  80. imgPanel.paint(g);
  81. option = "开始游戏";
  82. lo.showU(option);
  83. System.out.println("我去调用他");
  84. }
  85. if("重新开始".equals(name)){
  86. same = true;
  87. for(int i = 0; i < LINE; i++){
  88. for(int j = 0; j < LINE; j++){
  89. qz[i][j] = 0;
  90. }
  91. }
  92. imgPanel.paint(g);
  93. }
  94. }
  95. @Override
  96. public void mousePressed(MouseEvent e){
  97. if(renji) {
  98. cc[0] = cc[2];
  99. cc[1] = cc[3];
  100. }
  101. }
  102. @Override
  103. public void mouseReleased(MouseEvent e){
  104. System.out.println("释放被调用");
  105. if(renji) {
  106. int x = e.getX();
  107. int y = e.getY();
  108. if (count % 2 == 0) {
  109. index = 1;
  110. }
  111. if (count % 2 == 1) {
  112. index = 2;
  113. }
  114. if ((x > (X0 - SIZE / 2)) && (x < (X0 - SIZE / 2 + SIZE * LINE)) &&
  115. (y > (Y0 - SIZE / 2)) && (y < (Y0 - SIZE / 2 + SIZE * LINE))) {
  116. m = (x - X0 + SIZE / 2) / SIZE;
  117. n = (y - Y0 + SIZE / 2) / SIZE;
  118. if (qz[m][n] == 0) {
  119. cc[2] = m;
  120. cc[3] = n;
  121. qz[m][n] = index;
  122. count++;
  123. imgPanel.drawqz(qz, g);
  124. judge(qz);
  125. System.out.println("判断被调用");
  126. same = true;
  127. } else {
  128. option = "同一位置";
  129. lo.showU(option);
  130. same = false;
  131. }
  132. System.out.println("m=" + m + ";n=" + n);
  133. }
  134. try {
  135. Thread.sleep(500);
  136. } catch (InterruptedException ex) {
  137. ex.printStackTrace();
  138. }
  139. if (same && p < 5) {
  140. if (!moshi) {
  141. renji = false;
  142. AI(qz);
  143. if (yanse) qz[goalX][goalY] = 2;
  144. if (!yanse) qz[goalX][goalY] = 1;
  145. imgPanel.drawqz(qz, g);
  146. count++;
  147. cc[0] = cc[2];
  148. cc[1] = cc[3];
  149. cc[2] = goalX;
  150. cc[3] = goalY;
  151. judge(qz);
  152. }
  153. }
  154. }
  155. }
  156. private void judge(int[][] qz){
  157. //判断|情形
  158. for(int i = 0; i < LINE; i++){
  159. for(int j = 0; j < LINE-1; j++){
  160. if(qz[i][j] == qz[i][j+1] && qz[i][j] != 0){
  161. p++;
  162. if(p > 4 && qz[i][j] == 1) {
  163. option = "黑棋胜";
  164. lo.showU(option);
  165. // same = false;
  166. return;
  167. }
  168. if(p > 4 && qz[i][j] == 2) {
  169. option = "白棋胜";
  170. lo.showU(option);
  171. // same = false;
  172. return;
  173. }
  174. }
  175. if(qz[i][j] != qz[i][j+1]){
  176. p = 1;
  177. }
  178. }
  179. }
  180. //判断——情形
  181. for(int j = 0; j < LINE; j++){
  182. for(int i = 0; i < LINE-1; i++){
  183. if(qz[i][j] == qz[i+1][j] && qz[i][j] != 0){
  184. p++;
  185. if(p > 4 && qz[i][j] == 1) {
  186. option = "黑棋胜";
  187. lo.showU(option);
  188. // same = false;
  189. }
  190. if(p > 4 && qz[i][j] == 2) {
  191. option = "白棋胜";
  192. lo.showU(option);
  193. // same = false;
  194. return;
  195. }
  196. }
  197. if(qz[i][j] != qz[i+1][j]){
  198. p = 1;
  199. }
  200. }
  201. }
  202. //判断\情形
  203. for(int i = 0; i < LINE-4; i++){
  204. for(int j = 0; j < LINE-4; j++){
  205. int m = i;
  206. int n = j;
  207. for(int cishu = 0; cishu < 5; cishu++) {
  208. if (qz[m][n] == qz[m + 1][n + 1] && qz[m][n] != 0) {
  209. p++;
  210. m++;
  211. n++;
  212. if (p > 4 && qz[m][n] == 1) {
  213. System.out.println("调用黑棋胜1");
  214. option = "黑棋胜";
  215. lo.showU(option);
  216. // same = false;
  217. return;
  218. }
  219. if (p > 4 && qz[m][n] == 2) {
  220. System.out.println("调用白棋胜1");
  221. option = "白棋胜";
  222. lo.showU(option);
  223. // same = false;
  224. return;
  225. }
  226. }
  227. if (qz[m][n] != qz[m + 1][n + 1]) {
  228. p = 1;
  229. }
  230. }
  231. }
  232. }
  233. //判断/情形
  234. for(int i = 4; i < LINE; i++){
  235. for(int j = 0; j < LINE-4; j++){
  236. int m = i;
  237. int n = j;
  238. for(int cishu = 0; cishu < 5; cishu++) {
  239. if (qz[m][n] == qz[m - 1][n + 1] && qz[m][n] != 0) {
  240. p++;
  241. m--;
  242. n++;
  243. if (p > 4 && qz[m][n] == 1) {
  244. System.out.println("调用黑棋胜2");
  245. option = "黑棋胜";
  246. lo.showU(option);
  247. // same = false;
  248. return;
  249. }
  250. if (p > 4 && qz[m][n] == 2) {
  251. System.out.println("调用白棋胜2");
  252. option = "白棋胜";
  253. lo.showU(option);
  254. // same = false;
  255. return;
  256. }
  257. }
  258. if (qz[m][n] != qz[m - 1][n + 1]) {
  259. p = 1;
  260. }
  261. }
  262. }
  263. }
  264. }
  265. private void cancel(int[][] qz){
  266. qz[cc[0]][cc[1]] = 0;
  267. qz[cc[2]][cc[3]] = 0;
  268. imgPanel.paint(g);
  269. }
  270. private void AI(int[][] qz){
  271. for(int i = 0; i < LINE; i++){
  272. for(int j = 0; j < LINE; j++){
  273. score[i][j] = 0;
  274. }
  275. }
  276. int blackChessNum = 0;
  277. int whiteChessNum = 0;
  278. //扫描|
  279. for(int i = 0; i < LINE; i++){
  280. System.out.println("|被扫描了");
  281. for(int j = 0; j < LINE-4; j++){
  282. int k = j;
  283. while(k < j + 5){
  284. if(qz[i][k] == 1) blackChessNum++;
  285. if(qz[i][k] == 2) whiteChessNum++;
  286. k++;
  287. }
  288. mark = weight(blackChessNum, whiteChessNum);
  289. for(k = j; k < j + 5; k++){
  290. if(qz[i][k] == 0) score[i][k] += mark;
  291. }
  292. blackChessNum = 0;
  293. whiteChessNum = 0;
  294. mark = 0;
  295. }
  296. }
  297. //扫描——
  298. for(int j = 0; j < LINE; j++){
  299. System.out.println("——被扫描了");
  300. for(int i = 0; i < LINE-4; i++){
  301. int k = i;
  302. while(k < i + 5){
  303. if(qz[k][j] == 1) blackChessNum++;
  304. if(qz[k][j] == 2) whiteChessNum++;
  305. k++;
  306. }
  307. mark = weight(blackChessNum, whiteChessNum);
  308. for(k = i; k < i + 5; k++){
  309. if(qz[k][j] == 0) score[k][j] += mark;
  310. }
  311. blackChessNum = 0;
  312. whiteChessNum = 0;
  313. mark = 0;
  314. }
  315. }
  316. //扫描\
  317. for(int i = 0; i < LINE-4; i++){
  318. System.out.println("\\被扫描了");
  319. for(int j = 0; j < LINE-4; j++){
  320. int k = 0;
  321. while(k < 5){
  322. if(qz[i+k][j+k] == 1) blackChessNum++;
  323. if(qz[i+k][j+k] == 2) whiteChessNum++;
  324. k++;
  325. }
  326. mark = weight(blackChessNum, whiteChessNum);
  327. for(k = 0; k < 5; k++){
  328. if(qz[i+k][j+k] == 0) score[i+k][j+k] += mark;
  329. }
  330. blackChessNum = 0;
  331. whiteChessNum = 0;
  332. mark = 0;
  333. }
  334. }
  335. //扫描/
  336. for(int i = 4; i < LINE; i++){
  337. System.out.println("/被扫描了");
  338. for(int j = 0; j < LINE-4; j++){
  339. int k = 0;
  340. while(k < 5){
  341. if(qz[i-k][j+k] == 1) blackChessNum++;
  342. if(qz[i-k][j+k] == 2) whiteChessNum++;
  343. k++;
  344. }
  345. mark = weight(blackChessNum, whiteChessNum);
  346. for(k = 0; k < 5; k++){
  347. if(qz[i-k][j+k] == 0) score[i-k][j+k] += mark;
  348. }
  349. blackChessNum = 0;
  350. whiteChessNum = 0;
  351. mark = 0;
  352. }
  353. }
  354. //从空位置中找到得分最大的位置
  355. int scoremax = 0;
  356. for(int j = 0; j < LINE; j++){
  357. System.out.println("");
  358. for(int i = 0; i < LINE; i++){
  359. System.out.print(score[i][j]+";");
  360. if(scoremax < score[i][j]){
  361. scoremax = score[i][j];
  362. goalX = i;
  363. goalY = j;
  364. }
  365. }
  366. }
  367. renji = true;
  368. }
  369. private int weight(int blackChessNum,int whiteChessNum) {
  370. System.out.println("blackChessNum="+blackChessNum+";whiteChessNum="+whiteChessNum);
  371. if(blackChessNum > 0 && whiteChessNum > 0) mark = 0;
  372. if(blackChessNum == 0 && whiteChessNum == 0) mark = 7;
  373. if(blackChessNum == 1 && whiteChessNum == 0 && yanse) mark = 15;
  374. if(blackChessNum == 1 && whiteChessNum == 0 && !yanse) mark = 35;
  375. if(whiteChessNum == 1 && blackChessNum == 0 && yanse) mark = 35;
  376. if(whiteChessNum == 1 && blackChessNum == 0 && !yanse) mark = 15;
  377. if(blackChessNum == 2 && whiteChessNum == 0 && yanse) mark = 400;
  378. if(blackChessNum == 2 && whiteChessNum == 0 && !yanse) mark = 800;
  379. if(whiteChessNum == 2 && blackChessNum == 0 && yanse) mark = 800;
  380. if(whiteChessNum == 2 && blackChessNum == 0 && !yanse) mark = 400;
  381. if(blackChessNum == 3 && whiteChessNum == 0 && yanse) mark = 1800;
  382. if(blackChessNum == 3 && whiteChessNum == 0 && !yanse) mark = 15000;
  383. if(whiteChessNum == 3 && blackChessNum == 0 && yanse) mark = 15000;
  384. if(whiteChessNum == 3 && blackChessNum == 0 && !yanse) mark = 1800;
  385. if(blackChessNum == 4 && whiteChessNum == 0 && yanse) mark = 100000;
  386. if(blackChessNum == 4 && whiteChessNum == 0 && !yanse) mark = 800000;
  387. if(whiteChessNum == 4 && blackChessNum == 0 && yanse) mark = 800000;
  388. if(whiteChessNum == 4 && blackChessNum == 0 && !yanse) mark = 100000;
  389. return mark;
  390. }
  391. }

        ImageListener 类是五子棋 AI 代码的核心部分,下面从模式选择,胜负判断,悔棋三方面进行简要分析:

       模式判断:

        在人人对战中,我们用 count 计数,偶数下黑子,奇数下白子,为了实现下的第一颗棋子是黑子还是白子,可对应给 count 的初值赋 1 或 2,每下完一步棋进行胜负判断;

        在人机对战模式中,主要难点是电脑自主判断落子位置,本篇文章采用了已经公开的五元组评分算法,即对15X15的572个五元组分别评分,一个五元组的得分就是该五元组为其中每个位置贡献的分数,一个位置的分数就是其所在所有五元组分数之和,所有空位置中分数最高的那个位置就是落子位置。

        五子棋 AI 算法其实还有很多,这边再介绍一种也较广泛使用的算法,即根据特征来赋评估分值,同一特征可能在不同路上(甚至同一路上)会重复出现,一些威胁性小的特征即使出现多次,累加得分也不应该超过一个极有威胁性的特征得分。如特征“++OO++”威胁性小,特征“+OOOO+”威胁性非常大,不管前者出现多少个,原则上得分都不应该超过后者出现一个的得分,在此提供一个已有的特征评分表供参考:

        胜负判断

        在游戏过程中,我们初始化了一个和棋盘线数一样的二维数组,用于记录每一个交点的落子情况,我们规定:若该交点处没有棋子,则数组相应位置元素为0;若该交点处有一颗黑子,则数组相应位置为1;若该交点处有一颗白子,则数组相应位置为2;每下完一颗棋子,都会调用 judge 函数对该二维数组进行横向,竖向,左斜向,右斜向扫描,判断是否有连续五个1或者五个2.

        悔棋

        我们定义了一个长度为4的一维数组,分别记录倒数第二颗棋子的 x,y 坐标和最后一颗棋子的 x, y 坐标,当点击“悔棋一步”按钮时,把这两个棋子对应二维数组中的值置为0,并调用 paint 函数,则在重绘时这两颗棋子将不会绘制出来,即实现了悔棋功能!

        当界面发生改变时,会调用 paint 函数,因此我们还写了一个 ImagePanel 类,里面重写了 paint 函数。

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class ImagePanel extends JPanel implements Config{
  4. public int[][] qz = new int[LINE][LINE];
  5. public void paint(Graphics gr){
  6. super.paint(gr);
  7. drawChessTable(gr);
  8. drawqz(qz,gr);
  9. }
  10. public void drawqz(int[][] qz,Graphics gr) {
  11. System.out.println("棋子重绘");
  12. for(int i = 0; i < LINE; i++){
  13. for(int j = 0; j < LINE; j++){
  14. if(qz[i][j] == 1){
  15. System.out.println("qz["+i+"]["+j+"]="+qz[i][j]);
  16. gr.setColor(Color.BLACK);
  17. gr.fillOval(X0+i*SIZE-CHESS/2, Y0+j*SIZE-CHESS/2, CHESS, CHESS);
  18. }
  19. if(qz[i][j] == 2){
  20. System.out.println("qz["+i+"]["+j+"]="+qz[i][j]);
  21. gr.setColor(Color.WHITE);
  22. gr.fillOval(X0+i*SIZE-CHESS/2, Y0+j*SIZE-CHESS/2, CHESS, CHESS);
  23. }
  24. }
  25. }
  26. }
  27. private void drawChessTable(Graphics gr) {
  28. Graphics2D gr2 = (Graphics2D)gr;
  29. gr2.setStroke(new BasicStroke(2.0f));
  30. for(int i = 0; i < LINE; i++){
  31. gr2.drawLine(X0,SIZE*i+Y0,X0+SIZE*(LINE-1),SIZE*i+Y0);
  32. }
  33. for(int j = 0; j < LINE; j++){
  34. gr2.drawLine(X0+SIZE*j,Y0,X0+SIZE*j,Y0+SIZE*(LINE-1));
  35. }
  36. }
  37. }

        最后,为了能使这个代码能够顺利运行,还需要加上以下几个类:

  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.awt.event.MouseEvent;
  4. import java.awt.event.MouseListener;
  5. public class Listen implements MouseListener, ActionListener {
  6. @Override
  7. public void actionPerformed(ActionEvent e) {
  8. }
  9. @Override
  10. public void mouseClicked(MouseEvent e) {
  11. }
  12. @Override
  13. public void mousePressed(MouseEvent e) {
  14. }
  15. @Override
  16. public void mouseReleased(MouseEvent e) {
  17. }
  18. @Override
  19. public void mouseEntered(MouseEvent e) {
  20. }
  21. @Override
  22. public void mouseExited(MouseEvent e) {
  23. }
  24. }
  1. public interface Config {
  2. public static final int X0 = 50;
  3. public static final int Y0 = 50;
  4. public static final int SIZE = 50;
  5. public static final int LINE = 15;
  6. public static final int CHESS = 50;
  7. }
  1. public class Login {
  2. public void showU(String option){
  3. JFrame jf = new JFrame();
  4. jf.setSize(500,300);
  5. switch (option){
  6. case"黑棋胜":
  7. ImageIcon image1 = new ImageIcon("D:\\03.jpg");
  8. JLabel jl1 = new JLabel(image1);
  9. jf.add(jl1);
  10. jf.setTitle("黑棋胜");
  11. jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  12. break;
  13. case"白棋胜":
  14. ImageIcon image2 = new ImageIcon("D:\\04.jpg");
  15. JLabel jl2 = new JLabel(image2);
  16. jf.add(jl2);
  17. jf.setTitle("白棋胜");
  18. jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  19. break;
  20. case"开始游戏":
  21. ImageIcon image3 = new ImageIcon("D:\\01.jpg");
  22. JLabel jl3 = new JLabel(image3);
  23. jf.add(jl3);
  24. jf.setTitle("开始游戏");
  25. jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  26. break;
  27. case"认输结束":
  28. ImageIcon image4 = new ImageIcon("D:\\02.jpg");
  29. JLabel jl4 = new JLabel(image4);
  30. jf.add(jl4);
  31. jf.setTitle("认输结束");
  32. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33. break;
  34. case"同一位置":
  35. ImageIcon image5 = new ImageIcon("D:\\05.jpg");
  36. JLabel jl5 = new JLabel(image5);
  37. jf.add(jl5);
  38. jf.setTitle("无效棋");
  39. jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  40. break;
  41. }
  42. jf.setLocationRelativeTo(null);
  43. jf.setVisible(true);
  44. }
  45. }

在最后,献上代码中使用到的五张图片:

01:

 

02:

03:

04:

 

05:

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/353692
推荐阅读
相关标签
  

闽ICP备14008679号