本文实例讲述了 Java 实现的五子棋游戏代码 , 分享给大家供大家参考 , 具体代码如下
package gyb.exam; import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.font; import java.awt.graphics; import java.awt.point; java.awt.event.mousadapter; import java.awt.event.mousevent; import java.util.arraylist; Importer javax.swing.icon; importer javax.swing.imageicon; import javax.swing.jbutton; importer javax.swing.jframe; import javax.swing.jlabel; importer javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenem; import javax.wing. javax.swing.jpanel; import javax.swing.border.titledborder; classe publique mainframe étend JFrame {private static int big = 15; Final statique privé int 1 = 25; Final statique privé int start = 10; Static privé int x = big * start / 2; Intatique statique privée = Big * start / 2; privé int hl = big * one + start; Isblack booléen statique privé; Canvasframe canvas = new canvasframe (); CanvasFrame peut; Graphiques G; public mainframe () {init (); } point privé; Private ArrayList <point> BlackList = new ArrayList <point> (); Private ArrayList <point> WhiteList = new ArrayList <point> (); private void init () {jMenubar bar = new JMenubar (); setjMenubar (bar); setSize (600 600); Setttitle ("五子棋"); MENU JMENU = new JMenu ("Game"); bar.add (menu); JMenuItem start = new JMenuItem ("start"); JMenuItem end = new JMenuItem ("棋盘大小"); end.addactionListener (new ActionListener () {public void ActionPerformed (ActionEvent e) {}}); JMenUitem renouvellement = new JMenuItem ("重新开始"); JMenuItem exit = new JMenuItem ("退出"); exit.addactionListener (new ActionListener () {public void ActionPerformed (actionEvent e) {System.exit (0);}}); renouveler.addactionListener (new ActionListener () {public void ActionPerformed (ActionEvent e) {BlackList.Clear (); Whitelist.Clear ();}}); menu.add (start); menu.add (renouveler); menu.add (fin); menu.add (sortie); setLayout (new BorderLayout ()); Add (borderLayout.Center, CreateCenter ()); ajouter (borderLayout.East, createEast ()); } private boolean iswin (arrayList <point> list) {int x; int y; int idx = 0; LOOP: pour (point p: list) {x = px; y = py; idx = 0; while (list.contains (nouveau point (x, y))) {idx ++; x = x-one; if (idx == 5) Break Loop; } x = px; y = py; idx = 0; while (list.contains (nouveau point (x, y))) {idx ++; y = y-one; if (idx == 5) Break Loop; } x = px; y = py; idx = 0; while (list.contains (nouveau point (x, y))) {idx ++; x = x-one; y = y-one; if (idx == 5) Break Loop; } x = px; y = py; idx = 0; while (list.contains (nouveau point (x, y))) {idx ++; x = x-one; y = y + un; if (idx == 5) Break Loop; }} return idx == 5; } private jpanel createEast () {jpanel pane = new JPanel (); BorderLayout Border = new BorderLayout (); PANE.SetLayout (bordure); PANE.SETBORDER (NOUVEAU TITLEDEDBORDER ("五子棋")); Icône image = new ImageIcon (getClass (). GetResource ("mm.png")); PANE.ADD (BorderLayout.North, New JLabel (image, JLabel.Center)); PANE.ADD (BorderLayout.Center, BallColor ()); PANE.ADD (BorderLayout.South, Button ()); volet de retour; } private jpanel ballcolor () {jpanel pane = new JPanel (); PANE.SetLayout (new BorderLayout ()); can = new CanvasFrame () {public void peinture (graphiques g) {g.setColor (colore.pink); G.Fillrect (1, 1 200 300); g.setfont (new Font ("SANS", font.bold, 20)); g.setColor (color.black); G.Filloval (55, 20,25,30); G.Drawstring ("黑子", 10, 43); g.setColor (Color.white); G.Filloval (55, 60,25,30); G.Drawstring ("白子", 10, 83); if (IsBlack) {g.setColor (Color.Black); G.Drawstring ("黑方落子", 5, 120); } else {g.setColor (color.white); G.Drawstring ("白方落子", 5 120); } can.Repaint (); }}; PANE.ADD (BorderLayout.Center, Can); volet de retour; } Button JPanel privé () {jpanel Pane = new JPanel (); BorderLayout bor = new BorderLayout (); JBUTTON START = NOUVEAU JBUTTON ("开始"); Jbutton end = new Jbutton ("结束"); end.addactionListener (new ActionListener () {public void ActionPerformed (actionEvent e) {System.Exit (0);}}); PANE.SetLayout (BOR); bor.setvgap (5); PANE.SETBBORDER (NOUVEAU TITLEDBORDER ("")); PANE.ADD (BorderLayout.North, Start); PANE.ADD (BorderLayout.South, End); volet de retour; } private jpanel createCenter () {jpanel pane = new JPanel (); canvas.setPreferRedSize (nouvelle dimension (HL + 10, HL + 10)); Canvas.AddMouseListener (new Mouseadapter () {public void MouseClicked (Mouseevent E) {Loop: if (e.getButton () == Mousevent.Button1) {x = e.getx (); y = e.gety (); int x = (x / un) * un; int y = (y / one) * un; if (! (BlackList.Conse if (Iswin (WhiteList)) {jOptionPane.ShowMessageDialog (null, "白方胜利!"); }); PANE.ADD (Canvas); volet de retour; } public static void main (string [] args) {mainframe frame = new MainFrame (); frame.center (cadre); frame.pack (); frame.setVisible (true); } public void Center (JFrame Frame) {// Toolkit 是当前 绘图系统集合工具包 Toolkit Toolkit = toolkit.getDefaultToolkit (); Dimension Screen = Toolkit.getScreenSize (); // 屏幕的宽高 int framewidth = frame.getWidth (); int framehigh = frame.getheight (); int x = (screen.width-framewidth) / 2; int y = (screen.height-framehigh) / 2; frame.setLocation (x, y); } Class CanvasFrame étend jpanel {public void peinture (graphiques g) {g.setColor (color.pink); G.FillRect (0, 0, Hl + 20, HL + 20); g.setColor (color.black); for (int i = start; i <= hl; i = i + one) {for (int j = start; j <= hl; j = j + one) {g.drawline (i, j, hl, j); }} pour (int i = start; i <= hl; i = i + one) {for (int j = start; j <= hl; j = j + one) {g.drawline (i, j, i, hl); }} g.setColor (color.yellow); G.Drawrect (démarrage / 2, démarrage / 2, HL + 1, HL + 1); g.setColor (color.black); pour (point de point: BlackList) {g.FillOval (point.x, point.y, 20, 25); } g.setColor (Color.white); pour (point de point: liste blanche) {g.filloval (point.x, point.y, 20, 25); }}}}:
以上所述就是本文的全部内容了 , 希望能够对大家熟练掌握 Java 有所帮助。