Java Gobang Games

Source: Internet
Author: User

Gobang Games

One, 1. basic knowledge of Gobang

Like any kind of athletic chess, Gobang's chess game is divided into three stages: opening, middlegame and Endgame.

The beginning of a Gobang is called a start, or layout. Its opening phase is very short, about seven to more than 10. In this stage of the competition, the layout of the two sides, the response will be decisive for the outcome of the future play a crucial role. In the opening phase of the situation achieved good or bad, active and passive, initiator and the pros and cons of the degree, often directly affect the battle of Middlegame. So it is very important to actively deal with the transition from opening and opening to middlegame.

Gobang is from one to five, gradually dichotoma, the development of a link, at the same time the use of restrictions and anti-restriction of wisdom, in the process for their own chess pieces to win the relative initiative and advantages, gradually expand the advantage, or from a disadvantage to the advantage of defeating each other's line of defense, and finally even five win or catch the other side of the

2, Gobang competition of the relevant provisions

(1) career alignment rules

A. Black to the bottom, after the white, from Tianyuan began to order Lazi with each other.

B. The first side of the board transverse, vertical, inclined to form a continuous five pieces of the same color wins.

C. Black is forbidden to accept the negative hand, white chess no forbidden hand. Black hands include "33"( including "433"), "44"( including "443"), "Long Lian". Black chess can only win with "43". See the illustration for the explanation of the terms.

D. If the outcome is not divided, it will be a draw. In the game, the withdrawal is negative.

E. Five company and the prohibition of the formation of the same time, the award wins.

(2) rules and exceptions in the competition

A. Before the game starts, the two sides guess the son, the large number decreases, the singular exchange, even does not change.

B. White first -hand ( that is , the second game on the plate ) should be in the Tian Yuan for their own side of the dichotoma, mainly to show respect for each other, after the two sides can arbitrarily line.

C. The Parties shall abide by the "professional Gobang rules" and shall be judged by the referee if there is any dispute.

D. A party that loses a child ( a piece falling on a board ) is awarded a negative. In case of fader or rubbing, the first landing point of the plate is subject to. It is not a violation to straighten a piece by hand.

E. The game should be polite chess, to prevent abnormal sounds, actions affect the other side of thinking.

F: After the game is over, both sides should put their pieces back in place.

G. In the event of black in the presence of the forbidden hand, white should immediately point out the ban, the black side is negative. such as white in black, after the prohibition of hand, and the next white son, black chess ban is not set up.

H. The time of the match, the Two sides are1 hours,2 hours,5 hours and four grade standards respectively. The timeout is negative.

(3) Special provisions

Although the professional Gobang have adopted various restrictions on black chess, the advantage of sunspots is still very large. Therefore, in the high-grade professional competition, there are three kinds of special rules.

A. Designated play: refers to the match between the two sides in accordance with the agreed good start to play chess, from White first.

B. Three hand exchangeable: refers to the black chess plate after the first 3 hand Chess, white before the fourth hand, such as feeling black chess shape is not conducive to their own, you can put forward the exchange, that is, white chess one side into the black Chess party, and Black can not be changed.

C. Five hand two play: refers to the black chess in the next plate on the key of the first 5 hand chess, must be the next two moves, let white chess in the two steps to take off a piece of chess, and then continue to play chess. Generally speaking, white must take away the negative side of the dialogue, while preserving the black side of the more unfavorable point to let black chess.

Two, code address Https://github.com/kongxiangyu/test/tree/master

Three

1. System use case model design

Gobang system should have the ability to choose whether the player first Lazi or the computer Lazi, should have the function of starting the game and restarting, of course, must have the function of Lazi in the player's designated position. Described above, use case diagram in Figure 1 to further describe. This part of the function, mainly through the interface and system interaction to achieve.

Each use case is briefly described as follows:

L Select Computer First Lazi: Select, in the game process, by the computer to hold the sunspot, first Lazi; the player holds the white son, after the computer lazi;

L Select Players First Lazi: After the game, in the process, by the player to hold the sunspot, first Lazi, computer white, after the player Lazi;

L Lazi: In principle, as long as do not violate the prohibition of hand rules, the player mouse click the nearest Lazi point Lazi, but in this system, for simplicity, did not consider the prohibition of hand rules;

L START the game: after the choice, according to the set computer or the player first Lazi, start the game;

L Start over: In the game, you can choose to start again, break the game and start the game again.

At the same time, the Gobang system itself should have board drawing, chess potential scan, chess potential ranking, chess potential evaluation, Lazi, and the function of winning and losing, especially for the Order of chess, the evaluation of chess potential and Lazi, this is the embodiment of the Gobang system artificial intelligence (AI) key points. Described above, the use case diagram is further described in Figure 2 .

L Checkerboard Drawing: Draw 19*19 board and related buttons and options, and responsible for computer, player Lazi after the specific drawing;

L Chess Scan: Scan the current board on all eight directions on the Lazi situation, to obtain the chess potential of both sides;

L Chess Potential ranking: The eight direction of the acquisition of the chess potential (in fact, the next one, left and right, one on top, the right to sit on the bottom of one, for four directions), to facilitate the evaluation of chess potential;

L Evaluation of the chess potential: the computer and the player to evaluate the chess potential, quantitative chess potential;

L Select Lazi: Compare the computer and the player's chess potential, if it is beneficial to the player, then take "Keep, block" strategy, if the benefit of the computer, then take the "attack, go" strategy;

L Decision: Every time the computer or player Lazi, judging the outcome of the situation, if the "five sub-alignment", then the "alignment" of one side to win, the other party failed, the game is over.

3. System Module Design

In order to realize the function of Gobang system itself, the system is divided into these modules: Gobang main module, chess potential scan module, chess potential sequencing module, chess potential evaluation module and stochastic Lazi module. Among them, the Gobang main module realizes the board drawing, the victory and defeat judgment function, as well as in the chess process compares both sides chess potential the choice Lazi module. Chess potential scan module, chess potential sequencing module and Chess potential evaluation module respectively realize the functions of chess potential scan, chess potential sequencing and chess potential evaluation, while the random Lazi module realizes the function of random position Lazi of the player Lazi location when the first two steps have not yet formed, which is part of the Lazi function in the use case diagram.

Corresponding, the system has a total of five classes, corresponding to five modules respectively: Gobang Main module Gobang class, chess potential scan Statusscan class, chess potential sequencing statussort class, the statusevaluation class, and the random Lazi randomplay class .

Iv. realization and testing of Gobang

1, The realization of Gobang class

Gobang class is the core class of Gobang, board drawing, chess Lazi, Response event processing, game control and mode setting, winning and losing judgment, as well as the call of chess potential scanning, sequencing, evaluation, etc. are realized here. class diagram of the Gobang class .

Where the checkerboard is drawn, there is paint ()and theInit ()to achieve, pawn Lazi bySetqizi ()to implement, the handling of the response event is also added to the monitor and processed in this class, while the game controls and settings areGame_start ()and theGame_rastart (), the decision of the game winsJudge ()method and four determining whether five sub-alignments in different directionsgame_win_* ()function to implement. The most important of these is the scanning, sequencing, and evaluation of the potentialSCANP,Scanc,Sort,Evaluatep,Evaluatecthe method to invoke them separately is implemented. Random Lazi by attributeRandomplaycall its method implementation.

2, The realization of Statusscan class

The Statusscan class obtains the chess potential of both sides of the game by scanning the lazi situation in all eight directions on the current board. class diagram of the Statusscan class .

Where the three-dimensional array shapeas a20*20*5array to store the chess potential of the entire game. Of course, in the actual implementation, the instantiation of twoStatusscanobject of the classSCANPand theScanc, these two objects are used to store the player's chess potential and the computer's chess potential respectively. andStatusscan ()The concrete process of obtaining the entire chess potential from the chessboard is realized by the method, and the parametersChesspadStore detailed lazi on the board, parametersColourdecide what you need to get is Black's chess potential or White's chess potential. For specific scans, the board(i, J), there are up and down, left and right, down to the bottom of the top, sitting on the lower right eight directions, but the statistics, will be up and down, left and right, down to the top, left and right, respectively, there is a total of four results, respectively, stored inShape[i][j][0],Shape[i][j][1],Shape[i][j][2],Shape[i][j][3]the.

3, The realization of Statussort class

The Statussort class sorts the statusscan of the scan , allowing for further evaluation of the chess potential. Class diagram of the Statussort class .

Among them, the Statussort class has only one method Statussort (), the parameter shape is the statusscan class scan obtained the chess potential data, this method mainly is to The data in shape is sorted so that the shape array satisfies the following conditions:

The sorted results are still stored in the shape array.

4, The realization of Statusevaluation class

The Statusevaluate class evaluates the Statussort sort of chess potential , which provides the basis for the computer to choose the Lazi. Of the Statusevaluate class .

Among them, statusevaluateclassThere's only one way. Statusevaluate (), the parameterShapethat isStatussort, which is based on the chess potential data of each position (I, J) on the boardShap[i][j][0],Shap[i][j][1],Shap[i][j][2]as wellShap[i][j][3]To calculate the value of the position, and save it inShap[i][j][4]the. In addition, compare the value of the chess potential in each location and record the maximum evaluation value in the attributeMax, the corresponding positional coordinates are recorded in themax_xand themax_ythe.

5, The realization of Randomplay class

Randomplay class is mainly in the first two steps, the chess potential has not formed, after the player Lazi, the computer close to the player Lazi position, random lazi. Class diagram of the Randomplay class

Wherein, the method Randomplay () method implements this process concretely, the attribute chesspad stores the lazi situation on the current board, parameter a, b This is the last Lazi position of the player. the randomplay () method detects the values of the random numbers, respectively , directly up, down, right, left, right, down, left, top, and bottom of the player Lazi (A, b) . One position in the top left and bottom eight positions Lazi, the final selected computer Lazi location, stored in the properties x,y .

6, the Gobang system test

After running the system, choose "Let the computer also no harm", click on "Man-machine battle, hurry Up", start the game. During the game, the relevant game information can be displayed correctly, as shown in.

Run

Five, problems and solutions, experience

Through the Gobang experiment, from the analysis, design, programming, debugging, to the last successful operation, I got a lot of exercise.

Java Gobang Games

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.