Java implementation of single version Gobang game _java

Source: Internet
Author: User

This article describes the Java implementation of the Gobang game code, shared for everyone to refer to, the specific code as follows

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;
Import Java.awt.Toolkit;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.MouseAdapter;
Import java.awt.event.MouseEvent;
 
Import java.util.ArrayList;
Import Javax.swing.Icon;
Import Javax.swing.ImageIcon;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JMenu;
Import Javax.swing.JMenuBar;
Import Javax.swing.JMenuItem;
Import Javax.swing.JOptionPane;
Import Javax.swing.JPanel;
 
Import Javax.swing.border.TitledBorder;
  public class MainFrame extends jframe{private static int big=15;
  private static final int one=25;
  private static final int start=10;
  private static int x=big*start/2;
  private static int y=big*start/2;
  private int hl=big*one+start;
  private static Boolean isblack; Canvasframe canvas=new CanvAsframe ();
  Canvasframe can;
  Graphics G;
  Public MainFrame () {init ();
  private point Point;
  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);
    Settitle ("Gobang");
    JMenu menu=new JMenu ("game");
    Bar.add (menu);
    JMenuItem start=new JMenuItem ("Start");
    JMenuItem end=new JMenuItem ("chessboard size");
    End.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {}});
    JMenuItem renew=new JMenuItem ("re-start");
    JMenuItem exit=new JMenuItem ("exit");
      Exit.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {system.exit (0);
    }
    }); Renew.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {blacklist.clear ()
        ; Whitelist.cleaR ();
    }
    });
    Menu.add (start);
    Menu.add (renew);
    Menu.add (end);
    Menu.add (exit);
    SetLayout (New BorderLayout ());
    Add (Borderlayout.center,createcenter ());
  Add (Borderlayout.east,createeast ());
    Private Boolean Iswin (arraylist<point> list) {int x;
    int y;
    int idx=0;
      Loop:for (Point p:list) {x=p.x;
      Y=P.Y;
      idx=0;
        while (List.contains (x,y)) {idx++;
        X=x-one;
      if (idx==5) break loop;
      } x=p.x;
      Y=P.Y;
      idx=0;
        while (List.contains (x,y)) {idx++;
        Y=y-one;
      if (idx==5) break loop;
      } x=p.x;
      Y=P.Y;
      idx=0;
        while (List.contains (x,y)) {idx++;
        X=x-one;
        Y=y-one;
      if (idx==5) break loop;
      } x=p.x;
      Y=P.Y;
      idx=0;
        while (List.contains (x,y)) {idx++;
        X=x-one;
        Y=y+one; IF (idx==5) break loop;
  } return idx==5;
    Private JPanel Createeast () {JPanel pane=new JPanel ();
    BorderLayout border=new BorderLayout ();
    Pane.setlayout (border);
     
    Pane.setborder (New Titledborder ("Gobang"));
    Icon 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 ());
  return pane;
     
    Private JPanel Ballcolor () {JPanel pane=new JPanel ();
    Pane.setlayout (New BorderLayout ());
        Can=new Canvasframe () {public void paint (Graphics g) {G.setcolor (color.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 ("Sunspots", 10, 43);
        G.setcolor (Color.White);
        G.filloval (55, 60,25,30);
      g.DrawString ("White Son", 10, 83);  if (isblack) {g.setcolor (color.black);
        g.DrawString ("Black Side Drop", 5, 120);
          }else{G.setcolor (Color.White);
        g.DrawString ("White Drop", 5,120);
      } can.repaint ();
    }
    };
    Pane.add (Borderlayout.center,can);
  return pane;
    Private JPanel button () {JPanel pane=new JPanel ();
    BorderLayout bor=new BorderLayout ();
    JButton start=new JButton ("Start");
    JButton end=new JButton ("End");
      End.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {system.exit (0);
    }});
    Pane.setlayout (BOR);
    Bor.setvgap (5);
    Pane.setborder (New Titledborder (""));
    Pane.add (Borderlayout.north,start);
    Pane.add (Borderlayout.south,end);
  return pane;
    Private JPanel Createcenter () {JPanel pane=new JPanel ();
    Canvas.setpreferredsize (New Dimension (hl+10,hl+10));
   Canvas.addmouselistener (New Mouseadapter () {public void mouseclicked (MouseEvent e) {     Loop:if (E.getbutton () ==mouseevent.button1) {x=e.getx ();
            Y=e.gety ();
            int x= (x/one) *one;
            int y= (y/one) *one;
             
            Point=new Point (X,y); if (!) ( Blacklist.contains (point) | |
                Whitelist.contains (point)) {if (isblack) {blacklist.add (point); if (Iswin (blacklist)) {Joptionpane.showmessagedialog (null, "Black Victory!")
                  ");
                  Blacklist.clear ();
                Whitelist.clear ();
                }}else{Whitelist.add (point); if (Iswin (whitelist)) {Joptionpane.showmessagedialog (NULL, white victory!)
                  ");
                  Blacklist.clear ();
                Whitelist.clear ();
              }} else{Joptionpane.showmessagedialog (null, "Here has a son, change a place drop!");
            Break loop;
          } canvas.repaint (); Isblack=!isbLack
    }
       
      }
    });
    Pane.add (canvas);
  return pane;
    public static void Main (string[] args) {MainFrame frame=new MainFrame ();
    Frame.center (frame);
     
    Frame.pack ();
  Frame.setvisible (TRUE);
    The public void Center (JFrame frame) {//toolkit is the current drawing System Collection Toolkit Toolkit Toolkit=toolkit.getdefaulttoolkit ();
    Dimension screen=toolkit.getscreensize ();//Screen wide-high 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 extends JPanel {public void paint (Graphics 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); (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 (START/2, START/2, hl+1, hl+1);
      G.setcolor (Color.Black);
      for (point point:blacklist) {g.filloval (Point.x, Point.y, 20, 25);
      } g.setcolor (Color.White);
      for (point point:whitelist) {g.filloval (Point.x, Point.y, 20, 25);
 }
    }
  }
}

Effect Chart:

The above mentioned is the entire content of this article, I hope to be able to master Java Help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.