A preface.

Just learning Java, last time I wrote a random name extractor,Click to view, took part in the plan, the force is small make up recommend to the home page, for the first time to see what they write on the front page, is happy to see the moment of real at that time, actually we learn many things, just need a little encouragement and valued, and yes, I am such people, by the official recommendation, my little heart got a sense of accomplishment, Being recognized is indeed a wonderful thing, so I made up my mind to challenge the more difficult backgammon, but due to my limited ability, so I only wrote everyone versus, this period is also encountered numerous bugs, so I always think that writing comprehensive program, have further tips for their own technology. In the last written randomly, someone give me a message, said that if you add the extract can be displayed in the process of dynamic name was better, then I also wanted to think seriously, as the saying goes, began in level, appearance, and appearance level, namely justice before hand code code, so I have the experience, I use PS serious all the resources I need to be ready. Here it is:I also found the background of the game, the falling pieces when there will be music, because of the dynamic figure recorded, so can’t give you present here music effect, here are the game effect and flow chart, because the code may be tedious, I have prepared a flow chart to help you to understand, as for the code, I will comment clear as far as possible, as for all the needed resources, Of course, if you do not have points, I can understand you, you can add my QQ, I send to you for free, but prohibited white whoring, original is not easy, remember to pay attention to me, your attention is the power of my creation! I hope this article can enter the home page again! Let’s take a look at the game effect.


Second, the game effect

How’s it going? Not bad. Hey, hey, hey.


Iii. Flow chart

In order to help readers understand the process as much as possible, I have written this flow chart, which is not really a flow chart, but a simplified knowledge system.


Iv. Judging winning and losing (key points)

The board can be considered as four directions: horizontal, vertical, left skew, right skew! [insert picture description here] (img – blog. Csdnimg. Cn / 20200314175… =300x) we don’t need to go through all the pieces’ information every time, it would be too much work. No one is so stupid as to play five pieces in a row and then continue playing. What we can determine is that there will be no five pieces in a row before a piece is placed, so we only need to judge the last piece, such as in the horizontal direction: We can obtain the coordinates of the pieces, and then through the horizontal pieces pieces left, if there are pieces and the pawns of continuous attributes (color), add a number of initial is 1, then traverse the right horizontal pieces, when after the traverse if the quantity is greater than or equal to 5 is proved five renju game victory.

int count =1;
  for(int posX =x-1; posX>=0; posX--) {if(myArrayGame[posX][y].getBW()==White_Black&&myArrayGame[posX][y].getTF()==1) {
    count_x++;
    if(count_x>=5) {
     return true; }}else {
    break; }}// Go right again
  for(int posX =x+1; posX<14; posX++) {if(myArrayGame[posX][y].getBW()==White_Black&&myArrayGame[posX][y].getTF()==1) {
    count_x++;
    if(count_x>=5) {
     return true; }}else {
    break; }}Copy the code

Five. Code implementation

// First is the package to be used, and last is the third party package, which I have packed
import java.awt.*;
import java.awt.event.*;  
import javax.swing.*;
import java.net.URL; 
import java.net.URI;
import java.lang.*;
import java.applet.*;
import java.io.*;
import javazoom.jl.player.*;

// Then there are two classes, which act as structures in C language, both of which are used to store the data of the chess pieces, but there are differences

class ArrayGame{
 private  int x;
 private  int y;
 private  int true_flase;
 private  boolean Black_White;
 public ArrayGame(int x,int y,int TF,boolean BW){
  this.x=x;
  this.y=y;
  this.true_flase=TF;
  this.Black_White=BW;
 }
    public boolean getBW(a) {
     return this.Black_White;
    }
    public void setTF(int TF) {
z     this.true_flase =TF;
    }
    public int getTF(a) {
     return this.true_flase; }}class ArrayLabel{
 private static Icon WhiteIcon = new ImageIcon("E:\\dianmingqi\\call\\lib\\baiqi.png");/ / white
 private static Icon BlackIcon = new ImageIcon("E:\\dianmingqi\\call\\lib\\heiqi.png");/ / black
 private  int x;
 private  int y;
 private  boolean Black_White;
 private  JLabel myjlabel =new JLabel();
 public  ArrayLabel(int x,int y ,boolean Black_White) {
  this.x=x;
  this.y=y;
  this.Black_White=Black_White;
  if(Black_White==true) {
   myjlabel.setIcon(WhiteIcon);
  }
  else {
   myjlabel.setIcon(BlackIcon);
  }
  
  myjlabel.setBounds(21+x*54.20+y*54.54.54);
 }
 public void setArrayLabel(int x,int y ,boolean Black_White) {
  this.x=x;
  this.y=y;
  this.Black_White=Black_White;
 }
 public JLabel getLabel(a) {
  return this.myjlabel;
 }
 public int getX(a) {
  return this.x; 
 }
 public int getY(a) {
  return this.y; 
 }
 public boolean getBlack_White(a) {
  returnBlack_White; }}// This is the main class that inherits JFrame and uses the Runnable interface to create multithreading
public class gobang extends JFrame implements Runnable {
 private static Player player =null;
 // Each button has two images, the first one represents the unpressed state, and the second one represents the pressed state, which adds visual sensation
 private static Icon StartIcon1 =new ImageIcon("E:\\dianmingqi\\call\\lib\\kaishiyouxi.jpg");// Start the game
 private static Icon StartIcon2 =new ImageIcon("E:\\dianmingqi\\call\\lib\\kaishiyouxi2.jpg");
 private static Icon EndIcon1 = new ImageIcon("E:\\dianmingqi\\call\\lib\\jieshuyouxi.jpg");// End the game
 private static Icon EndIcon2 = new ImageIcon("E:\\dianmingqi\\call\\lib\\jieshuyouxi2.jpg");
 private static Icon PauseIcon1 = new ImageIcon("E:\\dianmingqi\\call\\lib\\zantingyouxi.jpg");// Pause the game
 private static Icon PauseIcon2 = new ImageIcon("E:\\dianmingqi\\call\\lib\\zantingyouxi2.jpg");
 private static Icon PersonIcon1 = new ImageIcon("E:\\dianmingqi\\call\\lib\\renrenduizhan.jpg");// Everyone against everyone
 private static Icon PersonIcon2 = new ImageIcon("E:\\dianmingqi\\call\\lib\\renrenduizhan2.jpg");
 private static Icon MachineIcon1 = new ImageIcon("E:\\dianmingqi\\call\\lib\\renjiduizhan.jpg");// Man-machine vs. machine
 private static Icon MachineIcon2 = new ImageIcon("E:\\dianmingqi\\call\\lib\\renjiduizhan2.jpg");
 private static Icon AboutIcon1 = new ImageIcon("E:\\dianmingqi\\call\\lib\\guanyu.jpg");/ / about
 private static Icon AboutIcon2 = new ImageIcon("E:\\dianmingqi\\call\\lib\\guanyu2.jpg");
 private static Icon BackIcon1 = new ImageIcon("E:\\dianmingqi\\call\\lib\\huiqi.jpg");/ / back
 private static Icon BackIcon2 = new ImageIcon("E:\\dianmingqi\\call\\lib\\huiqi2.jpg");
 private static ArrayLabel[] myArrayLabel =new ArrayLabel[200];// Create an array to store the sequence information
 private static ArrayGame[][] myArrayGame =new ArrayGame[14] [14];// Create an array to store the sequence information
 private static JButton Start_end_Button = new JButton(); // Start the game button
 private static JButton Pause_Butten = new JButton();// Pause button
 private static JButton Person_Person_Button = new JButton();// Everyone vs button
 private static JButton Person_Machine_Button = new JButton();// Man-machine versus button
 private static JButton Back_Button = new JButton();// Undo button
 private static JButton About_Button = new JButton();// About buttons
 private static JLabel Countdown_Label = new JLabel();// Label countdown
 private static JLabel Now_Label = new JLabel();// The tag shows which side to play chess on
 private static JLabel Show_Label = new JLabel("Current mode:");// The label displays the current mode
 private static JTextField Countdown_TextField =new JTextField();// The text box displays the countdown
 private static Font font = new Font("Founder big Black simplified",Font.BOLD,20); // Set the font size
 private static Font font_2 = new Font("Founder big Black simplified",Font.BOLD,15); // Set the font size
 private static JFrame jf=null; // Create a form
 private static Container c =null;// Create a container
 private static boolean Combat_TF =false;// Determine whether to select battle mode
 private static boolean Person_Machine_TF =true;// Judge the choice of mode, really for everyone
 private static boolean Showboonum=true; // True for everyone, false for man-machine
 private static volatile boolean boonum=false;// When this variable is true, chess can be played, if false, pause mode
 private static boolean White_Black_Label=true;// Set black and white
 private static volatile int time =0; // Set the countdown
 private static volatile int ss =time % 60;
 private static int sum = 0;// Record the array usage


// Determine the winner
public boolean getPerson_Person_combat(int x,int y,boolean White_Black) {
  // Get information about this piece
     // horizontal traversal
  // Go left first
    int count_x = 1;
    int count_y = 1;
    int count_1 = 1;
    int count_2 = 1;
  for(int posX =x-1; posX>=0; posX--) {if(myArrayGame[posX][y].getBW()==White_Black&&myArrayGame[posX][y].getTF()==1) {
    count_x++;
    if(count_x>=5) {
     return true; }}else {
    break; }}// Go right again
  for(int posX =x+1; posX<14; posX++) {if(myArrayGame[posX][y].getBW()==White_Black&&myArrayGame[posX][y].getTF()==1) {
    count_x++;
    if(count_x>=5) {
     return true; }}else {
    break; }}// Vertical direction
  // Walk up first
   for(int posY = y - 1; posY >= 0; posY--) {
    if(myArrayGame[x][posY].getBW()==White_Black&&myArrayGame[x][posY].getTF()==1) {
     count_y++;
     if(count_y>=5) {
      return true; }}else {
     break; }}// walk down
   for(int posY = y + 1; posY <14; posY++) {
    if(myArrayGame[x][posY].getBW()==White_Black&&myArrayGame[x][posY].getTF()==1) {
     count_y++;
     if(count_y>=5) {
      return true; }}else {
     break; }}// Two diagonal traversals
  // Rule # 1: Judge upper left
   for(int posX = x - 1, posY = y - 1; posX >= 0 && posY >= 0; posX--, posY--) {
    if(myArrayGame[posX][posY].getBW()==White_Black && myArrayGame[posX][posY].getTF()==1) {
     count_1++;
     if(count_1>=5) {
      return true; }}else {
     break; }}// Determine the lower right
   for(int posX = x + 1, posY = y + 1; posX < 14 && posY < 14; posX++, posY++) {
    if(myArrayGame[posX][posY].getBW()==White_Black && myArrayGame[posX][posY].getTF()==1) {
     count_1++;
     if(count_1>=5) {
      return true; }}else {
     break; }}// Second: lower left
   for(int posX = x + 1, posY = y - 1; posX < 14 && posY >=0; posX++, posY--) {
    if(myArrayGame[posX][posY].getBW()==White_Black && myArrayGame[posX][posY].getTF()==1) {
     count_1++;
     if(count_1>=5) {
      return true; }}else {
     break; }}// Judge the upper right
   for(int posX = x - 1, posY = y + 1; posX >=0 && posY <14; posX--, posY++) {
    if(myArrayGame[posX][posY].getBW()==White_Black && myArrayGame[posX][posY].getTF()==1) {
     count_1++;
     if(count_1>=5) {
      return true; }}else {
     break; }}return false;
 }

// Initialize the array
 public void getArratGame(a) {
  for(int i=0; i<14; i++) {for(int j=0; j<14; j++) { myArrayGame[i][j]=new ArrayGame(0.0.0.true); }}}// Create a form
 public  void getwindows(a) {
  jf = new JFrame("Dog Backgammon.");
  jf.setIconImage(new ImageIcon("Icon.jpg").getImage());
  ImageIcon img =new ImageIcon("lib\\qipan.jpg");
  jf.setLayout(null);// Causes the form to unset the layout manager
  JLabel imgLabel = new JLabel(img);
  jf.getLayeredPane().add(imgLabel,new Integer(Integer.MIN_VALUE));
  imgLabel.setSize(1090.800);
      c =jf.getContentPane();
  Cursor cursor =Toolkit.getDefaultToolkit().createCustomCursor(new ImageIcon("E:\\dianmingqi\\call\\lib\\shouzhi.png").getImage(),new Point(0.0), "stick");
        jf.setCursor(cursor);
  ((JPanel)c).setOpaque(false);
  Countdown_Label.setFont(font);// Set the font
  Now_Label.setFont(font);// Set the font
  Show_Label.setFont(font);// Set the font
  Countdown_TextField.setFont(font_2);
  Countdown_Label.setHorizontalAlignment(SwingConstants.CENTER);
  Countdown_Label.setText("Countdown:");
  Now_Label.setHorizontalAlignment(SwingConstants.CENTER); 
  Countdown_TextField.setBackground(Color.orange);
  // Set the button position
  Start_end_Button.setBounds(870.150.143.47);
  Pause_Butten.setBounds(870.150.143.47);
  Show_Label.setBounds(855.240.200.30);
  Now_Label.setBounds(870.275.150.30);
  Countdown_Label.setBounds(870.310.90.30);
  Countdown_TextField.setBounds(950.310.60.26);
  Person_Person_Button.setBounds(870.370.143.47);
  Person_Machine_Button.setBounds(870.480.143.47);
  Back_Button.setBounds(870.590.143.47);
  About_Button.setBounds(870.700.143.47);
  Start_end_Button.setIcon(StartIcon1);
  Pause_Butten.setIcon(PauseIcon1);
  Person_Person_Button.setIcon(PersonIcon1);
  Person_Machine_Button.setIcon(MachineIcon1);
  Back_Button.setIcon(BackIcon1);
  About_Button.setIcon(AboutIcon1);
  c.add(Start_end_Button);
  c.add(Show_Label);
  c.add(Now_Label);
  c.add(Countdown_Label);
  c.add(Countdown_TextField);
  c.add(Person_Person_Button);
  c.add(Person_Machine_Button);
  c.add(Back_Button);
  c.add(About_Button);
  jf.setSize(1100.840);
  jf.setResizable(false);
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  jf.setVisible(true); 
 }
// Generate chess pieces
public  void getLabelShow(int x,int y ,boolean Black_White,int sum_c) {
  // Set the checkerboard up to a two-dimensional coordinate
  // Check whether the coordinates are empty when creating
  // Put all the pieces together, using the last in, first out rule
  if(myArrayGame[x][y].getTF()==0) {
  myArrayLabel[sum_c]=new ArrayLabel(x, y, Black_White);
  myArrayGame[x][y] = new ArrayGame(x,y,1,Black_White);
  if(White_Black_Label==true) {
  Now_Label.setText("The sunspot plays chess....");
   White_Black_Label=false;
  }
  else {
  Now_Label.setText("This white son plays chess....");
   White_Black_Label=true;
  }
  c.add(myArrayLabel[sum_c].getLabel());
  getRefreshWindows();
  try {
      File f = new File("E:\\dianmingqi\\call\\lib\\xiaqi1.mp3");
      FileInputStream fileInputStream = new FileInputStream(f);
      BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
      player = new Player(bufferedInputStream);
      player.play();// Load the music and play
   }catch(Exception e) {
    e.printStackTrace();
   }
  sum++;
  boolean b = getPerson_Person_combat(x,y,Black_White);
  if(b==true) {
   JOptionPane about =new JOptionPane();
   about.showMessageDialog(null."Congratulations, you've won the competition!"); }}}/ / back
public void getBackLabel(a) {
  // Make sure that you don't
  if(sum! =0) {
  int sum_c =--sum;
  c.remove(myArrayLabel[sum_c].getLabel());
  getRefreshWindows();
  int x = myArrayLabel[sum_c].getX();
  int y = myArrayLabel[sum_c].getY();
  myArrayGame[x][y].setTF(0);
  }
  else {
   JOptionPane showMessage =new JOptionPane();
   showMessage.showMessageDialog(null."No game can be destroyed."); }}// Listen for mouse events
public  void getMouse(a) { 
Start_end_Button.addMouseListener(new MouseListener() {
   public void mouseEntered(MouseEvent e) {// Triggered when moving components in
    Start_end_Button.setIcon(StartIcon2);
   }
   public void mousePressed(MouseEvent e) {// The mouse button is triggered when pressed
   }
   public void mouseReleased(MouseEvent e) {// Triggered when the mouse button is released
    if(Combat_TF==true) {
     c.remove(Start_end_Button);
     c.add(Pause_Butten);
     time =1800;
     boonum =true;
     getWindowsMouse();
     getRefreshWindows();
     // The mouse button is released
     Now_Label.setText("This white son plays chess....");
    }
    else {
     JOptionPane about =new JOptionPane();
     about.showMessageDialog(null."Please select battle mode first."); }}public void mouseClicked(MouseEvent e) {// Triggered when a click event occurs
   }
   public void mouseExited(MouseEvent e) {// Triggered when the component is removedStart_end_Button.setIcon(StartIcon1); }}); Pause_Butten.addMouseListener(new MouseListener() {
   public void mouseEntered(MouseEvent e) {
    Pause_Butten.setIcon(PauseIcon2);
   }
   public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {
    c.remove(Pause_Butten);
    c.add(Start_end_Button);
    boonum =false;
    getWindowsMouse();
    getRefreshWindows();
    // The mouse button is released
   }
   public void mouseClicked(MouseEvent e) {}public void mouseExited(MouseEvent e) { Pause_Butten.setIcon(PauseIcon1); }}); Person_Person_Button.addMouseListener(new MouseListener() {
   public void mouseEntered(MouseEvent e) {
    Person_Person_Button.setIcon(PersonIcon2);
   }
   public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {
    Show_Label.setText("Current Mode: Everyone Vs. Everyone");
    Combat_TF=true;
    Person_Machine_TF=true;
   }
   public void mouseClicked(MouseEvent e) {}public void mouseExited(MouseEvent e) { Person_Person_Button.setIcon(PersonIcon1); }}); Person_Machine_Button.addMouseListener(new MouseListener() {
   public void mouseEntered(MouseEvent e) {
    Person_Machine_Button.setIcon(MachineIcon2);
   }
   public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {
    Show_Label.setText("Current mode: Man versus machine");
    Combat_TF=true;
    Person_Machine_TF=false;
   }
   public void mouseClicked(MouseEvent e) {}public void mouseExited(MouseEvent e) { Person_Machine_Button.setIcon(MachineIcon1); }}); Back_Button.addMouseListener(new MouseListener() {
   public void mouseEntered(MouseEvent e) {
    Back_Button.setIcon(BackIcon2);
   }
   public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {
    getBackLabel();
   }
   public void mouseClicked(MouseEvent e) {}public void mouseExited(MouseEvent e) { Back_Button.setIcon(BackIcon1); }}); About_Button.addMouseListener(new MouseListener() {
   public void mouseEntered(MouseEvent e) {
    About_Button.setIcon(AboutIcon2);
   }
   public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {
    JOptionPane jo =new JOptionPane();
    jo.showMessageDialog(null."Use Java Swing to write \n More resources follow my blog: fdogcsdn.com\nQQ/WX: 2506897252");
   }
   public void mouseClicked(MouseEvent e) {}public void mouseExited(MouseEvent e) { About_Button.setIcon(AboutIcon1); }}); }// form listening event
 public  void getWindowsMouse(a) {
  jf.addMouseListener(new MouseListener() {
   public void mouseEntered(MouseEvent e) {}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}public void mouseClicked(MouseEvent e) {
    // Get the mouse click event. When the mouse clicks, get the coordinates
    if(boonum==true) {
    if(19<=(e.getX()-30) && (e.getX()-30) < =725 
    && 18<=(e.getY()-50) && (e.getY()-50) < =724) {
    for(int i=0; i<=13; i++) {for(int j=0; j<=13; j++) {if(((20+i*54-27)<=(e.getX()-30)&&(e.getX()-30) < = (21+i*54+27)) && ((20+j*54-27)<=(e.getY()-50)&&(e.getY()-50) < = (20+j*54+27))) {  
    time =1800;
    getLabelShow(i,j,White_Black_Label,sum);
    break;
     }
    }
    }
    }
    }
   }
   public void mouseExited(MouseEvent e) {}}); }// Refresh the form
 public  void getRefreshWindows(a) {
  jf.validate();
  jf.repaint();
 }
// Multithreading is used to display the countdown
 public  void run(a) {
  while(true) {
  while(time>0) {
   if(boonum==false) {}else {
    time--;
   }
   try {
   Thread.sleep(1000);
               ss = (time % 60);
              String str =""+ss+"Seconds";
              if(ss==0) {}else {
               if(ss-1= =0) {
                Countdown_TextField.setText(str);
                 JOptionPane jo =new JOptionPane();
            jo.showMessageDialog(null."I'm sorry you lost!");
            time=0;
               }
               else{ Countdown_TextField.setText(str); }}}catch(Exception e1) { e1.printStackTrace(); }}}}public static void main(String[] args) {
  gobang go = new gobang();
  Thread myThread_1 = new Thread(go,Thread 1 "");
  myThread_1.start();
  go.getArratGame();
  go.getwindows();
  go.getMouse();
  Music music = newMusic(); music.getMusic(); }}// The last class plays music
mport java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import javazoom.jl.player.*;
public class Music {
 private static Player player =null;
 public void getMusic(a) {
 try {
     File f = new File("E:\\dianmingqi\\call\\lib\\xiaqi2.mp3");
     FileInputStream fileInputStream = new FileInputStream(f);
     BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
     player = new Player(bufferedInputStream);
     player.play();
  }catch(Exception e) { e.printStackTrace(); }}}Copy the code

If there are mistakes, welcome to criticize, welcome to comment. The firmness of the goal is one of the most necessary sources of strength in character, and one of the tools of success. Without it, genius will be in vain in the contradictory and uncertain path. = =