In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to achieve Gobang Mini Game with Java". In daily operation, I believe many people have doubts about how to achieve Gobang Mini Game with Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to achieve Gobang Mini Game with Java". Next, please follow the editor to study!
By judging the score of each vacant position on the chessboard and removing the points with the highest score, the idea of calculating the score of playing chess at random:
If you can get five points, you can win and give the highest score.
Can not be 5, the other side can be 5, it means that the other side is going to win, give the second highest score
Can survive 4, give the third highest score
Can survive 3, give the fourth high score
Can become 4, give the fifth high score
Can go to Chong 3 and give the sixth highest score
Can survive 2, give the seventh high score
Can become Chong 2, give the eighth high score
In addition, setting the minimum score can be defined by yourself.
Here is an example of the code for AI Gobang:
Package FivechessClient
Import Java.awt.Cursor
Import java.awt.Dimension
Import java.awt.Graphics
Import java.awt.event.MouseAdapter
Import java.awt.event.MouseEvent
Import java.awt.event.MouseMotionAdapter
Import java.awt.image.BufferedImage
Import java.io.File
Import java.util.ArrayList
Import java.util.Random
Import javax.imageio.ImageIO
Import javax.swing.JFrame
Import javax.swing.JOptionPane
Import javax.swing.JPanel
/ * *
*
* @ ClassName: Game
* @ Description: AI Gobang
* @ author xiaoxiong
* @ date 8:59:02 on July 3, 2015
*
, /
Public class Game {
BufferedImage table
BufferedImage black
BufferedImage white
BufferedImage selected
/ * *
* number of pieces
, /
Private static int BOARD_SIZE = 15
/ * *
* Chessboard width and height
, /
Private final int TABLE_WIDTH = 535
Private final int TABLE_HEIGHT = 536
/ * *
* 15 equal points on the chessboard
, /
Private final int RATE = TABLE_WIDTH / BOARD_SIZE
/ * *
* outside margin of chessboard
, /
Private final int X_OFFSET = 5
Private final int Y_OFFSET = 6
/ * *
* Chessboard
, /
Private int [] [] board = new int [bod _ SIZE] [BOARD_SIZE]
/ * *
* AI score
, /
Private int [] [] computerScore = new int [bod _ SIZE] [BOARD_SIZE]
/ / private int [] [] gamerScore = new int [bod _ SIZE] [BOARD_SIZE]
JFrame f = new JFrame (Gobang-Little Bear)
ChessBoard chessBoard = new ChessBoard ()
Private static int selectedX =-1
Private static int selectedY =-1
Private static int computerX =-1
Private static int computerY =-1
Private static boolean flagGamer = false; / / record whether the player wins or not
Private static boolean flagComputer = false; / / record whether the computer wins or not
Private static int computerscore = 0; / / computer maximum score
Private static int comx, comy; / / sub-coordinates of the player
Private final int HUO = 1
Private final int CHONG = 2
Private static int chesscou = 0
/ * *
* record the pieces found with the same score, and randomly play one of these pieces in case the footwork is fixed.
, /
Private ArrayList chessList = new ArrayList ()
Random rand = new Random ()
/ * *
*
* @ Title: initto @ Description: reset the game @ param [url=home.php?mod=space&uid=570] @ return [/ url] void @ throws
, /
Public void initto () {
For (int I = 0; I
< BOARD_SIZE; ++i) { for (int j = 0; j < BOARD_SIZE; ++j) { board[i][j] = 0; computerScore[i][j] = 100000; } } chesscou = 0; computerX = -1; computerY = -1; flagGamer = false; flagComputer = false; } /** * * @Title: isRun @Description: 判断该位置是否可以走 @param @param x @param @param * y @param @return @return boolean @throws */ public boolean isRun(int x, int y) { if (board[x][y] == 0) { return true; } return false; } /** * * @Title: isWin @Description: 判断下该子是否能赢 @param @param f 颜色 @param @param x * 坐标 @param @param y @param @return @return boolean @throws */ public boolean isWin(int f, int x, int y) { int i, count = 1; boolean up, down, right, left, rup, lup, rdown, ldown; up = down = right = left = rup = lup = rdown = ldown = true; /** * * 上下 * */ for (i = 1; i < 5; ++i) { if ((y + i) < BOARD_SIZE) { if (board[x][y + i] == f && down) count++; else down = false; } if ((y - i) >= 0) {
If (board [x] [y-I] = = f & & up)
Count++
Else
Up = false
}
}
If (count > = 5) {
Return true
}
Count = 1
/ * *
*
* left and right
*
, /
For (I = 1; I
< 5; ++i) { if ((x + i) < BOARD_SIZE) { if (board[x + i][y] == f && right) count++; else right = false; } if ((x - i) >= 0) {
If (board [x-I] [y] = = f & & left)
Count++
Else
Left = false
}
}
If (count > = 5) {
Return true
}
Count = 1
/ * *
*
* Top left, bottom right
*
, /
For (I = 1; I
< 5; ++i) { if ((x + i) < BOARD_SIZE && (y + i) < BOARD_SIZE) { if (board[x + i][y + i] == f && rdown) count++; else rdown = false; } if ((x - i) >= 0 & & (y-I) > = 0) {
If (board [x-I] [y-I] = = f & & lup)
Count++
Else
Lup = false
}
}
If (count > = 5) {
Return true
}
Count = 1
/ * *
*
* Top right, bottom left
*
, /
For (I = 1; I
< 5; ++i) { if ((x + i) < BOARD_SIZE && (y - i) >= 0) {
If (board [x + I] [y-I] = = f & & rup)
Count++
Else
Rup = false
}
If ((x-I) > = 0 & & (y + I))
< BOARD_SIZE) { if (board[x - i][y + i] == f && ldown) count++; else ldown = false; } } if (count >= 5) {
Return true
}
Return false
}
/ * *
*
* @ Title: Computer_AI @ Description: AI plays chess @ param @ return void @ throws
, /
Public void Computer_AI () {
Computerscore = 0
For (int I = 0; I
< BOARD_SIZE; ++i) { for (int j = 0; j < BOARD_SIZE; ++j) { computerScore[i][j] = 0; // gamerScore[i][j] = 0; } } getScore(); for (int i = 0; i < BOARD_SIZE; ++i) { for (int j = 0; j < BOARD_SIZE; ++j) { if (computerScore[i][j] == computerscore) { ChessXY chess = new ChessXY(i, j); chessList.add(chess); } } } int n = rand.nextInt(chessList.size()); // 电脑根据分值一样的点随机走,防止每次都走相同的步数 comx = chessList.get(n).x; comy = chessList.get(n).y; chessList.clear(); } /** * * @Title: getScore @Description: 评分 @param @return void @throws */ public void getScore() { for (int i = 0; i < BOARD_SIZE; ++i) { for (int j = 0; j < BOARD_SIZE; ++j) { if (board[i][j] == 0) { if (isWin(2, i, j)) // 电脑能赢,故给分最高,因为可以结束,所以不再检测 { computerscore = 13; computerScore[i][j] = 13; return; } else if (isWin(1, i, j)) // 电脑不能赢,玩家能赢,要阻止,所以给12分 { computerscore = 12; computerScore[i][j] = 12; } else if (isHuoOrChong(2, i, j, 4, HUO)) // 电脑玩家都不能赢,电脑能形成活四,给11分 { computerscore = (computerscore >11? Computerscore: 11)
ComputerScore [I] [j] = 11
} else if (isHuoOrChong (2, I, j, 4, CHONG)) / / computer players can not win, the computer can form a rush four, give 10 points
{
Computerscore = (computerscore > 10? Computerscore: 10)
ComputerScore [I] [j] = 10
} else if (isHuoOrChong (1, I, j, 4, HUO)) / / computer players can not win, players can form a living four, give 9 points
{
Computerscore = (computerscore > 9? Computerscore: 9)
ComputerScore [I] [j] = 9
} else if (isHuoOrChong (2, I, j, 3, HUO)) / / computer players can not win, the computer can form a living three, give 8 points
{
Computerscore = (computerscore > 8? Computerscore: 8)
ComputerScore [I] [j] = 8
} else if (isHuoOrChong (1, I, j, 4, CHONG)) / / computer players can not win, players can form a rush four, give 7 points
{
Computerscore = (computerscore > 7? Computerscore: 7)
ComputerScore [I] [j] = 7
} else if (isHuoOrChong (2, I, j, 3, CHONG)) / / computer players cannot win, the computer can form a rush three, give 6 points
{
Computerscore = (computerscore > 6? Computerscore: 6)
ComputerScore [I] [j] = 6
} else if (isHuoOrChong (2, I, j, 2, HUO)) / / computer players can't win, computers can form living two, give 5 points
{
Computerscore = (computerscore > 5? Computerscore: 5)
ComputerScore [I] [j] = 5
} else if (isHuoOrChong (1, I, j, 3, CHONG)) / / computer players can not win, players can form a rush three, give 4 points
{
Computerscore = (computerscore > 4? Computerscore: 4)
ComputerScore [I] [j] = 4
} else if (isHuoOrChong (1, I, j, 2, HUO)) / / computer players can not win, players can form a living two, give 3 points
{
Computerscore = (computerscore > 3? Computerscore: 3)
ComputerScore [I] [j] = 3
} else if (isHuoOrChong (2, I, j, 2, CHONG)) / / computer players can not win, the computer can form a rush two, give two points
{
Computerscore = (computerscore > 2? Computerscore: 2)
ComputerScore [I] [j] = 2
} else if (isHuoOrChong (1, I, j, 2, CHONG)) / / computer players can not win, players can form a rush two, give 1 point
{
Computerscore = (computerscore > 1? Computerscore: 1)
ComputerScore [I] [j] = 1
} else {
ComputerScore [I] [j] = 0
}
}
}
}
}
/ * *
*
* @ Title: isHuoOrChong @ Description: determine whether it is live @ param @ param f @ param @ param
* x @ param @ param y @ param @ param num @ param @ param
* hORc @ param @ return @ return boolean @ throws
, /
Private boolean isHuoOrChong (int f, int x, int y, int num, int hORc) / / active
{
Num + = 1
Int I, count = 1
Boolean terminal1 = false
Boolean terminal2 = false
Boolean up, down, right, left, rup, lup, rdown, ldown
Up = down = right = left = rup = lup = rdown = ldown = true
/ * *
*
* up and down
*
, /
For (I = 1; I
< num; ++i) { if ((y + i) < BOARD_SIZE) {if (board[x][y + i] == f && down) count++; else { if (board[x][y + i] == 0 && down) { terminal1 = true; } down = false; } } if ((y - i) >= 0) {
If (board [x] [y-I] = = f & & up)
Count++
Else {
If (board [x] [y-I] = = 0 & & up) {
Terminal2 = true
}
Up = false
}
}
}
If (count = = num-1 & & hORc = = HUO & & terminal1 & & terminal2) {
Return true
}
If (count = = num-1 & & hORc = = CHONG & & ((terminal1 & &! terminal2) | | (! terminal1 & & terminal2) {
Return true
}
Count = 1
Terminal1 = false
Terminal2 = false
/ * left and right * /
For (I = 1; I
< num; ++i) { if ((x + i) < BOARD_SIZE) { if (board[x + i][y] == f && right) count++; else { if (board[x + i][y] == 0 && right) { terminal1 = true; } right = false; } } if ((x - i) >= 0) {
If (board [x-I] [y] = = f & & left)
Count++
Else {
If (board [x-I] [y] = = 0 & & left) {
Terminal2 = true
}
Left = false
}
}
}
If (count = = num-1 & & hORc = = HUO & & terminal1 & & terminal2) {
Return true
}
If (count = = num-1 & & hORc = = CHONG & & ((terminal1 & &! terminal2) | | (! terminal1 & & terminal2) {
Return true
}
Count = 1
Terminal1 = false
Terminal2 = false
/ * *
*
* Top left, bottom right
*
, /
For (I = 1; I
< num; ++i) { if ((x + i) < BOARD_SIZE && (y + i) < BOARD_SIZE) { if (board[x + i][y + i] == f && rdown) count++; else { if (board[x + i][y + i] == 0 && rdown) { terminal1 = true; } rdown = false; } } if ((x - i) >= 0 & & (y-I) > = 0) {
If (board [x-I] [y-I] = = f & & lup)
Count++
Else {
If (board [x-I] [y-I] = = 0 & & lup) {
Terminal2 = true
}
Lup = false
}
}
}
If (count = = num-1 & & hORc = = HUO & & terminal1 & & terminal2) {
Return true
}
If (count = = num-1 & & hORc = = CHONG & & ((terminal1 & &! terminal2) | | (! terminal1 & & terminal2) {
Return true
}
Count = 1
Terminal1 = false
Terminal2 = false
/ * *
*
* Top right, bottom left
*
, /
For (I = 1; I
< num; ++i) { if ((x + i) < BOARD_SIZE && (y - i) >= 0) {
If (board [x + I] [y-I] = = f & & rup)
Count++
Else {
If (board [x + I] [y-I] = = 0 & & rup) {
Terminal1 = true
}
Rup = false
}
}
If ((x-I) > = 0 & & (y + I))
< BOARD_SIZE) { if (board[x - i][y + i] == f && ldown) count++; else { if (board[x - i][y + i] == 0 && ldown) { terminal2 = true; } ldown = false; } } } if (count == num - 1 && hORc == HUO && terminal1 && terminal2) { return true; } if (count == num - 1 && hORc == CHONG && ((terminal1 && !terminal2) || (!terminal1 && terminal2))) { return true; } return false; } public void init() throws Exception { table = ImageIO.read(new File("image/board.jpg")); black = ImageIO.read(new File("image/black.gif")); white = ImageIO.read(new File("image/white.gif")); selected = ImageIO.read(new File("image/selected.gif")); for (int i = 0; i < BOARD_SIZE; ++i) { for (int j = 0; j < BOARD_SIZE; ++j) { board[i][j] = 0; computerScore[i][j] = 0; } } chessBoard.setPreferredSize(new Dimension(TABLE_WIDTH, TABLE_HEIGHT)); chessBoard.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { int xPos = (int) ((e.getX() - X_OFFSET) / RATE); int yPos = (int) ((e.getY() - Y_OFFSET) / RATE); // System.out.println("1 " + xPos + " " + yPos); if (isRun(xPos, yPos)) { flagGamer = isWin(1, xPos, yPos); board[xPos][yPos] = 1; chesscou++; // do //电脑下棋,随机 // { // comx = (rand.nextInt(535) - X_OFFSET) / RATE; // comy = (rand.nextInt(536) - Y_OFFSET) / RATE; // } while (!isRun(comx, comy)); if (chesscou == (BOARD_SIZE * BOARD_SIZE)) { JOptionPane.showMessageDialog(null, "不相上下!!!\n再来一盘吧!!!", "结束", JOptionPane.ERROR_MESSAGE); initto(); } else { Computer_AI(); // 电脑下棋,AI算法 chesscou++; // System.out.println("2 " + comx + " " + comy); flagComputer = isWin(2, comx, comy); board[comx][comy] = 2; computerX = comx; computerY = comy; } } chessBoard.repaint(); if (flagGamer) { JOptionPane.showMessageDialog(null, "厉害厉害!!!\n你赢了!!!", "结束", JOptionPane.ERROR_MESSAGE); initto(); } else if (flagComputer) { JOptionPane.showMessageDialog(null, "哈哈哈哈!!!\n你输了!!!", "结束", JOptionPane.ERROR_MESSAGE); initto(); } } public void mouseExited(MouseEvent e) { selectedX = -1; selectedY = -1; chessBoard.repaint(); } }); chessBoard.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { selectedX = (e.getX() - X_OFFSET) / RATE; selectedY = (e.getY() - Y_OFFSET) / RATE; chessBoard.repaint(); } }); f.add(chessBoard); f.setCursor(new Cursor(Cursor.HAND_CURSOR)); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setResizable(false); f.pack(); f.setVisible(true); } public static void main(String[] args) throws Exception { Game game = new Game(); game.init(); } @SuppressWarnings("serial") class ChessBoard extends JPanel { public void paint(Graphics g) { g.drawImage(table, 0, 0, null); if (selectedX >= 0 & & selectedY > = 0) {
G.drawImage (selected, selectedX * RATE + X_OFFSET, selectedY * RATE + Y_OFFSET, null)
}
If (computerX > = 0 & & computerY > = 0) {
G.drawImage (selected, computerX * RATE + X_OFFSET, computerY * RATE + Y_OFFSET, null)
}
For (int I = 0; I < BOARD_SIZE; + + I) {
For (int j = 0; j < BOARD_SIZE; + + j) {
If (board [I] [j] = = 1) {
G.drawImage (black, I * RATE + X_OFFSET, j * RATE + Y_OFFSET, null)
}
If (board [I] [j] = = 2) {
G.drawImage (white, I * RATE + X_OFFSET, j * RATE + Y_OFFSET, null)
}
}
}
}
}
}
Class ChessXY {
Int x
Int y
Public ChessXY (int x, int y) {
This.x = x
This.y = y
}
}
At this point, the study on "how to achieve Gobang Mini Game by Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.