//本次添加了判断输赢的功能,悔棋的功能,棋子重绘的功能,还有对棋盘刷新的优化,对棋子外形的优化 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.util.Collection; import javax.swing.*; public class GoListener implements MouseListener, ActionListener { int X = 50, Y = 50, SIZE = 46, ROW = 15, COL = 15; Graphics g; int chessFlag = 0; //创建二维数组,对棋盘上下的棋子进行一个记录 int[][] arr = new int[16][16]; //创建一维数组,对下的棋子进行一个存储 Chess[] chesslist = new Chess[16 * 16]; int chessindex = 0; public void mouseClicked(MouseEvent e) { } ; public void actionPerformed(ActionEvent e) { System.out.println("按钮被点击了"); String btnStr = e.getActionCommand(); JButton btn = (JButton) e.getSource(); if (btnStr.equals("开始游戏")) { chessFlag = 1; btn.setText("结束游戏"); } else if (btnStr.equals("结束游戏")) { chessFlag = 0; arr = new int[16][16]; paintchess(g); chessindex = 0; chesslist = new Chess[16 * 16]; btn.setText("开始游戏"); } //增加的悔棋按钮,先判断游戏是否开始,再判断游戏内棋子数量是否可以进行悔棋 else if (btnStr.equals("悔棋")) { if (chessFlag == 0) { JOptionPane.showMessageDialog(null, "游戏未开始,不能悔棋"); return; } if (chessindex < 1) { JOptionPane.showMessageDialog(null, "没有棋子可以悔棋"); return; } Chess chess = chesslist[chessindex - 1]; arr[chess.H][chess.S] = 0; chesslist[chessindex - 1] = null; chessindex--; chessFlag = chess.chessFlag; paintchess(g); } ; } @Override public void mousePressed(MouseEvent e) { if (chessFlag == 0) { System.out.println("游戏未开始"); JOptionPane.showMessageDialog(null, "游戏未开始"); return; } int x = e.getX(); int y = e.getY(); x = (x - X + SIZE / 2) / SIZE; int H = x; x = X / 2 + x * SIZE + 4; y = (y - Y + SIZE / 2) / SIZE; int S = y; y = Y / 2 + y * SIZE + 4; if (H < 0 || H > 15 || S < 0 || S > 15) { System.out.println("此处不能下棋"); JOptionPane.showMessageDialog(null, "此处不能下棋"); return; } if (arr[H][S] != 0) { JOptionPane.showMessageDialog(null, "此处已有棋子"); return; } //对棋子的值进行一个赋值(注意要新创建一个class来存储chess的信息) Chess chess = new Chess(); chess.H = H; chess.S = S; chess.chessFlag = chessFlag; chesslist[chessindex] = chess; chessindex++; //for循环绘制3D棋子 if (chessFlag == 1) { arr[H][S] = chessFlag; for (int i = 0; i < 42; i++) { Color colorblack = new Color(i * 6, i * 6, i * 6); g.setColor(colorblack); g.fillOval(x + i / 2, y + i / 2, 42 - i, 42 - i); } chessFlag = 2; } else if (chessFlag == 2) { arr[H][S] = chessFlag; for (int i = 0; i < SIZE; i++) { Color colorwhite = new Color(210 + i, 210 + i, 210 + i); g.setColor(colorwhite); g.fillOval(x + i / 2, y + i / 2, 42 - i, 42 - i); } chessFlag = 1; } //调用判断胜利的方法 boolean isWin = IsWin.juge(H, S, arr); if (isWin) { chessFlag = 0; } } //创建新方法对棋盘,棋子进行重绘 public void paintchess(Graphics g) { Color c2 = new Color(246, 150, 31); g.setColor(c2); g.fillRect(0, 0, 1000, 1000); g.setColor(Color.BLACK); for (int i = 0; i < 16; i++) { g.drawLine(X, Y + i * SIZE, X + SIZE * COL, Y + i * SIZE); g.drawLine(X + i * SIZE, Y, X + i * SIZE, Y + SIZE * ROW); } ; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { int n = arr[i][j]; if (n != 0) { int chessY = j * SIZE + X / 2 + 4; int chessX = i * SIZE + Y / 2 + 4; if (n == 1) { for (int a = 0; a < 42; a++) { Color colorblack = new Color(a * 6, a * 6, a * 6); g.setColor(colorblack); g.fillOval(chessX + a / 2, chessY + a / 2, 42 - a, 42 - a); } } else if (n == 2) { for (int b = 0; b < SIZE; b++) { Color colorwhite = new Color(210 + b, 210 + b, 210 + b); g.setColor(colorwhite); g.fillOval(chessX + b / 2, chessY + b / 2, 42 - b, 42 - b); } } } } } } public void mouseReleased(MouseEvent e) { } ; public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } ; }
import javax.swing.*; //对棋盘上的棋子进行一个判断,有五个就退出就胜利,用for循环对各方向上的棋子进行数量统计 public class IsWin { public static int hengxiang(int H, int S, int[][] chessArr){ int count = 0; int c1 = chessArr[H][S]; for(int i = S + 1; i < chessArr[0].length; i++){ int cn = chessArr[H][i]; if(c1 == cn){ count++; } else{ break; }} for(int i = S - 1; i >= 0; i--){ int cn = chessArr[H][i]; if(c1 == cn){ count++; } else{ break; }} count++; return count;} public static int shuxiang(int H,int S,int[][] chessArr){ int count=0; int c1=chessArr[H][S]; for(int i = H + 1; i < chessArr.length; i++){ int cn=chessArr[i][S]; if(c1 == cn){ count++; }else { break; }} for(int i = H - 1; i >=0; i--){ int cn=chessArr[i][S]; if(c1 == cn){ count++; }else { break; }} count++; return count;} public static int zuoshangyouxia(int H,int S,int[][] chessArr){ int count=0; int c1=chessArr[H][S]; for (int i=H+1, j=S+1;i<chessArr[0].length && j<chessArr.length;i++,j++){ int cn=chessArr[i][j]; if (c1 == cn) { count++; }else { break; }} for (int i=H-1, j=S-1; i>=0&&j>=0;i--,j--){ int cn=chessArr[i][j]; if (c1 == cn) { count++; }else { break; }} count++; return count; } public static int youshangzuoxia(int H,int S,int[][] chessArr){ int count=0; int c1=chessArr[H][S]; for (int i=H-1, j=S+1;i>=0&&j<chessArr.length;i--,j++){ int cn=chessArr[i][j]; if (c1 == cn) { count++; }else { break; }} for (int i=H+1,j=S-1; i<chessArr[0].length&&j>=0;i++,j--){ int cn=chessArr[i][j]; if (c1 == cn) { count++; }else { break; }} count++; return count;} public static boolean juge(int H, int S, int[][] chessArr){ int c1 = chessArr[H][S];// 取出最后下的棋子 int hnum = hengxiang (H, S, chessArr); int srnum = shuxiang (H, S, chessArr); int lnum=zuoshangyouxia(H, S, chessArr); int rnum=youshangzuoxia(H, S, chessArr); if(hnum >= 5 || srnum >= 5||lnum>=5||rnum>=5){ if(c1 == 1){ System.out.println ("黑棋胜利"); JOptionPane.showMessageDialog (null, "黑棋胜利"); } else{ System.out.println ("白棋胜利"); JOptionPane.showMessageDialog (null, "白棋胜利"); } return true; } else{ return false; }}}
import javax.swing.*; import java.awt.*; public class goUI extends JPanel { GoListener gol = new GoListener(); int X = 50, Y = 50, SIZE = 46, ROW = 15, COL = 15; Graphics g; public void showUI() { JFrame pang = new JFrame(); pang.setTitle("AI五子棋"); pang.setSize(900, 800); pang.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); pang.setLocationRelativeTo(null); Color c1 = new Color(113, 154, 255); Color c2 = new Color(246, 150, 31); JPanel btnpane = new JPanel(); btnpane.setBackground(c1); Dimension dimW = new Dimension(125, 0); btnpane.setPreferredSize(dimW); JPanel chesspane = this; chesspane.setBackground(c2); JButton btn1 = new JButton("开始游戏"); btn1.setBounds(20, 20, 100, 35); btnpane.add(btn1); btn1.addActionListener(gol); JButton btn2=new JButton("悔棋"); btn2.setBounds(20, 20, 100, 35); btnpane.add(btn2); btn2.addActionListener(gol); pang.add(btnpane, BorderLayout.WEST); pang.add(chesspane, BorderLayout.CENTER); pang.setVisible(true); chesspane.addMouseListener(gol); gol.g = chesspane.getGraphics(); } //paint语句对界面进行刷新 public void paint(Graphics g) { super.paint(g); gol.paintchess(g); } public static void main (String[]args){ goUI go = new goUI(); go.showUI(); } }
//悔棋前
//悔棋后
//游戏结束胜利界面