“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

【 related knowledge 】

Control array; 2. Sequential file reading and writing; 3. Mouse event; Timer control.

[Function Introduction]

The Go board consists of 19 vertical and 19 horizontal lines that are orthogonal to form a grid, with a total of 19×19=361 grid points. Two players, each holding a black and white round piece, take turns placing it on a grid point that is not occupied by the piece. This program to complete a simulation of the board, and can be placed on the board pieces (lok), from the board to remove the son (raisin). This program can be used for two players.

[Function subdivision]

Run the program, the window as shown in the figure, the main part of the window is the chessboard, the text box above the chessboard shows the number of pieces on the current chessboard, the chessboard below is several functional buttons. Use the mouse to click on the grid point, place a sunspot; Right-click on the grid point and place a white seed. Holding down the Ctrl key and clicking on an existing spot on the board will remove the spot; Holding down the Ctrl key and right-clicking on an existing piece of white on the board will remove it. Click the “Undo Last Step” button to automatically remove the last placed chess piece, regardless of black and white, and only one undo (continue to click useless). Click the “Remove sunspots” and “Remove white” buttons to clear all sunspots and white, respectively. Whether it is a move, a raisin or a removal piece, the text box at the top of the window always shows the current number of black, white and total pieces on the board in real time. If you click outside the board while operating, the program places the chess piece on the border grid point closest to the mouse. If there is a child on the grid point clicked when placing a child, or no child on the grid point clicked when lifting a child, the prompt message box will be displayed.

If the board is full of pieces, the Game Over message box is displayed.

【 Concrete implementation 】

The line on the chessboard has 38, can use line control when designing “draw” good, put 38 line control on the form namely. But the adjustment, alignment workload is large, will be very tedious. Therefore, using control arrays is a good idea.

At design time, place a Line control on the form, change its name to linH, and set its Index property to 0. At this point, the control becomes the first control element in the linH control array. During the Load event on the form, use a loop to Load the other 18 horizontal lines at the calculated position and spacing. Vertical grid lines can be arranged in the same way using another linear control array, linV. The checkerboard grid does not need to change while the program is running, so the form’s Load event is loaded once.

2. To achieve the function of the chess pieces, must use the control array, can be used to achieve the circular Shape Shape control. In the course of chess, the number of pieces will be increased or decreased. Load 361 control array elements at the beginning of the program. During the program run, set its visibility (VisibIe attribute) to display the pieces needed. The color can be set by FillColor attribute. The second option is to load or unload control array elements dynamically at run time, so that there are several control array elements for every piece displayed.

3. Since the control array can only be a one-dimensional array, it is necessary to establish the corresponding relationship between each piece and the grid points. If the chess control array is shpChess(0 To 360), then the relationship between a specific grid shpChess(n) and the grid point Board(I,j) is: n= I +j*19 j=n\19; J =n Mod 19 where I is the row number and j is the column number. The elements of the Board array can be written to a file during the UnLoad event for the form, and read into the recoverable Board during the Load event for the form.

4. The function of user operation is realized by writing the MouseDown event of the form. Determine whether the user presses the Ctrl key based on whether the Shift parameter of the event process is O or 2. Depending on whether the Button parameter is 1 or 2, you can determine whether the user clicked the left or right mouse Button. Through the X, Y parameter (i.e. the coordinates of the mouse click position), the nearest checker board (I,j) can be calculated.

The core code is as follows:

Private Sub Form_Load() Dim I As Integer Randomize 'generates horizontal line For I = 1 To 18 Load LinH(I) Next For I = 0 To 18 LinH(I).visible =  True LinH(i).X1 = HDist LinH(i).Y1 = VDist + Grid * i LinH(i).Y2 = VDist + Grid * i LinH(i).X2 = HDist + Grid * 18 Next For I = 1 To 18 Load LinV(I) Next For I = 0 To 18 LinV(I).visible = True LinV(I).y1 = VDist LinV(I).x1 = HDist + Y2 = VDist + Grid * 18 LinV(I).X2 = HDist + Grid * I Next 'For I = 1 To 360 Load shpChess(I) shpChess(i).FillStyle = 0 shpChess(i).FillColor = vbWhite Next For i = 0 To 360 shpChess(i).Top = (i \ 19) * Grid + VDist -100 shpChess(I).left = (I Mod 19) * Grid + HDist -100 shpChess(I).visible = True Next Dir("save.txt") <> "" Then Open "save.txt" For Input As #1 For i = 0 To 360 Input #1, Board(i Mod 19, i \ 19) Next Close End If Call CountChess End SubCopy the code

There is a need to take the complete source code, please move to the public number: like the code poem. Since come in, liver text is not easy. Let’s go with a “like”.