Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Gobang Game by Java

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to achieve Gobang in Java". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Brief introduction

Compared to before, the following changes have been made:

1. Added menu bar and moved the restart and exit buttons to the menu bar

two。 Time can be displayed in real time (multithreaded)

3. When playing chess, you can show which side is currently playing chess.

4. You can change the background color

5. You can change the advance party (the default sunspot).

Result

Complete code

1.Frame.java (main interface)

Extension class of package Gobang;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.awt.image.*;//import java.applet.*;//import java.net.*;//import java.io.*;//import javax.imageio.*;public class Frame extends JFrame implements MouseListener,ActionListener {/ / JFrame / / ImageIcon image; / / JLayeredPane layeredPane; / / JPanel jp / * it was intended to be used to play background music, but it was not successful. Temporarily abandon File f; URI uri; URL url; @ SuppressWarnings ("deprecation") * / private static final long serialVersionUID = 1L; public JButton AdmitDefeatButton,RegretButton;// has its own functions. JLabel TimeLabel;// is used to display the time JLabel jl1,jl2,jl3;// game information Graphics gumbnail / brushes BufferedImage buf; int x racquets / mouse coordinates int y; int [] [] Chess = new int [20] [20]; / / save chess pieces, 1 for sunspots, 2 for white boolean IsBlack = true / / indicates whether the current game is to be played with sunspots or whites, true for sunspots, false for boolean IsFinish = false; / / indicates whether the current game is over, int xRange; int yRange; int [] chessX = new int [400]; / / it is used to save all the pieces from the beginning to the present, for repentance. Int [] chessY = new int; int countX = 0; int countY = 0; / / menu bar JMenuBar menubar; JMenu menu; JMenu setmenu; JMenuItem RestartItem,ExitItem,IntroItem,BackgroundItem,FirstItem; / / get the width and height of the screen Toolkit kit = Toolkit.getDefaultToolkit (); Dimension screenSize = kit.getScreenSize (); int screenWidth = screenSize.width; int screenHeight = screenSize.height Public Frame () {/ * insert background image / / layeredPane=new JLayeredPane (); / / image=new ImageIcon ("F:\\ JAVA\\ eclipse-workspace\\ Gobang\\ src\\ 1.jpg"); / / find any picture and you can see the effect. / / jp=new JPanel (); / / jp.setBounds (0Magne0600600); / / jl=new JLabel (image); / / jl.setBounds (0memo image.getIconWidth (), image.getIconHeight ()); / / jp.add (jl); / / layeredPane.add (jp,JLayeredPane.DEFAULT_LAYER); / / this.setLayeredPane (layeredPane) * / / * Audio playback part try {f = new File ("); uri = f.toURI (); url = uri.toURL (); AudioClip aau; aau = Applet.newAudioClip (url); aau.loop () / / play} catch (Exception e) {e.printStackTrace ();} * / / set title, size, arrangement, etc. This.setTitle ("Gobang"); this.setSize (600600); this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); this.setVisible (true) This.setLayout (null); int height = this.getHeight (); int width = this.getWidth (); this.setLocation (screenWidth/2-width/2, screenHeight/2-height/2); / / Real-time display time, using multithreading for real-time display. Jl1 = new JLabel (Beijing time); jl1.setLocation (430,120); jl1.setSize (80Magazine 20); this.add (jl1); TimeLabel = new JLabel (); new Thread (new Time (TimeLabel)). Start (); / / create a new thread TimeLabel.setLocation (510,120); TimeLabel.setSize (80Jing 20); this.add (TimeLabel) / / displays game information, who is currently in charge of the game Jl2 = new JLabel ("Game Information"); jl2.setLocation (430,150); jl2.setSize (80Magazine 20); jl3 = new JLabel ("Black comes first"); jl3.setLocation (510,150); jl3.setSize (80Magin20); this.add (jl2); this.add (jl3) / / set the background color this.getContentPane (). SetBackground (new Color (255,239,213); this.getContentPane (). SetVisible (true); / / set menu bar menubar = new JMenuBar (); / / menu bar menu = new JMenu ("Game Operations"); RestartItem = new JMenuItem ("restart") ExitItem = new JMenuItem ("exit"); menu.add (RestartItem); menu.add (ExitItem); menubar.add (menu); setmenu = new JMenu ("setup"); IntroItem = new JMenuItem ("game description"); BackgroundItem = new JMenuItem ("background color"); FirstItem = new JMenuItem ("first party"); setmenu.add (IntroItem) Setmenu.add (BackgroundItem); setmenu.add (FirstItem); menubar.add (setmenu); menubar.setBackground (new Color (249205173)); menubar.setVisible (true); this.setJMenuBar (menubar); / / two buttons to throw in the towel and regret AdmitDefeatButton = new JButton ("throw in the towel"); AdmitDefeatButton.setSize (80Magazine 40); AdmitDefeatButton.setLocation (120,480); RegretButton = new JButton ("repentant Chess"); RegretButton.setSize (80Jing 40); RegretButton.setLocation (240,480); this.add (AdmitDefeatButton); this.add (RegretButton); / * five buttons added to the middle container Panel1 = new JPanel (); panel1.setBorder (BorderFactory.createLoweredBevelBorder ()); / / set the border panel1.setLayout (new GridLayout (1Magne5); panel1.add (RestartButton); panel1.add (SetButton); panel1.add (AdmitDefeatButton); panel1.add (RegretButton); panel1.add (ExitButton); this.add (panel1); panel1.setSize (460Jol 30) Panel1.setLocation (0460); * / this.repaint (); / / indicates that the canvas is redrawn and the paint function can be called automatically / / this class acts as a listening class, including mouse monitoring and button action monitoring This.addMouseListener (this); IntroItem.addActionListener (this); BackgroundItem.addActionListener (this); FirstItem.addActionListener (this); RestartItem.addActionListener (this); AdmitDefeatButton.addActionListener (this); RegretButton.addActionListener (this); ExitItem.addActionListener (this) } / / canvas painting public void paint (Graphics g) {if (g = = null) / / if you paint for the first time, create a new picture and create a canvas. {buf = new BufferedImage (450,450, BufferedImage.TYPE_INT_RGB); g = buf.createGraphics ();} if (g! = null) / / {super.paint (g); / / draw g.setColor (new Color (249205173)) on the basis of the original image; / / adjust the brush color to brown G.fill3DRect (20,130,400,400 true); / / draw a square with a length of 400 with a margin of 20130 for (int I = 0; I = 130 & & y 10 & & xRange)

< 20) //如果在交叉点的边长为10的范围内,就把棋子下在这; { x = (x - 20) / 20 + 1; } else { x = (x - 20) / 20; } yRange = (y-130)%20; if(yRange >

10 & & yRange

< 20) { y = (y - 130) / 20 + 1; } else { y = (y - 130) / 20; } if(Chess[x][y] == 0) //如果该交叉点没有被下过; { chessX[countX++] = x; //存储当前棋子的位置; chessY[countY++] = y; if(jl3.getText().equals("黑方先行")) //如果是黑子 { Chess[x][y] = 1; IsBlack = false; jl3.setText("白方先行"); } else if(jl3.getText().equals("白方先行")) { Chess[x][y] = 2; IsBlack = true; jl3.setText("黑方先行"); } this.repaint();//重新绘制画布 } if(this.isWin())//如果下棋之后赢了,弹出对话框 { if(Chess[x][y] == 1) { JOptionPane.showMessageDialog(this, "黑方胜利"); } else { JOptionPane.showMessageDialog(this, "白方胜利"); } this.IsFinish = true; //游戏结束 } } } } public boolean isWin(){ boolean flag = false; int count = 1; int color = Chess[x][y]; //判断横向是否有5个棋子相连 count = this.checkCount(1,0,color); if(count >

= 5) {flag = true;} else {/ / judge vertical count = this.checkCount; if (count > = 5) {flag = true;} else {/ / upper right, lower left count = this.checkCount If (count > = 5) {flag = true;} else {/ / judge lower right, upper left count = this.checkCount; if (count > = 5) {flag = true } return flag;} / / check whether the Gobang in the chessboard is connected into five pieces. XChange,yChange is the amount of change relative to the position of the current pieces public int checkCount (int xChange, int yChange, int color) {int count = 1. There are several connected pieces in total. Int tempX = xChange; int tempy = yChange; / / determine whether there are pieces of the same color on the right side of the piece; while (x+xChange > = 0 & & x+xChange = 0 & & y+yChange

< 20 && color == Chess[x+xChange][y+yChange]) { count++; //如果有,棋子数加一 if(xChange != 0) xChange++; //如果横向方向变化,x相对位置加一 if(yChange != 0 ) { if(yChange != 0) { if(yChange >

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report