




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
此文档收集于网络,如有侵权,请联系网站删除本程序共有3个类,下载后将三个类复制到三个记事本里然后重名记事本(为了阅读方便分三个文件),其中main()方法在StartChessJFrame类中。我是菜鸟,交流QQ:609429837import java.awt.Color;public class Point private int x;/棋盘中X的索引private int y;/棋盘中Y的索引private Color color;/颜色public static final int DIAMETER = 30;/直径public Point(int x,int y,Color color)this.x=x;this.y=y;this.color =color;/拿到棋盘中的Y索引public int getX() return x;public int getY() return y;/得到颜色public Color getColor()return color;import javax.swing.*;import java.awt.*;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.event.MouseEvent;/* 五子棋-棋盘类 */public class ChessBoard extends JPanel implements MouseListener public static final int MARGIN = 30;/边距protected static final int GRID_SPAN = 35;/网格间距public static final int ROWS = 10;/棋盘行数public static final int COLS = 10;/棋盘列数Point chessList = new Point(ROWS+1)*(COLS+1);/初始化每个数组元素为nullboolean isBlack = true;/默认开始是黑棋先下boolean gameOver = false;/游戏是否结束int chessCount;/当前棋盘棋子的个数int xIndex,yIndex;/当前刚下的棋子的索引public ChessBoard()super.setBackground(Color.ORANGE);/setBackground(Color.ORANGE);/设置背景颜色为橘黄色addMouseListener(this);/添加监听器addMouseMotionListener(new MouseMotionListener()/匿名内部类public void mouseDragged(MouseEvent e)public void mouseMoved(MouseEvent e)int x1 = (e.getX()- MARGIN +GRID_SPAN/2)/GRID_SPAN;/将鼠标单击的坐标位置转换成网格索引int y1 = (e.getY()- MARGIN +GRID_SPAN/2)/GRID_SPAN;/游戏已经结束,落在棋盘外,x、y位置已经有棋子存在,不能下if(x1ROWS|y1COLS|gameOver|findChess(x1,y1)setCursor(new Cursor(Cursor.DEFAULT_CURSOR);/设置成默认形状elsesetCursor(new Cursor(Cursor.HAND_CURSOR);/设置成手型);/绘制public void paintComponent(Graphics g)super.paintComponent(g);/画棋类for(int i=0;i=ROWS;i+)/画横线g.drawLine(MARGIN,MARGIN+i*GRID_SPAN,MARGIN+COLS*GRID_SPAN,MARGIN+i*GRID_SPAN);for(int i=0;i=COLS;i+)/画直线g.drawLine(MARGIN+i*GRID_SPAN,MARGIN,MARGIN+i*GRID_SPAN,MARGIN+ROWS*GRID_SPAN);/画棋子for(int i=0;ichessCount;i+) int xPos = chessListi.getX()*GRID_SPAN+MARGIN;/网络交叉点的x坐标int yPos = chessListi.getY()*GRID_SPAN+MARGIN;/网络交叉点的y坐标g.setColor(chessListi.getColor();/设置颜色g.fillOval(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER);/标记最后一个棋子的红矩形框if(i = chessCount -1)/最后一个棋子g.setColor(Color.red);g.drawRect(xPos - Point.DIAMETER/2,yPos-Point.DIAMETER/2,Point.DIAMETER,Point.DIAMETER);/鼠标按键在组建上按下时调用public void mousePressed(MouseEvent e)/游戏已经结束,不能下if(gameOver)return;String colorName = isBlack ?黑棋:白棋;xIndex = (e.getX() - MARGIN + GRID_SPAN/2)/GRID_SPAN;/将鼠标单击的坐标位置转换成网格索引yIndex = (e.getY() - MARGIN + GRID_SPAN/2)/GRID_SPAN;/落在棋盘外,不能下if(xIndex ROWS | yIndex COLS)return;/x、y位置都已经有棋子存在,不能下if(findChess(xIndex , yIndex)return;Point ch = new Point(xIndex,yIndex,isBlack ? Color.black:Color.white);chessListchessCount+ = ch;repaint();/通知系统重新绘制if(isWin()/给出胜利信息,不能再继续下棋String msg = String.format(恭喜,%s赢了!, colorName);JOptionPane.showMessageDialog(this, msg);gameOver = true;isBlack = !isBlack;/覆盖MouseListener的方法public void mouseClicked(MouseEvent e)/鼠标按键在组件上单击(按下并释放)时调用public void mouseEntered(MouseEvent e)/鼠标进入到组件上时调用public void mouseExited(MouseEvent e)/鼠标离开组件时调用public void mouseReleased(MouseEvent e)/鼠标离开组件时调用/在棋子数组中查找是否有索引为x、y的棋子存在private boolean findChess(int x, int y)for(Point c : chessList)if(c !=null & c.getX() = x & c.getY() = y)return true;return false;/判定哪方赢private boolean isWin()int continueCount =1;/连续棋子的个数/横向向西寻找for(int x = xIndex-1; x=0;x-)Color c = isBlack ? Color.black : Color.white;if(getChess(x,yIndex,c) !=null)continueCount+;elsebreak;/横向向东寻找for(int x =xIndex+1;x=5)return true;elsecontinueCount =1;/继续另一种情况的搜索:纵向/纵向向上寻找for(int y = yIndex - 1; y = 0; y-)Color c =isBlack ? Color.black : Color.white;if(getChess(xIndex,y,c) !=null)continueCount+;elsebreak;/纵向向下寻找for(int y = yIndex + 1; y=5)return true;elsecontinueCount =1;/继续另一种情况的搜索:斜向/东北寻找for(int x = xIndex + 1,y=yIndex-1; y=0 & x=COLS; x+,y-)Color c = isBlack ? Color.black : Color.white;if(getChess(x,y,c)!=null)continueCount+;elsebreak;/西南寻找for(int x = xIndex - 1,y=yIndex+1; y=0; x-,y+)Color c = isBlack ? Color.black : Color.white;if(getChess(x,y,c)!=null)continueCount+;elsebreak;if(continueCount =5)return true;elsecontinueCount = 1;/继续另一种情况的搜索:斜向/西北寻找for(int x = xIndex - 1,y = yIndex-1; y=0 & x=0; x-,y-)Color c = isBlack ? Color.black : Color.white;if(getChess(x,y,c)!=null)continueCount+;elsebreak;/西南寻找for(int x = xIndex + 1,y=yIndex+1; y=ROWS & x=5)return true;elsecontinueCount = 1;return false;private Point getChess(int xIndex, int yIndex, Color color)for(Point c: chessList)if(c !=null & c.getX() = xIndex & c.getY() = yIndex & c.getColor() = color)return c;return null;public void restartGame()/清除棋子for(int i=0; i0)xIndex = chessListchessCount -1.getX();yIndex = chessListchessCount -1.getY();isBlack = !isBlack;/repaint();/Dimension:矩形public Dimension getPreferredSize()return new Dimension (MARGIN*2 + GRID_SPAN*COLS,MARGIN*2 + GRID_SPAN*ROWS);import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;public class StartChessJFrame extends JFrame private ChessBoard chessBoard;/对战面板private JPanel toolbar;/工具条面板private JButton startButton;private JButton backButton;private JButton exitButton;/“重新开始”按钮,“悔棋”按钮,“退出”按钮private JMenuBar menuBar;/菜单栏private JMenu sysMenu;/系统菜单private JMenuItem startMenuItem;private JMenuItem exitMenuIatem;private JMenuItem backMenuItem;/“重新开始”,“退出”和“悔棋”菜单项public StartChessJFrame()setTitle(单机版五子棋);/设置标题chessBoard =new ChessBoard();/初始化面板对象/创建和添加菜单menuBar = new JMenuBar();/初始化菜单栏sysMenu = new JMenu(系统);/初始化菜单startMenuItem = new JMenuItem(重新开始);exitMenuIatem = new JMenuItem(退出);backMenuItem = new JMenuItem(悔棋);/初始化菜单项sysMenu.add(startMenuItem);/将三个菜单项添加到菜单上sysMenu.add(backMenuItem);sysMenu.add(exitMenuIatem);MyItemListener lis = new MyItemListener();/初始化按钮事件监听器内部类this.startMenuItem.addActionListener(lis);/将三个菜单项注册到事件监听器上backMenuItem.addActionListener(lis);exitMenuIatem.addActionListener(lis);menuBar.add(sysMenu);/将“系统”菜单添加到菜单栏上setJMenuBar(menuBar);/将menuBar设置为菜单栏toolbar =new JPanel();/工具面板实例化startButton = new JButton(重新开始);/三个按钮初始化backButton = new JButton(悔棋);exitButton = new JButton(退出);toolbar.setLayout(new FlowLayout(FlowLayout.LEFT);/将工具面板按钮用FlowLayout布局toolbar.add(startButton);/将三个按钮添加到工具面板上toolbar.add(backButton);toolbar.add(exitButton);startButton.addActionListener(lis);/将三个按钮注册监听事件backButton.addActionListener(lis);exitButton.addActionListener(lis);add(toolbar,BorderLayout.SOUTH
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 投资岗面试题及答案
- 冲突营销面试题及答案
- T管引流的护理措施
- 小木屋创意画课件
- 溶液配置考试题库及答案
- 球迷入门测试题及答案
- 西南证券面试题及答案
- 气体交换测试题及答案
- 高考试题数学及答案
- 支气管超声内镜技术临床应用
- 2025年小学五年级数学期末冲刺卷:数学基础知识巩固
- CSCO恶性血液病诊疗指南(2025)解读
- T/CHTS 20036-2023公路桥梁用硬聚氯乙烯声测管
- 《动物保定技术》课件
- 北京市朝阳区2023-2024学年四年级下学期语文期末考试卷(含答案)
- 上样合作协议合同协议
- 公司2025庆七一活动方案七一活动方案2025
- 留学机构合作协议书范本
- 太极拳教学合同协议
- 2024慢性鼻窦炎诊断和治疗指南解读课件
- 大国工匠精神课件
评论
0/150
提交评论