《java五子棋人机.doc》由会员分享,可在线阅读,更多相关《java五子棋人机.doc(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、介绍:这是一款很简单的人机对战五子棋,只实现了简单的防守功能,适合刚开始设计五子棋的朋友参考。程序执行结果:框架程序:package wuziqi;import java.awt.BorderLayout;import java.awt.Color;import javax.swing.JFrame;/* * 五子棋 */public class MainFrame extends JFrame public static void main(String args) MainPanel panel = new MainPanel();MainFrame frame = new MainFra
2、me(五子棋);frame.setSize(680,680); panel.setBackground(Color.LIGHT_GRAY);frame.add(panel,BorderLayout.CENTER);panel.addMouseListener(panel);frame.setVisible(true);frame.setDefaultCloseOperation(3);frame.setLocationRelativeTo(null); public MainFrame() super(); public MainFrame(String str) super(str); 面板
3、设计:package wuziqi;import java.awt.*;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;/* * JFrame中的层次分布及相对关系是:最底层是:JRootPane; * 第二层是:JlayerPane;最上层就是ContentPane,也正是我们常说的内容面板。 * 所以一般我们拖放的控件就是在ContentPane层上。 */import javax.swing.JOptionPane;/* * 五子棋的面板设计 */public class MainPanel ext
4、ends Panel implements MouseListener private static final int COLUMN = 16;/ 列数private static final int ROW = 16;/ 行数private static final int GAP = 40;/ 间距private static boolean isBlack = true; / 判断是否是黑棋private static int click_X; / 点击时候的x坐标private static int click_Y; / 点击时候的y坐标private char allChess =
5、 new charROWCOLUMN; / 存下了旗子的位置public MainPanel() super();for (int i = 0; i allChess.length; i+) for (int j = 0; j allChessi.length; j+) allChessij = *; / 初始化数组allChess,*表示没有棋子存在/* 画棋盘要放在paint(Graphics g)里面 */public void paint(Graphics g) for (int i = 0; i ROW; i+) / 划横线g.setColor(Color.BLACK); / 画线为
6、黑色,g.drawLine(20, 20 + i * GAP, 640 - 20, 20 + i * GAP);/ 棋盘间隔分布for (int i = 0; i COLUMN; i+) / 划纵线g.setColor(Color.BLACK);g.drawLine(20 + i * GAP, 20, 20 + i * GAP, 640 - 20); / 棋盘间隔分布/ 画棋子 w表示白棋 b表示黑棋for (int i = 0; i allChess.length; i+) / allChess.length是行数for (int j = 0; j = 0 & allChessRLastXy
7、 = ch) RLastX-;int LRLastX=RLastX; /标记最左边的位置int RNum = 0;/ 统计横向相同的棋子数RLastX+;while (RLastX = 0 & allChessxLLastY = ch) LLastY-;int LLLastY=LLastY;/标记最上面棋盘的位置int LNum = 0;/ 统计纵向相同的棋子数LLastY+;while ( LLastY = 0 & RULastY = 0& LDLastX = 0 & LDLastY = 0 & allChessRULastXLDLastY = ch) RULastX-;LDLastY-;i
8、nt LRULastX=RULastX;/记录左上棋子的横坐标int ULDLastY=LDLastY;/记录左上棋子的横坐标int RUNum = 0;RULastX+;LDLastY+;while (RULastX = 0 & RULastX allChess.length& LDLastY allChessx.length& allChessRULastXLDLastY = ch) RULastX+;LDLastY+;RUNum+;/取最大值也就是最危险的状况int max=0;if(maxRNum) max=RNum;if(maxLNum) max=LNum;if(maxLDNum)
9、max=LDNum;if(max0)allChessx-1y=w;isBlack=true;return;elseif(allChessx+1y=*)allChessx+1y=w;isBlack=true;return;elsedo x = (int) (Math.round(float) (Math.random() * 15);y = (int) (Math.round(float) (Math.random() * 15); while(allChessxy != *); allChessxy = w;/ 令棋盘(x,y)设置为白棋 isBlack = true; isBlack=tru
10、e;return;elseif(allChessLRLastXy=*)allChessLRLastXy=w;isBlack=true;return;else if(allChessRLastXy=*)allChessRLastXy=w;isBlack=true;return;/上下判断else if(max=LNum)if(allChessxLLLastY=*)/最上方的棋盘是否是空的allChessxLLLastY=w;isBlack=true;return;else if(allChessxLLastY=*)/最下方棋盘是否为空allChessxLLastY=w;isBlack=true;
11、return;/左上右下else if(max=LDNum)if(allChessLLDLastXDRULastY=*)/最上方的棋盘是否是空的allChessLLDLastXDRULastY=w;isBlack=true;return;else if(allChessLDLastXRULastY=*)/最下方棋盘是否为空allChessLDLastXRULastY=w;isBlack=true;return;/左下右上else if(max=RUNum)if(allChessLRULastXULDLastY=*)/最上方的棋盘是否是空的allChessLRULastXULDLastY=w;i
12、sBlack=true;return;else if(allChessRULastXLDLastY=*)/最下方棋盘是否为空allChessRULastXLDLastY=w;isBlack=true;return;/ 判断是否为5个相同的棋子,是返回true,否返回falsepublic boolean isWin(int x, int y, boolean isColor) char ch = allChessxy;/* 横向判断 */int RLastX = x;/ 横向判断是否到达5个相同的棋子,先向左回溯相同颜色,再向右搜索是否有连续5个while (RLastX = 0 & allC
13、hessRLastXy = ch) RLastX-;int RNum = 0;/ 统计横向相同的棋子数RLastX+;int itemRLastx = RLastX + 5;while (RLastX itemRLastx & RLastX = 0 & allChessxLLastY = ch) LLastY-;int LNum = 0;/ 统计纵向相同的棋子数LLastY+;int itemLLastY = LLastY + 5;while (LLastY itemLLastY & LLastY = 0 & RULastY allChessx.length& allChessLDLastXR
14、ULastY = ch) LDLastX-;RULastY+;int LDNum = 0;LDLastX+;RULastY-;int itemLDLastX = LDLastX + 5;while (LDLastX = 0& LDLastX = 0 & LDLastY = 0 & allChessRULastXLDLastY = ch) RULastX-;LDLastY-;int RUNum = 0;RULastX+;LDLastY+;int itemLDLastY = LDLastY + 5;while (RULastX = 0 & RULastX allChess.length& LDLa
15、stY itemLDLastY & LDLastY allChessx.length& allChessRULastXLDLastY = ch) RULastX+;LDLastY+;RUNum+;if (RNum = 5 | LNum = 5 | RUNum = 5 | LDNum = 5) return true;return false;Overridepublic void mouseClicked(MouseEvent e) / 鼠标按下时候画出棋子public void mousePressed(MouseEvent e) / 鼠标点击事件处理过程/ 获取点击位置坐标int clic
16、k_x = e.getX();int click_y = e.getY();/* * 返回x最接近的整数,如果x的小数部分大于等于0.5,返回值是大于x的最小整数, 否则round函数返回小于等于x的最大整数 */int chess_x = Math.round(float) (click_x - 20) / GAP);int chess_y = Math.round(float) (click_y - 20) / GAP);click_X = chess_x; / 获得点击后的横坐标click_Y = chess_y; / 获得点击后的纵坐标/ 玩家执黑棋if (isBlack = true
17、 & allChesschess_xchess_y = *) allChesschess_xchess_y = b;/ 令棋盘(x,y)设置为黑棋isBlack = false;repaint();if (isWin(chess_x, chess_y, isBlack) if (isBlack) / 弹出对话框JOptionPane.showMessageDialog(null, 白棋赢了);System.exit(0); else JOptionPane.showMessageDialog(null, 黑棋赢了);System.exit(0);if (isBlack = false) / 防
18、止再次点击已有棋子区域时下了白棋isCon(chess_x, chess_y);/* * do chess_x = (int) (Math.round(float) (Math.random() * 15); * chess_y = (int) (Math.round(float) (Math.random() * 15); * System.out.println(chess_x + + chess_y); while * (allChesschess_xchess_y != *); * System.out.println(allChesschess_xchess_y); * allChe
19、sschess_xchess_y = w;/ 令棋盘(x,y)设置为白棋 isBlack = true; * * /* if(isBlack=false&allChesschess_xchess_y=*) * allChesschess_xchess_y = w; isBlack = true; /令棋盘(x,y)设置为白棋 * */ 刷新页面repaint();if (isWin(chess_x, chess_y, isBlack) if (isBlack) / 弹出对话框JOptionPane.showMessageDialog(null, 白棋赢了); else JOptionPane.showMessageDialog(null, 黑棋赢了);public void mouseReleased(MouseEvent e) public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e)
限制150内