In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This "Java how to achieve simple interface-free Gobang" article knowledge points most people do not understand, so the editor summarized the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "Java how to achieve simple interface-free Gobang" article.
Project description:
This design is based on the knowledge point Java class and object as well as array development of a small Gobang game program. At the beginning of the game, choose black or white chess to start, drop a piece on one coordinate of the chessboard, and then drop the pieces in turn, until one side first connects the five pieces in the vertical, horizontal or two oblique directions of the chessboard into a line.
The idea of realizing the project:
1. The chessboard is designed as 10-10 squares, the chessboard type is Chess [] [] two-dimensional array, and the attribute String chessType; chesstype is "➕" at first.
2. Initialize two-dimensional array
3. After the player chooses the black and white circle, he starts to play chess. Enter the row coordinates of the pieces to be played, and the black and white pieces fall in turn. When one side is connected into five pieces or full of chessboards, the game is over (the party with five players wins and the full board is a draw).
4. After each successful fall, immediately judge the eight directions centered on that position: upper, lower, left, right, upper left, lower left, upper right and lower right whether there are pieces of the same color connected into five pieces, if connected into five pieces, then the game is over and output the corresponding information.
5. When one side of the game wins, the victory message is displayed. From the surface of the program, this is a two-dimensional plan, so the data is represented by a two-dimensional array, the two subscripts of the array can represent the position on the chessboard, and the values of the array elements represent the state on the checker. there are three cases, namely, ⭕ represents white chess, ● represents black chess, and ➕ represents the grid.
source code
1. Chess piece
/ * @ author hudongsheng * @ date 2020-10-29-9:28 * / public class ChessType {private String chessType; private int x; private int y; public int getX () {return x;} public void setX (int x) {this.x = x;} public int getY () {return y;} public void setY (int y) {this.y = y } public ChessType () {} public String getChessType () {return chessType;} public void setChessType (String chessType) {this.chessType = chessType;}}
two。 Play chess
/ * * @ author hudongsheng * @ date 2020-10-29-9:27 * / public class Gobang {private int size = 1; private ChessType [] [] chessTypes; private int row; private int colum; private int x; private int y; / / create a chessboard public Gobang (int row,int colum) {this.row = row; this.colum = colum; chessTypes = new ChessType [row] [colum] } / / initialize the chessboard public void initChessType () {for (int I = 0; I)
< chessTypes.length; i++){ for (int j = 0; j< chessTypes[i].length; j++){ chessTypes[i][j] = new ChessType(); chessTypes[i][j].setChessType("➕"); } } } //下白棋 public void setWhiteChess(int x,int y){ chessTypes[x][y].setChessType("⭕"); } //下黑棋 public void setBlackChess(int x,int y){ chessTypes[x][y].setChessType("●"); } //判断是否胜利 public boolean checkWin(int i,int j) { // TODO Auto-generated method stub boolean flag = false; //判断纵向是否有五个棋子是相同的颜色 int count1 = 1;//相同颜色棋子的个数 String color = chessTypes[i][j].getChessType(); //刚下的棋子的颜色 int a = 1; //棋子索引的增量 while((i+a)=0 && color == chessTypes[i-a][j].getChessType()){ count1++; a++; } if(count1 >= 5) {flag = true;} / / judge whether five pieces in the vertical direction are of the same color int count2 = 1; a = 1; while ((Jaima) = 0 & & color = = chessTypes [I] [jmera] .getChessType ()) {count2++; astata + } if (count2 > = 5) {flag = true;} / / whether there are five pieces in the upper right and lower left that are the same color int count3 = 1; a = 1; while ((iTuna) = 0 & & color = = chessTypes [iFasa] [Jmura] .getChessType ()) {count3++ A match;} a = 1; while ((iMura) > = 0 & & (Jacua) = 5) {flag = true;} / / whether there are five pieces on the upper left and lower right of the same color int count4 = 1; a = 1 While ((iMura) > 0 & & (jMura) > = 0 & & color = = chessTypes [iMura] [JMura] .getChessType () {count4++; aquifers;} a = 1; while ((iMura))
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.