Java implementation Gobang Small game _java

Source: Internet
Author: User
Tags gety stub thread class

Java implementation Gobang games

 package Gomoku;
 
Import Java.awt.Toolkit;
 
 
Import Javax.swing.JFrame;
   
  public class Gomokuframe extends JFrame {//define an operator panel Operatorpane op=null;
    Public Gomokuframe () {//Set name This.settitle ("Gobang");
    Set window size this.setsize (510,510);
    Set window position//Get computer screen size int computer_width=toolkit.getdefaulttoolkit (). Getscreensize (). width;
    int Computer_height=toolkit.getdefaulttoolkit (). Getscreensize (). Height;
        System.out.println ("The width of the computer screen: \ n" +computer_width+ "\ nthe height of the computer screen: \ n" +computer_height);    
     
    Center This.setlocation ((computer_width-510)/2, (computer_height-510)/2);
    Materialized curtain op=new Operatorpane ();
    Import Curtain This.add (OP);
     
    Add Mouse Monitor This.addmouselistener (OP);
    Sets the display this.setvisible (true) of the window;
  Set the normal closure of the window this.setdefaultcloseoperation (jframe.exit_on_close);
  ///execute test public static void main (string[] args) {new gomokuframe (); }
     
} 
  
Package Gomoku;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.Image;
Import java.awt.event.MouseEvent;
 
 
 
Import Java.awt.event.MouseListener;
Import Java.awt.image.BufferedImage;
 
Import Java.awt.image.BufferedImageFilter;
Import Javax.swing.ImageIcon;
Import Javax.swing.JOptionPane;
 
Import Javax.swing.JPanel;
  public class Operatorpane extends JPanel implements mouselistener,runnable {//Definition background Image object Image imagebackground = null;
  Defines the number of rows int boardrows=18 of the checkerboard;
  Defines the number of columns of a checkerboard int boardcols=18;
  Defines the size of the grid of the chessboard int boardsize=20;
  Save pawn coordinates int x=0,y=0;
  All previous pieces of the piece coordinates//where the data content 0: that this point does not have a piece, 1: This point is a sunspot, 2: This point is a white int allchess[][]=new int [19][19];
  Mark next Next Black or white Boolean isblack=true;
  To determine whether the game can continue the Boolean canplay=true;
  Save display information String message= "black side First";
  Save up to how much time (sec) int maxtime = 0;
  To do the countdown thread class thread t = new thread (this);
  Save the remaining time of the black and white side int blacktime = 0;
  int whitetime = 0; Save the display letter for the remaining time of the two partiesCoupon String Blackmessage = "Unrestricted";
   
   
  String whitemessage = "Unrestricted";
    @SuppressWarnings ("deprecation") public Operatorpane () {T.start ();
     
     T.suspend ();
  Imagebackground=new ImageIcon ("Image/background.jpg"). GetImage (); } public void Paint (Graphics g) {//double buffering technology bufferedimage b1=new BufferedImage (495,475,bufferedimage.type_i
    NT_ARGB);
     
    Graphics G2=b1.creategraphics ();
     
     
    Draw a background picture g2.drawimage (imagebackground, 0, 0,495,475,null);
    Draw the Checkerboard Line Color C=g2.getcolor ();
    G2.setcolor (Color.Black);
    for (int i=0;i<=boardrows;i++) {g2.drawline (10,50+boardsize*i,10+boardsize*boardrows,50+boardsize*i);
    for (int i=0;i<=boardcols;i++) {g2.drawline (10+boardsize*i,50,10+boardsize*i,50+boardsize*boardcols);
    ///Draw 33 position G2.filloval (67, 107, 6, 6);
    G2.filloval (67, 347, 6, 6);
    G2.filloval (307, 107, 6, 6);
    G2.filloval (307, 347, 6, 6); Draw the attachment point G2.filloval (67, 227, 6, 6);
    G2.filloval (307, 227, 6, 6);
    G2.filloval (187, 107, 6, 6);
     
    G2.filloval (187, 347, 6, 6);
     
     
     
     
    Draw the Tianyuan G2.filloval (187, 227, 6, 6);
    Draw a text hint/*font f=new Font ("bold", Font.Bold, 24);
    G.setfont (f); */G2.setfont (new Font ("bold", Font.Bold, 20));
    G2.setcolor (Color.Black);
    G2.drawstring ("Game info:" +message, 130, 40);
    G2.setfont (New Font ("Song Body", Font.Italic, 15));
    G2.drawstring ("Black Party Time:" +blackmessage,25, 445);
    
    G2.drawstring ("Bai Fang Time:" +whitemessage,245, 445);
        Draw all pieces for (int i=0;i<=boardrows;i++) {for (int j=0;j<=boardcols;j++) {//Storage black
          if (allchess[i][j]==1) {int tempx=i*20-10;
          int tempy=j*20+30;
          Draw Black Chess G2.setcolor (color.black);
        G2.filloval (tempx+12, tempy+13, 15, 15);
          }//Storage white if (allchess[i][j]==2) {int tempx=i*20-10;
        int tempy=j*20+30;  Draw White Chess G2.setcolor (color.black);
          G2.drawoval (tempx+12, tempy+13, 15, 15);
          G2.setcolor (Color.White);
        G2.filloval (tempx+12, tempy+13, 15, 15);
     
    }} g2.setcolor (c);
  G.drawimage (B1,0,0,this);
     
    Private Boolean Checkwin () {Boolean flag=false;
     
    int color = Allchess[x][y];
    /*//save a total of the same color how many pieces connected int count1=1;
    int count2=1;
    int count3=1;
     
    int count4=1;
    To determine whether there are 5 pieces connected horizontally, the characteristics of the ordinate is the same, that is, Allchess[x][y] y value is the same//through the loop to make pieces connected to the judgment int i = 1; 
      while (color = = Allchess[x+i][y]) {count1++; 
    i++;
    //Reset i = 1; 
      while (color = = Allchess[x-i][y]) {count1++; 
    i++; 
    } if (Count1 >= 5) {flag = true;
    }//Judge portrait, that is, Allchess[x][y] The x value is the same int j = 1; 
      while (color = = Allchess[x][y+j]) {count2++; 
    j + +;
    //Reset the J value to J = 1; while (color = = = Allchess[x][y-j]) {count2++; 
    j + +; 
    } if (count2>= 5) {flag = true;
    //Judgment oblique "\" int m1=1;
    int n1=1; 
      while (color = = Allchess[x+m1][y+n1]) {count3++; 
      m1++;
    n1++;
    } m1=1;
    N1=1; 
      while (color = = Allchess[x-m1][y-n1]) {count3++; 
      m1++;
    n1++; 
    } if (count3>= 5) {flag = true;
    //Judgment Oblique "/" int m2=1;
    int n2=1; 
      while (color = = Allchess[x+m2][y-n2]) {count4++; 
      m2++;
    n2++;
    } m2=1;
    N2=1; 
      while (color = = Allchess[x-m2][y+n2]) {count4++; 
      m2++;
    n2++; 
    } if (count4>= 5) {flag = true;
    } */int count;
    Lateral judgment count=this.checkcount (1, 0, color); 
    if (count>=5) {flag = true;
      else {//Portrait judge Count=this.checkcount (0, 1, color); if (count>=5) {flag = true; 
      else {//oblique "/" Count=this.checkcount (1, 1, color); 
        if (count>=5) {flag = true;
          else {//oblique "\" Count=this.checkcount (1,-1, color); 
          if (count>=5) {flag = true;
  }}} return flag;
    private int Checkcount (int xchange,int ychange,int color) {int count=1;
    int tempx=xchange;
     
    int tempy=ychange;
      while (Color==allchess[x+xchange][y+ychange]) {count++; 
      if (xchange!=0) {xchange++;
        } if (ychange!=0) {if (ychange<0) {ychange--;
          else {ychange++;
    }}//Reset xchange=tempx;
    Ychange=tempy;
      while (Color==allchess[x-xchange][y-ychange]) {count++; if (xchange!=0) {XChange++;
        } if (ychange!=0) {if (ychange<0) {ychange--;
        else {ychange++;
 
  }} return count; 
    public void mouseclicked (MouseEvent e) {System.out.println ("x:" +e.getx () + "Y:" +e.gety ());
    X=e.getx ();
    Y=e.gety (); if (x>=10&&x<= (10+boardsize*boardrows+20) &&y>=50&&y<= (50+boardsize* boardcols+40)) {//system.out.println ("dot in the chessboard.")
      ");
      x= (x-10)/20;
       
       
      y= (y-50-20)/20;
          if (canplay==true) {//Determine what color is currently under if (allchess[x][y]==0) {if (isblack==true)
            {allchess[x][y]=1;
            Isblack=false;
          Message= "Turn to the white side";
            else {allchess[x][y]=2;
            Isblack=true;
          Message= "Turn to black side"; //Determine if this piece is connected to 5 of other pieces, that is, to determine whether the game is over Boolean WinflaG=this.checkwin (); if (winflag==true) {Joptionpane.showmessagedialog (this, "Game over!") "+ (allchess[x][y]==1?") Black side ": White side") + "win."
            ");
          Canplay=false; } else {Joptionpane.showmessagedialog (this, "there is already a pawn in the current position, please drop!")
        ");
    } this.repaint (); //Click, Game Start button//Restart new game if (E.getx () >=400&&e.getx () <=470&&e.gety () >=8
      0&&e.gety () <=110) {int Result=joptionpane.showconfirmdialog (this, "Setup completed, restart game?"); 
        The information in the IF (result==0) {//re-start operation, allchess[][] array is all 0//empty board for (int i = 0; i < i++)
          {for (int j = 0; J < J + +) {Allchess[i][j] = 0;
        }//Another way allchess = new int[19][19];
        Blacktime = MaxTime;
        Whitetime = MaxTime; if (MaxTime > 0) {blackmessage = MaxTime/3600 + ":" + (maxtime/60-maxtime/3600 *) + ":" + (MAXTIME-MAXTIME/60 * 60); Whitemessage = maxtime/3600 + ":" + (maxtime/60-maxtime/3600 *) + ":" + (MA
          XTIME-MAXTIME/60 * 60);
        T.resume ();
          else {blackmessage = "unrestricted";
        Whitemessage = "Unrestricted";
        } message = "Black side First";
        Isblack = true; 
        This.canplay = true;
      This.repaint (); }//Click the Game Settings button if (E.getx () >=400&&e.getx () <=470&&e.gety () >=130&&e.gety ()
      <=160) {String input = Joptionpane. Showinputdialog ("Please enter the maximum time (in minutes) of the game, if you enter 0, there is no time limit:");
        try {maxtime = integer.parseint (input) * 60;
        if (MaxTime < 0) {Joptionpane.showmessagedialog (this, "Please enter the correct information, do not allow negative numbers!"); } if (MaxTime = = 0) {int result = Joptionpane.showConfirmdialog (This, "Setup completes, do you want to restart the game?"); if (result = = 0) {for (int i = 0; i < i++) {for (int j = 0; J & Lt 19;
              J + +) {Allchess[i][j] = 0;
            }//Another way allchess = new int[19][19];
            Message = "Black side First";
            Isblack = true;
            Blacktime = MaxTime;
            Whitetime = MaxTime;
            Blackmessage = "Unrestricted";
            Whitemessage = "Unrestricted"; 
            This.canplay = true;
          This.repaint ();
              } if (MaxTime > 0) {int result = Joptionpane.showconfirmdialog (this,
          "Set to complete, do you want to restart the game?"); if (result = = 0) {for (int i = 0; i < i++) {for (int j = 0; J &L T 19;
              J + +) {Allchess[i][j] = 0; }//Another way allchess = new INT[19][19];
            Message = "Black side First";
            Isblack = true;
            Blacktime = MaxTime;
            Whitetime = MaxTime; Blackmessage = maxtime/3600 + ":" + (maxtime/60-maxtime/3600 *) + ":" + (Maxtim
            E-MAXTIME/60 * 60); Whitemessage = maxtime/3600 + ":" + (maxtime/60-maxtime/3600 *) + ":" + (Maxtim
            E-MAXTIME/60 * 60);
            T.resume (); 
            This.canplay = true;
          This.repaint (); '} ' catch (NumberFormatException E1) {//TODO auto-generated catch block Jop
      Tionpane.showmessagedialog (This, "Please enter the information correctly!"); }//Click on the Game Description button if (E.getx () >=400&&e.getx () <=470&&e.gety () >=180&&e.gety () <=210) {Joptionpane.showmessagedialog (this, "a Gobang game program, the black and white sides play chess, when one side to five, the game is over.")
    "); //Click the Concede button if (E.getx () >=400&&amP;e.getx () <=470&&e.gety () >=280&&e.gety () <=310) {int result=joptionpane.showconfirmdial OG (This, "Is it OK to admit defeat?)
      ");
        if (result = = 0) {if (isblack) {Joptionpane.showmessagedialog (this, "Black side has conceded, game over!");
        else {Joptionpane.showmessagedialog (this, "White side has conceded, game over!");
      } Canplay = false; }//Click on button if (E.getx () >=400&&e.getx () <=470&&e.gety () >=330&&e.gety () &
    lt;=360) {Joptionpane.showmessagedialog (this, "The game is produced by the South Wood studio, there are related questions to visit www.yiyiinformation.com"); //Click exit button if (E.getx () >=400&&e.getx () <=470&&e.gety () >=380&&e.gety () <=41
      0) {Joptionpane.showmessagedialog (this, "Game Over");
    System.exit (0); }//************************//@Override public void mouseentered (MouseEvent arg0) {//
 TODO auto-generated Method Stub    
  @Override public void mouseexited (MouseEvent arg0) {//TODO auto-generated method stub} @Override public void mousepressed (MouseEvent arg0) {//TODO auto-generated method stub} @Overrid e public void mousereleased (MouseEvent arg0) {//TODO auto-generated ' stub} @Override public V OID Run () {if (MaxTime > 0) {while (true) {if (isblack) {Blackti
          me--;
          if (blacktime = 0) {Joptionpane.showmessagedialog (this, "Black side timeout, game over!");
          } else {whitetime--;
          if (whitetime = 0) {Joptionpane.showmessagedialog (this, "Bai Fangsu when the game is over!");
            } blackmessage = blacktime/3600 + ":" + (BLACKTIME/60-BLACKTIME/3600 * 60) + ":"
        + (BLACKTIME-BLACKTIME/60 * 60);
  Whitemessage = whitetime/3600 + ":"          + (whitetime/60-whitetime/3600 *) + ":" + (WHITETIME-WHITETIME/60 * 60);
        This.repaint ();
        try {thread.sleep (1000); catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktra
        CE ();
      } System.out.println (Blacktime + "--" + whitetime);
   
 
   
   }
    }
  }
     
}

Demo diagram:

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

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.