Java implements backgammon mini game
package Gomoku; import java.awt.Toolkit; import javax.swing.JFrame; public class GomokuFrame extends JFrame { //Define an operation panel OperatorPane op=null; public GomokuFrame() { //Set the name this.setTitle("Gomoku" ); //Set the window size this.setSize(510,510); //Set the window position//Get the 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+"/ncomputer Screen height:/n"+computer_height); //Center this.setLocation((computer_width-510)/2, (computer_height-510)/2); //Instantiate the curtain op=new OperatorPane(); //Import the curtain this.add(op); //Add Mouse listening this.addMouseListener(op); //Set the window display this.setVisible(true); //Set the normal closing of the window this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //Execute the 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{ //Define the background image object Image imageBackground = null; //Define the number of rows of the checkerboard grid int boardrows=18; //define the number of columns of the chessboard grid int boardcols=18; //define the size of the chessboard grid int boardsize=20; //Save the coordinates of the chess pieces int x=0,y=0; //Save the coordinates of all the chess pieces played before//The data content 0: means that there is no chess piece at this point, 1: means that this point is a black piece, 2: Indicates that this point is a white piece int allchess[][]=new int [19][19]; //Mark whether the next step is black or white boolean isBlack=true; //Judge whether the game can continue boolean canPlay=true ; //Save the displayed information String message="Black goes first"; //Save the maximum time (seconds) int maxTime = 0; //Thread class for countdown Thread t = new Thread(this); //Save Black Remaining time with White int blackTime = 0; int whiteTime = 0; // Save the display information of the remaining time of both parties String blackMessage = "Unlimited"; String whiteMessage = "Unlimited"; @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_INT_ARGB); Graphics g2=b1.createGraphics(); //Draw the background image 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 the three-three 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 additional points 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 Tianyuan g2 .fillOval(187, 227, 6, 6); //Draw text prompt/*Font f=new Font("黑体", Font.BOLD, 24); g.setFont(f);*/ g2.setFont(new Font("黑体", Font.BOLD, 20)); g2.setColor(Color.BLACK); g2.drawString("Game information:"+message, 130, 40); g2.setFont(new Font("宋体", Font.ITALIC, 15)); g2.drawString("Black time:"+blackMessage,25, 445); g2.drawString("White time:"+whiteMessage,245, 445); //Draw all chess pieces for(int i= 0;i<=boardrows;i++) { for(int j=0;j<=boardcols;j++) { //Storage black chess if(allchess[i][j]==1) { int tempX=i*20-10; int tempY=j*20+30; //Draw black g2.setColor(Color.BLACK); g2.fillOval(tempX+12, tempY+13, 15, 15) ; } //Storage white chess if(allchess[i][j]==2) { int tempX=i*20-10; int tempY=j*20+30; //Draw the white piece 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 all the same How many chess pieces of the color are connected int count1=1; int count2=1; int count3=1; int count4=1; // Determine whether there are 5 chess pieces connected in the horizontal direction. The vertical coordinates of the characteristics are the same. That is, the y values in allChess[x][y] are the same // Use a loop to determine whether the chess pieces are connected int i = 1; while (color == allchess[x+i][y]) { count1++; i++; } / /Reset i value i = 1; while (color == allchess[xi][y]) { count1++; i++; } if(count1 >= 5) { flag = true; } //Judge the vertical direction, that is, the x values in allChess[x][y] are the same int j = 1; while (color == allchess[x][y+j]) { count2++; j++; } //Reset the j value j = 1; while (color == allchess[x][yj]) { count2++; j++; } if(count2>= 5) { flag = true; } //Judge the oblique direction "/" 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; } //Judge the oblique direction "/" 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; //Horizontal judgment count=this.checkCount(1, 0, color); if(count>=5) { flag = true; } else { //Vertical judgment count=this.checkCount(0, 1, color); if (count>=5) { flag = true; } else { //Slant "/" count=this.checkCount(1, 1, color); if(count>=5) { flag = true; } else { / /slant "/" 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("The point is on the board. "); ==0) { if(isBlack==true) { allchess[x][y]=1; isBlack=false; message="White's turn"; } else { allchess[x][y]=2; isBlack =true; message="Black's turn"; } // Determine whether this chess piece is connected with other chess pieces in a 5-connection, that is, whether the game is over. boolean winFlag=this.checkWin(); if(winFlag==true) { JOptionPane.showMessageDialog (this,"Game over!"+ (allchess[x][y]==1?"Black":"White")+"Win."); canPlay=false; } } else { JOptionPane.showMessageDialog(this, "There is already a chess piece at the current position, please place it again!"); } } this.repaint(); } //Click, game start button //Restart a new game if(e.getX() >=400&&e.getX()<=470&&e.getY()>=80&&e.getY()<=110) { int result=JOptionPane.showConfirmDialog(this, "Settings completed, do you want to restart the game?"); if(result==0) { //Restart operation, all information in the allchess[][] array is 0 //Clear chessboard for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; 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 * 60) + ":" + (maxTime - maxTime / 60 * 60); whiteMessage = maxTime / 3600 + ":" + (maxTime / 60 - maxTime / 3600 * 60) + ":" + (maxTime - maxTime / 60 * 60); t.resume(); } else { blackMessage = "Unlimited"; whiteMessage = "Unlimited"; } message = "Black goes 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 game The maximum time (unit: minutes), if you enter 0, it means there is no time limit: "); try { maxTime = Integer.parseInt(input) * 60; if (maxTime < 0) { JOptionPane.showMessageDialog(this, "Please enter correct information, negative numbers are not allowed!"); } if (maxTime == 0) { int result = JOptionPane.showConfirmDialog(this, "Setup completed ,Restart the game?"); if (result == 0) { for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) { allchess[i][j] = 0; } } // Another way allChess = new int[19][19]; message = "Black goes first"; isBlack = true; blackTime = maxTime; whiteTime = maxTime; blackMessage = "No limit"; whiteMessage = "No limit"; this.canPlay = true; this.repaint(); } } if (maxTime > 0) { int result = JOptionPane.showConfirmDialog(this, "Settings completed, do you want to restart the game?"); if (result == 0) { for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) { allchess[i][j] = 0; } } // Another way allChess = new int[19][19]; message = "Black goes first"; isBlack = true; blackTime = maxTime; whiteTime = maxTime; blackMessage = maxTime / 3600 + ":" + (maxTime / 60 - maxTime / 3600 * 60) + ":" + (maxTime - maxTime / 60 * 60); whiteMessage = maxTime / 3600 + ":" + (maxTime / 60 - maxTime / 3600 * 60) + ":" + (maxTime - maxTime / 60 * 60); t.resume(); this.canPlay = true; this.repaint(); } } } catch (NumberFormatException e1) { / / TODO Auto-generated catch block JOptionPane.showMessageDialog(this, "Please enter the information correctly!"); } } //Click the game description button if(e.getX()>=400&&e.getX()<=470&&e.getY()>=180&&e.getY()<=210) { JOptionPane.showMessageDialog(this, "This is a backgammon game Program, the black and white sides take turns to play chess. When one side reaches five pieces in a row, the game ends. "); } //Click the admit defeat button if(e.getX()>=400&&e.getX()<=470&&e.getY()>=280&&e.getY()<=310) { int result=JOptionPane.showConfirmDialog(this ,"Are you sure you want to admit defeat?"); if (result == 0) { if (isBlack) { JOptionPane.showMessageDialog(this, "Black has given in, the game is over!"); } else { JOptionPane.showMessageDialog(this, "White has given in, the game is over!"); } canPlay = false; } } //Click the About button if(e.getX()>=400&&e.getX()<=470&&e.getY()>=330&&e.getY()<=360) { JOptionPane.showMessageDialog(this,"This game is provided by Nanmu Produced by the studio, if you have related questions, please visit www.yiyiinformation.com"); } //Click the exit button if(e.getX()>=400&&e.getX()<=470&&e.getY()>=380&&e.getY()<=410) { 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 } @Override public void mouseReleased (MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void run() { if (maxTime > 0) { while (true) { if (isBlack) { blackTime--; if (blackTime == 0) { JOptionPane.showMessageDialog(this, "Black has timed out, the game is over!"); } } else { whiteTime--; if (whiteTime == 0) { JOptionPane.showMessageDialog(this, "White has timed out, the game is over!"); } } blackMessage = blackTime / 3600 + ":" + (blackTime / 60 - blackTime / 3600 * 60) + ":" + (blackTime - blackTime / 60 * 60); whiteMessage = whiteTime / 3600 + ":" + (whiteTime / 60 - whiteTime / 3600 * 60) + ":" + (whiteTime - whiteTime / 60 * 60); this.repaint(); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(blackTime + " -- " + whiteTime); } } } }
Demo picture:
The above is the entire content of this article. I hope it will be helpful to everyone to master Java proficiently.