【Java精品源码栏目提醒】:以下是网学会员为您推荐的Java精品源码-【精品】Java五子棋游戏源代码 - 其它资料,希望本篇文章对您学习有所帮助。
//
Java 编程:五子棋游戏
源代码import
java.awt.import
java.awt.event.import
java.applet.import javax.swing.import
java.io.PrintStreamimport javax.swing.JComponentimport javax.swing.JPanel/main 方法创建了 ChessFrame 类的一个实例对象(cf),并启动屏幕显示显示该实例对象。
/public class FiveChessAppletDemo public static void mainString args ChessFrame cf new ChessFrame cf.show/类 ChessFrame 主要功能是创建五子棋游戏主窗体和菜单/class ChessFrame extends JFrame implements ActionListener private String strsizequot20x15quotquot30x20quotquot40x30quotprivate String strmodequot人机对弈quotquot人人对弈quotpublic static boolean iscomputertruecheckcomputertrueprivate int widthheightprivate ChessModel cmprivate MainPanel mp//构造五子棋游戏的主窗体public ChessFrame this.setTitlequot五子棋游戏quot cmnew ChessModel1 mpnew MainPanelcm Container conthis.getContentPane con.addmpquotCenterquot this.setResizablefalse this.addWindowListenernew ChessWindowEvent MapSize2015 JMenuBar mbar new JMenuBar this.setJMenuBarmbar JMenu gameMenu new JMenuquot游戏quot mbar.addmakeMenugameMenu new Object quot开局quot quot棋盘quotquot模式quot null quot退出quot this JMenu lookMenu new JMenuquot视图quot mbar.addmakeMenulookMenunew Object quotMetalquotquotMotifquotquotWindowsquot this JMenu helpMenu new JMenuquot帮助quot mbar.addmakeMenuhelpMenu new Object quot关于quot this//构造五子棋游戏的主菜单public JMenu makeMenuObject parent Object items Object target JMenu m null ifparent instanceof JMenu m JMenuparent else ifparent instanceof String m new JMenuStringparent else return null forint i 0 i lt items.length i ifitemsi null m.addSeparator else ifitemsi quot棋盘quot JMenu jm new JMenuquot棋盘quot ButtonGroup groupnew ButtonGroup JRadioButtonMenuItem rmenu for int j0jltstrsize.lengthj rmenumakeRadioButtonMenuItemstrsizejtarget if j0 rmenu.setSelectedtrue jm.addrmenu group.addrmenu m.addjm else ifitemsi quot模式quot JMenu jm new JMenuquot模式quot ButtonGroup groupnew ButtonGroup JRadioButtonMenuItem rmenu for int h0hltstrmode.lengthh rmenumakeRadioButtonMenuItemstrmodehtarget ifh0 rmenu.setSelectedtrue jm.addrmenu group.addrmenu m.addjm else m.addmakeMenuItemitemsi target return m//构造五子棋游戏的菜单项public JMenuItem makeMenuItemObject item Object target JMenuItem r null ifitem instanceof String r new JMenuItemStringitem else ifitem instanceof JMenuItem r JMenuItemitem else return null iftarget instanceof ActionListener r.addActionListenerActionListenertarget return r//构造五子棋游戏的单选按钮式菜单项public JRadioButtonMenuItem makeRadioButtonMenuItem Object item Object target JRadioButtonMenuItem r null ifitem instanceof String r new JRadioButtonMenuItemStringitem else ifitem instanceof JRadioButtonMenuItem r JRadioButtonMenuItemitem else return null iftarget instanceof ActionListener r.addActionListenerActionListenertarget return r public void MapSizeint wint h setSizew 2050 h 20100 ifthis.checkcomputer this.iscomputertrue else this.iscomputerfalse mp.setModelcm mp.repaint public boolean getiscomputer return this.iscomputer public void restart int modeChess cm.getModeChess ifmodeChess lt 3 ampamp modeChess gt 1 cm new ChessModelmodeChess MapSizecm.getWidthcm.getHeight else System.out.printlnquotu81EAu5B9Au4E49quot public void actionPerformedActionEvent e String arge.getActionCommand try if arg.equalsquotWindowsquot UIManager.setLookAndFeel quotcom.sun.
java.swing.plaf.windows.WindowsLookAndFeelquot else ifarg.equalsquotMotifquot UIManager.setLookAndFeel quotcom.sun.
java.swing.plaf.motif.MotifLookAndFeelquot else UIManager.setLookAndFeel quotjavax.swing.plaf.metal.MetalLookAndFeelquot SwingUtilities.updateComponentTreeUIthiscatchException eeifarg.equalsquot20x15quot this.width20 this.height15 cmnew ChessModel1 MapSizethis.widththis.height SwingUtilities.updateComponentTreeUIthisifarg.equalsquot30x20quot this.width30 this.height20 cmnew ChessModel2 MapSizethis.widththis.height SwingUtilities.updateComponentTreeUIthis ifarg.equalsquot40x30quot this.width40 this.height30 cmnew ChessModel3 MapSizethis.widththis.height SwingUtilities.updateComponentTreeUIthis ifarg.equalsquot人机对弈quot this.checkcomputertrue this.iscomputertrue cmnew ChessModelcm.getModeChess MapSizecm.getWidthcm.getHeight SwingUtilities.updateComponentTreeUIthis ifarg.equalsquot人人对弈quot this.checkcomputerfalse this.iscomputerfalse cmnew ChessModelcm.getModeChess MapSizecm.getWidthcm.getHeight SwingUtilities.updateComponentTreeUIthis ifarg.equalsquot开局quot restart ifarg.equalsquot关于quot JOptionPane.showMessageDialogthis quot五子棋游戏测试版本quot quot关于quot 0 ifarg.equalsquot退出quot System.exit0/类 ChessModel 实现了整个五子棋
程序算法的核心/class ChessModel //棋盘的宽度、高度、棋盘的模式(如 20×15)private int widthheightmodeChess//棋盘方格的横向、纵向坐标private int x0y0//棋盘方格的横向、纵向坐标所对应的棋子颜色,//数组 arrMapShow 只有 3 个值:1,2,3,-5,//其中 1 代表该棋盘方格上下的棋子为黑子,//2 代表该棋盘方格上下的棋子为白子,//3 代表为该棋盘方格上没有棋子,//-5 代表该棋盘方格不能够下棋子private int arrMapShow//交换棋手的标识,棋盘方格上是否有棋子的标识符private boolean isOddisExistpublic ChessModel //该构造方法根据不同的棋盘模式(modeChess)来构建对应大小的棋盘public ChessModelint modeChess this.isOddtrue ifmodeChess 1 PanelInit20 15 modeChess ifmodeChess 2 PanelInit30 20 modeChess ifmodeChess 3 PanelInit40 30 modeChess //按照棋盘模式构建棋盘大小private void PanelInitint width int height int modeChess this.width width this.height height this.modeChess modeChess arrMapShow new intwidth1height1 forint i 0 i lt width i forint j 0 j lt height j arrMapShowij -5 //获取是否交换棋手的标识符public boolean getisOdd return this.isOdd//设置交换棋手的标识符public void setisOddboolean isodd ifisodd this.isOddtrue else this.isOddfalse//获取某棋盘方格是否有棋子的标识值public boolean getisExist return this.isExist//获取棋盘宽度public int getWidth return this.width//获取棋盘高度public int getHeight return this.height//获取棋盘模式public int getModeChess return this.modeChess//获取棋盘方格上棋子的信息public int getarrMapShow return arrMapShow//判断下子的横向、纵向坐标是否越界private boolean badxyint x int y ifx gt width20 x lt 0 return true return y gt height20 y lt 0//计算棋盘上某一方格上八个方向棋子的最大值,//这八个方向分别是:左、右、上、下、左上、左下、右上、右下public boolean chessExistint iint j ifthis.arrMapShowij1 this.arrMapShowij2 return true return false//判断该坐标位置是否可下棋子public void readyplayint xint y ifbadxyxy return if chessExistxy return this.arrMapShowxy3//在该坐标位置下棋子public void playint xint y ifbadxyxy return ifchessExistxy this.isExisttrue return else this.isExistfalse ifgetisOdd setisOddfalse this.arrMapShowxy1 else setisOddtrue this.arrMapShowxy2 //计算机走棋/说明:用穷举法判断每一个坐标点的四个方向的的最大棋子数,最后得出棋子数最大值的坐标,下子/public void computerDoint widthint height int max_blackmax_whitemax_tempmax0 setisOddtrue System.out.printlnquot
计算机走棋 ...quot forint i 0 i lt width i forint j 0 j lt height j ifchessExistij//算法判断是否下子 max_whitecheckMaxij2//判断白子的最大值 max_blackcheckMaxij1//判断黑子的最大值 max_tempMath.maxmax_whitemax_black ifmax_tempgtmax maxmax_temp this.xi this.yj setXthis.x setYthis.y this.arrMapShowthis.xthis.y2//记录电脑下子后的横向坐标public void setXint x this.xx//记录电脑下子后的纵向坐标public void setYint y this.yy//获取电脑下子的横向坐标public int getX return this.x//获取电脑下子的纵向坐标public int getY return this.y//计算棋盘上某一方格上八个方向棋子的最大值,//这八个方向分别是:左、右、上、下、左上、左下、右上、右下public int checkMaxint x int yint black_or_white int num0max_nummax_temp0 int x_tempxy_tempy int x_temp1x_tempy_temp1y_temp //judge right forint i1ilt5i x_temp11 ifx_temp1gtthis.width break ifthis.arrMapShowx_temp1y_temp1black_or_white num else break//judge leftx_temp1x_tempforint i1ilt5i x_temp1-1 ifx_temp1lt0 break ifthis.arrMapShowx_temp1y_temp1black_or_white num else breakifnumlt5 max_tempnum//judge upx_temp1x_tempy_temp1y_tempnum0forint i1ilt5i y_temp1-1 ify_temp1lt0 break ifthis.arrMapShowx_temp1y_temp1black_or_white num else break//judge downy_temp1y_tempforint i1ilt5i y_temp11 ify_temp1gtthis.height break ifthis.arrMapShowx_temp1y_temp1black_or_white num else breakifnumgtmax_tempampampnumlt5 max_tempnum//judge left_upx_temp1x_tempy_temp1y_tempnum0forint i1ilt5i x_temp1-1 y_temp1-1 ify_temp1lt0 x_temp1lt0 break ifthis.arrMapShowx_temp1y_temp1black_or_white num else break//judge right_downx_temp1x_tempy_temp1y_tempforint i1ilt5i x_temp11 y_temp11 ify_temp1gtthis.height x_temp1gtthis.width break ifthis.arrMapShowx_temp1y_temp1black_or_white num else breakifnumgtmax_tempampampnumlt5 max_tempnum//judge right_upx_temp1x_tempy_temp1y_tempnum0forint i1ilt5i x_temp11 y_temp1-1 ify_temp1lt0 x_temp1gtthis.width break ifthis.arrMapShowx_temp1y_temp1black_or_white num else break //judge left_down x_temp1x_temp y_temp1y_temp forint i1ilt5i x_temp1-1 y_temp11 ify_temp1gtthis.height x_temp1lt0 break ifthis.arrMapShowx_temp1y_temp1black_or_white num else break ifnumgtmax_tempampampnumlt5 max_tempnum max_nummax_temp return max_num//判断胜负public boolean judgeSuccessint xint yboolean isodd int num1 int arrvalue int x_tempxy_tempy ifisodd arrvalue2 else arrvalue1 int x_temp1x_tempy_temp1y_temp //判断右边 forint i1ilt6i x_temp11 ifx_temp1gtthis.width break ifthis.arrMapShowx_temp1y_temp1arrvalue num else break //判断左边 x_temp1x_temp forint i1ilt6i x_temp1-1 ifx_temp1lt0 break ifthis.arrMapShowx_temp1y_temp1arrvalue num else breakifnum5 return true//判断上方x_temp1x_tempy_temp1y_tempnum1forint i1ilt6i y_temp1-1 ify_temp1lt0 break ifthis.arrMapShowx_temp1y_temp1arrvalue num else break//判断下方y_temp1y_tempforint i1ilt6i y_temp11 ify_temp1gtthis.height break ifthis.arrMapShowx_temp1y_temp1arrvalue num else breakifnum5 return true//判断左上x_temp1x_tempy_temp1y_tempnum1forint i1ilt6i x_temp1-1 y_temp1-1 ify_temp1lt0 x_temp1lt0 break ifthis.arrMapShowx_temp1y_temp1arrvalue num else break//判断右下x_temp1x_tempy_temp1y_tempforint i1ilt6ix_temp11y_temp11ify_temp1gtthis.height x_temp1gtthis.width break ifthis.arrMapShowx_temp1y_temp1arrvalue num else breakifnum5 return true//判断右上x_temp1x_tempy_temp1y_tempnum1forint i1ilt6i x_temp11 y_temp1-1 ify_temp1lt0 x_temp1gtthis.width break ifthis.arrMapShowx_temp1y_temp1arrvalue num else break//判断左下x_temp1x_tempy_temp1y_tempforint i1ilt6i x_temp1-1 y_temp11 ify_temp1gtthis.height x_temp1lt0 break ifthis.arrMapShowx_temp1y_temp1arrvalue num else break ifnum5 return true return false//赢棋后的提示public void showSuccessJPanel jp JOptionPane.showMessageDialogjpquot你赢了,好厉害quotquotwinquot JOptionPane.INFORMATION_MESSAGE//输棋后的提示public void showDefeatJPanel jp JOptionPane.showMessageDialogjpquot你输了,请重新开始quotquotlostquot JOptionPane.INFORMATION_MESSAGE/类 MainPanel 主要完成如下功能:1、构建一个面板,在该面板上画上棋盘;2、处理在该棋盘上的鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等)/class MainPanel extends JPanelimplements MouseListenerMouseMotionListenerprivate int widthheight//棋盘的宽度和高度private ChessModel cm//根据棋盘模式设定面板的大小MainPanelChessModel mm cmmm widthcm.getWidth heightcm.getHeight addMouseListenerthis //根据棋盘模式设定棋盘的宽度和高度public void setModelChessModel mm cm mm width cm.getWidth height cm.getHeight ,//根据坐标计算出棋盘方格棋子的信息(如白子还是黑子)//然后调用 draw 方法在棋盘上画出相应的棋子public void paintComponentGraphics g super.paintComponentg forint j 0 j lt height j forint i 0 i lt width i int v cm.getarrMapShowij drawg i j v //根据提供的棋子信息(颜色、坐标)画棋子public void drawGraphics g int i int j int v int x 20 i20 int y 20 j20 //画棋盘 ifiwidth ampamp jheight g.setColorColor.white g.drawRectxy2020 //画黑色棋子 ifv 1 g.setColorColor.gray g.drawOvalx-8y-81616 g.setColorColor.black g.fillOvalx-8y-81616 //画白色棋子 ifv 2 g.setColorColor.gray g.drawOvalx-8y-81616 g.setColorColor.white g.fillOvalx-8y-81616 ifv 3 g.setColorColor.cyan g.drawOvalx-8y-81616 //响应鼠标的点击事件,根据鼠标的点击来下棋,//根据下棋判断胜负等public void mousePressedMouseEvent evt int x evt.getX-10 / 20 int y evt.getY-10 / 20 System.out.printlnxquot quoty if evt.getModifiersMouseEvent.BUTTON1_MASK cm.playxy System.out.printlncm.getisOddquot quotcm.getarrMapShowxy repaint ifcm.judgeSuccessxycm.getisOdd cm.showSuccessthis evt.consume ChessFrame.iscomputerfalse //判断是否为人机对弈 ifChessFrame.iscomputerampampcm.getisExist cm.comput.