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 simple Gobang Game with Java

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

Share

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

This article Xiaobian for you to introduce in detail "Java how to achieve simple Gobang game", the content is detailed, the steps are clear, the details are handled properly, I hope this "Java how to achieve a simple Gobang game" article can help you solve your doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

Project structure

This is a resource found on the Internet. I can't remember the provenance. Record it. The overall structure of the program is very simple:

Core code

The code is as follows:

ArrComparator.java class import java.util.Comparator;/** * sort Comparator * / class ArrComparator implements Comparator {int column = 2; int sortOrder =-1; / / decreasing public ArrComparator () {} public int compare (Object a, Object b) {if (an instanceof int []) {return sortOrder * ((int []) a) [column]-(int []) b) [column]) } throw new IllegalArgumentException ("param a must int b must int [].");}} ChessMap.java class import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.net.URL;@SuppressWarnings ("serial") public class ChessMap extends JFrame {private ImageIcon map; / / chessboard background bitmap private ImageIcon blackchess / / sunspot bitmap private ImageIcon whitechess; / / white bitmap private ChessPanel cp; / / chessboard private JPanel east; private JPanel west; private static final int FINAL_WIDTH = 450; private static final int FINAL_HEIGHT = 500; / / the following is the drop-down menu private JMenuBar menubar Private JMenu [] menu= {new JMenu ("start"), new JMenu ("setup"), new JMenu ("help"); private JMenuItem [] menuitem1= {new JMenuItem ("restart"), new JMenuItem ("repentance"), new JMenuItem ("quit"); private JMenuItem [] menuitem2= {new JMenuItem ("forbidden choice"), new JMenuItem ("man-machine game"), new JMenuItem ("everyone's game")} Private JMenuItem [] menuitem3= {new JMenuItem ("rules"), new JMenuItem ("about")}; private boolean haveai=true; / / under people or computers, true and Mouseclicked mouseclicked=new Mouseclicked under computers (); MouseMoved mousemoved=new MouseMoved (); Menuitemclicked menuclicked=new Menuitemclicked () / / Constructor public ChessMap () {/ / change the system default font Font font = new Font ("Dialog", Font.PLAIN, 12); java.util.Enumeration keys = UIManager.getDefaults () .keys (); while (keys.hasMoreElements ()) {Object key = keys.nextElement () Object value = UIManager.get (key); if (value instanceof javax.swing.plaf.FontUIResource) {UIManager.put (key, font);}} setTitle (Gobang); setSize (FINAL_WIDTH,FINAL_HEIGHT) SetResizable (false); init (); setLocation (Toolkit.getDefaultToolkit (). GetScreenSize (). Width / 2-FINAL_WIDTH / 2, Toolkit.getDefaultToolkit (). GetScreenSize (). Height / 2-FINAL_HEIGHT / 2) SetDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); cp.reset (); setVisible (true);} / / initialization and default value public void init () {map=new ImageIcon (getClass (). GetResource ("bg.jpg")) Blackchess=new ImageIcon (getClass (). GetResource ("blackchess.gif")); whitechess=new ImageIcon (getClass (). GetResource ("whitechess.gif")); cp=new ChessPanel (map,blackchess,whitechess); menubar=new JMenuBar (); menuitem1 [0] .setActionCommand ("Restart"); menuitem1 [1] .setActionCommand ("Rollback") Menuitem1 [2] .setActionCommand ("Exit"); menuitem2 [0] .setActionCommand ("Forbid"); menuitem2 [1] .setActionCommand ("Robot"); menuitem2 [2] .setActionCommand ("Human"); menuitem3 [0] .setActionCommand ("Rule"); menuitem3 [1] .setActionCommand ("About") For (int iTuno Tinci 3 & & flag_i1

< 4) { ok_1--; } } // 最后一个位置不是空格,且搜索了2步以上,若前一个是空格, 则不算中空,且为活的边 else if( isChessOn[x + i * ex][y + i * ey] != bwf && i >

= 2) if (isChessOn [x + (iMel 1) * ex] [y + (iMel 1) * ey] = = 2) {ok_1++; flag_mid1 = false }} / / the last position is that the boundary has been searched for more than 2 steps, and the previous one is blank, then it is not hollow, and it is a living edge else if (I > = 2 & & isChessOn [x + (iMel 1) * ex] [y + (iMul 1) * ey] = 2) {ok_1++; flag_mid1 = false } / / search for for in the opposite direction (I = 1; x-I * ex > = 0 & & x-I * ex

< 15 && y - i * ey >

= 0 & & y-I * ey

< 15; i++) { if (isChessOn[x - i * ex][y - i * ey] == bwf) rt_2++; else if(isChessOn[x - i * ex][y - i * ey] == 2) { if(!flag_mid2) { flag_mid2 = true; flag_i2 = i; } else break; } else break; } // 计算反方向活度 if (x - i * ex < 15 && x - i * ex >

= 0 & & y-I * ey

< 15 && y - i * ey >

= 0) {if (isChessOn [x-I * ex] [y-I * ey] = = 2) {ok_2++; if (rt_2 = = flag_i2) flag_mid2 = false; if (flag_mid2 & & rt_2 > 3 & & flag_i2

< 4) { ok_2--; } } else if( isChessOn[x - i * ex][y - i * ey] != bwf && i >

= 2) if (isChesson [x-(imur1) * ex] [y-(iMur1) * ey] = = 2) {ok_2++; flag_mid2 = false }} else if (I > = 2 & & isChessOn [x-(iMur1) * ex] [y-(iMur1) * ey] = = 2) {ok_2++; flag_mid2 = false } / /-analyze the chess piece type / / both sides are not hollow, and directly synthesize if (! flag_mid1 & &! flag_mid2) {rt = rt_1 + rt_2-1; ok = ok_1 + ok_2; return new int [] {rt, ok} } / / Hollow else if (flag_mid1 & & flag_mid2) {int temp = flag_i1 + flag_i2-1 on both sides; / / determine the number of pure concatenates in the middle. If the number is above 5, return directly; if 4, return live 4; if (temp > = 5) return new int [] {temp, 2} If (temp = = 4) return new int [] {temp, 2}; / / see if there are dead 4, then live 3, and the rest can only be dead 3 if (rt_1 + flag_i2-1 > = 4 | | rt_2 + flag_i1-1 > = 4) return new int [] {4,1} If (rt_1+flag_i2-1 = = 3 & & ok_1 > 0 | | rt_2+flag_i1-1 = = 3 & & ok_2 > 0) return new int [] {3,2}; return new int [] {3,1} } / / one side has hollow else {/ / the total number of pieces is less than 5, and if (rt_1 + rt_2-1) is directly synthesized.

< 5 ) return new int[] {rt_1 + rt_2 - 1, ok_1 + ok_2}; // 多于5,先找成5,再找活4,剩下的只能是死4 else { if(flag_mid1 && rt_2 + flag_i1 - 1 >

= 5) return new int [] {rt_2 + flag_i1-1, ok_2 + 1}; if (flag_mid2 & & rt_1 + flag_i2-1 > = 5) return new int [] {rt_1 + flag_i2-1, ok_1 + 1} If (flag_mid1 & & (rt_2 + flag_i1-1 = = 4 & & ok_2 = = 1 | | flag_i1 = = 4)) return new int [] {4,2} If (flag_mid2 & & (rt_1 + flag_i2-1 = = 4 & & ok_1 = = 1 | | flag_i2 = = 4)) return new int [] {4,2}; return new int [] {4,1} } /-- to determine whether it is meaningful to play chess in a specified direction, that is, whether the maximum number of possible pieces is > = 5 the benchmark point / / ex of the evaluation. Ey direction vector / / k chess color / / true: meaningful false: meaningless private Boolean makesense (int x, int y, int ex, int ey, int bwf) {int rt = 1 For (int I = 1; x + I * ex

< 15 && x + i * ex >

= 0 & & y + I * ey

< 15 && y + i * ey >

= 0 & & rt

< 5; i++) if (isChessOn[x + i * ex][y + i * ey] != 1 - bwf) rt++; else break; for (int i = 1; x - i * ex >

= 0 & & x-I * ex

< 15 && y - i * ey >

= 0 & & y-I * ey

< 15 && rt < 5; i++) if (isChessOn[x - i * ex][y - i * ey] != 1 - bwf) rt++; else break; return (rt >

= 5) } / /-- Chess pattern discrimination-- / XMagol y falling seed position / / bwf chess color 0: sunspot 1: Baizi / / corresponding chess type: the corresponding chess code is as follows: / / 1: Cheng 5 / / 2: survive 4 or double death 4 or double death 4 or death 4 live 3 / 3: live in pairs 3 / 4: live 3 / 5: live 4 / 5 6: single life 3 / / 7: live in pairs 2 / 8: die 3 / 9: die 2 / 10: survive 2 / 11: die 2 / 12: other / / 20: long forbidden hands / / 21 Double four forbidden hands / / 22: double live three forbidden hands protected int getType (int x Int y, int bwf) {if (isChessOn [x] [y]! = 2) return-1 Int [] [] types = new int [4] [2]; types [0] = count (x, y, 0, 1, bwf); / / Vertical types [1] = count (x, y, 1, 0, bwf); / / horizontal types [2] = count (x, y,-1, 1, bwf); / / oblique upward types [3] = count (x, y, 1, 1, bwf) / / the number of directions of various chess patterns int longfive = 0; int five_OR_more = 0; int four_died = 0, four_live = 0; int three_died = 0, three_live = 0; int two_died = 0, two_live = 0; / / the discriminant for of chess patterns in all directions (int k = 0; k

< 4; k++) { if (types[k][0] >

5) {longfive++; / / long connection five_OR_more++;} else if (types [k] [0] = = 5) five_OR_more++; / / into 5 else if (types [k] [0] = = 4 & & types [k] [1] = = 2) four_live++ / / Live 4 else if (types [k] [0] = = 4 & & types [k] [1]! = 2) four_died++; / / Dead 4 else if (types [k] [0] = = 3 & & types [k] [1] = = 2) three_live + + / / Live 3 else if (types [k] [0] = = 3 & & types [k] [1]! = 2) three_died++; / / Dead 3 else if (types [k] [0] = = 2 & & types [k] [1] = = 2) two_live++ / / Live 2 else if (types [k] [0] = = 2 & & types [k] [1]! = 2) two_died++; / / Dead 2 else } / / the discrimination of total chess type if (bwf = = 0 & & able_flag) {/ / black chess and choose forbidden if (longfive! = 0) / / long forbidden return 20; if (four_live + four_died > = 2) / / A pair of 4 forbidden return 21 If (three_live > = 2) / / A pair of live three forbidden hands return 22;} if (five_OR_more! = 0) return 1; / / into 5 if (four_live! = 0 | | four_died > = 2 | | four_died! = 0 & & three_live! = 0) return 2 / / Survival 4 or double death 4 or death 4 living 3 if (three_live > = 2) return 3; / / living in pairs 3 if (three_died! = 0 & & three_live! = 0) return 4; / / living 3 if (four_died! = 0) return 5 / / 4 if (three_live! = 0) return 6; / / single live 3 if (two_live > = 2) return 7; / / live in pairs 2 if (three_died! = 0) return 8; / / mature death 3 if (two_live! = 0 & & two_died! = 0) return 9 / / Survival 2 if (two_live! = 0) return 10; / Survival 2 if (two_died! = 0) return 11; / / Survival 2 return 12 } / /-score the current chess face-- / / protected int evaluate () {int rt = 0, mt_c = 1, mt_m = 1 If (bw = = sbw) mt_m = 2; else mt_c = 2; int itemin = (x_min==0? X_min:x_min-1); int junimins = (y_min==0? Y_min:y_min-1); int iTunes max = (x_max==15? X_max:x_max+1); int jacks max = (y_max==15? Y_max:y_max+1); for (int I = itemin; I

< i_max; i++) for (int j = j_min; j < j_max; j++) if (isChessOn[i][j] == 2) { // 电脑棋面分数 int type = getType(i, j, 1 - sbw ); if(type == 1) // 棋型1,棋型2以及棋型3,加权. 防止"4个双活3"的局分大于"1个双四"之类的错误出现 rt += 30 * mt_c * getMark(type); else if(type == 2) rt += 10 * mt_c * getMark(type); else if(type == 3) rt += 3 * mt_c * getMark(type); else rt += mt_c * getMark(type); // 玩家棋面分数 type = getType(i, j, sbw ); if(type == 1) rt -= 30 * mt_m * getMark(type); else if(type == 2) rt -= 10 * mt_m * getMark(type); else if(type == 3) rt -= 3 * mt_m * getMark(type); else rt -= mt_m * getMark(type); } return rt; } //--------------------------------下棋后,更新信息-----------------------------// void update(int x,int y) { isChessOn[x][y] = bw; bw = 1 - bw; pre[chess_num][0] = x; pre[chess_num][1] = y; chess_num++; } //-------------------------------------- 下棋后,重设边界值------------------------------// // x 当前下棋位置的x坐标 // y 当前下棋位置的y坐标 public void resetMaxMin(int x,int y){ if(x-1>

= 0) flag = true; if (! flag & & count (x, y, 0,1, bwf) [0] > = 5) flag = true; if (! flag & & count (x, y, 1,0, bwf) [0] > = 5) flag = true If (! flag & & count (x, y, 1,-1, bwf) [0] > = 5) flag = true; if (! flag & & count (x, y, 1, bwf) [0] > = 5) flag = true; / / for testing purposes, if you activate this line of code, you will not win or lose. Flag = false; return flag;} public void wined (int bw) {boolean hh=getHumanhuman (); if (! hh) {/ / not everyone's game win = true; win_bw = bw; String str = (bw = = sbw? "Congratulations! you win!" "the computer has won, you have to keep working hard!") ; if (bw==sbw) winVoice (); else lostVoice (); JOptionPane.showMessageDialog (null,str);} else {/ / everyone win = true; win_bw = bw; String str = (bw== BLACK_ONE? "Congratulations! black wins!" Congratulations! White wins!) ; winVoice (); JOptionPane.showMessageDialog (null,str);}} public void setHumanhuman (boolean humanhuman) {this.humanhuman = humanhuman;} public boolean getHumanhuman () {return humanhuman;}} effect display

After reading this, the article "how to achieve simple Gobang game with Java" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

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