java五子棋人机.doc
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 五子棋 人机
![提示](https://www.deliwenku.com/images/bang_tan.gif)
限制150内